]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/DiskCacheConfig.java
Version 10, May 2010
[GpsPrune.git] / tim / prune / function / DiskCacheConfig.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.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
10 import java.io.File;
11
12 import javax.swing.JButton;
13 import javax.swing.JCheckBox;
14 import javax.swing.JDialog;
15 import javax.swing.JFileChooser;
16 import javax.swing.JLabel;
17 import javax.swing.JOptionPane;
18 import javax.swing.JPanel;
19 import javax.swing.JTextField;
20
21 import tim.prune.App;
22 import tim.prune.DataSubscriber;
23 import tim.prune.GenericFunction;
24 import tim.prune.I18nManager;
25 import tim.prune.UpdateMessageBroker;
26 import tim.prune.config.Config;
27
28 /**
29  * Class to show the popup window for setting the path to disk cache
30  */
31 public class DiskCacheConfig extends GenericFunction
32 {
33         private JDialog _dialog = null;
34         private JCheckBox _cacheCheckbox = null;
35         private JTextField _cacheDirBox = null;
36         private JButton _browseButton = null;
37         private JButton _okButton = null;
38         private boolean _initialCheckState = false;
39         private String _initialCacheDir = null;
40
41         /**
42          * Constructor
43          * @param inApp app object
44          */
45         public DiskCacheConfig(App inApp)
46         {
47                 super(inApp);
48         }
49
50         /**
51          * Return the name key for this function
52          */
53         public String getNameKey()
54         {
55                 return "function.diskcache";
56         }
57
58         /**
59          * @return the contents of the window as a Component
60          */
61         private Component makeContents()
62         {
63                 JPanel dialogPanel = new JPanel();
64                 dialogPanel.setLayout(new BorderLayout(0, 5));
65                 // top panel
66                 JPanel topPanel = new JPanel();
67                 _cacheCheckbox = new JCheckBox(I18nManager.getText("dialog.diskcache.save"));
68                 _cacheCheckbox.addActionListener(new ActionListener() {
69                         public void actionPerformed(ActionEvent arg0) {
70                                 enableOk();
71                         }
72                 });
73                 topPanel.add(_cacheCheckbox);
74                 dialogPanel.add(topPanel, BorderLayout.NORTH);
75                 // dir panel
76                 JPanel dirPanel = new JPanel();
77                 dirPanel.setLayout(new BorderLayout());
78                 dirPanel.add(new JLabel(I18nManager.getText("dialog.diskcache.dir")), BorderLayout.WEST);
79                 _cacheDirBox = new JTextField(24);
80                 _cacheDirBox.addKeyListener(new KeyAdapter() {
81                         public void keyReleased(KeyEvent arg0) {
82                                 super.keyReleased(arg0);
83                                 enableOk();
84                         }
85                 });
86                 dirPanel.add(_cacheDirBox, BorderLayout.CENTER);
87                 _browseButton = new JButton(I18nManager.getText("button.browse"));
88                 _browseButton.addActionListener(new ActionListener() {
89                         public void actionPerformed(ActionEvent arg0) {
90                                 chooseDir();
91                         }
92                 });
93                 dirPanel.add(_browseButton, BorderLayout.EAST);
94                 dialogPanel.add(dirPanel, BorderLayout.CENTER);
95
96                 // Cancel button at the bottom
97                 JPanel buttonPanel = new JPanel();
98                 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
99                 _okButton = new JButton(I18nManager.getText("button.ok"));
100                 _okButton.addActionListener(new ActionListener()
101                 {
102                         public void actionPerformed(ActionEvent e)
103                         {
104                                 finish();
105                         }
106                 });
107                 buttonPanel.add(_okButton);
108                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
109                 cancelButton.addActionListener(new ActionListener() {
110                         public void actionPerformed(ActionEvent e)
111                         {
112                                 _dialog.dispose();
113                         }
114                 });
115                 buttonPanel.add(cancelButton);
116                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
117                 return dialogPanel;
118         }
119
120         /**
121          * Enable or disable the ok button according to what's changed
122          */
123         private void enableOk()
124         {
125                 boolean checkState = _cacheCheckbox.isSelected();
126                 _cacheDirBox.setEditable(checkState);
127                 _browseButton.setEnabled(checkState);
128                 boolean ok = false;
129                 // If checkbox has stayed off then disable ok
130                 if (!_initialCheckState && !checkState) {ok = false;}
131                 else {
132                         // If checkbox has been switched off then enable
133                         if (!checkState) {ok = true;}
134                         else {
135                                 // checkbox is on, check value
136                                 String path = _cacheDirBox.getText();
137                                 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
138                                         // Value blank or same as before
139                                         ok = false;
140                                 }
141                                 else {
142                                         ok = true;
143                                 }
144                         }
145                 }
146                 _okButton.setEnabled(ok);
147         }
148
149         /**
150          * Show window
151          */
152         public void begin()
153         {
154                 if (_dialog == null)
155                 {
156                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
157                         _dialog.setLocationRelativeTo(_parentFrame);
158                         _dialog.getContentPane().add(makeContents());
159                         _dialog.pack();
160                 }
161                 // Set controls according to current config
162                 String currPath = Config.getConfigString(Config.KEY_DISK_CACHE);
163                 _cacheCheckbox.setSelected(currPath != null);
164                 _cacheDirBox.setText(currPath==null?"":currPath);
165                 enableOk();
166                 // Remember current state
167                 _initialCheckState = _cacheCheckbox.isSelected();
168                 _dialog.setVisible(true);
169         }
170
171         /**
172          * Function activated by the "Browse..." button to select a directory for the cache
173          */
174         private void chooseDir()
175         {
176                 JFileChooser chooser = new JFileChooser();
177                 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
178                 // Set start path from currently selected dir
179                 String path = _cacheDirBox.getText();
180                 if (path.length() > 1) {chooser.setCurrentDirectory(new File(path));}
181                 if (chooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
182                 {
183                         _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
184                 }
185                 enableOk();
186         }
187
188         /**
189          * OK pressed, save selected settings in Config
190          */
191         private void finish()
192         {
193                 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
194                 // Create dir if it doesn't exist already and creation confirmed
195                 if (cachePath != null)
196                 {
197                         File cacheDir = new File(cachePath);
198                         if ((!cacheDir.exists() || !cacheDir.isDirectory()) && (JOptionPane.showConfirmDialog(_dialog,
199                                 I18nManager.getText("dialog.diskcache.createdir") + ": " + cacheDir.getAbsolutePath() + " ?",
200                                 I18nManager.getText(getNameKey()), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION || !cacheDir.mkdir()))
201                         {
202                                 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
203                                         I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
204                                 return;
205                         }
206                 }
207                 Config.setConfigString(Config.KEY_DISK_CACHE, cachePath);
208                 // inform subscribers so that tiles are wiped from memory and refetched
209                 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
210                 _dialog.dispose();
211         }
212 }