1 package tim.prune.function.settings;
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;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.util.Enumeration;
14 import java.util.Properties;
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;
24 import tim.prune.GenericFunction;
25 import tim.prune.I18nManager;
26 import tim.prune.config.Config;
29 * Class to provide the function to save the config settings
31 public class SaveConfig extends GenericFunction
33 private JDialog _dialog = null;
37 * @param inApp application object for callback
39 public SaveConfig(App inApp)
44 /** Get the name key */
45 public String getNameKey() {
46 return "function.saveconfig";
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());
60 _dialog.setVisible(true);
65 * Create dialog components
66 * @return Panel containing all gui elements in dialog
68 private Component makeDialogComponents()
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);
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())
84 String key = keys.nextElement().toString();
85 String keyLabel = I18nManager.getText("dialog.saveconfig." + key);
86 if (!keyLabel.equals("dialog.saveconfig." + key))
88 mainPanel.add(new JLabel(keyLabel));
89 String val = conf.getProperty(key);
91 if (Config.isKeyBoolean(key)) {
92 val = Config.getConfigBoolean(key)?I18nManager.getText("dialog.about.yes"):I18nManager.getText("dialog.about.no");
94 else if (val != null && val.length() > 30) {
95 val = val.substring(0, 30) + " ...";
97 JLabel label = new JLabel(val);
98 label.setToolTipText(tipText);
102 dialogPanel.add(mainPanel, BorderLayout.CENTER);
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)
114 buttonPanel.add(okButton);
115 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
116 cancelButton.addActionListener(new ActionListener() {
117 public void actionPerformed(ActionEvent e)
123 buttonPanel.add(cancelButton);
124 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
130 * Finish the dialog when OK pressed
132 private void finish()
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)
141 File saveFile = chooser.getSelectedFile();
142 saveConfig(saveFile);
149 * Autosave the settings file without any prompts
151 public void silentSave()
153 saveConfig(Config.getConfigFile());
157 * Actually save the config file
158 * @param inSaveFile file to save to
160 private void saveConfig(File inSaveFile)
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);
168 // TODO: Check for null inSaveFile, then just call finish() ?
169 FileOutputStream outStream = null;
172 outStream = new FileOutputStream(inSaveFile);
173 Config.getAllConfig().store(outStream, "GpsPrune config file");
175 catch (IOException ioe) {
176 _app.showErrorMessageNoLookup(getNameKey(),
177 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
179 catch (NullPointerException npe) {} // no config file given
181 try {outStream.close();} catch (Exception e) {}