[Patchanim-commit] SF.net SVN: patchanim: [40] trunk/patchanim/src/com/mebigfatguy/patchanim
Brought to you by:
dbrosius
From: <dbr...@us...> - 2008-01-27 23:19:25
|
Revision: 40 http://patchanim.svn.sourceforge.net/patchanim/?rev=40&view=rev Author: dbrosius Date: 2008-01-27 15:19:28 -0800 (Sun, 27 Jan 2008) Log Message: ----------- add basic file io Modified Paths: -------------- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-01-27 22:29:19 UTC (rev 39) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/gui/JPatchAnimFrame.java 2008-01-27 23:19:28 UTC (rev 40) @@ -24,14 +24,20 @@ import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; +import java.io.File; +import java.io.IOException; import java.util.ResourceBundle; +import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import javax.swing.filechooser.FileFilter; import com.mebigfatguy.patchanim.PatchAnimDocument; +import com.mebigfatguy.patchanim.io.PatchAnimIO; import com.mebigfatguy.patchanim.main.PatchAnimBundle; public class JPatchAnimFrame extends JFrame { @@ -45,6 +51,7 @@ private JMenuItem saveAsItem; private JMenuItem quitItem; private PatchAnimDocument document; + private File documentLocation; public JPatchAnimFrame() { @@ -60,6 +67,7 @@ JPatchAnimPanel patchPanel = new JPatchAnimPanel(); document = new PatchAnimDocument(); + documentLocation = null; PatchPanelMediator mediator = PatchPanelMediator.getMediator(); mediator.setDocument(document); @@ -101,6 +109,29 @@ } }); + openItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent ae) { + //Ask save + load(); + } + }); + + saveItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent ae) { + if (documentLocation == null) { + saveAs(); + } else { + save(); + } + } + }); + + saveAsItem.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent ae) { + saveAs(); + } + }); + quitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { dispose(); @@ -108,4 +139,71 @@ } }); } + + private void load() { + try { + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + chooser.setFileFilter(new FileFilter() { + + @Override + public boolean accept(File f) { + if (f.isDirectory()) + return true; + return (f.getPath().endsWith("paf")); + } + + @Override + public String getDescription() { + return "PatchAnim Files (*.paf)"; + } + }); + chooser.setCurrentDirectory(new File(System.getProperty("user.dir"))); + int option = chooser.showOpenDialog(JPatchAnimFrame.this); + if (option == JFileChooser.APPROVE_OPTION) { + documentLocation = chooser.getSelectedFile(); + document = PatchAnimIO.loadFile(documentLocation); + } + } catch (Exception e) { + JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.SAVEFAILED); + documentLocation = null; + document = new PatchAnimDocument(); + } finally { + PatchPanelMediator mediator = PatchPanelMediator.getMediator(); + mediator.setDocument(document); + } + } + + private void saveAs() { + try { + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + File defLocation; + if (documentLocation == null) + defLocation = new File(System.getProperty("user.dir")); + else + defLocation = documentLocation.getParentFile(); + + chooser.setCurrentDirectory(defLocation); + int option = chooser.showSaveDialog(JPatchAnimFrame.this); + if (option == JFileChooser.APPROVE_OPTION) { + String path = chooser.getSelectedFile().getPath(); + if (!path.toLowerCase().endsWith(".paf")) + path += ".paf"; + documentLocation = new File(path); + PatchAnimIO.saveFile(documentLocation, document); + documentLocation = chooser.getSelectedFile(); + } + } catch (IOException ioe) { + JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.SAVEFAILED); + } + } + + private void save() { + try { + PatchAnimIO.saveFile(documentLocation, document); + } catch (IOException ioe) { + JOptionPane.showMessageDialog(JPatchAnimFrame.this, PatchAnimBundle.SAVEFAILED); + } + } } Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java 2008-01-27 22:29:19 UTC (rev 39) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/io/PatchAnimIO.java 2008-01-27 23:19:28 UTC (rev 40) @@ -1,15 +1,14 @@ package com.mebigfatguy.patchanim.io; import java.beans.XMLDecoder; -import java.beans.XMLEncoder; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import com.mebigfatguy.patchanim.PatchAnimDocument; @@ -20,23 +19,22 @@ } public static void saveFile(File f, PatchAnimDocument document) throws IOException { - OutputStream os = null; + ObjectOutputStream os = null; try { - os = new BufferedOutputStream(new FileOutputStream(f)); - XMLEncoder encoder = new XMLEncoder(os); - encoder.writeObject(document); - encoder.flush(); + document.setDirty(false); + os = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(f))); + os.writeObject(document); + os.flush(); } finally { Closer.close(os); } } - public static PatchAnimDocument loadFile(File f) throws IOException { - InputStream is = null; + public static PatchAnimDocument loadFile(File f) throws IOException, ClassNotFoundException { + ObjectInputStream is = null; try { - is = new BufferedInputStream(new FileInputStream(f)); - XMLDecoder decoder = new XMLDecoder(is); - return (PatchAnimDocument)decoder.readObject(); + is = new ObjectInputStream(new BufferedInputStream(new FileInputStream(f))); + return (PatchAnimDocument)is.readObject(); } finally { Closer.close(is); } Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-01-27 22:29:19 UTC (rev 39) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/main/PatchAnimBundle.java 2008-01-27 23:19:28 UTC (rev 40) @@ -47,6 +47,7 @@ public static final String ADD = "patchanim.add"; public static final String REMOVE = "patchanim.remove"; public static final String COLOR = "patchanim.color"; + public static final String SAVEFAILED = "patchanim.err.savefailed"; private static ResourceBundle rb = ResourceBundle.getBundle("com/mebigfatguy/patchanim/resources"); Modified: trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties =================================================================== --- trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-01-27 22:29:19 UTC (rev 39) +++ trunk/patchanim/src/com/mebigfatguy/patchanim/resources.properties 2008-01-27 23:19:28 UTC (rev 40) @@ -40,3 +40,4 @@ patchanim.add = Add patchanim.remove = Remove patchanim.color = Color +patchanim.err.savefailed = Failed saving Patch Animation File This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |