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;
14 import tim.prune.data.DoubleRange;
15 import tim.prune.data.Track;
18 * Class to provide the function to save the config settings
20 public class SaveConfig extends GenericFunction
24 * @param inApp application object for callback
26 public SaveConfig(App inApp)
31 /** Get the name key */
32 public String getNameKey() {
33 return "function.saveconfig";
41 File configFile = Config.getConfigFile();
42 if (configFile == null) {configFile = Config.HOME_CONFIG_FILE;}
43 JFileChooser chooser = new JFileChooser(configFile.getAbsoluteFile().getParent());
44 chooser.setSelectedFile(configFile);
45 int response = chooser.showSaveDialog(_parentFrame);
46 if (response == JFileChooser.APPROVE_OPTION)
48 File saveFile = chooser.getSelectedFile();
54 * Autosave the settings file without any prompts
56 public void silentSave()
58 saveConfig(Config.getConfigFile());
62 * Autosave has been turned on or off, so maybe need to save
63 * @param inSaveOn true if autosave was switched on
65 public void autosaveSwitched(boolean inSaveOn)
67 File configFile = Config.getConfigFile();
68 if (inSaveOn && configFile == null)
72 else if (!inSaveOn && configFile != null)
74 // TODO: Ask whether to save or not?
80 * Actually save the config file
81 * @param inSaveFile file to save to
83 private void saveConfig(File inSaveFile)
85 // Set current window position in config
86 Rectangle currBounds = _app.getFrame().getBounds();
87 String windowBounds = "" + currBounds.x + "x" + currBounds.y + "x"
88 + currBounds.width + "x" + currBounds.height;
89 Config.setConfigString(Config.KEY_WINDOW_BOUNDS, windowBounds);
91 final String latlonString = createLatLonStringForConfig();
92 if (latlonString != null)
94 Config.setConfigString(Config.KEY_LATLON_RANGE, latlonString);
97 FileOutputStream outStream = null;
100 outStream = new FileOutputStream(inSaveFile);
101 Config.getAllConfig().store(outStream, "GpsPrune config file");
103 catch (IOException ioe) {
104 _app.showErrorMessageNoLookup(getNameKey(),
105 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
107 catch (NullPointerException npe) {} // no config file given
109 try {outStream.close();} catch (Exception e) {}
111 // Remember where it was saved to
112 Config.setConfigFile(inSaveFile);
116 * @return semicolon-separated string containing the four values, or null
118 private String createLatLonStringForConfig()
120 Track track = _app.getTrackInfo().getTrack();
121 if (track.getNumPoints() >= 2)
123 final DoubleRange latRange = track.getLatRange();
124 final DoubleRange lonRange = track.getLonRange();
125 if (latRange.getRange() > 0.0 && lonRange.getRange() > 0.0)
127 StringBuffer buffer = new StringBuffer();
128 buffer.append(Double.toString(latRange.getMinimum()));
130 buffer.append(Double.toString(latRange.getMaximum()));
132 buffer.append(Double.toString(lonRange.getMinimum()));
134 buffer.append(Double.toString(lonRange.getMaximum()));
135 return buffer.toString();