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