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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/tools/browser/dialog/TGBrowserDialog.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.tools.browser.dialog;
 
2
 
 
3
import java.io.InputStream;
 
4
import java.util.Collections;
 
5
import java.util.Iterator;
 
6
import java.util.List;
 
7
 
 
8
import org.eclipse.swt.SWT;
 
9
import org.eclipse.swt.events.DisposeEvent;
 
10
import org.eclipse.swt.events.DisposeListener;
 
11
import org.eclipse.swt.layout.GridData;
 
12
import org.eclipse.swt.layout.GridLayout;
 
13
import org.eclipse.swt.widgets.Composite;
 
14
import org.eclipse.swt.widgets.Event;
 
15
import org.eclipse.swt.widgets.Listener;
 
16
import org.eclipse.swt.widgets.Shell;
 
17
import org.eclipse.swt.widgets.Table;
 
18
import org.eclipse.swt.widgets.TableColumn;
 
19
import org.eclipse.swt.widgets.TableItem;
 
20
import org.herac.tuxguitar.gui.TuxGuitar;
 
21
import org.herac.tuxguitar.gui.actions.ActionLock;
 
22
import org.herac.tuxguitar.gui.actions.file.FileActionUtils;
 
23
import org.herac.tuxguitar.gui.helper.SyncThread;
 
24
import org.herac.tuxguitar.gui.system.config.TGConfigKeys;
 
25
import org.herac.tuxguitar.gui.system.icons.IconLoader;
 
26
import org.herac.tuxguitar.gui.system.language.LanguageLoader;
 
27
import org.herac.tuxguitar.gui.tools.browser.TGBrowserCollection;
 
28
import org.herac.tuxguitar.gui.tools.browser.TGBrowserConnection;
 
29
import org.herac.tuxguitar.gui.tools.browser.TGBrowserConnectionHandler;
 
30
import org.herac.tuxguitar.gui.tools.browser.TGBrowserFactoryHandler;
 
31
import org.herac.tuxguitar.gui.tools.browser.TGBrowserManager;
 
32
import org.herac.tuxguitar.gui.tools.browser.base.TGBrowserElement;
 
33
import org.herac.tuxguitar.gui.tools.browser.base.TGBrowserFactory;
 
34
import org.herac.tuxguitar.gui.util.ConfirmDialog;
 
35
import org.herac.tuxguitar.gui.util.DialogUtils;
 
36
import org.herac.tuxguitar.gui.util.MessageDialog;
 
37
import org.herac.tuxguitar.io.base.TGFileFormatException;
 
38
import org.herac.tuxguitar.io.base.TGFileFormatManager;
 
39
import org.herac.tuxguitar.song.models.TGSong;
 
40
 
 
41
public class TGBrowserDialog implements TGBrowserFactoryHandler,TGBrowserConnectionHandler,IconLoader,LanguageLoader{
 
42
        private static final int SHELL_WIDTH = 500;
 
43
        private static final int SHELL_HEIGHT = 350;
 
44
        
 
45
        public static final int CALL_OPEN = 1;
 
46
        public static final int CALL_CLOSE = 2;
 
47
        public static final int CALL_CD_ROOT = 3;
 
48
        public static final int CALL_CD_UP = 4;
 
49
        public static final int CALL_LIST = 5;
 
50
        public static final int CALL_ELEMENT = 6;
 
51
        
 
52
        private TGBrowserCollection collection;
 
53
        private TGBrowserConnection connection;
 
54
        private Shell dialog;
 
55
        protected Table table;
 
56
        protected TableColumn column;
 
57
        protected List elements;
 
58
        protected TGBrowserMenuBar menu;
 
59
        protected TGBrowserToolBar toolBar;
 
60
        
 
61
        public TGBrowserDialog(){
 
62
                this.connection = new TGBrowserConnection(this);
 
63
                this.menu = new TGBrowserMenuBar(this);
 
64
                this.toolBar = new TGBrowserToolBar(this);
 
65
        }
 
66
        
 
67
        public TGBrowserConnection getConnection(){
 
68
                return this.connection;
 
69
        }
 
70
        
 
71
        public TGBrowserCollection getCollection() {
 
72
                return this.collection;
 
73
        }
 
74
        
 
75
        public void setCollection(TGBrowserCollection collection) {
 
76
                this.collection = collection;
 
77
        }
 
78
        
 
79
        public Shell getShell(){
 
80
                return this.dialog;
 
81
        }
 
82
        
 
83
        public void exit(){
 
84
                this.getConnection().release();
 
85
                this.getConnection().close(CALL_CLOSE);
 
86
                TGBrowserManager.instance().writeCollections();
 
87
                TuxGuitar.instance().getIconManager().removeLoader(this);
 
88
        }
 
89
        
 
90
        public void show(){
 
91
                this.dialog = DialogUtils.newDialog(TuxGuitar.instance().getShell(),SWT.DIALOG_TRIM | SWT.RESIZE);
 
92
                this.dialog.setLayout(new GridLayout());
 
93
                this.dialog.setImage(TuxGuitar.instance().getIconManager().getAppIcon());
 
94
                
 
95
                this.menu.init(getShell());
 
96
                this.toolBar.init(getShell());
 
97
                this.initTable(this.dialog);
 
98
                this.updateCollections(null);
 
99
                this.updateTable();
 
100
                this.dialog.setSize(SHELL_WIDTH,SHELL_HEIGHT);
 
101
                this.dialog.addDisposeListener(new DisposeListener() {
 
102
                        public void widgetDisposed(DisposeEvent e) {
 
103
                                exit();
 
104
                        }
 
105
                });
 
106
                
 
107
                this.loadProperties();
 
108
                this.updateBars();
 
109
                
 
110
                TGBrowserManager.instance().setFactoryHandler(this);
 
111
                TuxGuitar.instance().getIconManager().addLoader(this);
 
112
                TuxGuitar.instance().getLanguageManager().addLoader(this);
 
113
                DialogUtils.openDialog(this.dialog, DialogUtils.OPEN_STYLE_CENTER);
 
114
        }
 
115
        
 
116
        private void initTable(Composite parent){
 
117
                this.table = new Table(parent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION);
 
118
                this.table.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
 
119
                this.table.setLinesVisible(TuxGuitar.instance().getConfig().getBooleanConfigValue(TGConfigKeys.BROWSER_LINES_VISIBLE));
 
120
                this.table.setHeaderVisible(false);
 
121
                
 
122
                this.column = new TableColumn(this.table, SWT.LEFT);
 
123
                
 
124
                this.table.addListener (SWT.MouseDoubleClick, new Listener() {
 
125
                        public void handleEvent (Event event) {
 
126
                                openElement();
 
127
                        }
 
128
                });
 
129
        }
 
130
        
 
131
        public boolean isDisposed(){
 
132
                return (this.dialog == null || this.dialog.isDisposed());
 
133
        }
 
134
        
 
135
        public void dispose(){
 
136
                if(!isDisposed()){
 
137
                        this.dialog.dispose();
 
138
                }
 
139
        }
 
140
        
 
141
        private void updateTable(){
 
142
                if(!isDisposed()){
 
143
                        new SyncThread(new Runnable() {
 
144
                                public void run() {
 
145
                                        if(!isDisposed()){
 
146
                                                TGBrowserDialog.this.table.removeAll();
 
147
                                                if(TGBrowserDialog.this.elements != null){
 
148
                                                        Iterator it = TGBrowserDialog.this.elements.iterator();
 
149
                                                        while(it.hasNext()){
 
150
                                                                TGBrowserElement element = (TGBrowserElement)it.next();
 
151
                                                                TableItem item = new TableItem(TGBrowserDialog.this.table, SWT.NONE);
 
152
                                                                item.setImage(element.isFolder()?TuxGuitar.instance().getIconManager().getBrowserFolder():TuxGuitar.instance().getIconManager().getBrowserFile());
 
153
                                                                item.setText(element.getName());
 
154
                                                        }
 
155
                                                }
 
156
                                                updateColumn();
 
157
                                        }
 
158
                                }
 
159
                        }).start();
 
160
                }
 
161
        }
 
162
        
 
163
        protected void updateColumn(){
 
164
                if(!isDisposed()){
 
165
                        this.column.pack();
 
166
                }
 
167
        }
 
168
        
 
169
        public void updateBars(){
 
170
                if(!isDisposed()){
 
171
                        this.menu.updateItems();
 
172
                        this.toolBar.updateItems();
 
173
                }
 
174
        }
 
175
        
 
176
        public void updateCollections(final TGBrowserCollection selection){
 
177
                if(!isDisposed()){
 
178
                        new SyncThread(new Runnable() {
 
179
                                public void run() {
 
180
                                        if(!isDisposed()){
 
181
                                                TGBrowserDialog.this.menu.updateCollections(selection);
 
182
                                                TGBrowserDialog.this.toolBar.updateCollections(selection);
 
183
                                        }
 
184
                                }
 
185
                        }).start();
 
186
                }
 
187
        }
 
188
        
 
189
        public TGBrowserElement getSelection(int index){
 
190
                if(!isDisposed() && getConnection().isOpen()){
 
191
                        if(this.elements != null && index >= 0 && index < this.elements.size()){
 
192
                                return (TGBrowserElement)this.elements.get(index);
 
193
                        }
 
194
                }
 
195
                return null;
 
196
        }
 
197
        
 
198
        protected void removeElements(){
 
199
                this.elements = null;
 
200
        }
 
201
        
 
202
        protected void addElements(List elements){
 
203
                if(elements != null){
 
204
                        Collections.sort(elements,new TGBrowserElementComparator());
 
205
                }
 
206
                this.elements = elements;
 
207
        }
 
208
        
 
209
        protected void openCollection(){
 
210
                if(!isDisposed() && getCollection() != null){
 
211
                        TGBrowserFactory factory = TGBrowserManager.instance().getFactory(getCollection().getType());
 
212
                        getConnection().open(CALL_OPEN,factory.newTGBrowser(getCollection().getData()));
 
213
                }
 
214
        }
 
215
        
 
216
        protected void closeCollection(){
 
217
                if(!isDisposed() && getCollection() != null){
 
218
                        this.getConnection().close(CALL_CLOSE);
 
219
                }
 
220
        }
 
221
        
 
222
        protected void removeCollection(TGBrowserCollection collection){
 
223
                if(collection != null){
 
224
                        TGBrowserManager.instance().removeCollection(collection);
 
225
                        if( getCollection() != null && getCollection().equals( collection ) ){
 
226
                                this.getConnection().close(CALL_CLOSE);
 
227
                        }else{
 
228
                                this.updateCollections( getCollection() );
 
229
                        }
 
230
                }
 
231
        }
 
232
        
 
233
        public void openElement(){
 
234
                TGBrowserElement element = getSelection(this.table.getSelectionIndex());
 
235
                if(element != null){
 
236
                        this.getConnection().openStream(CALL_ELEMENT,element);
 
237
                }
 
238
        }
 
239
        
 
240
        public void notifyLockStatusChanged(){
 
241
                new SyncThread(new Runnable() {
 
242
                        public void run() {
 
243
                                if(!isDisposed()){
 
244
                                        updateBars();
 
245
                                        TuxGuitar.instance().loadCursor(getShell(),( getConnection().isLocked() ? SWT.CURSOR_WAIT : SWT.CURSOR_ARROW ) );
 
246
                                }
 
247
                        }
 
248
                }).start();
 
249
        }
 
250
        
 
251
        public void notifyOpened(int callId) {
 
252
                if(!isDisposed()){
 
253
                        this.removeElements();
 
254
                        this.updateTable();
 
255
                        this.updateCollections(getCollection());
 
256
                        this.getConnection().release();
 
257
                        this.getConnection().listElements(CALL_LIST);
 
258
                }
 
259
        }
 
260
        
 
261
        public void notifyClosed(int callId) {
 
262
                if(callId != CALL_OPEN){
 
263
                        this.setCollection(null);
 
264
                }
 
265
                this.removeElements();
 
266
                this.updateCollections(getCollection());
 
267
                this.updateTable();
 
268
                if(callId != CALL_OPEN){
 
269
                        this.getConnection().release();
 
270
                }
 
271
        }
 
272
        
 
273
        public void notifyError(int callId,Throwable throwable){
 
274
                if(!isDisposed()){
 
275
                        this.updateTable();
 
276
                        this.getConnection().release();
 
277
                        MessageDialog.errorMessage(getShell(),throwable);
 
278
                }
 
279
        }
 
280
        
 
281
        public void notifyCd(int callId) {
 
282
                if(!isDisposed()){
 
283
                        this.getConnection().release();
 
284
                        this.getConnection().listElements(CALL_LIST);
 
285
                }
 
286
        }
 
287
        
 
288
        public void notifyElements(int callId,List elements) {
 
289
                if(!isDisposed()){
 
290
                        this.addElements(elements);
 
291
                        this.updateTable();
 
292
                        this.getConnection().release();
 
293
                }
 
294
        }
 
295
        
 
296
        public void notifyStream(int callId,final InputStream stream,final TGBrowserElement element) {
 
297
                if(!isDisposed()){
 
298
                        ActionLock.lock();
 
299
                        new SyncThread(new Runnable() {
 
300
                                public void run() {
 
301
                                        if(!TuxGuitar.isDisposed()){
 
302
                                                TuxGuitar.instance().getPlayer().reset();
 
303
                                                if(TuxGuitar.instance().getFileHistory().isUnsavedFile()){
 
304
                                                        ConfirmDialog confirm = new ConfirmDialog(TuxGuitar.getProperty("file.save-changes-question"));
 
305
                                                        confirm.setDefaultStatus( ConfirmDialog.STATUS_CANCEL );
 
306
                                                        int status = confirm.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO | ConfirmDialog.BUTTON_CANCEL, ConfirmDialog.BUTTON_YES);
 
307
                                                        if(status == ConfirmDialog.STATUS_CANCEL){
 
308
                                                                getConnection().release();
 
309
                                                                ActionLock.unlock();
 
310
                                                                return;
 
311
                                                        }
 
312
                                                        if(status == ConfirmDialog.STATUS_YES){
 
313
                                                                final String fileName = FileActionUtils.getFileName();
 
314
                                                                if(fileName == null){
 
315
                                                                        getConnection().release();
 
316
                                                                        ActionLock.unlock();
 
317
                                                                        return;
 
318
                                                                }
 
319
                                                                new Thread(new Runnable() {
 
320
                                                                        public void run() {
 
321
                                                                                if(!TuxGuitar.isDisposed()){
 
322
                                                                                        FileActionUtils.save(fileName);
 
323
                                                                                        new SyncThread(new Runnable() {
 
324
                                                                                                public void run() {
 
325
                                                                                                        if(!TuxGuitar.isDisposed()){
 
326
                                                                                                                openStream(stream,element);
 
327
                                                                                                        }
 
328
                                                                                                }
 
329
                                                                                        }).start();
 
330
                                                                                }
 
331
                                                                        }
 
332
                                                                }).start();
 
333
                                                                return;
 
334
                                                        }
 
335
                                                }
 
336
                                                openStream(stream,element);
 
337
                                        }
 
338
                                }
 
339
                        }).start();
 
340
                }
 
341
        }
 
342
        
 
343
        protected void openStream(final InputStream stream,final TGBrowserElement element){
 
344
                new Thread(new Runnable() {
 
345
                        public void run() {
 
346
                                if(!TuxGuitar.isDisposed()){
 
347
                                        try {
 
348
                                                TGSong song = TGFileFormatManager.instance().getLoader().load(TuxGuitar.instance().getSongManager().getFactory(),stream);
 
349
                                                TuxGuitar.instance().fireNewSong(song,null);
 
350
                                        }catch (Throwable throwable) {
 
351
                                                TuxGuitar.instance().newSong();
 
352
                                                MessageDialog.errorMessage(getShell(),new TGFileFormatException(TuxGuitar.getProperty("file.open.error", new String[]{element.getName()}),throwable));
 
353
                                        }
 
354
                                        getConnection().release();
 
355
                                        ActionLock.unlock();
 
356
                                }
 
357
                        }
 
358
                }).start();
 
359
        }
 
360
        
 
361
        public void loadIcons() {
 
362
                if(!isDisposed()){
 
363
                        this.getShell().setImage(TuxGuitar.instance().getIconManager().getAppIcon());
 
364
                        this.reload();
 
365
                }
 
366
        }
 
367
        
 
368
        public void loadProperties() {
 
369
                if(!isDisposed()){
 
370
                        this.dialog.setText(TuxGuitar.getProperty("browser.dialog"));
 
371
                        this.menu.loadProperties();
 
372
                        this.toolBar.loadProperties();
 
373
                }
 
374
        }
 
375
        
 
376
        public void notifyAdded() {
 
377
                reload();
 
378
        }
 
379
        
 
380
        public void notifyRemoved() {
 
381
                if(getCollection() != null){
 
382
                        closeCollection();
 
383
                }
 
384
                reload();
 
385
        }
 
386
        
 
387
        protected void reload(){
 
388
                if(!isDisposed()){
 
389
                        this.menu.reload(getShell());
 
390
                        this.toolBar.reload();
 
391
                        this.updateTable();
 
392
                        this.updateCollections(getCollection());
 
393
                        this.getShell().layout();
 
394
                }
 
395
        }
 
396
}