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)
111 buttonPanelr.add(_okButton);
112 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
113 cancelButton.addActionListener(new ActionListener() {
114 public void actionPerformed(ActionEvent e)
119 buttonPanelr.add(cancelButton);
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)
129 new ManageCacheFunction(_app).begin();
132 buttonPanell.add(_manageButton);
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);
143 * Enable or disable the buttons according to what's changed
145 private void enableButtons()
147 final boolean checkState = _cacheCheckbox.isSelected();
148 final String path = _cacheDirBox.getText();
149 _cacheDirBox.setEditable(checkState);
150 _browseButton.setEnabled(checkState);
152 // If checkbox has stayed off then disable ok
153 if (!_initialCheckState && !checkState) {ok = false;}
155 // If checkbox has been switched off then enable
156 if (!checkState) {ok = true;}
159 // checkbox is on, check value
160 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
161 // Value blank or same as before
169 _okButton.setEnabled(ok);
170 // Manage button needs a valid cache
171 boolean cacheDirGood = false;
172 if (checkState && !path.equals(""))
174 File dir = new File(path);
175 cacheDirGood = dir.exists() && dir.canRead() && dir.isDirectory();
177 _manageButton.setEnabled(cacheDirGood);
187 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
188 _dialog.setLocationRelativeTo(_parentFrame);
189 _dialog.getContentPane().add(makeContents());
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);
197 // Remember current state
198 _initialCheckState = _cacheCheckbox.isSelected();
199 _dialog.setVisible(true);
203 * Function activated by the "Browse..." button to select a directory for the cache
205 private void chooseDir()
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)
214 _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
220 * OK pressed, save selected settings in Config
222 private void finish()
224 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
225 // Create dir if it doesn't exist already and creation confirmed
226 if (cachePath != null)
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()))
233 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
234 I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
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);