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.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;
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;
30 * Class to show the popup window for setting the path to disk cache
32 public class DiskCacheConfig extends GenericFunction
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;
44 * @param inApp app object
46 public DiskCacheConfig(App inApp)
52 * Return the name key for this function
54 public String getNameKey()
56 return "function.diskcache";
60 * @return the contents of the window as a Component
62 private Component makeContents()
64 JPanel dialogPanel = new JPanel();
65 dialogPanel.setLayout(new BorderLayout(0, 5));
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) {
74 topPanel.add(_cacheCheckbox);
75 dialogPanel.add(topPanel, BorderLayout.NORTH);
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);
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) {
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);
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)
112 buttonPanelr.add(_okButton);
113 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
114 cancelButton.addActionListener(new ActionListener() {
115 public void actionPerformed(ActionEvent e)
120 buttonPanelr.add(cancelButton);
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)
130 new ManageCacheFunction(_app).begin();
133 buttonPanell.add(_manageButton);
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);
144 * Enable or disable the buttons according to what's changed
146 private void enableButtons()
148 final boolean checkState = _cacheCheckbox.isSelected();
149 final String path = _cacheDirBox.getText();
150 _cacheDirBox.setEditable(checkState);
151 _browseButton.setEnabled(checkState);
153 // If checkbox has stayed off then disable ok
154 if (!_initialCheckState && !checkState) {ok = false;}
156 // If checkbox has been switched off then enable
157 if (!checkState) {ok = true;}
160 // checkbox is on, check value
161 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
162 // Value blank or same as before
170 _okButton.setEnabled(ok);
171 // Manage button needs a valid cache
172 boolean cacheDirGood = false;
173 if (checkState && !path.equals(""))
175 File dir = new File(path);
176 cacheDirGood = dir.exists() && dir.canRead() && dir.isDirectory();
178 _manageButton.setEnabled(cacheDirGood);
188 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
189 _dialog.setLocationRelativeTo(_parentFrame);
190 _dialog.getContentPane().add(makeContents());
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);
198 // Remember current state
199 _initialCheckState = _cacheCheckbox.isSelected();
200 _dialog.setVisible(true);
204 * Function activated by the "Browse..." button to select a directory for the cache
206 private void chooseDir()
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)
215 _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
221 * OK pressed, save selected settings in Config
223 private void finish()
225 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
226 // Create dir if it doesn't exist already and creation confirmed
227 if (cachePath != null)
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()))
234 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
235 I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
238 // Check that the cache path is writable too, and give warning if not
239 if (cacheDir.exists() && cacheDir.isDirectory() && !cacheDir.canWrite())
241 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.cannotwrite"),
242 I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
245 Config.setConfigString(Config.KEY_DISK_CACHE, cachePath);
246 // inform subscribers so that tiles are wiped from memory and refetched
247 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);