1 package tim.prune.function;
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;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.util.Enumeration;
13 import java.util.Properties;
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;
23 import tim.prune.GenericFunction;
24 import tim.prune.I18nManager;
25 import tim.prune.config.Config;
28 * Class to provide the function to save the config settings
30 public class SaveConfig extends GenericFunction
32 private JDialog _dialog = null;
36 * @param inApp application object for callback
38 public SaveConfig(App inApp)
43 /** Get the name key */
44 public String getNameKey() {
45 return "function.saveconfig";
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());
59 _dialog.setVisible(true);
64 * Create dialog components
65 * @return Panel containing all gui elements in dialog
67 private Component makeDialogComponents()
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);
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())
83 String key = keys.nextElement().toString();
84 String keyLabel = I18nManager.getText("dialog.saveconfig." + key);
85 if (!keyLabel.equals("dialog.saveconfig." + key))
87 mainPanel.add(new JLabel(keyLabel));
88 String val = conf.getProperty(key);
89 if (Config.isKeyBoolean(key)) {
90 val = Config.getConfigBoolean(key)?I18nManager.getText("dialog.about.yes"):I18nManager.getText("dialog.about.no");
92 else if (val != null && val.length() > 30) {
93 val = val.substring(0, 30) + " ...";
95 mainPanel.add(new JLabel(val));
98 dialogPanel.add(mainPanel, BorderLayout.CENTER);
100 // button panel at bottom
101 JPanel buttonPanel = new JPanel();
102 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
103 JButton okButton = new JButton(I18nManager.getText("button.ok"));
104 okButton.addActionListener(new ActionListener() {
105 public void actionPerformed(ActionEvent e)
110 buttonPanel.add(okButton);
111 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
112 cancelButton.addActionListener(new ActionListener() {
113 public void actionPerformed(ActionEvent e)
119 buttonPanel.add(cancelButton);
120 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
126 * Finish the dialog when OK pressed
128 private void finish()
130 File configFile = Config.getConfigFile();
131 if (configFile == null) {configFile = new File(".pruneconfig");}
132 JFileChooser chooser = new JFileChooser(configFile.getAbsoluteFile().getParent());
133 chooser.setSelectedFile(configFile);
134 int response = chooser.showSaveDialog(_parentFrame);
135 if (response == JFileChooser.APPROVE_OPTION)
137 File saveFile = chooser.getSelectedFile();
140 Config.getAllConfig().store(new FileOutputStream(saveFile), "Prune config file");
142 catch (IOException ioe) {
143 _app.showErrorMessageNoLookup(getNameKey(),
144 I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());