/tuxguitar-src-1.2/TuxGuitar/src/org/herac/tuxguitar/gui/actions/file/FileActionUtils.java

# · Java · 188 lines · 171 code · 17 blank · 0 comment · 18 complexity · 77312a21671b8f7994d79694e0275eea MD5 · raw file

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