]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/SaveConfig.java
6c0e4d089cedcfc43e9b17ef7f5c0f328efcd6fb
[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                                 if (Config.isKeyBoolean(key)) {
90                                         val = Config.getConfigBoolean(key)?I18nManager.getText("dialog.about.yes"):I18nManager.getText("dialog.about.no");
91                                 }
92                                 else if (val != null && val.length() > 30) {
93                                         val = val.substring(0, 30) + " ...";
94                                 }
95                                 mainPanel.add(new JLabel(val));
96                         }
97                 }
98                 dialogPanel.add(mainPanel, BorderLayout.CENTER);
99
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)
106                         {
107                                 finish();
108                         }
109                 });
110                 buttonPanel.add(okButton);
111                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
112                 cancelButton.addActionListener(new ActionListener() {
113                         public void actionPerformed(ActionEvent e)
114                         {
115                                 _dialog.dispose();
116                                 _dialog = null;
117                         }
118                 });
119                 buttonPanel.add(cancelButton);
120                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
121                 return dialogPanel;
122         }
123
124
125         /**
126          * Finish the dialog when OK pressed
127          */
128         private void finish()
129         {
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)
136                 {
137                         File saveFile = chooser.getSelectedFile();
138                         FileOutputStream outStream = null;
139                         try
140                         {
141                                 outStream = new FileOutputStream(saveFile);
142                                 Config.getAllConfig().store(outStream, "Prune config file");
143                         }
144                         catch (IOException ioe) {
145                                 _app.showErrorMessageNoLookup(getNameKey(),
146                                         I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
147                         }
148                         finally {
149                                 try {outStream.close();} catch (Exception e) {}
150                         }
151                 }
152                 _dialog.dispose();
153                 _dialog = null;
154         }
155 }