1
package org.herac.tuxguitar.gui.editors.lyric;
3
import org.eclipse.swt.SWT;
4
import org.eclipse.swt.events.KeyAdapter;
5
import org.eclipse.swt.events.KeyEvent;
6
import org.eclipse.swt.events.SelectionAdapter;
7
import org.eclipse.swt.events.SelectionEvent;
8
import org.eclipse.swt.layout.GridData;
9
import org.eclipse.swt.layout.GridLayout;
10
import org.eclipse.swt.widgets.Button;
11
import org.eclipse.swt.widgets.Composite;
12
import org.eclipse.swt.widgets.Label;
13
import org.eclipse.swt.widgets.Shell;
14
import org.eclipse.swt.widgets.Spinner;
15
import org.eclipse.swt.widgets.Text;
16
import org.herac.tuxguitar.gui.TuxGuitar;
17
import org.herac.tuxguitar.gui.actions.edit.RedoAction;
18
import org.herac.tuxguitar.gui.actions.edit.UndoAction;
19
import org.herac.tuxguitar.gui.actions.track.GoNextTrackAction;
20
import org.herac.tuxguitar.gui.actions.track.GoPreviousTrackAction;
21
import org.herac.tuxguitar.gui.system.icons.IconLoader;
22
import org.herac.tuxguitar.gui.system.keybindings.KeyBinding;
23
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingAction;
24
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingConstants;
25
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
26
import org.herac.tuxguitar.gui.util.DialogUtils;
27
import org.herac.tuxguitar.song.models.TGTrack;
29
public class LyricEditor implements IconLoader,LanguageLoader{
30
private static int EDITOR_WIDTH = 450;
31
private static int EDITOR_HEIGHT = 200;
33
protected static final KeyBindingAction KB_ACTIONS[] = new KeyBindingAction[]{
34
new KeyBindingAction(UndoAction.NAME,new KeyBinding(122,KeyBindingConstants.CONTROL)),
35
new KeyBindingAction(RedoAction.NAME,new KeyBinding(121,KeyBindingConstants.CONTROL)),
38
private TGTrack track;
40
private LyricModifyListener listener;
42
private Button previous;
45
private Label fromLabel;
48
private int caretPosition;
50
private boolean updated;
51
private int lastTrack;
52
private int lastMeasuseCount;
53
private String lastTrackName;
56
this.listener = new LyricModifyListener(this);
57
TuxGuitar.instance().getIconManager().addLoader(this);
58
TuxGuitar.instance().getLanguageManager().addLoader(this);
62
this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(), SWT.DIALOG_TRIM | SWT.RESIZE);
63
this.dialog.setLayout(getDialogLayout());
64
this.dialog.setSize(EDITOR_WIDTH,EDITOR_HEIGHT);
66
this.track = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret().getTrack();
67
this.loadComposites();
68
this.loadProperties();
72
DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_WAIT);
80
private GridLayout getDialogLayout(){
81
GridLayout layout = new GridLayout();
82
layout.marginWidth = 0;
83
layout.marginHeight = 0;
84
layout.verticalSpacing = 0;
88
private void loadComposites(){
89
loadToolBar(this.dialog);
90
loadLyricText(this.dialog);
93
private void loadToolBar(Composite parent){
94
final Composite composite = new Composite(parent,SWT.NONE);
95
composite.setLayout(new GridLayout(5,false));
96
composite.setLayoutData(new GridData(SWT.FILL,SWT.TOP,true,false));
98
this.previous = new Button(composite, SWT.ARROW | SWT.LEFT);
99
this.next = new Button(composite, SWT.ARROW | SWT.RIGHT);
101
this.label = new Label(composite,SWT.NONE);
102
this.label.setText(this.track.getName());
103
this.label.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true));
105
this.fromLabel = new Label(composite,SWT.NONE);
106
this.fromLabel.setLayoutData(new GridData(SWT.RIGHT,SWT.CENTER,false,true));
108
this.from = new Spinner(composite,SWT.BORDER);
109
this.from.setLayoutData(new GridData(50,SWT.DEFAULT));
111
this.from.setMinimum(1);
112
this.from.setMaximum(this.track.countMeasures());
113
this.from.setSelection(this.track.getLyrics().getFrom());
114
this.from.setEnabled(this.track.countMeasures() > 1);
115
this.from.addModifyListener(this.listener);
117
this.previous.addSelectionListener(new SelectionAdapter() {
118
public void widgetSelected(SelectionEvent e) {
119
TuxGuitar.instance().getAction(GoPreviousTrackAction.NAME).process(e);
123
this.next.addSelectionListener(new SelectionAdapter() {
124
public void widgetSelected(SelectionEvent e) {
125
TuxGuitar.instance().getAction(GoNextTrackAction.NAME).process(e);
131
private void loadLyricText(Composite parent){
132
Composite composite = new Composite(parent,SWT.NONE);
133
composite.setLayout(new GridLayout());
134
composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
136
this.text = new Text(composite,SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
137
this.text.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
138
this.text.setFocus();
139
this.text.setText(this.track.getLyrics().getLyrics());
140
this.text.addModifyListener(this.listener);
141
this.text.addKeyListener(new KeyAdapter() {
142
public void keyPressed(KeyEvent event) {
143
for( int i = 0 ; i < KB_ACTIONS.length ; i ++ ){
144
if(event.keyCode == KB_ACTIONS[i].getKeyBinding().getKey() && event.stateMask == KB_ACTIONS[i].getKeyBinding().getMask()){
145
TuxGuitar.instance().getAction(KB_ACTIONS[i].getAction()).process(event);
153
public void updateItems(){
155
boolean enabled = !TuxGuitar.instance().getPlayer().isRunning();
157
this.listener.setEnabled(false);
160
this.lastTrackName = null;
161
this.lastMeasuseCount = 0;
163
this.track = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret().getTrack();
164
if( isTrackNameChanged() ){
165
this.label.setText(this.track.getName());
167
if( isMeasureCountChanged() ){
168
this.from.setMaximum(this.track.countMeasures());
170
if( isTrackChanged() ){
171
this.from.setSelection(this.track.getLyrics().getFrom());
172
this.text.setText(this.track.getLyrics().getLyrics());
173
this.text.setSelection( (this.caretPosition >= 0 ? this.caretPosition : this.text.getCharCount()));
176
this.from.setEnabled( enabled && (this.track.countMeasures() > 1) );
177
this.text.setEnabled( enabled );
179
this.setCaretPosition(-1);
181
this.listener.setEnabled( enabled );
182
this.updated = false;
186
private boolean isTrackChanged(){
187
int current = this.track.getNumber();
188
if(current != this.lastTrack){
189
this.lastTrack = current;
195
private boolean isTrackNameChanged(){
196
String current = this.track.getName();
197
if(this.lastTrackName == null || !current.equals( this.lastTrackName ) ){
198
this.lastTrackName = current;
204
private boolean isMeasureCountChanged(){
205
int current = this.track.countMeasures();
206
if(current != this.lastMeasuseCount){
207
this.lastMeasuseCount = current;
213
public void update(){
217
public void setCaretPosition(int caretPosition) {
218
this.caretPosition = caretPosition;
221
public TGTrack getTrack(){
225
public void loadProperties(){
227
this.dialog.setText(TuxGuitar.getProperty("lyric.editor"));
228
this.fromLabel.setText(TuxGuitar.getProperty("edit.from"));
232
public void loadIcons(){
234
this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
238
public boolean isDisposed() {
239
return (this.dialog == null || this.dialog.isDisposed());
242
public void dispose(){
244
this.dialog.dispose();