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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/TuxGuitar.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
 
/*
2
 
 * Created on 25-nov-2005
3
 
 *
4
 
 * TODO To change the template for this generated file go to
5
 
 * Window - Preferences - Java - Code Style - Code Templates
6
 
 */
7
 
package org.herac.tuxguitar.gui;
8
 
 
9
 
import java.io.File;
10
 
import java.util.Iterator;
11
 
 
12
 
import org.eclipse.swt.SWT;
13
 
import org.eclipse.swt.events.MouseAdapter;
14
 
import org.eclipse.swt.events.MouseEvent;
15
 
import org.eclipse.swt.events.SelectionAdapter;
16
 
import org.eclipse.swt.events.SelectionEvent;
17
 
import org.eclipse.swt.layout.FormAttachment;
18
 
import org.eclipse.swt.layout.FormData;
19
 
import org.eclipse.swt.layout.FormLayout;
20
 
import org.eclipse.swt.widgets.Composite;
21
 
import org.eclipse.swt.widgets.Display;
22
 
import org.eclipse.swt.widgets.Sash;
23
 
import org.eclipse.swt.widgets.Shell;
24
 
import org.herac.tuxguitar.gui.actions.Action;
25
 
import org.herac.tuxguitar.gui.actions.ActionContainer;
26
 
import org.herac.tuxguitar.gui.actions.dispose.DisposeAction;
27
 
import org.herac.tuxguitar.gui.editors.EditorCache;
28
 
import org.herac.tuxguitar.gui.editors.FretBoardEditor;
29
 
import org.herac.tuxguitar.gui.editors.PianoEditor;
30
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
31
 
import org.herac.tuxguitar.gui.editors.chord.CustomChordManager;
32
 
import org.herac.tuxguitar.gui.editors.lyric.LyricEditor;
33
 
import org.herac.tuxguitar.gui.helper.FileHistory;
34
 
import org.herac.tuxguitar.gui.helper.SyncThread;
35
 
import org.herac.tuxguitar.gui.items.ItemManager;
36
 
import org.herac.tuxguitar.gui.mixer.SongMixer;
37
 
import org.herac.tuxguitar.gui.scale.MusicScaleManager;
38
 
import org.herac.tuxguitar.gui.system.config.ConfigKeys;
39
 
import org.herac.tuxguitar.gui.system.config.ConfigManager;
40
 
import org.herac.tuxguitar.gui.system.keybindings.KeyBindingManager;
41
 
import org.herac.tuxguitar.gui.system.language.LanguageManager;
42
 
import org.herac.tuxguitar.gui.system.plugins.PluginManager;
43
 
import org.herac.tuxguitar.gui.transport.SongTransport;
44
 
import org.herac.tuxguitar.gui.util.ArgumentParser;
45
 
import org.herac.tuxguitar.gui.util.ClassLoaderUtil;
46
 
import org.herac.tuxguitar.gui.util.MessageDialog;
47
 
import org.herac.tuxguitar.gui.util.SplashShell;
48
 
import org.herac.tuxguitar.gui.util.SystemError;
49
 
import org.herac.tuxguitar.gui.util.WindowTitleUtil;
50
 
import org.herac.tuxguitar.io.FileFormatFormatException;
51
 
import org.herac.tuxguitar.player.NullPlayer;
52
 
import org.herac.tuxguitar.player.base.MidiPlayer;
53
 
import org.herac.tuxguitar.song.managers.SongManager;
54
 
 
55
 
/**
56
 
 * @author julian
57
 
 * 
58
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
59
 
 */
60
 
public class TuxGuitar {
61
 
        public static final String TUXGUITAR_NAME = "TuxGuitar";
62
 
        
63
 
        public static final String TUXGUITAR_VERSION = "0.9";
64
 
        
65
 
        private static final String appConfigDomain = ".tuxguitar";
66
 
        
67
 
        private static final String appConfigDomainEnvVar = "TUXGUITAR_CONFIG_HOME";
68
 
        
69
 
        public static final int MARGIN_WIDTH = 5;
70
 
        
71
 
        private static TuxGuitar instance;
72
 
 
73
 
        private Display display;
74
 
 
75
 
        private Shell shell;
76
 
 
77
 
        private SongManager songManager;
78
 
 
79
 
        private MidiPlayer player;
80
 
        
81
 
        private LanguageManager languageManager;
82
 
 
83
 
        private ConfigManager configManager;
84
 
        
85
 
        private KeyBindingManager keyBindingManager;
86
 
        
87
 
        private Composite sashComposite;
88
 
        
89
 
        private Sash sash;
90
 
 
91
 
        private EditorCache editorCache;
92
 
        
93
 
        private TablatureEditor tablatureEditor;
94
 
 
95
 
        private SongTableViewer tableViewer;
96
 
        
97
 
        private SongMixer songMixer;
98
 
        
99
 
        private SongTransport songTransport;
100
 
        
101
 
        private FretBoardEditor fretBoardEditor;
102
 
        
103
 
        private PianoEditor pianoEditor;
104
 
        
105
 
        private LyricEditor lyricEditor;
106
 
        
107
 
        private MusicScaleManager scaleManager;
108
 
        
109
 
        private ActionContainer actionContainer;
110
 
 
111
 
        private ItemManager itemManager;
112
 
 
113
 
        private CustomChordManager customChordManager;
114
 
        
115
 
        private FileHistory fileHistory;
116
 
 
117
 
        private PluginManager pluginManager;
118
 
        
119
 
        public static void main(String[] args) {
120
 
                TuxGuitar.instance().displayGUI(args);
121
 
        }
122
 
 
123
 
        public TuxGuitar() {
124
 
        }
125
 
 
126
 
        public static TuxGuitar instance() {
127
 
                if (instance == null) {
128
 
                        synchronized (TuxGuitar.class) {
129
 
                                instance = new TuxGuitar();
130
 
                        }
131
 
                }
132
 
                return instance;
133
 
        }
134
 
        
135
 
        private void initLanguage() {
136
 
                this.languageManager = new LanguageManager();
137
 
                this.languageManager.setLanguage(getConfig().getStringConfigValue(ConfigKeys.LANGUAGE));
138
 
        }
139
 
 
140
 
        public void displayGUI(String[] args) {         
141
 
                //checkeo los argumentos
142
 
                ArgumentParser argumentParser = new ArgumentParser(args);
143
 
                if(argumentParser.printAndExit()){
144
 
                        return;
145
 
                }               
146
 
            this.initConfigManager();
147
 
                this.initLanguage();
148
 
                this.display = new Display();
149
 
 
150
 
                SplashShell splash = new SplashShell(display);
151
 
 
152
 
                this.shell = new Shell(display);                
153
 
                this.shell.setLayout(getShellLayout());         
154
 
                this.shell.setImage(SystemImages.TUXGUITAR_ICON);
155
 
 
156
 
                this.songManager = new SongManager();
157
 
                this.initPluginManager();
158
 
                if(argumentParser.getFile() != null){
159
 
                        try {
160
 
                                this.songManager.open(argumentParser.getFile().getPath());
161
 
                        }catch (FileFormatFormatException e) {                          
162
 
                                e.printStackTrace();
163
 
                                this.songManager.newSong();
164
 
                        }
165
 
                }
166
 
                this.initPlayer(false);
167
 
                this.fileHistory = new FileHistory();
168
 
                this.editorCache = new EditorCache();
169
 
                this.scaleManager = new MusicScaleManager();            
170
 
                this.tablatureEditor = new TablatureEditor(this.songManager);
171
 
                this.tableViewer = new SongTableViewer(this.tablatureEditor);
172
 
                this.fretBoardEditor = new FretBoardEditor(this.tablatureEditor);
173
 
                this.pianoEditor = new PianoEditor(this.tablatureEditor);
174
 
                this.songMixer = new SongMixer();
175
 
                this.songTransport = new SongTransport();
176
 
                this.customChordManager = new CustomChordManager();
177
 
                this.lyricEditor = new LyricEditor();
178
 
                
179
 
                this.initActions();
180
 
                this.adjustKeyBindings();               
181
 
                this.initItems();               
182
 
                this.createComposites(shell);
183
 
 
184
 
                boolean maximized = getConfig().getBooleanConfigValue(ConfigKeys.MAXIMIZED);
185
 
                this.shell.setMaximized(maximized);
186
 
                if(!maximized){
187
 
                        int width = getConfig().getIntConfigValue(ConfigKeys.WIDTH); 
188
 
                        int height = getConfig().getIntConfigValue(ConfigKeys.HEIGHT);
189
 
                        if(width > 0 && height > 0){
190
 
                                this.shell.setSize(width,height);
191
 
                        }
192
 
                }
193
 
                this.tablatureEditor.getTablature().setFocus();
194
 
                
195
 
                this.shell.setMinimumSize(640,480);             
196
 
                
197
 
                this.showDefaultControls();
198
 
                this.updateCache(true);
199
 
                this.showTitle();
200
 
                                
201
 
                splash.close();
202
 
                this.shell.addDisposeListener(getAction(DisposeAction.NAME));
203
 
                this.shell.open();              
204
 
                while (!shell.isDisposed()) {
205
 
                        if (!display.readAndDispatch()) {
206
 
                                display.sleep();
207
 
                        }
208
 
                }
209
 
                getDisplay().dispose();
210
 
                System.exit(0);
211
 
        }
212
 
 
213
 
        private FormLayout getShellLayout(){
214
 
                FormLayout layout = new FormLayout();
215
 
                layout.marginWidth = MARGIN_WIDTH;
216
 
                layout.marginHeight = MARGIN_WIDTH;
217
 
                return layout;
218
 
        }
219
 
        
220
 
        public void createComposites(Composite composite) {
221
 
                this.sashComposite = new Composite(composite,SWT.NONE);
222
 
                this.sashComposite.setLayout(new FormLayout());
223
 
                FormData data = new FormData();
224
 
                data.left = new FormAttachment(0,0); 
225
 
                data.right = new FormAttachment(100,0); 
226
 
                data.top = new FormAttachment(this.itemManager.getCoolbar(),MARGIN_WIDTH);
227
 
                data.bottom = new FormAttachment(100,0);
228
 
                sashComposite.setLayoutData(data);
229
 
                                
230
 
                this.sash = new Sash(sashComposite, SWT.HORIZONTAL | SWT.SEPARATOR);
231
 
            data = new FormData();
232
 
                data.left = new FormAttachment(0,0);
233
 
                data.right = new FormAttachment(100,0);
234
 
                data.top = new FormAttachment(75,0);            
235
 
                sash.setLayoutData(data);
236
 
            
237
 
            this.tablatureEditor.showTablature(sashComposite); 
238
 
            data = new FormData();
239
 
                data.left = new FormAttachment(0,0);
240
 
                data.right = new FormAttachment(100,0);
241
 
                data.top = new FormAttachment(0,0);
242
 
                data.bottom = new FormAttachment(sash, 0);
243
 
                this.tablatureEditor.getTablature().setLayoutData(data);            
244
 
                    
245
 
            this.tableViewer.showTable(sashComposite);
246
 
            data = new FormData();
247
 
                data.left = new FormAttachment(0,0);
248
 
                data.right = new FormAttachment(100,0);
249
 
                data.top = new FormAttachment(sash,10);
250
 
                data.bottom = new FormAttachment(100,0);
251
 
                this.tableViewer.getTableInfo().setLayoutData(data);              
252
 
                    
253
 
            sash.addSelectionListener(new SelectionAdapter() {
254
 
                public void widgetSelected(SelectionEvent event) {                  
255
 
                    int sashNumerator = (event.y * 100 / sashComposite.getBounds().height);
256
 
                    sashNumerator = Math.max(sashNumerator,0);
257
 
                    sashNumerator = Math.min(sashNumerator,100);                    
258
 
                    ((FormData) sash.getLayoutData()).top = new FormAttachment(sashNumerator,0);
259
 
                }
260
 
              });
261
 
            sash.addMouseListener(new MouseAdapter() {
262
 
            public void mouseUp(MouseEvent e) {
263
 
                sash.getParent().layout();
264
 
            }
265
 
        });
266
 
                            
267
 
                data = new FormData();
268
 
                data.left = new FormAttachment(0,0);
269
 
                data.right = new FormAttachment(100,0);
270
 
                data.top = new FormAttachment(sashComposite,0);
271
 
                data.bottom = new FormAttachment(100,0);
272
 
                
273
 
                Composite footer = new Composite(composite,SWT.NONE);
274
 
                footer.setLayout(new FormLayout());
275
 
                footer.setLayoutData(data);             
276
 
            this.fretBoardEditor.showFretBoard(footer);
277
 
        }
278
 
        
279
 
        public void checkSoundbank(){
280
 
                boolean customSoundbank = getConfig().getBooleanConfigValue(ConfigKeys.SOUNDBANK_CUSTOM);
281
 
                if(customSoundbank){
282
 
                        if(!getPlayer().loadSoundbank(new File(getConfig().getStringConfigValue(ConfigKeys.SOUNDBANK_CUSTOM_PATH)))){
283
 
                        String title = TuxGuitar.getProperty("soundbank.error");        
284
 
                        String message = TuxGuitar.getProperty("soundbank.error.custom");
285
 
                        new MessageDialog(title,message,SWT.ICON_ERROR | SWT.OK).show(shell);
286
 
                        }
287
 
                }
288
 
        }
289
 
        
290
 
        public void initPlayer(boolean forceDefault){           
291
 
                if(getPlayer() == null || forceDefault){
292
 
                        MidiPlayer player = null;
293
 
                        Object object = ClassLoaderUtil.newInstance(getConfig().getStringConfigValue(ConfigKeys.SYSTEM_PLAYER));
294
 
                        if(object instanceof MidiPlayer){
295
 
                                player = (MidiPlayer)object;
296
 
                        }
297
 
                        if(player == null){
298
 
                                player = new NullPlayer();
299
 
                        }
300
 
                        initPlayer(player);
301
 
                }
302
 
        }
303
 
        
304
 
        public void initPlayer(MidiPlayer player){              
305
 
                if(this.player != null){
306
 
                        this.player.close();
307
 
                }
308
 
                this.player = player;           
309
 
                Iterator it = player.getSystemErrors().iterator();
310
 
                while(it.hasNext()){
311
 
                        SystemError error = (SystemError)it.next();
312
 
                        new MessageDialog(error.getTitle(),error.getMessage(),SWT.ICON_ERROR | SWT.OK).show(shell);
313
 
                }               
314
 
        }
315
 
        
316
 
        public void showDefaultControls(){                  
317
 
                //---Fretboard---
318
 
            if(getConfig().getBooleanConfigValue(ConfigKeys.SHOW_FRETBOARD)){
319
 
                getFretBoardEditor().showFretBoard();
320
 
            }else{
321
 
                getFretBoardEditor().hideFretBoard();           
322
 
            }
323
 
            
324
 
            //---Mixer---
325
 
                if(getConfig().getBooleanConfigValue(ConfigKeys.SHOW_MIXER)){
326
 
                        new SyncThread(new Runnable() {                 
327
 
                                public void run() {
328
 
                                        getMixer().show();
329
 
                                }                       
330
 
                        }).start();
331
 
                        
332
 
                }else{
333
 
                        getMixer().dispose();
334
 
                }
335
 
                
336
 
            //---Transport---           
337
 
                if(getConfig().getBooleanConfigValue(ConfigKeys.SHOW_TRANSPORT)){
338
 
                        new SyncThread(new Runnable() {                 
339
 
                                public void run() {
340
 
                                        getTransport().show();
341
 
                                }                       
342
 
                        }).start();                                             
343
 
                }else{
344
 
                        getTransport().dispose();
345
 
                }
346
 
 
347
 
            //---Piano---
348
 
                if(getConfig().getBooleanConfigValue(ConfigKeys.SHOW_PIANO)){
349
 
                        new SyncThread(new Runnable() {                 
350
 
                                public void run() {
351
 
                                        getPianoEditor().show();
352
 
                                }                       
353
 
                        }).start();                                             
354
 
                }else{
355
 
                        getPianoEditor().dispose();
356
 
                }
357
 
        }
358
 
        
359
 
        public void updateShellFooter(int offset,int minimunWith,int minimunHeight){
360
 
                minimunWith = Math.max(640,minimunWith);
361
 
                minimunHeight = Math.max(480,minimunHeight);
362
 
                
363
 
            FormData data = ((FormData)this.sashComposite.getLayoutData());
364
 
            data.bottom.offset = -offset;
365
 
            shell.setMinimumSize(minimunWith,minimunHeight);
366
 
            shell.layout();
367
 
        }       
368
 
 
369
 
        public TablatureEditor getTablatureEditor(){
370
 
            return this.tablatureEditor;
371
 
        }
372
 
        
373
 
        public FretBoardEditor getFretBoardEditor(){
374
 
                return this.fretBoardEditor;
375
 
        }               
376
 
 
377
 
        public PianoEditor getPianoEditor(){
378
 
                return this.pianoEditor;
379
 
        }               
380
 
        
381
 
        private void initItems() {              
382
 
                this.itemManager = new ItemManager(this.tablatureEditor);
383
 
                this.itemManager.createItems(this.shell);
384
 
        }
385
 
 
386
 
        private void initActions() {
387
 
                this.actionContainer = new ActionContainer(this.tablatureEditor);
388
 
                this.actionContainer.initActions();
389
 
        }
390
 
        
391
 
        private void initConfigManager(){
392
 
                this.configManager = new ConfigManager();       
393
 
                this.configManager.init();
394
 
        }
395
 
 
396
 
        
397
 
        private void adjustKeyBindings() {
398
 
                this.keyBindingManager = new KeyBindingManager();
399
 
                this.keyBindingManager.init();
400
 
        }
401
 
        
402
 
        private void initPluginManager(){
403
 
                this.pluginManager = new PluginManager();       
404
 
                this.pluginManager.loadPlugins();
405
 
                this.pluginManager.initPLugins();
406
 
        }
407
 
        
408
 
        public SongManager getSongManager(){
409
 
                return this.songManager;
410
 
        }
411
 
        
412
 
        public SongMixer getMixer(){
413
 
                return this.songMixer;
414
 
        }
415
 
        
416
 
        public SongTransport getTransport(){
417
 
                return this.songTransport;
418
 
        }       
419
 
        
420
 
        public SongTableViewer getTableViewer(){
421
 
                return this.tableViewer;
422
 
        }
423
 
        
424
 
        public EditorCache getEditorCache(){
425
 
                return this.editorCache;
426
 
        }
427
 
        
428
 
        public LyricEditor getLyricEditor(){
429
 
                return this.lyricEditor;
430
 
        }
431
 
        
432
 
        
433
 
        public MusicScaleManager getScaleManager(){
434
 
                return this.scaleManager;
435
 
        }
436
 
        
437
 
        public PluginManager getPluginManager(){
438
 
                return this.pluginManager;
439
 
        }
440
 
        
441
 
        public CustomChordManager getCustomChordManager(){
442
 
                return this.customChordManager;
443
 
        }
444
 
        
445
 
        public MidiPlayer getPlayer(){
446
 
                return this.player;
447
 
        }
448
 
        
449
 
        public void showTitle(){
450
 
                getShell().setText(WindowTitleUtil.parseTitle());
451
 
        }
452
 
        
453
 
        public void updateCache(boolean updateItems){
454
 
                getEditorCache().updateCaretMode();                     
455
 
                if(updateItems){
456
 
                        getItemManager().updateItems();
457
 
                        getTransport().updateItems();
458
 
                        getLyricEditor().updateSongTrack();
459
 
                }
460
 
        }
461
 
                
462
 
 
463
 
 
464
 
        
465
 
        
466
 
        public void redraw(){
467
 
            if(!getDisplay().isDisposed()){             
468
 
                this.tablatureEditor.getTablature().redraw();           
469
 
                this.fretBoardEditor.redraw();
470
 
                this.pianoEditor.redraw();
471
 
                this.tableViewer.getTableInfo().redraw();               
472
 
            }
473
 
        }
474
 
        
475
 
        public void redrawPayingMode(){
476
 
            if(!getDisplay().isDisposed()){
477
 
                this.editorCache.updatePlayingMode();
478
 
                if(editorCache.shouldRedraw()){
479
 
                        this.tablatureEditor.getTablature().redrawPlayingMode();
480
 
                        this.fretBoardEditor.redrawPlayingMode();
481
 
                        this.pianoEditor.redrawPlayingMode();
482
 
                        this.tableViewer.getTableInfo().redrawPlayingMode();
483
 
                        this.songTransport.redraw();
484
 
                }
485
 
            }
486
 
        }
487
 
        
488
 
        public void fireUpdate(){
489
 
                this.editorCache.reset();
490
 
                this.tablatureEditor.getTablature().updateTablature();
491
 
                this.tableViewer.getTableInfo().fireUpdate();
492
 
        }
493
 
        
494
 
 
495
 
        
496
 
        public Display getDisplay(){
497
 
            return this.display;
498
 
        }
499
 
        
500
 
        public Shell getShell(){
501
 
            return this.shell;
502
 
        }
503
 
        
504
 
        public Action getAction(String name) {
505
 
                return this.actionContainer.getAction(name);
506
 
        }
507
 
 
508
 
        public ItemManager getItemManager() {
509
 
                return itemManager;
510
 
        }
511
 
 
512
 
        public ActionContainer getActionContainer() {
513
 
                return this.actionContainer;
514
 
        }
515
 
 
516
 
        public LanguageManager getLanguageManager() {
517
 
                return this.languageManager;
518
 
        }
519
 
 
520
 
        public static String getProperty(String key) {
521
 
                return TuxGuitar.instance().languageManager.getProperty(key);
522
 
        }
523
 
   
524
 
        public ConfigManager getConfig(){
525
 
            return this.configManager;
526
 
        }
527
 
 
528
 
        public KeyBindingManager getkeyBindingManager(){
529
 
                return this.keyBindingManager;
530
 
        }
531
 
 
532
 
        public FileHistory getFileHistory(){
533
 
                return this.fileHistory;
534
 
        }
535
 
        
536
 
        public static boolean isDisposed(){
537
 
            return TuxGuitar.instance().getDisplay().isDisposed();
538
 
        }
539
 
        
540
 
    public void showErrorMessage(final Throwable throwable){
541
 
        new SyncThread(new Runnable() {
542
 
            public void run() {
543
 
                        throwable.printStackTrace();
544
 
                        String title = getProperty("error");
545
 
                        String message = throwable.toString();
546
 
                        new MessageDialog(title,message,SWT.ICON_ERROR).show(shell);
547
 
            }
548
 
        }).start();             
549
 
    }
550
 
        
551
 
        public void loadLanguage(){
552
 
                getLanguageManager().setLanguage(getConfig().getStringConfigValue(ConfigKeys.LANGUAGE));                
553
 
        }
554
 
 
555
 
        public void loadProperties(){
556
 
                getItemManager().loadProperties();         
557
 
                getTableViewer().getTableInfo().loadProperties();
558
 
            getFretBoardEditor().loadProperties();
559
 
            getPianoEditor().loadProperties();
560
 
            getMixer().loadProperties();
561
 
            getTransport().loadProperties();            
562
 
        }
563
 
        
564
 
        public void loadToolBars(){
565
 
                getItemManager().makeCoolItems();
566
 
        }       
567
 
 
568
 
        public void loadStyles(){
569
 
                getTablatureEditor().getTablature().reloadStyles();
570
 
        }
571
 
            
572
 
 
573
 
}
 
 
b'\\ No newline at end of file'