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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/marker/MarkerList.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.marker;
 
2
 
 
3
import java.util.Iterator;
 
4
import java.util.List;
 
5
 
 
6
import org.eclipse.swt.SWT;
 
7
import org.eclipse.swt.events.SelectionAdapter;
 
8
import org.eclipse.swt.events.SelectionEvent;
 
9
import org.eclipse.swt.layout.GridData;
 
10
import org.eclipse.swt.layout.GridLayout;
 
11
import org.eclipse.swt.widgets.Button;
 
12
import org.eclipse.swt.widgets.Composite;
 
13
import org.eclipse.swt.widgets.Event;
 
14
import org.eclipse.swt.widgets.Listener;
 
15
import org.eclipse.swt.widgets.Shell;
 
16
import org.eclipse.swt.widgets.Table;
 
17
import org.eclipse.swt.widgets.TableColumn;
 
18
import org.eclipse.swt.widgets.TableItem;
 
19
import org.herac.tuxguitar.gui.TuxGuitar;
 
20
import org.herac.tuxguitar.gui.actions.ActionLock;
 
21
import org.herac.tuxguitar.gui.editors.tab.Caret;
 
22
import org.herac.tuxguitar.gui.helper.SyncThread;
 
23
import org.herac.tuxguitar.gui.system.icons.IconLoader;
 
24
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
 
25
import org.herac.tuxguitar.gui.undo.undoables.custom.UndoableChangeMarker;
 
26
import org.herac.tuxguitar.gui.util.DialogUtils;
 
27
import org.herac.tuxguitar.song.models.TGMarker;
 
28
 
 
29
public class MarkerList implements IconLoader,LanguageLoader{
 
30
        
 
31
        private static MarkerList instance;
 
32
        
 
33
        protected Shell dialog;
 
34
        private Table table;
 
35
        private List markers;
 
36
        
 
37
        private Composite compositeTable;
 
38
        private TableColumn measureColumn;
 
39
        private TableColumn titleColumn;
 
40
        
 
41
        private Composite compositeButtons;
 
42
        private Button buttonAdd;
 
43
        private Button buttonEdit;
 
44
        private Button buttonDelete;
 
45
        private Button buttonGo;
 
46
        private Button buttonClose;
 
47
        
 
48
        public static MarkerList instance(){
 
49
                if(instance == null){
 
50
                        instance = new MarkerList();
 
51
                }
 
52
                return instance;
 
53
        }
 
54
        
 
55
        private MarkerList() {
 
56
                TuxGuitar.instance().getIconManager().addLoader(this);
 
57
                TuxGuitar.instance().getLanguageManager().addLoader(this);
 
58
        }
 
59
        
 
60
        public void show() {
 
61
                this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(), SWT.DIALOG_TRIM);
 
62
                this.dialog.setLayout(new GridLayout(2,false));
 
63
                this.dialog.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
64
                // ----------------------------------------------------------------------
 
65
                this.compositeTable = new Composite(this.dialog, SWT.NONE);
 
66
                this.compositeTable.setLayout(new GridLayout());
 
67
                this.compositeTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
 
68
                
 
69
                this.table = new Table(this.compositeTable, SWT.BORDER | SWT.FULL_SELECTION);
 
70
                this.table.setLayoutData(new GridData(250,200));
 
71
                this.table.setHeaderVisible(true);
 
72
                this.table.addListener (SWT.MouseDoubleClick, new Listener() {
 
73
                        public void handleEvent (Event event) {
 
74
                                new MarkerNavigator().goToSelectedMarker(getSelectedMarker());
 
75
                                TuxGuitar.instance().updateCache(true);
 
76
                        }
 
77
                });
 
78
                this.measureColumn = new TableColumn(this.table, SWT.LEFT);
 
79
                this.measureColumn.setWidth(70);
 
80
                
 
81
                this.titleColumn = new TableColumn(this.table, SWT.LEFT);
 
82
                this.titleColumn.setWidth(180);
 
83
                
 
84
                this.loadTableItems(false);
 
85
                
 
86
                // ------------------BUTTONS--------------------------
 
87
                this.compositeButtons = new Composite(this.dialog, SWT.NONE);
 
88
                this.compositeButtons.setLayout(new GridLayout(1,false));
 
89
                this.compositeButtons.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
90
                
 
91
                this.buttonAdd = new Button(this.compositeButtons, SWT.PUSH);
 
92
                this.buttonAdd.setLayoutData(makeGridData(SWT.FILL, SWT.TOP,false));
 
93
                this.buttonAdd.addSelectionListener(new SelectionAdapter() {
 
94
                        public void widgetSelected(SelectionEvent e) {
 
95
                                if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
 
96
                                        ActionLock.lock();
 
97
                                        Caret caret = TuxGuitar.instance().getTablatureEditor().getTablature().getCaret();
 
98
                                        TGMarker marker = TuxGuitar.instance().getSongManager().getFactory().newMarker();
 
99
                                        marker.setMeasure(caret.getMeasure().getNumber());
 
100
                                        if(new MarkerEditor(marker,MarkerEditor.STATUS_NEW).open(MarkerList.this.dialog)){
 
101
                                                TuxGuitar.instance().updateCache(true);
 
102
                                                loadTableItems(true);
 
103
                                        }
 
104
                                        ActionLock.unlock();
 
105
                                }
 
106
                        }
 
107
                });
 
108
                
 
109
                this.buttonEdit = new Button(this.compositeButtons, SWT.PUSH);
 
110
                this.buttonEdit.setLayoutData(makeGridData(SWT.FILL, SWT.TOP,false));
 
111
                this.buttonEdit.addSelectionListener(new SelectionAdapter() {
 
112
                        public void widgetSelected(SelectionEvent arg0) {
 
113
                                if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
 
114
                                        ActionLock.lock();
 
115
                                        TGMarker marker = getSelectedMarker();
 
116
                                        if(marker != null){
 
117
                                                if(new MarkerEditor(marker,MarkerEditor.STATUS_EDIT).open(MarkerList.this.dialog)){
 
118
                                                        TuxGuitar.instance().updateCache(true);
 
119
                                                        loadTableItems(true);
 
120
                                                }
 
121
                                        }
 
122
                                        ActionLock.unlock();
 
123
                                }
 
124
                        }
 
125
                });
 
126
                
 
127
                this.buttonDelete = new Button(this.compositeButtons, SWT.PUSH);
 
128
                this.buttonDelete.setLayoutData(makeGridData(SWT.FILL, SWT.TOP,false));
 
129
                this.buttonDelete.addSelectionListener(new SelectionAdapter() {
 
130
                        public void widgetSelected(SelectionEvent arg0) {
 
131
                                if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
 
132
                                        ActionLock.lock();
 
133
                                        TGMarker marker = getSelectedMarker();
 
134
                                        // comienza el undoable
 
135
                                        UndoableChangeMarker undoable = UndoableChangeMarker.startUndo(marker);
 
136
                                        
 
137
                                        TuxGuitar.instance().getSongManager().removeMarker(marker);
 
138
                                        
 
139
                                        // termia el undoable
 
140
                                        TuxGuitar.instance().getUndoableManager().addEdit(undoable.endUndo(null));
 
141
                                        TuxGuitar.instance().getFileHistory().setUnsavedFile();
 
142
                                        TuxGuitar.instance().updateCache(true);
 
143
                                        loadTableItems(true);
 
144
                                        ActionLock.unlock();
 
145
                                }
 
146
                        }
 
147
                });
 
148
                
 
149
                this.buttonGo = new Button(this.compositeButtons, SWT.PUSH);
 
150
                this.buttonGo.setLayoutData(makeGridData(SWT.FILL, SWT.BOTTOM,true));
 
151
                this.buttonGo.addSelectionListener(new SelectionAdapter() {
 
152
                        public void widgetSelected(SelectionEvent arg0) {
 
153
                                if(!ActionLock.isLocked() && !TuxGuitar.instance().isLocked()){
 
154
                                        ActionLock.lock();
 
155
                                        new MarkerNavigator().goToSelectedMarker(getSelectedMarker());
 
156
                                        TuxGuitar.instance().updateCache(true);
 
157
                                        ActionLock.unlock();
 
158
                                }
 
159
                        }
 
160
                });
 
161
                
 
162
                this.buttonClose = new Button(this.compositeButtons, SWT.PUSH);
 
163
                this.buttonClose.setLayoutData(makeGridData(SWT.FILL, SWT.BOTTOM,false));
 
164
                this.buttonClose.addSelectionListener(new SelectionAdapter() {
 
165
                        public void widgetSelected(SelectionEvent arg0) {
 
166
                                MarkerList.this.dialog.dispose();
 
167
                        }
 
168
                });
 
169
                
 
170
                this.loadIcons();
 
171
                this.loadProperties(false);
 
172
                
 
173
                this.dialog.setDefaultButton( this.buttonGo );
 
174
                
 
175
                DialogUtils.openDialog(this.dialog,DialogUtils.OPEN_STYLE_CENTER | DialogUtils.OPEN_STYLE_PACK);
 
176
        }
 
177
        
 
178
        public void dispose(){
 
179
                if(!isDisposed()){
 
180
                        this.dialog.dispose();
 
181
                }
 
182
        }
 
183
        
 
184
        public void update(){
 
185
                this.update(false);
 
186
        }
 
187
        
 
188
        public void update(final boolean keepSelection){
 
189
                if(!isDisposed()){
 
190
                        new SyncThread(new Runnable() {
 
191
                                public void run() {
 
192
                                        if(!isDisposed()){
 
193
                                                loadTableItems(keepSelection);
 
194
                                        }
 
195
                                }
 
196
                        }).start();
 
197
                }
 
198
        }
 
199
        
 
200
        private GridData makeGridData(int horizontalAlignment,int verticalAlignment,boolean grabExcessVerticalSpace){
 
201
                GridData data = new GridData();
 
202
                data.horizontalAlignment = horizontalAlignment;
 
203
                data.verticalAlignment = verticalAlignment;
 
204
                data.grabExcessHorizontalSpace = true;
 
205
                data.grabExcessVerticalSpace = grabExcessVerticalSpace;
 
206
                data.minimumWidth = 80;
 
207
                data.minimumHeight = 25;
 
208
                
 
209
                return data;
 
210
        }
 
211
        
 
212
        protected void loadTableItems(boolean keepSelection){
 
213
                int itemSelected = (keepSelection ? this.table.getSelectionIndex() : -1 );
 
214
                
 
215
                this.table.removeAll();
 
216
                this.markers = TuxGuitar.instance().getSongManager().getMarkers();
 
217
                
 
218
                Iterator it = this.markers.iterator();
 
219
                while (it.hasNext()) {
 
220
                        TGMarker marker = (TGMarker) it.next();
 
221
                        
 
222
                        TableItem item = new TableItem(this.table, SWT.NONE);
 
223
                        item.setText(new String[] { Integer.toString(marker.getMeasure()),marker.getTitle() });
 
224
                }
 
225
                
 
226
                if(itemSelected >= 0 && itemSelected < this.markers.size()){
 
227
                        this.table.select(itemSelected);
 
228
                }
 
229
        }
 
230
        
 
231
        protected TGMarker getSelectedMarker(){
 
232
                int itemSelected = this.table.getSelectionIndex();
 
233
                if(itemSelected >= 0 && itemSelected < this.markers.size()){
 
234
                        return (TGMarker)this.markers.get(itemSelected);
 
235
                }
 
236
                return null;
 
237
        }
 
238
        
 
239
        public boolean isDisposed(){
 
240
                return (this.dialog == null || this.dialog.isDisposed());
 
241
        }
 
242
        
 
243
        public void loadIcons() {
 
244
                if(!isDisposed()){
 
245
                        this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
 
246
                }
 
247
        }
 
248
        
 
249
        public void loadProperties() {
 
250
                this.loadProperties(true);
 
251
        }
 
252
        
 
253
        public void loadProperties(boolean layout) {
 
254
                if(!isDisposed()){
 
255
                        this.dialog.setText(TuxGuitar.getProperty("marker.list"));
 
256
                        this.measureColumn.setText(TuxGuitar.getProperty("measure"));
 
257
                        this.titleColumn.setText(TuxGuitar.getProperty("title"));
 
258
                        this.buttonAdd.setText(TuxGuitar.getProperty("add"));
 
259
                        this.buttonEdit.setText(TuxGuitar.getProperty("edit"));
 
260
                        this.buttonDelete.setText(TuxGuitar.getProperty("remove"));
 
261
                        this.buttonGo.setText(TuxGuitar.getProperty("go"));
 
262
                        this.buttonClose.setText(TuxGuitar.getProperty("close"));
 
263
                        
 
264
                        if(layout){
 
265
                                this.table.layout();
 
266
                                this.compositeTable.layout();
 
267
                                this.compositeButtons.layout();
 
268
                                this.dialog.pack(true);
 
269
                        }
 
270
                }
 
271
        }
 
272
        
 
273
}