~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/editors/lyric/LyricEditor.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.editors.lyric;
 
2
 
 
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;
 
28
 
 
29
public class LyricEditor implements IconLoader,LanguageLoader{
 
30
        private static int EDITOR_WIDTH = 450;
 
31
        private static int EDITOR_HEIGHT = 200;
 
32
        
 
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)),
 
36
        };
 
37
        
 
38
        private TGTrack track;
 
39
        private Shell dialog;
 
40
        private LyricModifyListener listener;
 
41
        
 
42
        private Button previous;
 
43
        private Button next;
 
44
        private Label label;
 
45
        private Label fromLabel;
 
46
        private Spinner from;
 
47
        private Text text;
 
48
        private int caretPosition;
 
49
        
 
50
        private boolean updated;
 
51
        private int lastTrack;
 
52
        private int lastMeasuseCount;
 
53
        private String lastTrackName;
 
54
        
 
55
        public LyricEditor(){
 
56
                this.listener = new LyricModifyListener(this);
 
57
                TuxGuitar.instance().getIconManager().addLoader(this);
 
58
                TuxGuitar.instance().getLanguageManager().addLoader(this);
 
59
        }
 
60
        
 
61
        public void show() {
 
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);
 
65
                
 
66
                this.track = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret().getTrack();
 
67
                this.loadComposites();
 
68
                this.loadProperties();
 
69
                this.loadIcons();
 
70
                this.updateItems();
 
71
                
 
72
                DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_WAIT);
 
73
                
 
74
                this.track = null;
 
75
                this.label = null;
 
76
                this.text = null;
 
77
                this.dialog = null;
 
78
        }
 
79
        
 
80
        private GridLayout getDialogLayout(){
 
81
                GridLayout layout = new GridLayout();
 
82
                layout.marginWidth = 0;
 
83
                layout.marginHeight = 0;
 
84
                layout.verticalSpacing = 0;
 
85
                return layout;
 
86
        }
 
87
        
 
88
        private void loadComposites(){
 
89
                loadToolBar(this.dialog);
 
90
                loadLyricText(this.dialog);
 
91
        }
 
92
        
 
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));
 
97
                
 
98
                this.previous = new Button(composite, SWT.ARROW | SWT.LEFT);
 
99
                this.next = new Button(composite, SWT.ARROW | SWT.RIGHT);
 
100
                
 
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));
 
104
                
 
105
                this.fromLabel = new Label(composite,SWT.NONE);
 
106
                this.fromLabel.setLayoutData(new GridData(SWT.RIGHT,SWT.CENTER,false,true));
 
107
                
 
108
                this.from = new Spinner(composite,SWT.BORDER);
 
109
                this.from.setLayoutData(new GridData(50,SWT.DEFAULT));
 
110
                
 
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);
 
116
                
 
117
                this.previous.addSelectionListener(new SelectionAdapter() {
 
118
                        public void widgetSelected(SelectionEvent e) {
 
119
                                TuxGuitar.instance().getAction(GoPreviousTrackAction.NAME).process(e);
 
120
                                composite.layout();
 
121
                        }
 
122
                });
 
123
                this.next.addSelectionListener(new SelectionAdapter() {
 
124
                        public void widgetSelected(SelectionEvent e) {
 
125
                                TuxGuitar.instance().getAction(GoNextTrackAction.NAME).process(e);
 
126
                                composite.layout();
 
127
                        }
 
128
                });
 
129
        }
 
130
        
 
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));
 
135
                
 
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);
 
146
                                                return;
 
147
                                        }
 
148
                                }
 
149
                        }
 
150
                });
 
151
        }
 
152
        
 
153
        public void updateItems(){
 
154
                if(!isDisposed()){
 
155
                        boolean enabled = !TuxGuitar.instance().getPlayer().isRunning();
 
156
                        
 
157
                        this.listener.setEnabled(false);
 
158
                        if(this.updated){
 
159
                                this.lastTrack = 0;
 
160
                                this.lastTrackName = null;
 
161
                                this.lastMeasuseCount = 0;
 
162
                        }
 
163
                        this.track = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret().getTrack();
 
164
                        if( isTrackNameChanged() ){
 
165
                                this.label.setText(this.track.getName());
 
166
                        }
 
167
                        if( isMeasureCountChanged() ){
 
168
                                this.from.setMaximum(this.track.countMeasures());
 
169
                        }
 
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()));
 
174
                        }
 
175
                        
 
176
                        this.from.setEnabled( enabled && (this.track.countMeasures() > 1) );
 
177
                        this.text.setEnabled( enabled );
 
178
                        
 
179
                        this.setCaretPosition(-1);
 
180
                        
 
181
                        this.listener.setEnabled( enabled );
 
182
                        this.updated = false;
 
183
                }
 
184
        }
 
185
        
 
186
        private boolean isTrackChanged(){
 
187
                int current = this.track.getNumber();
 
188
                if(current != this.lastTrack){
 
189
                        this.lastTrack = current;
 
190
                        return true;
 
191
                }
 
192
                return false;
 
193
        }
 
194
        
 
195
        private boolean isTrackNameChanged(){
 
196
                String current = this.track.getName();
 
197
                if(this.lastTrackName == null || !current.equals( this.lastTrackName ) ){
 
198
                        this.lastTrackName = current;
 
199
                        return true;
 
200
                }
 
201
                return false;
 
202
        }
 
203
        
 
204
        private boolean isMeasureCountChanged(){
 
205
                int current = this.track.countMeasures();
 
206
                if(current != this.lastMeasuseCount){
 
207
                        this.lastMeasuseCount = current;
 
208
                        return true;
 
209
                }
 
210
                return false;
 
211
        }
 
212
        
 
213
        public void update(){
 
214
                this.updated = true;
 
215
        }
 
216
        
 
217
        public void setCaretPosition(int caretPosition) {
 
218
                this.caretPosition = caretPosition;
 
219
        }
 
220
        
 
221
        public TGTrack getTrack(){
 
222
                return this.track;
 
223
        }
 
224
        
 
225
        public void loadProperties(){
 
226
                if(!isDisposed()){
 
227
                        this.dialog.setText(TuxGuitar.getProperty("lyric.editor"));
 
228
                        this.fromLabel.setText(TuxGuitar.getProperty("edit.from"));
 
229
                }
 
230
        }
 
231
        
 
232
        public void loadIcons(){
 
233
                if(!isDisposed()){
 
234
                        this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
 
235
                }
 
236
        }
 
237
        
 
238
        public boolean isDisposed() {
 
239
                return (this.dialog == null || this.dialog.isDisposed());
 
240
        }
 
241
        
 
242
        public void dispose(){
 
243
                if(!isDisposed()){
 
244
                        this.dialog.dispose();
 
245
                }
 
246
        }
 
247
}