Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tilecache / trunk / org.gvsig.raster.tilecache / org.gvsig.raster.tilecache.app / src / main / java / org / gvsig / raster / tilecache / app / TileCachePreferencesPanel.java @ 867

History | View | Annotate | Download (13.3 KB)

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.tilecache.app;
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.util.BasePanel;
51
/**
52
 * This class provides a panel for tile cache configuration
53
 * 
54
 * @author Nacho Brodin (nachobrodin@gmail.com)
55
 */
56
public class TileCachePreferencesPanel extends BasePanel implements ActionListener {
57
        protected static final long serialVersionUID      = 1L;
58
        private JLabel              labelWarning          = null;
59
        private JLabel              labelCacheSize        = null;
60
        private JLabel              labelLevels           = null;
61
        private JLabel              labelStruct           = null;
62
        private JLabel              labelTileSize         = null;
63
        private JLabel              labelPath             = null;
64
        private JFormattedTextField textFieldCacheSize    = null;
65
        private JFormattedTextField textFieldLevels       = null;
66
        private JFormattedTextField textFieldTileSize     = null;
67
        private JTextField          textFieldPath         = null;
68
        private JComboBox           comboBoxStruct        = null;
69
        private JButton             buttonRemove          = null;
70

    
71
        /**
72
         *Inicializa componentes gr?ficos y traduce
73
         */
74
        public TileCachePreferencesPanel() {
75
                init();
76
                translate();
77
        }
78

    
79
        /**
80
         * Define todas las traducciones de PreferenceCache
81
         */
82
        protected void translate() {
83
                setBorder(BorderFactory.createTitledBorder(getText(this, "tilecache")));
84
                getLabelWarning().setText(getText(this, "preference_cache_warning"));
85
                getLabelCacheSize().setText(getText(this, "tamanyo_max_tilecache") + ":");
86
                getLabelLevels().setText(getText(this, "res_levels") + ":");
87
                getLabelTileSize().setText(getText(this, "tilesize") + ":");
88
                getLabelStruct().setText(getText(this, "tilecache_struct") + ":");
89
                getButtonRemove().setText(getText(this, "remove_cache"));
90
                getLabelPath().setText(getText(this, "path_tilecache"));
91
        }
92

    
93
        protected void init() {
94
                GridBagConstraints gridBagConstraints;
95

    
96
                setLayout(new GridBagLayout());
97

    
98
                gridBagConstraints = new GridBagConstraints();
99
                gridBagConstraints.gridwidth = 2;
100
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
101
                gridBagConstraints.weightx = 1.0;
102
                gridBagConstraints.insets = new Insets(5, 5, 2, 5);
103
                add(getLabelWarning(), gridBagConstraints);
104

    
105
                gridBagConstraints = new GridBagConstraints();
106
                gridBagConstraints.gridx = 0;
107
                gridBagConstraints.gridy = 1;
108
                gridBagConstraints.anchor = GridBagConstraints.EAST;
109
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
110
                add(getLabelCacheSize(), gridBagConstraints);
111
                
112
                gridBagConstraints = new GridBagConstraints();
113
                gridBagConstraints.gridx = 0;
114
                gridBagConstraints.gridy = 2;
115
                gridBagConstraints.anchor = GridBagConstraints.EAST;
116
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
117
                add(getLabelLevels(), gridBagConstraints);
118
                
119
                gridBagConstraints = new GridBagConstraints();
120
                gridBagConstraints.gridx = 0;
121
                gridBagConstraints.gridy = 3;
122
                gridBagConstraints.anchor = GridBagConstraints.EAST;
123
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
124
                add(getLabelTileSize(), gridBagConstraints);
125
                
126
                gridBagConstraints = new GridBagConstraints();
127
                gridBagConstraints.gridx = 0;
128
                gridBagConstraints.gridy = 4;
129
                gridBagConstraints.anchor = GridBagConstraints.EAST;
130
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
131
                add(getLabelStruct(), gridBagConstraints);
132
                
133
                gridBagConstraints = new GridBagConstraints();
134
                gridBagConstraints.gridx = 0;
135
                gridBagConstraints.gridy = 5;
136
                gridBagConstraints.gridwidth = 2;
137
                gridBagConstraints.anchor = GridBagConstraints.WEST;
138
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
139
                add(getButtonRemove(), gridBagConstraints);
140
                
141
                gridBagConstraints = new GridBagConstraints();
142
                gridBagConstraints.gridx = 0;
143
                gridBagConstraints.gridy = 6;
144
                gridBagConstraints.anchor = GridBagConstraints.EAST;
145
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
146
                add(getLabelPath(), gridBagConstraints);
147

    
148
                gridBagConstraints = new GridBagConstraints();
149
                gridBagConstraints.gridx = 1;
150
                gridBagConstraints.gridy = 1;
151
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
152
                gridBagConstraints.anchor = GridBagConstraints.WEST;
153
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
154
                add(getTextFieldCacheSize(), gridBagConstraints);
155
                
156
                gridBagConstraints = new GridBagConstraints();
157
                gridBagConstraints.gridx = 1;
158
                gridBagConstraints.gridy = 2;
159
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
160
                gridBagConstraints.anchor = GridBagConstraints.WEST;
161
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
162
                add(getTextFieldLevels(), gridBagConstraints);
163
                
164
                gridBagConstraints = new GridBagConstraints();
165
                gridBagConstraints.gridx = 1;
166
                gridBagConstraints.gridy = 3;
167
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
168
                gridBagConstraints.anchor = GridBagConstraints.WEST;
169
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
170
                add(getTextFieldTileSize(), gridBagConstraints);
171
                
172
                gridBagConstraints = new GridBagConstraints();
173
                gridBagConstraints.gridx = 1;
174
                gridBagConstraints.gridy = 4;
175
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
176
                gridBagConstraints.anchor = GridBagConstraints.WEST;
177
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
178
                add(getComboBoxStruct(), gridBagConstraints);
179
                
180
                gridBagConstraints = new GridBagConstraints();
181
                gridBagConstraints.gridx = 1;
182
                gridBagConstraints.gridy = 6;
183
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
184
                gridBagConstraints.anchor = GridBagConstraints.WEST;
185
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
186
                add(getTextFieldPath(), gridBagConstraints);
187
                
188
        }
189

    
190
        private JLabel getLabelWarning() {
191
                if (labelWarning == null) {
192
                        labelWarning = new JLabel();
193
                        labelWarning.setForeground(new Color(255, 0, 0));
194
                        labelWarning.setHorizontalAlignment(SwingConstants.CENTER);
195
                        labelWarning.setPreferredSize(new Dimension(0, 32));
196
                }
197
                return labelWarning;
198
        }
199

    
200
        private JButton getButtonRemove() {
201
                if(buttonRemove == null) {
202
                        buttonRemove = new JButton();
203
                        buttonRemove.addActionListener(this);
204
                }
205
                return buttonRemove;
206
        }
207
        
208
        private JComboBox getComboBoxStruct() {
209
                if (comboBoxStruct == null) {
210
                        comboBoxStruct = new JComboBox();
211
                        comboBoxStruct.setModel(new DefaultComboBoxModel(new String[] { "FLATX" }));
212
                }
213
                return comboBoxStruct;
214
        }
215
        
216
        private JTextField getTextFieldPath() {
217
                if (textFieldPath == null) {
218
                        textFieldPath = new JTextField();
219
                }
220
                return textFieldPath;
221
        }
222
        
223
        private JFormattedTextField getTextFieldCacheSize() {
224
                if (textFieldCacheSize == null) {
225
                        NumberFormat integerFormat = NumberFormat.getNumberInstance();
226
                        integerFormat.setParseIntegerOnly(true);
227
                        textFieldCacheSize = new JFormattedTextField(new DefaultFormatterFactory(
228
                                        new NumberFormatter(integerFormat),
229
                                        new NumberFormatter(integerFormat),
230
                                        new NumberFormatter(integerFormat)));
231
                }
232
                return textFieldCacheSize;
233
        }
234
        
235
        private JFormattedTextField getTextFieldTileSize() {
236
                if (textFieldTileSize == null) {
237
                        NumberFormat integerFormat = NumberFormat.getNumberInstance();
238
                        integerFormat.setParseIntegerOnly(true);
239
                        textFieldTileSize = new JFormattedTextField(new DefaultFormatterFactory(
240
                                        new NumberFormatter(integerFormat),
241
                                        new NumberFormatter(integerFormat),
242
                                        new NumberFormatter(integerFormat)));
243
                }
244
                return textFieldTileSize;
245
        }
246
        
247
        private JFormattedTextField getTextFieldLevels() {
248
                if (textFieldLevels == null) {
249
                        NumberFormat integerFormat = NumberFormat.getNumberInstance();
250
                        integerFormat.setParseIntegerOnly(true);
251
                        textFieldLevels = new JFormattedTextField(new DefaultFormatterFactory(
252
                                        new NumberFormatter(integerFormat),
253
                                        new NumberFormatter(integerFormat),
254
                                        new NumberFormatter(integerFormat)));
255
                }
256
                return textFieldLevels;
257
        }
258

    
259
        private JLabel getLabelPath() {
260
                if (labelPath == null)
261
                        labelPath = new JLabel();
262
                return labelPath;
263
        }
264
        
265
        private JLabel getLabelCacheSize() {
266
                if (labelCacheSize == null)
267
                        labelCacheSize = new JLabel();
268
                return labelCacheSize;
269
        }
270
        
271
        private JLabel getLabelLevels() {
272
                if (labelLevels == null)
273
                        labelLevels = new JLabel();
274
                return labelLevels;
275
        }
276
        
277
        private JLabel getLabelStruct() {
278
                if (labelStruct == null)
279
                        labelStruct = new JLabel();
280
                return labelStruct;
281
        }
282
        
283
        private JLabel getLabelTileSize() {
284
                if (labelTileSize == null)
285
                        labelTileSize = new JLabel();
286
                return labelTileSize;
287
        }
288

    
289
        /**
290
         * Establece los valores por defecto de la Cache
291
         */
292
        public void initializeDefaults() {
293
                getTextFieldCacheSize().setValue(Configuration.getDefaultValue("tilecache_size"));
294
                getTextFieldTileSize().setValue(Configuration.getDefaultValue("tilesize"));
295
                getTextFieldLevels().setValue(Configuration.getDefaultValue("tile_levels"));
296
                getTextFieldPath().setText(Configuration.getDefaultValue("path_tilecache").toString());
297
                String struct = (String) Configuration.getDefaultValue("cache_struct");
298
                if(struct != null) {
299
                        for (int i = 0; i < getComboBoxStruct().getItemCount(); i++)
300
                                if (getComboBoxStruct().getItemAt(i).toString().equals(struct.toString())) {
301
                                        getComboBoxStruct().setSelectedIndex(i);
302
                                        break;
303
                                }
304
                }
305
        }
306

    
307
        /**
308
         * Establece los valores que ha definido el usuario de la Cache
309
         */
310
        public void initializeValues() {
311
                getTextFieldCacheSize().setValue(Configuration.getValue("tilecache_size", Integer.valueOf(TileCacheLibrary.MAX_CACHE_SIZE)));
312
                getTextFieldTileSize().setValue(Configuration.getValue("tilesize", Integer.valueOf(TileCacheLibrary.DEFAULT_TILEWIDTH)));
313
                getTextFieldLevels().setValue(Configuration.getValue("tile_levels", Integer.valueOf(TileCacheLibrary.DEFAULT_LEVELS)));
314
                getTextFieldPath().setText(Configuration.getValue("path_tilecache", RasterLibrary.pathTileCache));
315
                String struct = Configuration.getValue("cache_struct", TileCacheLibrary.DEFAULT_STRUCTURE);
316
                if(struct != null) {
317
                        for (int i = 0; i < getComboBoxStruct().getItemCount(); i++)
318
                                if (getComboBoxStruct().getItemAt(i).toString().equals(struct.toString())) {
319
                                        getComboBoxStruct().setSelectedIndex(i);
320
                                        break;
321
                                }
322
                }
323
        }
324

    
325
        /**
326
         * Guarda los valores de la cache establecidos por el usuario
327
         */
328
        public void storeValues() {
329
                Configuration.setValue("tilecache_size", getTextFieldCacheSize().getText());
330
                Configuration.setValue("tilesize", getTextFieldTileSize().getText());
331
                Configuration.setValue("tile_levels", getTextFieldLevels().getText());
332
                Configuration.setValue("cache_struct", getComboBoxStruct().getSelectedItem() != null ? getComboBoxStruct().getSelectedItem().toString() : TileCacheLibrary.DEFAULT_STRUCTURE);
333
                Configuration.setValue("path_tilecache", getTextFieldPath().getText());
334
        }
335

    
336
        public void actionPerformed(ActionEvent e) {
337
                String path = getTextFieldPath().getText();
338
                IWindow parent = PluginServices.getMDIManager().getActiveWindow();
339
                IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
340
                for (int i = 0; i < wList.length; i++) {
341
                        if(wList[i] instanceof DlgPreferences)
342
                                parent = wList[i];
343
                }
344
                
345
                if(CacheExtension.messageBoxYesOrNot(PluginServices.getText(this, "remove_directory") + "<BR>" + path, parent)) {
346
                        File f = new File(path);
347
                        if(f.exists()) {
348
                                if(!deleteDirectory(f)) {
349
                                        CacheExtension.messageBoxError("open_files", parent);
350
                                }
351
                        }else
352
                                CacheExtension.messageBoxError("path_not_exists", parent);
353
                }
354
        }
355

    
356
        public boolean deleteDirectory(File directory) {  
357
                File[] files = directory.listFiles();  
358
                for(int i = 0; i < files.length; i++) {  
359
                        if(files[i].isDirectory()) {  
360
                                this.deleteDirectory(files[i]);  
361
                        } else {  
362
                                files[i].delete();  
363
                        }  
364
                }  
365
                return directory.delete();  
366
        }  
367
}