1 package tim.prune.function.settings;
3 import java.awt.Rectangle;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
8 import javax.swing.JFileChooser;
11 import tim.prune.GenericFunction;
12 import tim.prune.I18nManager;
13 import tim.prune.config.Config;
16 * Class to provide the function to save the config settings
18 public class SaveConfig extends GenericFunction
22 * @param inApp application object for callback
24 public SaveConfig(App inApp)
29 /** Get the name key */
30 public String getNameKey() {
31 return "function.saveconfig";
39 File configFile = Config.getConfigFile();
40 if (configFile == null) {configFile = Config.HOME_CONFIG_FILE;}
41 JFileChooser chooser = new JFileChooser(configFile.getAbsoluteFile().getParent());
42 chooser.setSelectedFile(configFile);
43 int response = chooser.showSaveDialog(_parentFrame);
44 if (response == JFileChooser.APPROVE_OPTION)
46 File saveFile = chooser.getSelectedFile();
52 * Autosave the settings file without any prompts
54 public void silentSave()
56 saveConfig(Config.getConfigFile());
60 * Autosave has been turned on or off, so maybe need to save
61 * @param inSaveOn true if autosave was switched on
63 public void autosaveSwitched(boolean inSaveOn)
65 File configFile = Config.getConfigFile();
66 if (inSaveOn && configFile == null)
70 else if (!inSaveOn && configFile != null)
72 // TODO: Ask whether to save or not?
78 * Actually save the config file
79 * @param inSaveFile file to save to
81 private void saveConfig(File inSaveFile)
83 // Set current window position in config
84 Rectangle currBounds = _app.getFrame().getBounds();
85 String windowBounds = "" + currBounds.x + "x" + currBounds.y + "x"
86 + currBounds.width + "x" + currBounds.height;
87 Config.setConfigString(Config.KEY_WINDOW_BOUNDS, windowBounds);
89 FileOutputStream outStream = null;
92 outStream = new FileOutputStream(inSaveFile);
93 Config.getAllConfig().store(outStream, "GpsPrune config file");
95 catch (IOException ioe) {
96 _app.showErrorMessageNoLookup(getNameKey(),
97 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
99 catch (NullPointerException npe) {} // no config file given
101 try {outStream.close();} catch (Exception e) {}
103 // Remember where it was saved to
104 Config.setConfigFile(inSaveFile);