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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/table/TGTableViewer.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.table;
 
2
 
 
3
import org.eclipse.swt.SWT;
 
4
import org.eclipse.swt.events.MouseAdapter;
 
5
import org.eclipse.swt.events.MouseEvent;
 
6
import org.eclipse.swt.events.MouseListener;
 
7
import org.eclipse.swt.graphics.Color;
 
8
import org.eclipse.swt.layout.GridLayout;
 
9
import org.eclipse.swt.widgets.Composite;
 
10
import org.eclipse.swt.widgets.Event;
 
11
import org.eclipse.swt.widgets.Listener;
 
12
import org.eclipse.swt.widgets.ScrollBar;
 
13
import org.herac.tuxguitar.gui.TuxGuitar;
 
14
import org.herac.tuxguitar.gui.actions.ActionLock;
 
15
import org.herac.tuxguitar.gui.actions.composition.ChangeInfoAction;
 
16
import org.herac.tuxguitar.gui.actions.track.GoToTrackAction;
 
17
import org.herac.tuxguitar.gui.actions.track.TrackPropertiesAction;
 
18
import org.herac.tuxguitar.gui.editors.TablatureEditor;
 
19
import org.herac.tuxguitar.gui.editors.tab.TGMeasureImpl;
 
20
import org.herac.tuxguitar.gui.editors.tab.TGTrackImpl;
 
21
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
 
22
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
 
23
import org.herac.tuxguitar.player.base.MidiInstrument;
 
24
import org.herac.tuxguitar.song.models.TGBeat;
 
25
import org.herac.tuxguitar.song.models.TGMeasure;
 
26
import org.herac.tuxguitar.song.models.TGTrack;
 
27
 
 
28
public class TGTableViewer implements LanguageLoader{
 
29
        
 
30
        public static final Color[] BACKGROUNDS = new Color[]{
 
31
                new Color(TuxGuitar.instance().getDisplay(),255,255,255),
 
32
                new Color(TuxGuitar.instance().getDisplay(),238,238,238),
 
33
                new Color(TuxGuitar.instance().getDisplay(),192,192,192)
 
34
        };
 
35
        
 
36
        private Composite composite;
 
37
        private ScrollBar hSroll;
 
38
        private TGTable table;
 
39
        private int selectedTrack;
 
40
        private int selectedMeasure;
 
41
        private int trackCount = 0;
 
42
        private boolean autoSizeEnabled;
 
43
        private boolean update;
 
44
        private boolean followScroll;
 
45
        
 
46
        public TGTableViewer() {
 
47
                TuxGuitar.instance().getLanguageManager().addLoader(this);
 
48
        }
 
49
        
 
50
        public void init(Composite parent){
 
51
                this.composite = new Composite(parent,SWT.H_SCROLL);
 
52
                this.addLayout();
 
53
                this.addTable();
 
54
                this.addHScroll();
 
55
                this.loadConfig();
 
56
        }
 
57
        
 
58
        private void addLayout(){
 
59
                GridLayout layout = new GridLayout();
 
60
                layout.marginWidth = 0;
 
61
                layout.marginHeight = 0;
 
62
                layout.horizontalSpacing = 0;
 
63
                layout.verticalSpacing = 0;
 
64
                getComposite().setLayout(layout);
 
65
        }
 
66
        
 
67
        private void addHScroll(){
 
68
                this.hSroll = getComposite().getHorizontalBar();
 
69
                this.hSroll.addListener(SWT.Selection, new Listener() {
 
70
                        public void handleEvent(Event e) {
 
71
                                redraw();
 
72
                        }
 
73
                });
 
74
        }
 
75
        
 
76
        private void addTable(){
 
77
                MouseListener listener = mouseFocusListener();
 
78
                this.table = new TGTable(getComposite());
 
79
                this.table.getColumnNumber().getControl().addMouseListener(listener);
 
80
                this.table.getColumnName().getControl().addMouseListener(listener);
 
81
                this.table.getColumnInstrument().getControl().addMouseListener(listener);
 
82
                this.table.getColumnCanvas().getControl().addMouseListener(listener);
 
83
                this.table.getColumnCanvas().getControl().addMouseListener(new MouseAdapter() {
 
84
                        public void mouseDoubleClick(MouseEvent e) {
 
85
                                TuxGuitar.instance().getAction(ChangeInfoAction.NAME).process(e);
 
86
                        }
 
87
                });
 
88
                this.fireUpdate(true);
 
89
                this.loadProperties();
 
90
        }
 
91
        
 
92
        public void loadProperties() {
 
93
                this.table.getColumnNumber().setTitle(TuxGuitar.getProperty("track.number"));
 
94
                this.table.getColumnName().setTitle(TuxGuitar.getProperty("track.name"));
 
95
                this.table.getColumnInstrument().setTitle(TuxGuitar.getProperty("track.instrument"));
 
96
        }
 
97
        
 
98
        public void fireUpdate(boolean newSong){
 
99
                this.update = true;
 
100
                if(newSong){
 
101
                        this.trackCount = 0;
 
102
                }
 
103
        }
 
104
        
 
105
        public void updateItems(){
 
106
                this.followScroll = true;
 
107
        }
 
108
        
 
109
        public void updateHScroll(){
 
110
                int width = (getEditor().getTablature().getCaret().getTrack().countMeasures() * this.table.getRowHeight());
 
111
                this.hSroll.setIncrement(this.table.getScrollIncrement());
 
112
                this.hSroll.setMaximum(width);
 
113
                this.hSroll.setThumb(Math.min(width ,this.table.getColumnCanvas().getControl().getClientArea().width));
 
114
        }
 
115
        
 
116
        public TGTable getTable(){
 
117
                return this.table;
 
118
        }
 
119
        
 
120
        public int getHScrollSelection(){
 
121
                return this.hSroll.getSelection();
 
122
        }
 
123
        
 
124
        public TablatureEditor getEditor(){
 
125
                return TuxGuitar.instance().getTablatureEditor();
 
126
        }
 
127
        
 
128
        private String getInstrument(TGTrack track){
 
129
                if(track.isPercussionTrack()){
 
130
                        return TuxGuitar.getProperty("track.name.default-percussion-name");
 
131
                }
 
132
                MidiInstrument[] list = TuxGuitar.instance().getPlayer().getInstruments();
 
133
                int index = track.getChannel().getInstrument();
 
134
                if(list != null && index >= 0 && index < list.length){
 
135
                        return list[index].getName();
 
136
                }
 
137
                return new String();
 
138
        }
 
139
        
 
140
        private void updateTable(){
 
141
                if(this.update){
 
142
                        int count = TuxGuitar.instance().getSongManager().getSong().countTracks();
 
143
                        this.table.removeRowsAfter(count);
 
144
                        for(int i = this.table.getRowCount(); i < count; i ++){
 
145
                                this.table.newRow();
 
146
                        }
 
147
                        
 
148
                        for(int i = 0; i < count; i ++){
 
149
                                final TGTrack track = TuxGuitar.instance().getSongManager().getSong().getTrack(i);
 
150
                                final TGTableRow row = this.table.getRow(i);
 
151
                                if(row != null){
 
152
                                        //Number
 
153
                                        row.getNumber().setText(Integer.toString(track.getNumber()));
 
154
                                        row.getNumber().setData(track);
 
155
                                        
 
156
                                        //Name
 
157
                                        row.getName().setText(track.getName());
 
158
                                        row.getName().setData(track);
 
159
                                        
 
160
                                        //Instrument
 
161
                                        row.getInstrument().setText(getInstrument(track));
 
162
                                        row.getInstrument().setData(track);
 
163
                                        
 
164
                                        row.setMouseListenerLabel(new MouseAdapter() {
 
165
                                                
 
166
                                                public void mouseUp(MouseEvent e) {
 
167
                                                        row.getPainter().setFocus();
 
168
                                                }
 
169
                                                
 
170
                                                public void mouseDown(MouseEvent e) {
 
171
                                                        if(track.getNumber() != getEditor().getTablature().getCaret().getTrack().getNumber()){
 
172
                                                                TuxGuitar.instance().getAction(GoToTrackAction.NAME).process(e);
 
173
                                                        }
 
174
                                                }
 
175
                                                
 
176
                                                public void mouseDoubleClick(final MouseEvent e) {
 
177
                                                        new Thread(new Runnable() {
 
178
                                                                public void run() {
 
179
                                                                        ActionLock.waitFor();
 
180
                                                                        TuxGuitar.instance().getAction(TrackPropertiesAction.NAME).process(e);
 
181
                                                                }
 
182
                                                        }).start();
 
183
                                                }
 
184
                                        });
 
185
                                        row.setMouseListenerCanvas(new MouseAdapter() {
 
186
                                                
 
187
                                                public void mouseUp(MouseEvent e) {
 
188
                                                        row.getPainter().setFocus();
 
189
                                                }
 
190
                                                
 
191
                                                public void mouseDown(MouseEvent e) {
 
192
                                                        int index = ((e.x + getHScrollSelection())/ getTable().getRowHeight());
 
193
                                                        if(index >= 0 && index < track.countMeasures()){
 
194
                                                                TGMeasureImpl measure = (TGMeasureImpl)track.getMeasure(index);
 
195
                                                                TGBeat beat = TuxGuitar.instance().getSongManager().getMeasureManager().getFirstBeat(measure.getBeats());
 
196
                                                                if(beat != null){
 
197
                                                                        getEditor().getTablature().getCaret().moveTo((TGTrackImpl)track,measure,beat,1);
 
198
                                                                        TuxGuitar.instance().updateCache(true);
 
199
                                                                }
 
200
                                                        }
 
201
                                                }
 
202
                                        });
 
203
                                        row.setPaintListenerCanvas(new TGTableCanvasPainter(this,track));
 
204
                                }
 
205
                        }
 
206
                        this.table.update();
 
207
                        this.selectedTrack = 0;
 
208
                        this.selectedMeasure = 0;
 
209
                        
 
210
                        if(this.autoSizeEnabled && this.trackCount != count){
 
211
                                TuxGuitar.instance().setTableHeight( getHeight() );
 
212
                                this.trackCount = count;
 
213
                        }
 
214
                        
 
215
                }
 
216
                this.update = false;
 
217
        }
 
218
        
 
219
        private int getHeight(){
 
220
                return ( this.table.getMinHeight() + getComposite().getHorizontalBar().getSize().y);
 
221
        }
 
222
        
 
223
        private void redrawRows(int selectedTrack){
 
224
                int rows = this.table.getRowCount();
 
225
                for(int i = 0; i < rows; i ++){
 
226
                        TGTableRow row = this.table.getRow(i); 
 
227
                        row.getPainter().redraw();
 
228
                        if(this.selectedTrack != selectedTrack){
 
229
                                row.setBackground( ((selectedTrack - 1) == i)?BACKGROUNDS[2]:BACKGROUNDS[ i % 2] );
 
230
                        }
 
231
                }
 
232
        }
 
233
        
 
234
        public void redraw(){
 
235
                if(!isDisposed() && !TuxGuitar.instance().isLocked()){
 
236
                        TuxGuitar.instance().lock();
 
237
                        this.updateTable();
 
238
                        this.table.getColumnCanvas().setTitle(TuxGuitar.instance().getSongManager().getSong().getName());
 
239
                        int selectedTrack = getEditor().getTablature().getCaret().getTrack().getNumber();
 
240
                        this.redrawRows(selectedTrack);
 
241
                        this.selectedTrack = selectedTrack;
 
242
                        this.selectedMeasure = 0;
 
243
                        this.updateHScroll();
 
244
                        
 
245
                        if(this.followScroll){
 
246
                                this.followHorizontalScroll(getEditor().getTablature().getCaret().getMeasure().getNumber());
 
247
                                this.followScroll = false;
 
248
                        }
 
249
                        getComposite().redraw();
 
250
                        TuxGuitar.instance().unlock();
 
251
                }
 
252
        }
 
253
        
 
254
        public void redrawPlayingMode(){
 
255
                if(!isDisposed() && !TuxGuitar.instance().isLocked()){
 
256
                        TuxGuitar.instance().lock();
 
257
                        TGMeasure measure =  TuxGuitar.instance().getEditorCache().getPlayMeasure();
 
258
                        if(measure != null && measure.getTrack() != null){
 
259
                                this.updateTable();
 
260
                                int selectedTrack = measure.getTrack().getNumber();
 
261
                                int selectedMeasure = measure.getNumber();
 
262
                                if(this.selectedTrack != selectedTrack || this.selectedMeasure != selectedMeasure){
 
263
                                        this.redrawRows(selectedTrack);
 
264
                                        this.followHorizontalScroll(selectedMeasure);
 
265
                                }
 
266
                                this.selectedTrack = selectedTrack;
 
267
                                this.selectedMeasure = selectedMeasure;
 
268
                        }
 
269
                        TuxGuitar.instance().unlock();
 
270
                }
 
271
        }
 
272
        
 
273
        private void followHorizontalScroll(int selectedMeasure){
 
274
                int hScrollSelection = this.hSroll.getSelection();
 
275
                int hScrollThumb = this.hSroll.getThumb();
 
276
                
 
277
                int measureSize = this.table.getRowHeight();
 
278
                int measurePosition = ( (selectedMeasure * measureSize) - measureSize );
 
279
                
 
280
                if( (measurePosition - hScrollSelection) < 0 || (measurePosition + measureSize - hScrollSelection ) > hScrollThumb){
 
281
                        this.hSroll.setSelection(measurePosition);
 
282
                }
 
283
        }
 
284
        
 
285
        public void loadConfig(){
 
286
                this.autoSizeEnabled = TuxGuitar.instance().getConfig().getBooleanConfigValue(TGConfigKeys.TABLE_AUTO_SIZE);
 
287
                this.trackCount = 0;
 
288
        }
 
289
        
 
290
        public static void disposeColors(){
 
291
                for(int i = 0;i < BACKGROUNDS.length;i++){
 
292
                        BACKGROUNDS[i].dispose();
 
293
                }
 
294
        }
 
295
        
 
296
        public Composite getComposite(){
 
297
                return this.composite;
 
298
        }
 
299
        
 
300
        public void dispose(){
 
301
                if(!isDisposed()){
 
302
                        getComposite().dispose();
 
303
                        disposeColors();
 
304
                }
 
305
        }
 
306
        
 
307
        public boolean isDisposed(){
 
308
                return (getComposite() == null || getComposite().isDisposed());
 
309
        }
 
310
        
 
311
        protected int getSelectedTrack(){
 
312
                return this.selectedTrack;
 
313
        }
 
314
        
 
315
        private MouseListener mouseFocusListener(){
 
316
                return new MouseAdapter() {
 
317
                        public void mouseUp(MouseEvent e) {
 
318
                                TGTable table = getTable();
 
319
                                if(table != null){
 
320
                                        TGTableRow row = table.getRow( ( getSelectedTrack() - 1 ) );
 
321
                                        if(row != null){
 
322
                                                row.getPainter().setFocus();
 
323
                                        }
 
324
                                }
 
325
                        }
 
326
                };
 
327
        }
 
328
}