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

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/gui/actions/file/ExportASCIIAction.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.io.FileNotFoundException;
11
 
 
12
 
import org.eclipse.swt.SWT;
13
 
import org.eclipse.swt.events.TypedEvent;
14
 
import org.eclipse.swt.widgets.Shell;
15
 
import org.herac.tuxguitar.gui.TuxGuitar;
16
 
import org.herac.tuxguitar.gui.actions.Action;
17
 
import org.herac.tuxguitar.gui.editors.TablatureEditor;
18
 
import org.herac.tuxguitar.gui.helper.SyncThread;
19
 
import org.herac.tuxguitar.gui.util.ConfirmDialog;
20
 
import org.herac.tuxguitar.gui.util.FileChooser;
21
 
import org.herac.tuxguitar.io.exporter.ASCIITabOutputStream;
22
 
 
23
 
/**
24
 
 * @author julian
25
 
 * 
26
 
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
27
 
 */
28
 
public class ExportASCIIAction extends Action {
29
 
        public static final String NAME = "action.file.export-ascii";
30
 
 
31
 
        public ExportASCIIAction(TablatureEditor tablatureEditor) {
32
 
                super(NAME,tablatureEditor);
33
 
        }
34
 
 
35
 
        public boolean doAction(TypedEvent e) {         
36
 
                Shell parent = getEditor().getTablature().getShell();
37
 
                String selectedName = FileChooser.instance().save(parent,FileChooser.EXPORT_ASCII_FORMAT);
38
 
                if (selectedName != null) {
39
 
                        boolean canWrite = true;
40
 
                        if (!isSuportedFormat(selectedName)) {
41
 
                                selectedName += getDefaultExtension();
42
 
                        }
43
 
                        final String fileName = selectedName;
44
 
 
45
 
                        File file = new File(fileName);
46
 
                        if (file.exists()) {
47
 
                                ConfirmDialog confirm = new ConfirmDialog(getEditor().getTablature().getShell(), TuxGuitar.getProperty("replace-file-question"));
48
 
 
49
 
                                if (!confirm.confirm()) {
50
 
                                        canWrite = false;
51
 
                                }
52
 
                        }
53
 
 
54
 
                        if (canWrite) {
55
 
                                getEditor().getTablature().changeCursor(SWT.CURSOR_WAIT);
56
 
                                new SyncThread(new Runnable() {
57
 
                                        public void run() {
58
 
                                                if(!TuxGuitar.isDisposed()){
59
 
                                                        try {                                                                                                                                                                   
60
 
                                                                new ASCIITabOutputStream(fileName).writeSong(getSongManager().getSong());
61
 
                                                                getEditor().getTablature().changeCursor(SWT.CURSOR_ARROW);                                                                                                                      
62
 
                                                        } catch (FileNotFoundException e) {
63
 
                                                                e.printStackTrace();
64
 
                                                        }       
65
 
                                                }
66
 
                                        }
67
 
                                }).start();
68
 
                        }
69
 
                }
70
 
 
71
 
                return true;
72
 
        }       
73
 
 
74
 
        private boolean isSuportedFormat(String fileName) {
75
 
                return (fileName.endsWith(getDefaultExtension()));
76
 
        }
77
 
 
78
 
        private String getDefaultExtension() {
79
 
                return ".tab";
80
 
        }
81
 
 
82
 
}