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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/actions/file/OpenFileAction.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 17-dic-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.actions.file;
 
8
 
 
9
import java.io.File;
 
10
import java.net.URL;
 
11
 
 
12
import org.eclipse.swt.SWT;
 
13
import org.eclipse.swt.events.TypedEvent;
 
14
import org.herac.tuxguitar.gui.TuxGuitar;
 
15
import org.herac.tuxguitar.gui.actions.Action;
 
16
import org.herac.tuxguitar.gui.actions.ActionLock;
 
17
import org.herac.tuxguitar.gui.helper.SyncThread;
 
18
import org.herac.tuxguitar.gui.util.ConfirmDialog;
 
19
import org.herac.tuxguitar.gui.util.FileChooser;
 
20
import org.herac.tuxguitar.io.base.TGFileFormatManager;
 
21
 
 
22
/**
 
23
 * @author julian
 
24
 * 
 
25
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
26
 */
 
27
public class OpenFileAction extends Action {
 
28
        public static final String NAME = "action.file.open";
 
29
        
 
30
        public OpenFileAction() {
 
31
                super(NAME, AUTO_LOCK | AUTO_UPDATE | KEY_BINDING_AVAILABLE);
 
32
        }
 
33
        
 
34
        protected int execute(final TypedEvent event){
 
35
                TuxGuitar.instance().getPlayer().reset();
 
36
                
 
37
                final Object data = event.widget.getData();
 
38
                if(TuxGuitar.instance().getFileHistory().isUnsavedFile()){
 
39
                        ConfirmDialog confirm = new ConfirmDialog(TuxGuitar.getProperty("file.save-changes-question"));
 
40
                        confirm.setDefaultStatus( ConfirmDialog.STATUS_CANCEL );
 
41
                        int status = confirm.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO | ConfirmDialog.BUTTON_CANCEL, ConfirmDialog.BUTTON_YES);
 
42
                        if(status == ConfirmDialog.STATUS_CANCEL){
 
43
                                return AUTO_UNLOCK;
 
44
                        }
 
45
                        if(status == ConfirmDialog.STATUS_YES){
 
46
                                final String fileName = FileActionUtils.getFileName();
 
47
                                if(fileName == null){
 
48
                                        return AUTO_UNLOCK;
 
49
                                }
 
50
                                TuxGuitar.instance().loadCursor(SWT.CURSOR_WAIT);
 
51
                                new Thread(new Runnable() {
 
52
                                        public void run() {
 
53
                                                if(!TuxGuitar.isDisposed()){
 
54
                                                        FileActionUtils.save(fileName);
 
55
                                                        new SyncThread(new Runnable() {
 
56
                                                                public void run() {
 
57
                                                                        if(!TuxGuitar.isDisposed()){
 
58
                                                                                TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
 
59
                                                                                openFile( data );
 
60
                                                                        }
 
61
                                                                }
 
62
                                                        }).start();
 
63
                                                }
 
64
                                        }
 
65
                                }).start();
 
66
                                return 0;
 
67
                        }
 
68
                }
 
69
                openFile( data );
 
70
                
 
71
                return 0;
 
72
        }
 
73
        
 
74
        protected void openFile(Object data){
 
75
                final URL url = getOpenFileName(data);
 
76
                if(url == null){
 
77
                        ActionLock.unlock();
 
78
                        return;
 
79
                }
 
80
                TuxGuitar.instance().loadCursor(SWT.CURSOR_WAIT);
 
81
                new Thread(new Runnable() {
 
82
                        public void run() {
 
83
                                if(!TuxGuitar.isDisposed()){
 
84
                                        FileActionUtils.open(url);
 
85
                                        TuxGuitar.instance().loadCursor(SWT.CURSOR_ARROW);
 
86
                                        ActionLock.unlock();
 
87
                                }
 
88
                        }
 
89
                }).start();
 
90
        }
 
91
        
 
92
        protected URL getOpenFileName(Object data){
 
93
                try{
 
94
                        if(data instanceof URL){
 
95
                                TuxGuitar.instance().getFileHistory().setChooserPath( (URL)data );
 
96
                                return (URL)data;
 
97
                        }
 
98
                        String path = FileChooser.instance().open(TuxGuitar.instance().getShell(),TGFileFormatManager.instance().getInputFormats());
 
99
                        if(path != null){
 
100
                                File file = new File(path);
 
101
                                return file.toURI().toURL();
 
102
                        }
 
103
                }catch(Throwable throwable){
 
104
                        throwable.printStackTrace();
 
105
                }
 
106
                return null;
 
107
        }
 
108
}
 
 
b'\\ No newline at end of file'