]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/function/DiskCacheConfig.java
1a4fdb19f1c7310b1613d45377abcf4d92c55f79
[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                                 _dialog.dispose();
110                         }
111                 });
112                 buttonPanelr.add(_okButton);
113                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
114                 cancelButton.addActionListener(new ActionListener() {
115                         public void actionPerformed(ActionEvent e)
116                         {
117                                 _dialog.dispose();
118                         }
119                 });
120                 buttonPanelr.add(cancelButton);
121
122                 // Manage button at the bottom left
123                 JPanel buttonPanell = new JPanel();
124                 buttonPanell.setLayout(new FlowLayout(FlowLayout.LEFT));
125                 _manageButton = new JButton(I18nManager.getText("button.manage"));
126                 _manageButton.addActionListener(new ActionListener() {
127                         public void actionPerformed(ActionEvent e)
128                         {
129                                 finish();
130                                 new ManageCacheFunction(_app).begin();
131                         }
132                 });
133                 buttonPanell.add(_manageButton);
134                 // Put them together
135                 JPanel buttonPanel = new JPanel();
136                 buttonPanel.setLayout(new BorderLayout());
137                 buttonPanel.add(buttonPanelr, BorderLayout.EAST);
138                 buttonPanel.add(buttonPanell, BorderLayout.WEST);
139                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
140                 return dialogPanel;
141         }
142
143         /**
144          * Enable or disable the buttons according to what's changed
145          */
146         private void enableButtons()
147         {
148                 final boolean checkState = _cacheCheckbox.isSelected();
149                 final String path = _cacheDirBox.getText();
150                 _cacheDirBox.setEditable(checkState);
151                 _browseButton.setEnabled(checkState);
152                 boolean ok = false;
153                 // If checkbox has stayed off then disable ok
154                 if (!_initialCheckState && !checkState) {ok = false;}
155                 else {
156                         // If checkbox has been switched off then enable
157                         if (!checkState) {ok = true;}
158                         else
159                         {
160                                 // checkbox is on, check value
161                                 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
162                                         // Value blank or same as before
163                                         ok = false;
164                                 }
165                                 else {
166                                         ok = true;
167                                 }
168                         }
169                 }
170                 _okButton.setEnabled(ok);
171                 // Manage button needs a valid cache
172                 boolean cacheDirGood = false;
173                 if (checkState && !path.equals(""))
174                 {
175                         File dir = new File(path);
176                         cacheDirGood = dir.exists() && dir.canRead() && dir.isDirectory();
177                 }
178                 _manageButton.setEnabled(cacheDirGood);
179         }
180
181         /**
182          * Show window
183          */
184         public void begin()
185         {
186                 if (_dialog == null)
187                 {
188                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
189                         _dialog.setLocationRelativeTo(_parentFrame);
190                         _dialog.getContentPane().add(makeContents());
191                         _dialog.pack();
192                 }
193                 // Set controls according to current config
194                 String currPath = Config.getConfigString(Config.KEY_DISK_CACHE);
195                 _cacheCheckbox.setSelected(currPath != null);
196                 _cacheDirBox.setText(currPath==null?"":currPath);
197                 enableButtons();
198                 // Remember current state
199                 _initialCheckState = _cacheCheckbox.isSelected();
200                 _dialog.setVisible(true);
201         }
202
203         /**
204          * Function activated by the "Browse..." button to select a directory for the cache
205          */
206         private void chooseDir()
207         {
208                 JFileChooser chooser = new JFileChooser();
209                 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
210                 // Set start path from currently selected dir
211                 String path = _cacheDirBox.getText();
212                 if (path.length() > 1) {chooser.setCurrentDirectory(new File(path));}
213                 if (chooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
214                 {
215                         _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
216                 }
217                 enableButtons();
218         }
219
220         /**
221          * OK pressed, save selected settings in Config
222          */
223         private void finish()
224         {
225                 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
226                 // Create dir if it doesn't exist already and creation confirmed
227                 if (cachePath != null)
228                 {
229                         File cacheDir = new File(cachePath);
230                         if ((!cacheDir.exists() || !cacheDir.isDirectory()) && (JOptionPane.showConfirmDialog(_dialog,
231                                 I18nManager.getText("dialog.diskcache.createdir") + ": " + cacheDir.getAbsolutePath() + " ?",
232                                 I18nManager.getText(getNameKey()), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION || !cacheDir.mkdir()))
233                         {
234                                 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
235                                         I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
236                                 return;
237                         }
238                         // TODO: Check path is writeable too, and give warning if not
239                 }
240                 Config.setConfigString(Config.KEY_DISK_CACHE, cachePath);
241                 // inform subscribers so that tiles are wiped from memory and refetched
242                 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);
243         }
244 }