]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/DiskCacheConfig.java
Version 20.4, May 2021
[GpsPrune.git] / src / 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.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;
22
23 import tim.prune.App;
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;
30
31 /**
32  * Class to show the popup window for setting the path to disk cache
33  */
34 public class DiskCacheConfig extends GenericFunction
35 {
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;
43
44         /**
45          * Constructor
46          * @param inApp app object
47          */
48         public DiskCacheConfig(App inApp)
49         {
50                 super(inApp);
51         }
52
53         /**
54          * Return the name key for this function
55          */
56         public String getNameKey()
57         {
58                 return "function.diskcache";
59         }
60
61         /**
62          * @return the contents of the window as a Component
63          */
64         private Component makeContents()
65         {
66                 JPanel dialogPanel = new JPanel();
67                 dialogPanel.setBorder(BorderFactory.createCompoundBorder(
68                         BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), BorderFactory.createEmptyBorder(4, 4, 4, 4))
69                 );
70                 dialogPanel.setLayout(new BorderLayout(0, 5));
71                 // top panel
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) {
76                                 enableButtons();
77                         }
78                 });
79                 topPanel.add(_cacheCheckbox);
80                 dialogPanel.add(topPanel, BorderLayout.NORTH);
81                 // dir panel
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);
89                                 enableButtons();
90                         }
91                 });
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) {
96                                 chooseDir();
97                         }
98                 });
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);
105
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)
112                         {
113                                 finish();
114                                 _dialog.dispose();
115                         }
116                 });
117                 buttonPanelr.add(_okButton);
118                 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
119                 cancelButton.addActionListener(new ActionListener() {
120                         public void actionPerformed(ActionEvent e)
121                         {
122                                 _dialog.dispose();
123                         }
124                 });
125                 buttonPanelr.add(cancelButton);
126
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)
133                         {
134                                 finish();
135                                 new ManageCacheFunction(_app).begin();
136                         }
137                 });
138                 buttonPanell.add(_manageButton);
139                 // Put them together
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);
145                 return dialogPanel;
146         }
147
148         /**
149          * Enable or disable the buttons according to what's changed
150          */
151         private void enableButtons()
152         {
153                 final boolean checkState = _cacheCheckbox.isSelected();
154                 final String path = _cacheDirBox.getText();
155                 _cacheDirBox.setEditable(checkState);
156                 _browseButton.setEnabled(checkState);
157                 boolean ok = false;
158                 // If checkbox has stayed off then disable ok
159                 if (!_initialCheckState && !checkState) {ok = false;}
160                 else {
161                         // If checkbox has been switched off then enable
162                         if (!checkState) {ok = true;}
163                         else
164                         {
165                                 // checkbox is on, check value
166                                 if (path.equals("") || (_initialCacheDir != null && path.equals(_initialCacheDir))) {
167                                         // Value blank or same as before
168                                         ok = false;
169                                 }
170                                 else {
171                                         ok = true;
172                                 }
173                         }
174                 }
175                 _okButton.setEnabled(ok);
176                 // Manage button needs a valid cache
177                 boolean cacheDirGood = false;
178                 if (checkState && !path.equals(""))
179                 {
180                         File dir = new File(path);
181                         cacheDirGood = dir.exists() && dir.canRead() && dir.isDirectory();
182                 }
183                 _manageButton.setEnabled(cacheDirGood);
184         }
185
186         /**
187          * Show window
188          */
189         public void begin()
190         {
191                 if (_dialog == null)
192                 {
193                         _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()));
194                         _dialog.setLocationRelativeTo(_parentFrame);
195                         _dialog.getContentPane().add(makeContents());
196                         _dialog.pack();
197                 }
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);
202                 enableButtons();
203                 // Remember current state
204                 _initialCheckState = _cacheCheckbox.isSelected();
205                 _dialog.setVisible(true);
206         }
207
208         /**
209          * Function activated by the "Browse..." button to select a directory for the cache
210          */
211         private void chooseDir()
212         {
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)
219                 {
220                         _cacheDirBox.setText(chooser.getSelectedFile().getAbsolutePath());
221                 }
222                 enableButtons();
223         }
224
225         /**
226          * OK pressed, save selected settings in Config
227          */
228         private void finish()
229         {
230                 String cachePath = (_cacheCheckbox.isSelected()?_cacheDirBox.getText():null);
231                 // Create dir if it doesn't exist already and creation confirmed
232                 if (cachePath != null)
233                 {
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()))
238                         {
239                                 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.nocreate"),
240                                         I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
241                                 return;
242                         }
243                         // Check that the cache path is writable too, and give warning if not
244                         if (cacheDir.exists() && cacheDir.isDirectory() && !cacheDir.canWrite())
245                         {
246                                 JOptionPane.showMessageDialog(_dialog, I18nManager.getText("dialog.diskcache.cannotwrite"),
247                                         I18nManager.getText(getNameKey()), JOptionPane.WARNING_MESSAGE);
248                         }
249                 }
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);
253         }
254 }