]> 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
15 /**
16  * Class to provide the function to save the config settings
17  */
18 public class SaveConfig extends GenericFunction
19 {
20         /**
21          * Constructor
22          * @param inApp application object for callback
23          */
24         public SaveConfig(App inApp)
25         {
26                 super(inApp);
27         }
28
29         /** Get the name key */
30         public String getNameKey() {
31                 return "function.saveconfig";
32         }
33
34         /**
35          * Begin the function
36          */
37         public void begin()
38         {
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)
45                 {
46                         File saveFile = chooser.getSelectedFile();
47                         saveConfig(saveFile);
48                 }
49         }
50
51         /**
52          * Autosave the settings file without any prompts
53          */
54         public void silentSave()
55         {
56                 saveConfig(Config.getConfigFile());
57         }
58
59         /**
60          * Autosave has been turned on or off, so maybe need to save
61          * @param inSaveOn true if autosave was switched on
62          */
63         public void autosaveSwitched(boolean inSaveOn)
64         {
65                 File configFile = Config.getConfigFile();
66                 if (inSaveOn && configFile == null)
67                 {
68                         begin();
69                 }
70                 else if (!inSaveOn && configFile != null)
71                 {
72                         // TODO: Ask whether to save or not?
73                         silentSave();
74                 }
75         }
76
77         /**
78          * Actually save the config file
79          * @param inSaveFile file to save to
80          */
81         private void saveConfig(File inSaveFile)
82         {
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);
88
89                 FileOutputStream outStream = null;
90                 try
91                 {
92                         outStream = new FileOutputStream(inSaveFile);
93                         Config.getAllConfig().store(outStream, "GpsPrune config file");
94                 }
95                 catch (IOException ioe) {
96                         _app.showErrorMessageNoLookup(getNameKey(),
97                                 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
98                 }
99                 catch (NullPointerException npe) {} // no config file given
100                 finally {
101                         try {outStream.close();} catch (Exception e) {}
102                 }
103                 // Remember where it was saved to
104                 Config.setConfigFile(inSaveFile);
105         }
106 }