1 |
|
/* gvSIG. Geographic Information System of the Valencian Government
|
2 |
|
*
|
3 |
|
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
|
* of the Valencian Government (CIT)
|
5 |
|
*
|
6 |
|
* This program is free software; you can redistribute it and/or
|
7 |
|
* modify it under the terms of the GNU General Public License
|
8 |
|
* as published by the Free Software Foundation; either version 2
|
9 |
|
* of the License, or (at your option) any later version.
|
10 |
|
*
|
11 |
|
* This program is distributed in the hope that it will be useful,
|
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
* GNU General Public License for more details.
|
15 |
|
*
|
16 |
|
* You should have received a copy of the GNU General Public License
|
17 |
|
* along with this program; if not, write to the Free Software
|
18 |
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
|
* MA 02110-1301, USA.
|
20 |
|
*
|
21 |
|
*/
|
22 |
|
package org.gvsig.raster.app.extension.raster.gui.preference.panel;
|
23 |
|
|
24 |
|
import java.awt.Color;
|
25 |
|
import java.awt.Dimension;
|
26 |
|
import java.awt.GridBagConstraints;
|
27 |
|
import java.awt.GridBagLayout;
|
28 |
|
import java.awt.Insets;
|
29 |
|
import java.awt.event.ActionEvent;
|
30 |
|
import java.awt.event.ActionListener;
|
31 |
|
import java.io.File;
|
32 |
|
import java.text.NumberFormat;
|
33 |
|
|
34 |
|
import javax.swing.BorderFactory;
|
35 |
|
import javax.swing.DefaultComboBoxModel;
|
36 |
|
import javax.swing.JButton;
|
37 |
|
import javax.swing.JComboBox;
|
38 |
|
import javax.swing.JFormattedTextField;
|
39 |
|
import javax.swing.JLabel;
|
40 |
|
import javax.swing.JTextField;
|
41 |
|
import javax.swing.SwingConstants;
|
42 |
|
import javax.swing.text.DefaultFormatterFactory;
|
43 |
|
import javax.swing.text.NumberFormatter;
|
44 |
|
|
45 |
|
import org.gvsig.andami.PluginServices;
|
46 |
|
import org.gvsig.andami.preferences.DlgPreferences;
|
47 |
|
import org.gvsig.andami.ui.mdiManager.IWindow;
|
48 |
|
import org.gvsig.fmap.dal.coverage.RasterLibrary;
|
49 |
|
import org.gvsig.raster.cache.tile.TileCacheLibrary;
|
50 |
|
import org.gvsig.raster.fmap.config.Configuration;
|
51 |
|
import org.gvsig.raster.util.BasePanel;
|
52 |
|
import org.gvsig.raster.util.RasterToolsUtil;
|
53 |
|
/**
|
54 |
|
* PreferenceCache es la clase que se encarga de configurar en RasterPreferences
|
55 |
|
* las opciones de cacheamiento de capas en Raster.
|
56 |
|
*
|
57 |
|
* @version 14/12/2007
|
58 |
|
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
59 |
|
*/
|
60 |
|
public class PreferenceTileCache extends BasePanel implements ActionListener {
|
61 |
|
protected static final long serialVersionUID = 1L;
|
62 |
|
private JLabel labelWarning = null;
|
63 |
|
private JLabel labelCacheSize = null;
|
64 |
|
private JLabel labelLevels = null;
|
65 |
|
private JLabel labelStruct = null;
|
66 |
|
private JLabel labelTileSize = null;
|
67 |
|
private JLabel labelPath = null;
|
68 |
|
private JFormattedTextField textFieldCacheSize = null;
|
69 |
|
private JFormattedTextField textFieldLevels = null;
|
70 |
|
private JFormattedTextField textFieldTileSize = null;
|
71 |
|
private JTextField textFieldPath = null;
|
72 |
|
private JComboBox comboBoxStruct = null;
|
73 |
|
private JButton buttonRemove = null;
|
74 |
|
|
75 |
|
/**
|
76 |
|
*Inicializa componentes gr?ficos y traduce
|
77 |
|
*/
|
78 |
|
public PreferenceTileCache() {
|
79 |
|
init();
|
80 |
|
translate();
|
81 |
|
}
|
82 |
|
|
83 |
|
/**
|
84 |
|
* Define todas las traducciones de PreferenceCache
|
85 |
|
*/
|
86 |
|
protected void translate() {
|
87 |
|
setBorder(BorderFactory.createTitledBorder(getText(this, "tilecache")));
|
88 |
|
getLabelWarning().setText(getText(this, "preference_cache_warning"));
|
89 |
|
getLabelCacheSize().setText(getText(this, "tamanyo_max_tilecache") + ":");
|
90 |
|
getLabelLevels().setText(getText(this, "res_levels") + ":");
|
91 |
|
getLabelTileSize().setText(getText(this, "tilesize") + ":");
|
92 |
|
getLabelStruct().setText(getText(this, "tilecache_struct") + ":");
|
93 |
|
getButtonRemove().setText(getText(this, "remove_cache"));
|
94 |
|
getLabelPath().setText(getText(this, "path_tilecache"));
|
95 |
|
}
|
96 |
|
|
97 |
|
protected void init() {
|
98 |
|
GridBagConstraints gridBagConstraints;
|
99 |
|
|
100 |
|
setLayout(new GridBagLayout());
|
101 |
|
|
102 |
|
gridBagConstraints = new GridBagConstraints();
|
103 |
|
gridBagConstraints.gridwidth = 2;
|
104 |
|
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
105 |
|
gridBagConstraints.weightx = 1.0;
|
106 |
|
gridBagConstraints.insets = new Insets(5, 5, 2, 5);
|
107 |
|
add(getLabelWarning(), gridBagConstraints);
|
108 |
|
|
109 |
|
gridBagConstraints = new GridBagConstraints();
|
110 |
|
gridBagConstraints.gridx = 0;
|
111 |
|
gridBagConstraints.gridy = 1;
|
112 |
|
gridBagConstraints.anchor = GridBagConstraints.EAST;
|
113 |
|
gridBagConstraints.insets = new Insets(2, 5, 2, 2);
|
114 |
|
add(getLabelCacheSize(), gridBagConstraints);
|
115 |
|
|
116 |
|
gridBagConstraints = new GridBagConstraints();
|
117 |
|
gridBagConstraints.gridx = 0;
|
118 |
|
gridBagConstraints.gridy = 2;
|
119 |
|
gridBagConstraints.anchor = GridBagConstraints.EAST;
|
120 |
|
gridBagConstraints.insets = new Insets(2, 5, 2, 2);
|
121 |
|
add(getLabelLevels(), gridBagConstraints);
|
122 |
|
|
123 |
|
gridBagConstraints = new GridBagConstraints();
|
124 |
|
gridBagConstraints.gridx = 0;
|
125 |
|
gridBagConstraints.gridy = 3;
|
126 |
|
gridBagConstraints.anchor = GridBagConstraints.EAST;
|
127 |
|
gridBagConstraints.insets = new Insets(2, 5, 2, 2);
|
128 |
|
add(getLabelTileSize(), gridBagConstraints);
|
129 |
|
|
130 |
|
gridBagConstraints = new GridBagConstraints();
|
131 |
|
gridBagConstraints.gridx = 0;
|
132 |
|
gridBagConstraints.gridy = 4;
|
133 |
|
gridBagConstraints.anchor = GridBagConstraints.EAST;
|
134 |
|
gridBagConstraints.insets = new Insets(2, 5, 2, 2);
|
135 |
|
add(getLabelStruct(), gridBagConstraints);
|
136 |
|
|
137 |
|
gridBagConstraints = new GridBagConstraints();
|
138 |
|
gridBagConstraints.gridx = 0;
|
139 |
|
gridBagConstraints.gridy = 5;
|
140 |
|
gridBagConstraints.gridwidth = 2;
|
141 |
|
gridBagConstraints.anchor = GridBagConstraints.WEST;
|
142 |
|
gridBagConstraints.insets = new Insets(2, 5, 2, 2);
|
143 |
|
add(getButtonRemove(), gridBagConstraints);
|
144 |
|
|
145 |
|
gridBagConstraints = new GridBagConstraints();
|
146 |
|
gridBagConstraints.gridx = 0;
|
147 |
|
gridBagConstraints.gridy = 6;
|
148 |
|
gridBagConstraints.anchor = GridBagConstraints.EAST;
|
149 |
|
gridBagConstraints.insets = new Insets(2, 5, 2, 2);
|
150 |
|
add(getLabelPath(), gridBagConstraints);
|
151 |
|
|
152 |
|
gridBagConstraints = new GridBagConstraints();
|
153 |
|
gridBagConstraints.gridx = 1;
|
154 |
|
gridBagConstraints.gridy = 1;
|
155 |
|
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
156 |
|
gridBagConstraints.anchor = GridBagConstraints.WEST;
|
157 |
|
gridBagConstraints.insets = new Insets(2, 2, 2, 5);
|
158 |
|
add(getTextFieldCacheSize(), gridBagConstraints);
|
159 |
|
|
160 |
|
gridBagConstraints = new GridBagConstraints();
|
161 |
|
gridBagConstraints.gridx = 1;
|
162 |
|
gridBagConstraints.gridy = 2;
|
163 |
|
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
164 |
|
gridBagConstraints.anchor = GridBagConstraints.WEST;
|
165 |
|
gridBagConstraints.insets = new Insets(2, 2, 2, 5);
|
166 |
|
add(getTextFieldLevels(), gridBagConstraints);
|
167 |
|
|
168 |
|
gridBagConstraints = new GridBagConstraints();
|
169 |
|
gridBagConstraints.gridx = 1;
|
170 |
|
gridBagConstraints.gridy = 3;
|
171 |
|
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
172 |
|
gridBagConstraints.anchor = GridBagConstraints.WEST;
|
173 |
|
gridBagConstraints.insets = new Insets(2, 2, 2, 5);
|
174 |
|
add(getTextFieldTileSize(), gridBagConstraints);
|
175 |
|
|
176 |
|
gridBagConstraints = new GridBagConstraints();
|
177 |
|
gridBagConstraints.gridx = 1;
|
178 |
|
gridBagConstraints.gridy = 4;
|
179 |
|
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
180 |
|
gridBagConstraints.anchor = GridBagConstraints.WEST;
|
181 |
|
gridBagConstraints.insets = new Insets(2, 2, 2, 5);
|
182 |
|
add(getComboBoxStruct(), gridBagConstraints);
|
183 |
|
|
184 |
|
gridBagConstraints = new GridBagConstraints();
|
185 |
|
gridBagConstraints.gridx = 1;
|
186 |
|
gridBagConstraints.gridy = 6;
|
187 |
|
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
188 |
|
gridBagConstraints.anchor = GridBagConstraints.WEST;
|
189 |
|
gridBagConstraints.insets = new Insets(2, 2, 2, 5);
|
190 |
|
add(getTextFieldPath(), gridBagConstraints);
|
191 |
|
|
192 |
|
}
|
193 |
|
|
194 |
|
private JLabel getLabelWarning() {
|
195 |
|
if (labelWarning == null) {
|
196 |
|
labelWarning = new JLabel();
|
197 |
|
labelWarning.setForeground(new Color(255, 0, 0));
|
198 |
|
labelWarning.setHorizontalAlignment(SwingConstants.CENTER);
|
199 |
|
labelWarning.setPreferredSize(new Dimension(0, 32));
|
200 |
|
}
|
201 |
|
return labelWarning;
|
202 |
|
}
|
203 |
|
|
204 |
|
private JButton getButtonRemove() {
|
205 |
|
if(buttonRemove == null) {
|
206 |
|
buttonRemove = new JButton();
|
207 |
|
buttonRemove.addActionListener(this);
|
208 |
|
}
|
209 |
|
return buttonRemove;
|
210 |
|
}
|
211 |
|
|
212 |
|
private JComboBox getComboBoxStruct() {
|
213 |
|
if (comboBoxStruct == null) {
|
214 |
|
comboBoxStruct = new JComboBox();
|
215 |
|
comboBoxStruct.setModel(new DefaultComboBoxModel(new String[] { "FLATX" }));
|
216 |
|
}
|
217 |
|
return comboBoxStruct;
|
218 |
|
}
|
219 |
|
|
220 |
|
private JTextField getTextFieldPath() {
|
221 |
|
if (textFieldPath == null) {
|
222 |
|
textFieldPath = new JTextField();
|
223 |
|
}
|
224 |
|
return textFieldPath;
|
225 |
|
}
|
226 |
|
|
227 |
|
private JFormattedTextField getTextFieldCacheSize() {
|
228 |
|
if (textFieldCacheSize == null) {
|
229 |
|
NumberFormat integerFormat = NumberFormat.getNumberInstance();
|
230 |
|
integerFormat.setParseIntegerOnly(true);
|
231 |
|
textFieldCacheSize = new JFormattedTextField(new DefaultFormatterFactory(
|
232 |
|
new NumberFormatter(integerFormat),
|
233 |
|
new NumberFormatter(integerFormat),
|
234 |
|
new NumberFormatter(integerFormat)));
|
235 |
|
}
|
236 |
|
return textFieldCacheSize;
|
237 |
|
}
|
238 |
|
|
239 |
|
private JFormattedTextField getTextFieldTileSize() {
|
240 |
|
if (textFieldTileSize == null) {
|
241 |
|
NumberFormat integerFormat = NumberFormat.getNumberInstance();
|
242 |
|
integerFormat.setParseIntegerOnly(true);
|
243 |
|
textFieldTileSize = new JFormattedTextField(new DefaultFormatterFactory(
|
244 |
|
new NumberFormatter(integerFormat),
|
245 |
|
new NumberFormatter(integerFormat),
|
246 |
|
new NumberFormatter(integerFormat)));
|
247 |
|
}
|
248 |
|
return textFieldTileSize;
|
249 |
|
}
|
250 |
|
|
251 |
|
private JFormattedTextField getTextFieldLevels() {
|
252 |
|
if (textFieldLevels == null) {
|
253 |
|
NumberFormat integerFormat = NumberFormat.getNumberInstance();
|
254 |
|
integerFormat.setParseIntegerOnly(true);
|
255 |
|
textFieldLevels = new JFormattedTextField(new DefaultFormatterFactory(
|
256 |
|
new NumberFormatter(integerFormat),
|
257 |
|
new NumberFormatter(integerFormat),
|
258 |
|
new NumberFormatter(integerFormat)));
|
259 |
|
}
|
260 |
|
return textFieldLevels;
|
261 |
|
}
|
262 |
|
|
263 |
|
private JLabel getLabelPath() {
|
264 |
|
if (labelPath == null)
|
265 |
|
labelPath = new JLabel();
|
266 |
|
return labelPath;
|
267 |
|
}
|
268 |
|
|
269 |
|
private JLabel getLabelCacheSize() {
|
270 |
|
if (labelCacheSize == null)
|
271 |
|
labelCacheSize = new JLabel();
|
272 |
|
return labelCacheSize;
|
273 |
|
}
|
274 |
|
|
275 |
|
private JLabel getLabelLevels() {
|
276 |
|
if (labelLevels == null)
|
277 |
|
labelLevels = new JLabel();
|
278 |
|
return labelLevels;
|
279 |
|
}
|
280 |
|
|
281 |
|
private JLabel getLabelStruct() {
|
282 |
|
if (labelStruct == null)
|
283 |
|
labelStruct = new JLabel();
|
284 |
|
return labelStruct;
|
285 |
|
}
|
286 |
|
|
287 |
|
private JLabel getLabelTileSize() {
|
288 |
|
if (labelTileSize == null)
|
289 |
|
labelTileSize = new JLabel();
|
290 |
|
return labelTileSize;
|
291 |
|
}
|
292 |
|
|
293 |
|
/**
|
294 |
|
* Establece los valores por defecto de la Cache
|
295 |
|
*/
|
296 |
|
public void initializeDefaults() {
|
297 |
|
getTextFieldCacheSize().setValue(Configuration.getDefaultValue("tilecache_size"));
|
298 |
|
getTextFieldTileSize().setValue(Configuration.getDefaultValue("tilesize"));
|
299 |
|
getTextFieldLevels().setValue(Configuration.getDefaultValue("tile_levels"));
|
300 |
|
getTextFieldPath().setText(Configuration.getDefaultValue("path_tilecache").toString());
|
301 |
|
String struct = (String) Configuration.getDefaultValue("cache_struct");
|
302 |
|
if(struct != null) {
|
303 |
|
for (int i = 0; i < getComboBoxStruct().getItemCount(); i++)
|
304 |
|
if (getComboBoxStruct().getItemAt(i).toString().equals(struct.toString())) {
|
305 |
|
getComboBoxStruct().setSelectedIndex(i);
|
306 |
|
break;
|
307 |
|
}
|
308 |
|
}
|
309 |
|
}
|
310 |
|
|
311 |
|
/**
|
312 |
|
* Establece los valores que ha definido el usuario de la Cache
|
313 |
|
*/
|
314 |
|
public void initializeValues() {
|
315 |
|
getTextFieldCacheSize().setValue(Configuration.getValue("tilecache_size", Integer.valueOf(TileCacheLibrary.MAX_CACHE_SIZE)));
|
316 |
|
getTextFieldTileSize().setValue(Configuration.getValue("tilesize", Integer.valueOf(TileCacheLibrary.DEFAULT_TILESIZE)));
|
317 |
|
getTextFieldLevels().setValue(Configuration.getValue("tile_levels", Integer.valueOf(TileCacheLibrary.DEFAULT_LEVELS)));
|
318 |
|
getTextFieldPath().setText(Configuration.getValue("path_tilecache", RasterLibrary.pathTileCache));
|
319 |
|
String struct = Configuration.getValue("cache_struct", TileCacheLibrary.DEFAULT_STRUCTURE);
|
320 |
|
if(struct != null) {
|
321 |
|
for (int i = 0; i < getComboBoxStruct().getItemCount(); i++)
|
322 |
|
if (getComboBoxStruct().getItemAt(i).toString().equals(struct.toString())) {
|
323 |
|
getComboBoxStruct().setSelectedIndex(i);
|
324 |
|
break;
|
325 |
|
}
|
326 |
|
}
|
327 |
|
}
|
328 |
|
|
329 |
|
/**
|
330 |
|
* Guarda los valores de la cache establecidos por el usuario
|
331 |
|
*/
|
332 |
|
public void storeValues() {
|
333 |
|
Configuration.setValue("tilecache_size", getTextFieldCacheSize().getText());
|
334 |
|
Configuration.setValue("tilesize", getTextFieldTileSize().getText());
|
335 |
|
Configuration.setValue("tile_levels", getTextFieldLevels().getText());
|
336 |
|
Configuration.setValue("cache_struct", getComboBoxStruct().getSelectedItem() != null ? getComboBoxStruct().getSelectedItem().toString() : TileCacheLibrary.DEFAULT_STRUCTURE);
|
337 |
|
Configuration.setValue("path_tilecache", getTextFieldPath().getText());
|
338 |
|
}
|
339 |
|
|
340 |
|
public void actionPerformed(ActionEvent e) {
|
341 |
|
String path = getTextFieldPath().getText();
|
342 |
|
IWindow parent = PluginServices.getMDIManager().getActiveWindow();
|
343 |
|
IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
|
344 |
|
for (int i = 0; i < wList.length; i++) {
|
345 |
|
if(wList[i] instanceof DlgPreferences)
|
346 |
|
parent = wList[i];
|
347 |
|
}
|
348 |
|
|
349 |
|
if(RasterToolsUtil.messageBoxYesOrNot(PluginServices.getText(this, "remove_directory") + "<BR>" + path, parent)) {
|
350 |
|
File f = new File(path);
|
351 |
|
if(f.exists()) {
|
352 |
|
if(!deleteDirectory(f)) {
|
353 |
|
RasterToolsUtil.messageBoxError("open_files", parent);
|
354 |
|
}
|
355 |
|
}else
|
356 |
|
RasterToolsUtil.messageBoxError("path_not_exists", parent);
|
357 |
|
}
|
358 |
|
}
|
359 |
|
|
360 |
|
public boolean deleteDirectory(File directory) {
|
361 |
|
File[] files = directory.listFiles();
|
362 |
|
for(int i = 0; i < files.length; i++) {
|
363 |
|
if(files[i].isDirectory()) {
|
364 |
|
this.deleteDirectory(files[i]);
|
365 |
|
} else {
|
366 |
|
files[i].delete();
|
367 |
|
}
|
368 |
|
}
|
369 |
|
return directory.delete();
|
370 |
|
}
|
371 |
|
}
|