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;
29 * Class to show the popup window for setting the path to disk cache
31 public class DiskCacheConfig extends GenericFunction
33 private JDialog _dialog = null;
34 private JCheckBox _cacheCheckbox = null;
35 private JTextField _cacheDirBox = null;
36 private JButton _browseButton = null;
37 private JButton _okButton = null;
38 private boolean _initialCheckState = false;
39 private String _initialCacheDir = null;
43 * @param inApp app object
45 public DiskCacheConfig(App inApp)
51 * Return the name key for this function
53 public String getNameKey()
55 return "function.diskcache";
59 * @return the contents of the window as a Component
61 private Component makeContents()
63 JPanel dialogPanel = new JPanel();
64 dialogPanel.setLayout(new BorderLayout(0, 5));
66 JPanel topPanel = new JPanel();
67 _cacheCheckbox = new JCheckBox(I18nManager.getText("dialog.diskcache.save"));
68 _cacheCheckbox.addActionListener(new ActionListener() {
69 public void actionPerformed(ActionEvent arg0) {
73 topPanel.add(_cacheCheckbox);
74 dialogPanel.add(topPanel, BorderLayout.NORTH);
76 JPanel dirPanel = new JPanel();
77 dirPanel.setLayout(new BorderLayout());
78 dirPanel.add(new JLabel(I18nManager.getText("dialog.diskcache.dir")), BorderLayout.WEST);
79 _cacheDirBox = new JTextField(24);
80 _cacheDirBox.addKeyListener(new KeyAdapter() {
81 public void keyReleased(KeyEvent arg0) {
82 super.keyReleased(arg0);
86 dirPanel.add(_cacheDirBox, BorderLayout.CENTER);
87 _browseButton = new JButton(I18nManager.getText("button.browse"));
88 _browseButton.addActionListener(new ActionListener() {
89 public void actionPerformed(ActionEvent arg0) {
93 dirPanel.add(_browseButton, BorderLayout.EAST);
94 dialogPanel.add(dirPanel, BorderLayout.CENTER);
96 // Cancel button at the bottom
97 JPanel buttonPanel = new JPanel();
98 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
99 _okButton = new JButton(I18nManager.getText("button.ok"));
100 _okButton.addActionListener(new ActionListener()
102 public void actionPerformed(ActionEvent e)
107 buttonPanel.add(_okButton);
108 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
109 cancelButton.addActionListener(new ActionListener() {
110 public void actionPerformed(ActionEvent e)
115 buttonPanel.add(cancelButton);
116 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
121 * Enable or disable the ok button according to what's changed
123 private void enableOk()
125 boolean checkState = _cacheCheckbox.isSelected();
126 _cacheDirBox.setEditable(checkState);
127 _browseButton.setEnabled(checkState);
129 // If checkbox has stayed off then disable ok
130 if (!_initialCheckState && !checkState) {ok = false;}
132 // If checkbox has been switched off then enable
133 if (!checkState) {ok = true;}
135 // checkbox is on, check value
136 String path = _cacheDirBox.getText();
137 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
138 // Value blank or same as before
146 _okButton.setEnabled(ok);
156 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
157 _dialog.setLocationRelativeTo(_parentFrame);
158 _dialog.getContentPane().add(makeContents());
161 // Set controls according to current config
162 String currPath = Config.getConfigString(Config.KEY_DISK_CACHE);
163 _cacheCheckbox.setSelected(currPath != null);
164 _cacheDirBox.setText(currPath==null?"":currPath);
166 // Remember current state
167 _initialCheckState = _cacheCheckbox.isSelected();
168 _dialog.setVisible(true);
172 * Function activated by the "Browse..." button to select a directory for the cache
174 private void chooseDir()
176 JFileChooser chooser = new JFileChooser();
177 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
178 // Set start path from currently selected dir
179 String path = _cacheDirBox.getText();
180 if (path.length() > 1) {chooser.setCurrentDirectory(new File(path));}
181 if (chooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
183 _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
189 * OK pressed, save selected settings in Config
191 private void finish()
193 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
194 // Create dir if it doesn't exist already and creation confirmed
195 if (cachePath != null)
197 File cacheDir = new File(cachePath);
198 if ((!cacheDir.exists() || !cacheDir.isDirectory()) && (JOptionPane.showConfirmDialog(_dialog,
199 I18nManager.getText("dialog.diskcache.createdir") + ": " + cacheDir.getAbsolutePath() + " ?",
200 I18nManager.getText(getNameKey()), JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION || !cacheDir.mkdir()))
202 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
203 I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
207 Config.setConfigString(Config.KEY_DISK_CACHE, cachePath);
208 // inform subscribers so that tiles are wiped from memory and refetched
209 UpdateMessageBroker.informSubscribers(DataSubscriber.MAPSERVER_CHANGED);