Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / properties / panel / NoDataPanel.java @ 865

History | View | Annotate | Download (11.6 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.tools.app.basic.tool.properties.panel;
23

    
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.awt.Insets;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.text.NumberFormat;
30

    
31
import javax.swing.BorderFactory;
32
import javax.swing.JButton;
33
import javax.swing.JCheckBox;
34
import javax.swing.JFormattedTextField;
35
import javax.swing.JLabel;
36
import javax.swing.JPanel;
37
import javax.swing.text.DefaultFormatterFactory;
38
import javax.swing.text.NumberFormatter;
39

    
40
import org.gvsig.fmap.dal.coverage.RasterLibrary;
41
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
42
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
43
import org.gvsig.raster.fmap.layers.FLyrRaster;
44
import org.gvsig.raster.tools.app.basic.RasterExtension;
45
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
46
import org.gvsig.raster.util.BasePanel;
47

    
48
/**
49
 * Panel para la gestion del valor NoData en el panel de propiedades
50
 *
51
 * @author Nacho Brodin (nachobrodin@gmail.com)
52
 */
53
public class NoDataPanel extends BasePanel implements ActionListener {
54
        private static final long   serialVersionUID       = 7645641060812458944L;
55
        private static final String NULL_NODATA            = "Not Defined";
56
        private JLabel              jLabelValue            = null;
57
        private JButton             buttonSave             = null;
58
        private JButton             buttonDelete           = null;
59
        private JButton             buttonDefaultValue     = null;
60
        private JButton             buttonRestore          = null;
61
        private JFormattedTextField jTextFieldValue        = null;
62
        private JPanel              panelCenter            = null;
63
        private JPanel              panelUp                = null;
64
        private JPanel              panelDown              = null;
65
        private JCheckBox           jCheckBoxNoDataEnabled = null;
66
        private NoData              noData                 = null;
67
        private int                 dataType               = Buffer.TYPE_UNDEFINED;
68
        private FLyrRaster          lyr                    = null;
69

    
70
        public NoDataPanel() {
71
                init();
72
                translate();
73
        }
74

    
75
        protected void translate() {
76
                getButtonSave().setText(getText(this, "guardar"));
77
                getButtonSave().setToolTipText(getText(this, "guardar_box_value"));
78
                getButtonRestore().setText(getText(this, "restore"));
79
                getButtonRestore().setToolTipText(getText(this, "restore_native_value"));
80
                getButtonDefaultValue().setText(getText(this, "default_value"));
81
                getButtonDefaultValue().setToolTipText(getText(this, "set_configuration_value"));
82
                getButtonDelete().setText(getText(this, "delete"));
83
                getButtonDelete().setToolTipText(getText(this, "delete_value"));
84
                getCheckBoxNoDataEnabled().setText(getText(this, "tratar_nodata_transparente"));
85
                getCheckBoxNoDataEnabled().setToolTipText(getCheckBoxNoDataEnabled().getText());
86
                getLabelValue().setText(getText(this, "value") + ":");
87
        }
88

    
89
        private JButton getButtonSave() {
90
                if (buttonSave == null) {
91
                        buttonSave = new JButton();
92
                        buttonSave.addActionListener(this);
93
                }
94
                return buttonSave;
95
        }
96
        
97
        private JButton getButtonDelete() {
98
                if (buttonDelete == null) {
99
                        buttonDelete = new JButton();
100
                        buttonDelete.addActionListener(this);
101
                }
102
                return buttonDelete;
103
        }
104
        
105
        private JButton getButtonDefaultValue() {
106
                if (buttonDefaultValue == null) {
107
                        buttonDefaultValue = new JButton();
108
                        buttonDefaultValue.addActionListener(this);
109
                }
110
                return buttonDefaultValue;
111
        }
112
        
113
        private JButton getButtonRestore() {
114
                if (buttonRestore == null) {
115
                        buttonRestore = new JButton();
116
                        buttonRestore.addActionListener(this);
117
                }
118
                return buttonRestore;
119
        }
120

    
121
        private JFormattedTextField getTextFieldValue() {
122
                if (jTextFieldValue == null) {
123
                        NumberFormat integerFormat = NumberFormat.getNumberInstance();
124
                        integerFormat.setParseIntegerOnly(false);
125
                        integerFormat.setMinimumFractionDigits(0);
126
                        integerFormat.setMaximumFractionDigits(3);
127
                        jTextFieldValue = new JFormattedTextField(new DefaultFormatterFactory(
128
                                        new NumberFormatter(integerFormat),
129
                                        new NumberFormatter(integerFormat),
130
                                        new NumberFormatter(integerFormat)));
131
                }
132
                return jTextFieldValue;
133
        }
134

    
135
        private JLabel getLabelValue() {
136
                if (jLabelValue == null) {
137
                        jLabelValue = new JLabel();
138
                }
139
                return jLabelValue;
140
        }
141

    
142
        protected void init() {
143
                GridBagConstraints gridBagConstraints;
144

    
145
                setLayout(new GridBagLayout());
146

    
147
                gridBagConstraints = new GridBagConstraints();
148
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
149
                gridBagConstraints.weightx = 1.0;
150
                add(getPanelUp(), gridBagConstraints);
151

    
152
                gridBagConstraints.gridy = 1;
153
                add(getPanelCenter(), gridBagConstraints);
154

    
155
                gridBagConstraints.gridy = 2;
156
                add(getPanelDown(), gridBagConstraints);
157
        }
158
        
159
        public JCheckBox getCheckBoxNoDataEnabled() {
160
                if (jCheckBoxNoDataEnabled == null) {
161
                        jCheckBoxNoDataEnabled = new JCheckBox();
162
                        jCheckBoxNoDataEnabled.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
163
                        jCheckBoxNoDataEnabled.setMargin(new Insets(0, 0, 0, 0));
164
                }
165
                return jCheckBoxNoDataEnabled;
166
        }
167
        
168
        
169
        private void setCheckBoxValue(double d) {
170
                String text = d + "";
171
                text = text.replaceAll("\\.", ",");
172
                getTextFieldValue().setText(text);
173
        }
174
        
175
        private String getCheckBoxValue() {
176
                String text = getTextFieldValue().getText();
177
                text = text.replaceAll("\\.", "");
178
                text = text.replaceAll(",", "\\.");
179
                return text;
180
        }
181

    
182
        public void setEnabledComponents(boolean b) {
183
                getTextFieldValue().setEnabled(b);
184
                getLabelValue().setEnabled(b);
185
                getButtonDelete().setEnabled(b);
186
                getButtonRestore().setEnabled(b);
187
                getButtonSave().setEnabled(b);
188
                getButtonDefaultValue().setEnabled(b);
189
        }
190

    
191
        public NoData getNoDataValue() {
192
                return noData;
193
        }
194
        
195
        /**
196
         * Loads the NoData value using the selected text. If the text is
197
         * null then it reads the text from the text field.
198
         * @param text
199
         */
200
        private void loadNoDataFromText(String text) {
201
                if(text == null) 
202
                        text = getCheckBoxValue();
203
                
204
                if(text.compareTo(NULL_NODATA) == 0) {
205
                        noData.setValue(null);
206
                        noData.save();
207
                        return;
208
                }
209
                
210
                Number number = null;
211
                try {
212
                        switch (dataType) {
213
                        case Buffer.TYPE_BYTE:
214
                                if(text.contains(".")) 
215
                                        text = text.substring(0, text.lastIndexOf("."));
216
                                number = new Byte(text);
217
                                break;
218
                        case Buffer.TYPE_SHORT:
219
                                if(text.contains(".")) 
220
                                        text = text.substring(0, text.lastIndexOf("."));
221
                                number = new Short(text);
222
                                break;
223
                        case Buffer.TYPE_INT:
224
                                if(text.contains(".")) 
225
                                        text = text.substring(0, text.lastIndexOf("."));
226
                                number = new Integer(text);
227
                                break;
228
                        case Buffer.TYPE_FLOAT:
229
                                number = new Float(text);
230
                                break;
231
                        case Buffer.TYPE_DOUBLE:
232
                                number = new Double(text);
233
                                break;
234
                        }
235
                        noData.setValue(number);
236
                        noData.save();
237
                }catch (NumberFormatException e1) {
238
                        RasterToolsUtil.messageBoxError("value_not_valid", this);
239
                }
240
        }
241

    
242
        public void actionPerformed(ActionEvent e) {
243
                //Saves nodata value
244
                if (e.getSource() == getButtonSave()) {
245
                        loadNoDataFromText(null);
246
                }
247
                
248
                //Saves default nodata
249
                if (e.getSource() == getButtonDefaultValue()) {
250
                        switch (dataType) {
251
                        case Buffer.TYPE_BYTE:
252
                                loadNoDataFromText(RasterLibrary.defaultByteNoDataValue + "");
253
                                break;
254
                        case Buffer.TYPE_SHORT:
255
                                loadNoDataFromText(RasterLibrary.defaultShortNoDataValue + "");
256
                                break;
257
                        case Buffer.TYPE_INT:
258
                                loadNoDataFromText(RasterLibrary.defaultIntegerNoDataValue + "");
259
                                break;
260
                        case Buffer.TYPE_FLOAT:
261
                                loadNoDataFromText(RasterLibrary.defaultFloatNoDataValue + "");
262
                                break;
263
                        case Buffer.TYPE_DOUBLE:
264
                                loadNoDataFromText(RasterLibrary.defaultDoubleNoDataValue + "");
265
                                break;
266
                        }
267
                        if(noData.isDefined()) {
268
                                setCheckBoxValue(noData.getValue().doubleValue());
269
                        }
270
                }
271
                
272
                //Deletes nodata value
273
                if (e.getSource() == getButtonDelete()) {
274
                        noData.delete();
275
                        noData.save();
276
                        getTextFieldValue().setText(NULL_NODATA);
277
                }
278
                
279
                //Restores original nodata value
280
                if (e.getSource() == getButtonRestore()) {
281
                        noData.restore();
282
                        noData.save();
283
                        if(noData.isDefined()) {
284
                                setCheckBoxValue(noData.getValue().doubleValue());
285
                        } else {
286
                                getTextFieldValue().setText(NULL_NODATA);
287
                        }
288
                }
289
                
290
                if (RasterExtension.autoRefreshView) {
291
                        if (lyr == null || lyr.getDataStore() == null)
292
                                return;
293

    
294
                        lyr.setNoDataTransparent(getCheckBoxNoDataEnabled().isSelected());
295

    
296
                        // Redibujamos
297
                        lyr.getMapContext().invalidate();
298
                }
299
        }
300

    
301
        /**
302
         * Assigns the data source
303
         * @param t
304
         */
305
        public void setLayer(FLyrRaster layer) {
306
                this.lyr = layer;
307
                this.noData = layer.getNoDataValue();
308
                this.dataType = layer.getDataStore().getDataType()[0];
309
                if(noData.isDefined()) {
310
                        setCheckBoxValue(noData.getValue().doubleValue());
311
                } else {
312
                        getTextFieldValue().setText("Not Defined");
313
                }
314
                getCheckBoxNoDataEnabled().setSelected(noData.isNoDataTransparent());
315
        }
316

    
317
        public JPanel getPanelCenter() {
318
                if(panelCenter == null) {
319
                        panelCenter = new JPanel();
320
                        panelCenter.setLayout(new GridBagLayout());
321
                        
322
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
323
                        gridBagConstraints.fill = GridBagConstraints.NONE;
324
                        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
325
                        panelCenter.add(getLabelValue(), gridBagConstraints);
326
                        
327
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
328
                        gridBagConstraints.weightx = 1.0;
329
                        gridBagConstraints.gridx = 1;
330
                        panelCenter.add(getTextFieldValue(), gridBagConstraints);
331
                        
332
                        gridBagConstraints.fill = GridBagConstraints.NONE;
333
                        gridBagConstraints.weightx = 0.0;
334
                        gridBagConstraints.gridx = 2;
335
                        panelCenter.add(getButtonSave(), gridBagConstraints);
336
                        
337
                        gridBagConstraints.gridx = 3;
338
                        panelCenter.add(getButtonDelete(), gridBagConstraints);
339

    
340
                }
341
                return panelCenter;
342
        }
343

    
344
        public JPanel getPanelUp() {
345
                if(panelUp == null) {
346
                        panelUp = new JPanel();
347
                        panelUp.setLayout(new GridBagLayout());
348
                        
349
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
350
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
351
                        gridBagConstraints.weightx = 1.0;
352
                        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
353
                        panelUp.add(getCheckBoxNoDataEnabled(), gridBagConstraints);
354
                }
355
                return panelUp;
356
        }
357

    
358
        public JPanel getPanelDown() {
359
                if(panelDown == null) {
360
                        panelDown = new JPanel();
361
                        panelDown.setLayout(new GridBagLayout());
362
                        
363
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
364
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
365
                        gridBagConstraints.weightx = 1.0;
366
                        gridBagConstraints.insets = new Insets(2, 2, 2, 2);
367
                        panelDown.add(getButtonDefaultValue(), gridBagConstraints);
368
                        
369
                        gridBagConstraints.gridx = 1;
370
                        panelDown.add(getButtonRestore(), gridBagConstraints);
371
                }
372
                return panelDown;
373
        }
374

    
375
}