]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/settings/SaveConfig.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / settings / SaveConfig.java
1 package tim.prune.function.settings;
2
3 import java.awt.Rectangle;
4 import java.io.File;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 import javax.swing.JFileChooser;
9
10 import tim.prune.App;
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;
16
17 /**
18  * Class to provide the function to save the config settings
19  */
20 public class SaveConfig extends GenericFunction
21 {
22         /**
23          * Constructor
24          * @param inApp application object for callback
25          */
26         public SaveConfig(App inApp)
27         {
28                 super(inApp);
29         }
30
31         /** Get the name key */
32         public String getNameKey() {
33                 return "function.saveconfig";
34         }
35
36         /**
37          * Begin the function
38          */
39         public void begin()
40         {
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)
47                 {
48                         File saveFile = chooser.getSelectedFile();
49                         saveConfig(saveFile);
50                 }
51         }
52
53         /**
54          * Autosave the settings file without any prompts
55          */
56         public void silentSave()
57         {
58                 saveConfig(Config.getConfigFile());
59         }
60
61         /**
62          * Autosave has been turned on or off, so maybe need to save
63          * @param inSaveOn true if autosave was switched on
64          */
65         public void autosaveSwitched(boolean inSaveOn)
66         {
67                 File configFile = Config.getConfigFile();
68                 if (inSaveOn && configFile == null)
69                 {
70                         begin();
71                 }
72                 else if (!inSaveOn && configFile != null)
73                 {
74                         // TODO: Ask whether to save or not?
75                         silentSave();
76                 }
77         }
78
79         /**
80          * Actually save the config file
81          * @param inSaveFile file to save to
82          */
83         private void saveConfig(File inSaveFile)
84         {
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);
90
91                 final String latlonString = createLatLonStringForConfig();
92                 if (latlonString != null)
93                 {
94                         Config.setConfigString(Config.KEY_LATLON_RANGE, latlonString);
95                 }
96
97                 FileOutputStream outStream = null;
98                 try
99                 {
100                         outStream = new FileOutputStream(inSaveFile);
101                         Config.getAllConfig().store(outStream, "GpsPrune config file");
102                 }
103                 catch (IOException ioe) {
104                         _app.showErrorMessageNoLookup(getNameKey(),
105                                 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
106                 }
107                 catch (NullPointerException npe) {} // no config file given
108                 finally {
109                         try {outStream.close();} catch (Exception e) {}
110                 }
111                 // Remember where it was saved to
112                 Config.setConfigFile(inSaveFile);
113         }
114
115         /**
116          * @return semicolon-separated string containing the four values, or null
117          */
118         private String createLatLonStringForConfig()
119         {
120                 Track track = _app.getTrackInfo().getTrack();
121                 if (track.getNumPoints() >= 2)
122                 {
123                         final DoubleRange latRange = track.getLatRange();
124                         final DoubleRange lonRange = track.getLonRange();
125                         if (latRange.getRange() > 0.0 && lonRange.getRange() > 0.0)
126                         {
127                                 StringBuffer buffer = new StringBuffer();
128                                 buffer.append(Double.toString(latRange.getMinimum()));
129                                 buffer.append(';');
130                                 buffer.append(Double.toString(latRange.getMaximum()));
131                                 buffer.append(';');
132                                 buffer.append(Double.toString(lonRange.getMinimum()));
133                                 buffer.append(';');
134                                 buffer.append(Double.toString(lonRange.getMaximum()));
135                                 return buffer.toString();
136                         }
137                 }
138                 return null;
139         }
140 }