From 043fec588c38c0e90aca12a1342c3f5eb8c7b651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=F8=EDk?= Date: Sun, 16 Dec 2012 18:57:05 +0100 Subject: [PATCH] Separated configuration. Remembering last open/save/export directories. --- trunk/src/com/jpexs/asdec/Configuration.java | 162 ++++++++++++++++++ trunk/src/com/jpexs/asdec/Main.java | 76 ++------ .../com/jpexs/asdec/abc/gui/MainFrame.java | 4 +- .../com/jpexs/asdec/gui/proxy/ProxyFrame.java | 5 +- 4 files changed, 179 insertions(+), 68 deletions(-) create mode 100644 trunk/src/com/jpexs/asdec/Configuration.java diff --git a/trunk/src/com/jpexs/asdec/Configuration.java b/trunk/src/com/jpexs/asdec/Configuration.java new file mode 100644 index 000000000..103814799 --- /dev/null +++ b/trunk/src/com/jpexs/asdec/Configuration.java @@ -0,0 +1,162 @@ +/* + * Copyright (C) 2010-2012 JPEXS + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.jpexs.asdec; + +import com.jpexs.proxy.Replacement; +import java.io.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Configuration { + private static final String CONFIG_NAME="asdec.cfg"; + private static final String REPLACEMENTS_NAME="replacements.ini"; + private static HashMap config = new HashMap(); + + public static String getASDecHome() { + String dir = ".";//System.getProperty("user.home"); + if (!dir.endsWith(File.separator)) dir += File.separator; + dir += "config" + File.separator; + return dir; + } + + private static String getReplacementsFile() { + return getASDecHome() + REPLACEMENTS_NAME; + } + + private static String getConfigFile() { + return getASDecHome() + CONFIG_NAME; + } + + /** + * List of replacements + */ + public static java.util.List replacements = new ArrayList(); + + /** + * Saves replacements to file for future use + */ + private static void saveReplacements() { + try { + if(replacements.isEmpty()){ + File rf=new File(getReplacementsFile()); + if(rf.exists()) rf.delete(); + }else{ + File f = new File(getASDecHome()); + if (!f.exists()) f.mkdir(); + PrintWriter pw = new PrintWriter(new FileWriter(getReplacementsFile())); + for (Replacement r : replacements) { + pw.println(r.urlPattern); + pw.println(r.targetFile); + } + pw.close(); + } + } catch (IOException e) { + + } + } + + /** + * Load replacements from file + */ + private static void loadReplacements() { + replacements = new ArrayList(); + try { + BufferedReader br = new BufferedReader(new FileReader(getReplacementsFile())); + String s = ""; + while ((s = br.readLine()) != null) { + Replacement r = new Replacement(s, br.readLine()); + replacements.add(r); + } + br.close(); + } catch (IOException e) { + + } + } + + public static Object getConfig(String cfg) + { + return getConfig(cfg,null); + } + + + public static Object getConfig(String cfg, Object defaultValue) + { + if(!config.containsKey(cfg)) + { + return defaultValue; + } + return config.get(cfg); + } + + public static Object setConfig(String cfg,Object value) + { + return config.put(cfg,value); + } + + public static void load() + { + ObjectInputStream ois=null; + try { + ois=new ObjectInputStream(new FileInputStream(getConfigFile())); + config=(HashMap)ois.readObject(); + } catch (FileNotFoundException ex) { + + } catch (ClassNotFoundException cnf) { + + }catch (IOException ex) { + + } finally { + if(ois!=null){ + try { + ois.close(); + } catch (IOException ex1) { + //ignore + } + } + } + loadReplacements(); + } + + public static void save() + { + ObjectOutputStream oos=null; + try { + oos=new ObjectOutputStream(new FileOutputStream(getConfigFile())); + oos.writeObject(config); + } catch (FileNotFoundException ex) { + + } catch (IOException ex) { + + } finally { + if(oos!=null){ + try { + oos.close(); + } catch (IOException ex1) { + //ignore + } + } + } + saveReplacements(); + } + + public static List getReplacements() { + return replacements; + } + + +} diff --git a/trunk/src/com/jpexs/asdec/Main.java b/trunk/src/com/jpexs/asdec/Main.java index dc11f9d01..0b8e312a2 100644 --- a/trunk/src/com/jpexs/asdec/Main.java +++ b/trunk/src/com/jpexs/asdec/Main.java @@ -70,7 +70,7 @@ public class Main { public static boolean isCommandLineMode(){ return commandLineMode; } - + public static boolean DEBUG_COPY = false; /** Debug mode = throwing an error when comparing original file and recompiled */ public static boolean DEBUG_MODE = false; @@ -96,12 +96,8 @@ public class Main { return file; } - /** - * List of replacements - */ - public static java.util.List replacements = new ArrayList(); - - + + public static void setSubLimiter(boolean value) { if(value){ @@ -110,58 +106,6 @@ public class Main { AVM2Code.toSourceLimit=-1; } } - - private static String getASDecHome() { - String dir = ".";//System.getProperty("user.home"); - if (!dir.endsWith(File.separator)) dir += File.separator; - dir += "config" + File.separator; - return dir; - } - - private static String getReplacementsFile() { - return getASDecHome() + "replacements.ini"; - } - - /** - * Saves replacements to file for future use - */ - public static void saveReplacements() { - try { - if(replacements.isEmpty()){ - File rf=new File(getReplacementsFile()); - if(rf.exists()) rf.delete(); - }else{ - File f = new File(getASDecHome()); - if (!f.exists()) f.mkdir(); - PrintWriter pw = new PrintWriter(new FileWriter(getReplacementsFile())); - for (Replacement r : replacements) { - pw.println(r.urlPattern); - pw.println(r.targetFile); - } - pw.close(); - } - } catch (IOException e) { - - } - } - - /** - * Load replacements from file - */ - public static void loadReplacements() { - replacements = new ArrayList(); - try { - BufferedReader br = new BufferedReader(new FileReader(getReplacementsFile())); - String s = ""; - while ((s = br.readLine()) != null) { - Replacement r = new Replacement(s, br.readLine()); - replacements.add(r); - } - br.close(); - } catch (IOException e) { - - } - } public static boolean isWorking() { return working; @@ -294,6 +238,7 @@ public class Main { public static boolean saveFileDialog() { JFileChooser fc = new JFileChooser(); + fc.setCurrentDirectory(new File((String)Configuration.getConfig("lastSaveDir","."))); JFrame f=new JFrame(); View.setWindowIcon(f); int returnVal = fc.showSaveDialog(f); @@ -301,6 +246,7 @@ public class Main { File file = fc.getSelectedFile(); try { Main.saveFile(file.getAbsolutePath()); + Configuration.setConfig("lastSaveDir", file.getParentFile().getAbsolutePath()); maskURL = null; return true; } catch (IOException ex) { @@ -313,8 +259,7 @@ public class Main { public static boolean openFileDialog() { maskURL = null; JFileChooser fc = new JFileChooser(); - // FIXME debug only, think about keeping/removing it - fc.setCurrentDirectory(new File(".")); + fc.setCurrentDirectory(new File((String)Configuration.getConfig("lastOpenDir","."))); fc.setFileFilter(new FileFilter() { @Override @@ -332,6 +277,7 @@ public class Main { View.setWindowIcon(f); int returnVal = fc.showOpenDialog(f); if (returnVal == JFileChooser.APPROVE_OPTION) { + Configuration.setConfig("lastOpenDir", fc.getSelectedFile().getParentFile().getAbsolutePath()); File selfile = fc.getSelectedFile(); Main.openFile(selfile.getAbsolutePath()); return true; @@ -472,7 +418,7 @@ public class Main { */ public static void main(String[] args) throws IOException { View.setWinLookAndFeel(); - loadReplacements(); + Configuration.load(); if (args.length < 1) { showModeFrame(); } else { @@ -575,9 +521,9 @@ public class Main { public static String tempFile(String url) { - File f = new File(getASDecHome() + "saved" + File.separator); + File f = new File(Configuration.getASDecHome() + "saved" + File.separator); if (!f.exists()) f.mkdirs(); - return getASDecHome() + "saved" + File.separator + "asdec_" + Integer.toHexString(url.hashCode()) + ".tmp"; + return Configuration.getASDecHome() + "saved" + File.separator + "asdec_" + Integer.toHexString(url.hashCode()) + ".tmp"; } @@ -658,7 +604,7 @@ public class Main { } public static void exit() { - saveReplacements(); + Configuration.save(); System.exit(0); } diff --git a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java index 632bd6dcc..12bc4877d 100644 --- a/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java +++ b/trunk/src/com/jpexs/asdec/abc/gui/MainFrame.java @@ -17,6 +17,7 @@ package com.jpexs.asdec.abc.gui; +import com.jpexs.asdec.Configuration; import com.jpexs.asdec.Main; import com.jpexs.asdec.abc.ABC; import com.jpexs.asdec.abc.avm2.AVM2Code; @@ -392,13 +393,14 @@ public class MainFrame extends JFrame implements ActionListener, ItemListener { if (e.getActionCommand().equals("EXPORT")||e.getActionCommand().equals("EXPORTPCODE")) { JFileChooser chooser = new JFileChooser(); - chooser.setCurrentDirectory(new java.io.File(".")); + chooser.setCurrentDirectory(new java.io.File((String)Configuration.getConfig("lastExportDir", "."))); chooser.setDialogTitle("Select directory to export"); chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { Main.startWork("Exporting..."); final String selFile = chooser.getSelectedFile().getAbsolutePath(); + Configuration.setConfig("lastExportDir", chooser.getSelectedFile().getParentFile().getAbsolutePath()); final boolean isPcode=e.getActionCommand().equals("EXPORTPCODE"); (new Thread() { diff --git a/trunk/src/com/jpexs/asdec/gui/proxy/ProxyFrame.java b/trunk/src/com/jpexs/asdec/gui/proxy/ProxyFrame.java index 7752d68d4..44142bcfb 100644 --- a/trunk/src/com/jpexs/asdec/gui/proxy/ProxyFrame.java +++ b/trunk/src/com/jpexs/asdec/gui/proxy/ProxyFrame.java @@ -17,6 +17,7 @@ package com.jpexs.asdec.gui.proxy; +import com.jpexs.asdec.Configuration; import com.jpexs.asdec.Main; import com.jpexs.asdec.gui.View; import com.jpexs.proxy.CatchedListener; @@ -74,7 +75,7 @@ public class ProxyFrame extends JFrame implements ActionListener, CatchedListene */ public ProxyFrame() { - listModel = new SWFListModel(Main.replacements); + listModel = new SWFListModel(Configuration.getReplacements()); swfList = new JList(listModel); swfList.addMouseListener(this); swfList.setFont(new Font("Monospaced", Font.PLAIN, 12)); @@ -245,7 +246,7 @@ public class ProxyFrame extends JFrame implements ActionListener, CatchedListene catchedContentTypes.add("text/xml"); catchedContentTypes.add("application/xml"); catchedContentTypes.add("application/octet-stream"); - Server.startServer(port, Main.replacements, catchedContentTypes, this, this); + Server.startServer(port, Configuration.getReplacements(), catchedContentTypes, this, this); switchButton.setText("Stop proxy"); portField.setEditable(false); } else {