]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/settings/SaveConfig.java
Version 18.6, December 2016
[GpsPrune.git] / tim / prune / function / settings / SaveConfig.java
1 package tim.prune.function.settings;
2
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.GridLayout;
7 import java.awt.Rectangle;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.io.File;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.util.Enumeration;
14 import java.util.Properties;
15
16 import javax.swing.BorderFactory;
17 import javax.swing.JButton;
18 import javax.swing.JDialog;
19 import javax.swing.JFileChooser;
20 import javax.swing.JLabel;
21 import javax.swing.JPanel;
22
23 import tim.prune.App;
24 import tim.prune.GenericFunction;
25 import tim.prune.I18nManager;
26 import tim.prune.config.Config;
27
28 /**
29  * Class to provide the function to save the config settings
30  */
31 public class SaveConfig extends GenericFunction
32 {
33         private JDialog _dialog = null;
34
35         /**
36          * Constructor
37          * @param inApp application object for callback
38          */
39         public SaveConfig(App inApp)
40         {
41                 super(inApp);
42         }
43
44         /** Get the name key */
45         public String getNameKey() {
46                 return "function.saveconfig";
47         }
48
49         /**
50          * Begin the function
51          */
52         public void begin()
53         {
54                 // Make new dialog window (don't reuse it)
55                 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
56                 _dialog.setLocationRelativeTo(_parentFrame);
57                 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
58                 _dialog.getContentPane().add(makeDialogComponents());
59                 _dialog.pack();
60                 _dialog.setVisible(true);
61         }
62
63
64         /**
65          * Create dialog components
66          * @return Panel containing all gui elements in dialog
67          */
68         private Component makeDialogComponents()
69         {
70                 JPanel dialogPanel = new JPanel();
71                 dialogPanel.setLayout(new BorderLayout());
72                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
73                 JLabel descLabel = new JLabel(I18nManager.getText("dialog.saveconfig.desc"));
74                 dialogPanel.add(descLabel, BorderLayout.NORTH);
75
76                 // Grid panel in centre
77                 JPanel mainPanel = new JPanel();
78                 mainPanel.setLayout(new GridLayout(0, 2, 15, 2));
79                 mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
80                 Properties conf = Config.getAllConfig();
81                 Enumeration<Object> keys = conf.keys();
82                 while (keys.hasMoreElements())
83                 {
84                         String key = keys.nextElement().toString();
85                         String keyLabel = I18nManager.getText("dialog.saveconfig." + key);
86                         if (!keyLabel.equals("dialog.saveconfig." + key))
87                         {
88                                 mainPanel.add(new JLabel(keyLabel));
89                                 String val = conf.getProperty(key);
90                                 String tipText = val;
91                                 if (Config.isKeyBoolean(key)) {
92                                         val = Config.getConfigBoolean(key)?I18nManager.getText("dialog.about.yes"):I18nManager.getText("dialog.about.no");
93                                 }
94                                 else if (val != null && val.length() > 30) {
95                                         val = val.substring(0, 30) + " ...";
96                                 }
97                                 JLabel label = new JLabel(val);
98                                 label.setToolTipText(tipText);
99                                 mainPanel.add(label);
100                         }
101                 }
102                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
103
104                 // button panel at bottom
105                 JPanel buttonPanel = new JPanel();
106                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
107                 JButton okButton = new JButton(I18nManager.getText("button.ok"));
108                 okButton.addActionListener(new ActionListener() {
109                         public void actionPerformed(ActionEvent e)
110                         {
111                                 finish();
112                         }
113                 });
114                 buttonPanel.add(okButton);
115                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
116                 cancelButton.addActionListener(new ActionListener() {
117                         public void actionPerformed(ActionEvent e)
118                         {
119                                 _dialog.dispose();
120                                 _dialog = null;
121                         }
122                 });
123                 buttonPanel.add(cancelButton);
124                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
125                 return dialogPanel;
126         }
127
128
129         /**
130          * Finish the dialog when OK pressed
131          */
132         private void finish()
133         {
134                 File configFile = Config.getConfigFile();
135                 if (configFile == null) {configFile = Config.HOME_CONFIG_FILE;}
136                 JFileChooser chooser = new JFileChooser(configFile.getAbsoluteFile().getParent());
137                 chooser.setSelectedFile(configFile);
138                 int response = chooser.showSaveDialog(_parentFrame);
139                 if (response == JFileChooser.APPROVE_OPTION)
140                 {
141                         File saveFile = chooser.getSelectedFile();
142                         saveConfig(saveFile);
143                 }
144                 _dialog.dispose();
145                 _dialog = null;
146         }
147
148         /**
149          * Autosave the settings file without any prompts
150          */
151         public void silentSave()
152         {
153                 saveConfig(Config.getConfigFile());
154         }
155
156         /**
157          * Actually save the config file
158          * @param inSaveFile file to save to
159          */
160         private void saveConfig(File inSaveFile)
161         {
162                 // Set current window position in config
163                 Rectangle currBounds = _app.getFrame().getBounds();
164                 String windowBounds = "" + currBounds.x + "x" + currBounds.y + "x"
165                         + currBounds.width + "x" + currBounds.height;
166                 Config.setConfigString(Config.KEY_WINDOW_BOUNDS, windowBounds);
167
168                 // TODO: Check for null inSaveFile, then just call finish() ?
169                 FileOutputStream outStream = null;
170                 try
171                 {
172                         outStream = new FileOutputStream(inSaveFile);
173                         Config.getAllConfig().store(outStream, "GpsPrune config file");
174                 }
175                 catch (IOException ioe) {
176                         _app.showErrorMessageNoLookup(getNameKey(),
177                                 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
178                 }
179                 catch (NullPointerException npe) {} // no config file given
180                 finally {
181                         try {outStream.close();} catch (Exception e) {}
182                 }
183         }
184 }