From ac503cff3ac7e619ca23e727ff3b160b2aaf077a Mon Sep 17 00:00:00 2001 From: 7Durandal7 <7durandal7@cardforge.org> Date: Fri, 11 May 2012 22:22:09 +0000 Subject: [PATCH] Added "Save Layout" feature. Save dialog function in forge.gui.toolbox improved. Save layout functions in SIOUtil slightly modified to accept these changes. --- .../java/forge/gui/framework/SIOUtil.java | 26 ++++++++++--- .../forge/gui/framework/SRearrangingUtil.java | 2 +- .../forge/gui/framework/SResizingUtil.java | 2 +- .../forge/gui/match/controllers/CDock.java | 27 +++++++++++-- .../java/forge/gui/match/views/VDock.java | 7 ++++ src/main/java/forge/gui/toolbox/FSkin.java | 1 + .../forge/gui/toolbox/SaveOpenDialog.java | 38 +++++++++++++++---- 7 files changed, 83 insertions(+), 20 deletions(-) diff --git a/src/main/java/forge/gui/framework/SIOUtil.java b/src/main/java/forge/gui/framework/SIOUtil.java index ecede89a1df..caf5cf34482 100644 --- a/src/main/java/forge/gui/framework/SIOUtil.java +++ b/src/main/java/forge/gui/framework/SIOUtil.java @@ -43,14 +43,19 @@ public final class SIOUtil { private static final XMLEvent NEWLINE = EF.createDTD("\n"); private static final XMLEvent TAB = EF.createDTD("\t"); - /** Publicly-accessible save method, to neatly handle exception handling. */ - public static void saveLayout() { + /** Publicly-accessible save method, to neatly handle exception handling. + * @param f0 file to save layout to, if null, saves to FILE_PREFERRED + * + * + */ + public static void saveLayout(final File f0) { if (SwingUtilities.isEventDispatchThread()) { throw new IllegalThreadStateException("This operation should be independent of the EDT."); } + + try { save(f0); } + catch (final Exception e) { e.printStackTrace(); } - try { save(); } - catch (final Exception e) { e.printStackTrace(); } } /** @@ -66,9 +71,18 @@ public final class SIOUtil { catch (final Exception e) { e.printStackTrace(); } } - private static void save() throws Exception { + private static void save(final File f0) throws Exception { + + final String fWriteTo; + if (f0==null) { + fWriteTo = FILE_PREFERRED; + } + else { + fWriteTo = f0.getPath(); + } + final XMLOutputFactory out = XMLOutputFactory.newInstance(); - final XMLEventWriter writer = out.createXMLEventWriter(new FileOutputStream(FILE_PREFERRED)); + final XMLEventWriter writer = out.createXMLEventWriter(new FileOutputStream(fWriteTo)); final List cells = FView.SINGLETON_INSTANCE.getDragCells(); final JPanel pnl = FView.SINGLETON_INSTANCE.getPnlContent(); double x0, y0, w0, h0; diff --git a/src/main/java/forge/gui/framework/SRearrangingUtil.java b/src/main/java/forge/gui/framework/SRearrangingUtil.java index c4fbcb14995..22f8732deea 100644 --- a/src/main/java/forge/gui/framework/SRearrangingUtil.java +++ b/src/main/java/forge/gui/framework/SRearrangingUtil.java @@ -307,7 +307,7 @@ public final class SRearrangingUtil { updateBorders(); final Thread t = new Thread() { @Override - public void run() { SIOUtil.saveLayout(); } }; + public void run() { SIOUtil.saveLayout(null); } }; t.start(); } diff --git a/src/main/java/forge/gui/framework/SResizingUtil.java b/src/main/java/forge/gui/framework/SResizingUtil.java index 1ff77fa20d2..d59630a7524 100644 --- a/src/main/java/forge/gui/framework/SResizingUtil.java +++ b/src/main/java/forge/gui/framework/SResizingUtil.java @@ -332,7 +332,7 @@ public final class SResizingUtil { /** */ public static void endResize() { final Thread t = new Thread() { @Override - public void run() { SIOUtil.saveLayout(); } }; + public void run() { SIOUtil.saveLayout(null); } }; t.start(); } diff --git a/src/main/java/forge/gui/match/controllers/CDock.java b/src/main/java/forge/gui/match/controllers/CDock.java index 8a282ae130a..f1a8fa72716 100644 --- a/src/main/java/forge/gui/match/controllers/CDock.java +++ b/src/main/java/forge/gui/match/controllers/CDock.java @@ -83,6 +83,22 @@ public enum CDock implements ICDoc { }; w.execute(); } + + private void saveLayout() { + final SwingWorker w = new SwingWorker() { + @Override + public Void doInBackground() { + SaveOpenDialog dlgSave = new SaveOpenDialog(); + File DefFile = new File(SIOUtil.FILE_PREFERRED); + File SaveFile = dlgSave.SaveDialog(DefFile, Filetypes.LAYOUT); + if (SaveFile!=null) { + SIOUtil.saveLayout(SaveFile); + } + return null; + } + }; + w.execute(); + } private void openLayout() { SOverlayUtils.genericOverlay(); @@ -98,11 +114,9 @@ public enum CDock implements ICDoc { if (LoadFile!=null) { SIOUtil.loadLayout(LoadFile); + SIOUtil.saveLayout(null); } - else { - SIOUtil.loadLayout(DefFile); - } - SIOUtil.saveLayout(); + SOverlayUtils.hideOverlay(); return null; } @@ -235,6 +249,11 @@ public enum CDock implements ICDoc { .addMouseListener(new MouseAdapter() { @Override public void mousePressed(final MouseEvent e) { openLayout(); } }); + + VDock.SINGLETON_INSTANCE.getBtnSaveLayout() + .addMouseListener(new MouseAdapter() { @Override + public void mousePressed(final MouseEvent e) { + saveLayout(); } }); } /* (non-Javadoc) diff --git a/src/main/java/forge/gui/match/views/VDock.java b/src/main/java/forge/gui/match/views/VDock.java index 8e544903904..a69f21c7ecf 100644 --- a/src/main/java/forge/gui/match/views/VDock.java +++ b/src/main/java/forge/gui/match/views/VDock.java @@ -63,6 +63,8 @@ public enum VDock implements IVDoc { new DockButton(FSkin.getIcon(FSkin.DockIcons.ICO_REVERTLAYOUT), "Revert Layout"); private final JLabel btnOpenLayout = new DockButton(FSkin.getIcon(FSkin.DockIcons.ICO_OPENLAYOUT), "Open Layout"); + private final JLabel btnSaveLayout = + new DockButton(FSkin.getIcon(FSkin.DockIcons.ICO_SAVELAYOUT), "Save Layout"); //========= Overridden methods @@ -82,6 +84,7 @@ public enum VDock implements IVDoc { pnl.add(btnViewDeckList); pnl.add(btnRevertLayout); pnl.add(btnOpenLayout); + pnl.add(btnSaveLayout); } /* (non-Javadoc) @@ -154,6 +157,10 @@ public enum VDock implements IVDoc { public JLabel getBtnOpenLayout() { return btnOpenLayout; } + + public JLabel getBtnSaveLayout() { + return btnSaveLayout; + } //========= Custom class handling /** diff --git a/src/main/java/forge/gui/toolbox/FSkin.java b/src/main/java/forge/gui/toolbox/FSkin.java index 9bec76bf2a0..2d5ec0dbdb1 100644 --- a/src/main/java/forge/gui/toolbox/FSkin.java +++ b/src/main/java/forge/gui/toolbox/FSkin.java @@ -215,6 +215,7 @@ public enum FSkin { ICO_CONCEDE (new int[] {240, 640, 80, 80}), /** */ ICO_REVERTLAYOUT (new int[] {400, 720, 80, 80}), /** */ ICO_OPENLAYOUT (new int[] {480, 640, 80, 80}), + ICO_SAVELAYOUT (new int[] {480, 720, 80, 80}), ICO_DECKLIST (new int[] {400, 640, 80, 80}); private int[] coords; diff --git a/src/main/java/forge/gui/toolbox/SaveOpenDialog.java b/src/main/java/forge/gui/toolbox/SaveOpenDialog.java index 08d8569c965..800cbf33e6b 100644 --- a/src/main/java/forge/gui/toolbox/SaveOpenDialog.java +++ b/src/main/java/forge/gui/toolbox/SaveOpenDialog.java @@ -43,7 +43,8 @@ public class SaveOpenDialog extends JPanel { * */ public enum Filetypes { - LAYOUT ("Layout File", "xml"); + LAYOUT ("Layout File", "xml"), + DECK ("Deck File", "dck"); private final String TypeName; private final String TypeExtension; @@ -70,39 +71,60 @@ public class SaveOpenDialog extends JPanel { * * */ - public File OpenDialog(File defFileName, Filetypes type) { - File RetFile = defFileName; + public File OpenDialog(final File defFileName, final Filetypes type) { fc.setCurrentDirectory(defFileName); if (type!=null) { fc.setAcceptAllFileFilterUsed(false); - FileFilter filter = new FileNameExtensionFilter(type.TypeName, type.TypeExtension); + final FileFilter filter = new FileNameExtensionFilter(type.TypeName, type.TypeExtension); fc.addChoosableFileFilter(filter); } int RetValue = fc.showOpenDialog(getParent()); if (RetValue == JFileChooser.APPROVE_OPTION) { - RetFile = fc.getSelectedFile(); + final File RetFile = fc.getSelectedFile(); return RetFile; } return null; } /** - * Shows the save dialog. Not implemented anywhere yet. + * Shows the save dialog. + * + * @param defFileName default file name/directory to show when save dialog is opened + * @param type file types to show in dialog * * */ - public File SaveDialog(File defFileName) { + public File SaveDialog(final File defFileName, final Filetypes type) { File RetFile = defFileName; fc.setCurrentDirectory(defFileName); + /* set the file filter if desired */ + if (type!=null) { + fc.setAcceptAllFileFilterUsed(false); + final FileFilter filter = new FileNameExtensionFilter(type.TypeName, type.TypeExtension); + fc.addChoosableFileFilter(filter); + } + int RetValue = fc.showSaveDialog(getParent()); + + /* user picked save */ if (RetValue == JFileChooser.APPROVE_OPTION) { RetFile = fc.getSelectedFile(); + + /* Adds extension if it is known and not given */ + if (type!=null & !(RetFile.getAbsolutePath().endsWith(type.TypeExtension))) { + RetFile = new File(RetFile.getAbsolutePath()+"."+type.TypeExtension); + } + + + return RetFile; } - return RetFile; + + /* user picked cancel */ + return null; } } \ No newline at end of file