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

« back to all changes in this revision

Viewing changes to TuxGuitar/src/org/herac/tuxguitar/gui/actions/file/FileActionUtils.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.actions.file;
 
2
 
 
3
import java.io.ByteArrayInputStream;
 
4
import java.io.ByteArrayOutputStream;
 
5
import java.io.File;
 
6
import java.io.FileInputStream;
 
7
import java.io.FileOutputStream;
 
8
import java.io.InputStream;
 
9
import java.net.URL;
 
10
import java.util.Iterator;
 
11
 
 
12
import org.herac.tuxguitar.gui.TuxGuitar;
 
13
import org.herac.tuxguitar.gui.util.ConfirmDialog;
 
14
import org.herac.tuxguitar.gui.util.FileChooser;
 
15
import org.herac.tuxguitar.gui.util.MessageDialog;
 
16
import org.herac.tuxguitar.io.base.TGFileFormat;
 
17
import org.herac.tuxguitar.io.base.TGFileFormatException;
 
18
import org.herac.tuxguitar.io.base.TGFileFormatManager;
 
19
import org.herac.tuxguitar.io.base.TGOutputStreamBase;
 
20
import org.herac.tuxguitar.io.base.TGSongExporter;
 
21
import org.herac.tuxguitar.io.base.TGSongImporter;
 
22
import org.herac.tuxguitar.song.managers.TGSongManager;
 
23
import org.herac.tuxguitar.song.models.TGSong;
 
24
 
 
25
public class FileActionUtils {
 
26
        
 
27
        public static String getFileName(){
 
28
                if (TuxGuitar.instance().getFileHistory().isNewFile() || !TuxGuitar.instance().getFileHistory().isLocalFile()) {
 
29
                        return chooseFileName();
 
30
                }
 
31
                String path = TuxGuitar.instance().getFileHistory().getCurrentFilePath();
 
32
                String file = TuxGuitar.instance().getFileHistory().getCurrentFileName(FileChooser.DEFAULT_SAVE_FILENAME);
 
33
                String fullPath = path + File.separator + file;
 
34
                return ( isSuportedFormat(fullPath) ? fullPath : chooseFileName() );
 
35
        }
 
36
        
 
37
        public static String chooseFileName(){
 
38
                String fileName = FileChooser.instance().save(TuxGuitar.instance().getShell(),TGFileFormatManager.instance().getOutputFormats());
 
39
                if (fileName != null) {
 
40
                        if (!isSuportedFormat(fileName)) {
 
41
                                fileName += TGFileFormatManager.DEFAULT_EXTENSION;
 
42
                        }
 
43
                        if(!canWrite(fileName)){
 
44
                                return null;
 
45
                        }
 
46
                }
 
47
                return fileName;
 
48
        }
 
49
        
 
50
        public static String chooseFileName(TGFileFormat format){
 
51
                String fileName = FileChooser.instance().save(TuxGuitar.instance().getShell(),format);
 
52
                if (fileName != null && !canWrite(fileName)){
 
53
                        return null;
 
54
                }
 
55
                return fileName;
 
56
        }
 
57
        
 
58
        public static boolean isSuportedFormat(String path) {
 
59
                if(path != null){
 
60
                        int index = path.lastIndexOf(".");
 
61
                        if(index > 0){
 
62
                                Iterator it = TGFileFormatManager.instance().getOutputStreams();
 
63
                                while(it.hasNext()){
 
64
                                        TGOutputStreamBase writer = (TGOutputStreamBase)it.next();
 
65
                                        if(writer.isSupportedExtension(path.substring(index))){
 
66
                                                return true;
 
67
                                        }
 
68
                                }
 
69
                        }
 
70
                }
 
71
                return false;
 
72
        }
 
73
        
 
74
        public static boolean canWrite(String fileName){
 
75
                boolean canWrite = true;
 
76
                File file = new File(fileName);
 
77
                if (file.exists()) {
 
78
                        ConfirmDialog confirm = new ConfirmDialog(TuxGuitar.getProperty("file.overwrite-question"));
 
79
                        confirm.setDefaultStatus( ConfirmDialog.STATUS_NO );
 
80
                        if (confirm.confirm(ConfirmDialog.BUTTON_YES | ConfirmDialog.BUTTON_NO , ConfirmDialog.BUTTON_NO ) == ConfirmDialog.STATUS_NO) {
 
81
                                canWrite = false;
 
82
                        }
 
83
                }
 
84
                return canWrite;
 
85
        }
 
86
        
 
87
        public static void open(final String fileName){
 
88
                try {
 
89
                        TGSong song = TGFileFormatManager.instance().getLoader().load(TuxGuitar.instance().getSongManager().getFactory(),new FileInputStream(fileName));
 
90
                        TuxGuitar.instance().fireNewSong(song,new File(fileName).toURI().toURL());
 
91
                }catch (Throwable throwable) {
 
92
                        TuxGuitar.instance().newSong();
 
93
                        MessageDialog.errorMessage(new TGFileFormatException(TuxGuitar.getProperty("file.open.error", new String[]{fileName}),throwable));
 
94
                }
 
95
        }
 
96
        
 
97
        public static void save(final String fileName){
 
98
                try {
 
99
                        TGSongManager manager = TuxGuitar.instance().getSongManager();
 
100
                        TGFileFormatManager.instance().getWriter().write(manager.getFactory(),manager.getSong(), fileName);
 
101
                        TuxGuitar.instance().fireSaveSong(new File(fileName).toURI().toURL());
 
102
                } catch (Throwable throwable) {
 
103
                        MessageDialog.errorMessage(new TGFileFormatException(TuxGuitar.getProperty("file.save.error", new String[]{fileName}),throwable));
 
104
                }
 
105
        }
 
106
        
 
107
        public static void open(final URL url){
 
108
                try {
 
109
                        InputStream stream = (isLocalFile(url) ? url.openStream() : getInputStream(url.openStream()));
 
110
                        TGSong song = TGFileFormatManager.instance().getLoader().load(TuxGuitar.instance().getSongManager().getFactory(),stream);
 
111
                        TuxGuitar.instance().fireNewSong(song,url);
 
112
                }catch (Throwable throwable) {
 
113
                        TuxGuitar.instance().newSong();
 
114
                        MessageDialog.errorMessage(new TGFileFormatException(TuxGuitar.getProperty("file.open.error", new String[]{url.toString()}),throwable));
 
115
                }
 
116
        }
 
117
        
 
118
        public static void exportSong(TGSongExporter exporter, String path){
 
119
                try {
 
120
                        TGSongManager manager = TuxGuitar.instance().getSongManager();
 
121
                        exporter.exportSong(new FileOutputStream(new File(path)),manager.getSong());
 
122
                } catch (Throwable throwable) {
 
123
                        MessageDialog.errorMessage(new TGFileFormatException(TuxGuitar.getProperty("file.export.error", new String[]{path}),throwable));
 
124
                }
 
125
        }
 
126
        
 
127
        public static void importSong(final TGSongImporter importer, String path){
 
128
                try {
 
129
                        TGSong song = importer.importSong(TuxGuitar.instance().getSongManager().getFactory(),new FileInputStream(new File(path)));
 
130
                        TuxGuitar.instance().fireNewSong(song,null);
 
131
                }catch (Throwable throwable) {
 
132
                        TuxGuitar.instance().newSong();
 
133
                        MessageDialog.errorMessage(new TGFileFormatException(TuxGuitar.getProperty("file.import.error", new String[]{path}),throwable));
 
134
                }
 
135
        }
 
136
        
 
137
        private static boolean isLocalFile(URL url){
 
138
                try {
 
139
                        if(url.getProtocol().equals( new File(url.getFile()).toURI().toURL().getProtocol() ) ){
 
140
                                return true;
 
141
                        }
 
142
                }catch(Throwable throwable){
 
143
                        throwable.printStackTrace();
 
144
                }
 
145
                return false;
 
146
        }
 
147
        
 
148
        private static InputStream getInputStream(InputStream in)throws Throwable {
 
149
                ByteArrayOutputStream out = new ByteArrayOutputStream();
 
150
                int read = 0;
 
151
                while((read = in.read()) != -1){
 
152
                        out.write(read);
 
153
                }
 
154
                byte[] bytes = out.toByteArray();
 
155
                in.close();
 
156
                out.close();
 
157
                out.flush();
 
158
                return new ByteArrayInputStream(bytes);
 
159
        }
 
160
}