1 package tim.prune.function;
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;
12 import javax.swing.BorderFactory;
13 import javax.swing.JButton;
14 import javax.swing.JCheckBox;
15 import javax.swing.JDialog;
16 import javax.swing.JFileChooser;
17 import javax.swing.JLabel;
18 import javax.swing.JOptionPane;
19 import javax.swing.JPanel;
20 import javax.swing.JTextField;
21 import javax.swing.border.EtchedBorder;
24 import tim.prune.DataSubscriber;
25 import tim.prune.GenericFunction;
26 import tim.prune.I18nManager;
27 import tim.prune.UpdateMessageBroker;
28 import tim.prune.config.Config;
29 import tim.prune.function.cache.ManageCacheFunction;
32 * Class to show the popup window for setting the path to disk cache
34 public class DiskCacheConfig extends GenericFunction
36 private JDialog _dialog = null;
37 private JCheckBox _cacheCheckbox = null;
38 private JTextField _cacheDirBox = null;
39 private JButton _browseButton = null;
40 private JButton _okButton = null, _manageButton = null;
41 private boolean _initialCheckState = false;
42 private String _initialCacheDir = null;
46 * @param inApp app object
48 public DiskCacheConfig(App inApp)
54 * Return the name key for this function
56 public String getNameKey()
58 return "function.diskcache";
62 * @return the contents of the window as a Component
64 private Component makeContents()
66 JPanel dialogPanel = new JPanel();
67 dialogPanel.setBorder(BorderFactory.createCompoundBorder(
68 BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(4, 4, 4, 4))
70 dialogPanel.setLayout(new BorderLayout(0, 5));
72 JPanel topPanel = new JPanel();
73 _cacheCheckbox = new JCheckBox(I18nManager.getText("dialog.diskcache.save"));
74 _cacheCheckbox.addActionListener(new ActionListener() {
75 public void actionPerformed(ActionEvent arg0) {
79 topPanel.add(_cacheCheckbox);
80 dialogPanel.add(topPanel, BorderLayout.NORTH);
82 JPanel dirPanel = new JPanel();
83 dirPanel.setLayout(new BorderLayout());
84 dirPanel.add(new JLabel(I18nManager.getText("dialog.diskcache.dir") + " : "), BorderLayout.WEST);
85 _cacheDirBox = new JTextField(24);
86 _cacheDirBox.addKeyListener(new KeyAdapter() {
87 public void keyReleased(KeyEvent arg0) {
88 super.keyReleased(arg0);
92 dirPanel.add(_cacheDirBox, BorderLayout.CENTER);
93 _browseButton = new JButton(I18nManager.getText("button.browse"));
94 _browseButton.addActionListener(new ActionListener() {
95 public void actionPerformed(ActionEvent arg0) {
99 dirPanel.add(_browseButton, BorderLayout.EAST);
100 // holder panel so it doesn't expand vertically
101 JPanel dirHolderPanel = new JPanel();
102 dirHolderPanel.setLayout(new BorderLayout());
103 dirHolderPanel.add(dirPanel, BorderLayout.NORTH);
104 dialogPanel.add(dirHolderPanel, BorderLayout.CENTER);
106 // OK, Cancel buttons at the bottom right
107 JPanel buttonPanelr = new JPanel();
108 buttonPanelr.setLayout(new FlowLayout(FlowLayout.RIGHT));
109 _okButton = new JButton(I18nManager.getText("button.ok"));
110 _okButton.addActionListener(new ActionListener() {
111 public void actionPerformed(ActionEvent e)
117 buttonPanelr.add(_okButton);
118 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
119 cancelButton.addActionListener(new ActionListener() {
120 public void actionPerformed(ActionEvent e)
125 buttonPanelr.add(cancelButton);
127 // Manage button at the bottom left
128 JPanel buttonPanell = new JPanel();
129 buttonPanell.setLayout(new FlowLayout(FlowLayout.LEFT));
130 _manageButton = new JButton(I18nManager.getText("button.manage"));
131 _manageButton.addActionListener(new ActionListener() {
132 public void actionPerformed(ActionEvent e)
135 new ManageCacheFunction(_app).begin();
138 buttonPanell.add(_manageButton);
140 JPanel buttonPanel = new JPanel();
141 buttonPanel.setLayout(new BorderLayout());
142 buttonPanel.add(buttonPanelr, BorderLayout.EAST);
143 buttonPanel.add(buttonPanell, BorderLayout.WEST);
144 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
149 * Enable or disable the buttons according to what's changed
151 private void enableButtons()
153 final boolean checkState = _cacheCheckbox.isSelected();
154 final String path = _cacheDirBox.getText();
155 _cacheDirBox.setEditable(checkState);
156 _browseButton.setEnabled(checkState);
158 // If checkbox has stayed off then disable ok
159 if (!_initialCheckState && !checkState) {ok = false;}
161 // If checkbox has been switched off then enable
162 if (!checkState) {ok = true;}
165 // checkbox is on, check value
166 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
167 // Value blank or same as before
175 _okButton.setEnabled(ok);
176 // Manage button needs a valid cache
177 boolean cacheDirGood = false;
178 if (checkState && !path.equals(""))
180 File dir = new File(path);
181 cacheDirGood = dir.exists() && dir.canRead() && dir.isDirectory();
183 _manageButton.setEnabled(cacheDirGood);
193 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
194 _dialog.setLocationRelativeTo(_parentFrame);
195 _dialog.getContentPane().add(makeContents());
198 // Set controls according to current config
199 String currPath = Config.getConfigString(Config.KEY_DISK_CACHE);
200 _cacheCheckbox.setSelected(currPath != null);
201 _cacheDirBox.setText(currPath==null?"":currPath);
203 // Remember current state
204 _initialCheckState = _cacheCheckbox.isSelected();
205 _dialog.setVisible(true);
209 * Function activated by the "Browse..." button to select a directory for the cache
211 private void chooseDir()
213 JFileChooser chooser = new JFileChooser();
214 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
215 // Set start path from currently selected dir
216 String path = _cacheDirBox.getText();
217 if (path.length() > 1) {chooser.setCurrentDirectory(new File(path));}
218 if (chooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
220 _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
226 * OK pressed, save selected settings in Config
228 private void finish()
230 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
231 // Create dir if it doesn't exist already and creation confirmed
232 if (cachePath != null)
234 File cacheDir = new File(cachePath);
235 if ((!cacheDir.exists() || !cacheDir.isDirectory()) && (JOptionPane.showConfirmDialog(_dialog,
236 I18nManager.getText("dialog.diskcache.createdir") + ": " + cacheDir.getAbsolutePath() + " ?",
237 I18nManager.getText(getNameKey()), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION || !cacheDir.mkdir()))
239 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
240 I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
243 // Check that the cache path is writable too, and give warning if not
244 if (cacheDir.exists() && cacheDir.isDirectory() && !cacheDir.canWrite())
246 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.cannotwrite"),
247 I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
250 Config.setConfigString(Config.KEY_DISK_CACHE, cachePath);
251 // inform subscribers so that tiles are wiped from memory and refetched
252 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);