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