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 / NoDataNewPanel.java @ 859
History | View | Annotate | Download (10.8 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.RasterToolsUtil; |
45 |
import org.gvsig.raster.util.BasePanel; |
46 |
|
47 |
/**
|
48 |
* Panel para la gestion del valor NoData en el panel de propiedades
|
49 |
*
|
50 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
51 |
*/
|
52 |
public class NoDataNewPanel extends BasePanel implements ActionListener { |
53 |
private static final long serialVersionUID = 7645641060812458944L; |
54 |
|
55 |
private JLabel jLabelValue = null; |
56 |
private JButton buttonSave = null; |
57 |
private JButton buttonDelete = null; |
58 |
private JButton buttonDefaultValue = null; |
59 |
private JButton buttonRestore = null; |
60 |
private JFormattedTextField jTextFieldValue = null; |
61 |
private JPanel panelCenter = null; |
62 |
private JPanel panelUp = null; |
63 |
private JPanel panelDown = null; |
64 |
private JCheckBox jCheckBoxNoDataEnabled = null; |
65 |
private NoData noData = null; |
66 |
private int dataType = Buffer.TYPE_UNDEFINED; |
67 |
|
68 |
public NoDataNewPanel() {
|
69 |
init(); |
70 |
translate(); |
71 |
} |
72 |
|
73 |
protected void translate() { |
74 |
getButtonSave().setText(getText(this, "guardar")); |
75 |
getButtonSave().setToolTipText(getText(this, "guardar_box_value")); |
76 |
getButtonRestore().setText(getText(this, "restore")); |
77 |
getButtonRestore().setToolTipText(getText(this, "restore_native_value")); |
78 |
getButtonDefaultValue().setText(getText(this, "default_value")); |
79 |
getButtonDefaultValue().setToolTipText(getText(this, "set_configuration_value")); |
80 |
getButtonDelete().setText(getText(this, "delete")); |
81 |
getButtonDelete().setToolTipText(getText(this, "delete_value")); |
82 |
getCheckBoxNoDataEnabled().setText(getText(this, "tratar_nodata_transparente")); |
83 |
getCheckBoxNoDataEnabled().setToolTipText(getCheckBoxNoDataEnabled().getText()); |
84 |
getLabelValue().setText(getText(this, "value") + ":"); |
85 |
} |
86 |
|
87 |
private JButton getButtonSave() { |
88 |
if (buttonSave == null) { |
89 |
buttonSave = new JButton(); |
90 |
buttonSave.addActionListener(this);
|
91 |
} |
92 |
return buttonSave;
|
93 |
} |
94 |
|
95 |
private JButton getButtonDelete() { |
96 |
if (buttonDelete == null) { |
97 |
buttonDelete = new JButton(); |
98 |
buttonDelete.addActionListener(this);
|
99 |
} |
100 |
return buttonDelete;
|
101 |
} |
102 |
|
103 |
private JButton getButtonDefaultValue() { |
104 |
if (buttonDefaultValue == null) { |
105 |
buttonDefaultValue = new JButton(); |
106 |
buttonDefaultValue.addActionListener(this);
|
107 |
} |
108 |
return buttonDefaultValue;
|
109 |
} |
110 |
|
111 |
private JButton getButtonRestore() { |
112 |
if (buttonRestore == null) { |
113 |
buttonRestore = new JButton(); |
114 |
buttonRestore.addActionListener(this);
|
115 |
} |
116 |
return buttonRestore;
|
117 |
} |
118 |
|
119 |
private JFormattedTextField getTextFieldValue() { |
120 |
if (jTextFieldValue == null) { |
121 |
NumberFormat integerFormat = NumberFormat.getNumberInstance(); |
122 |
integerFormat.setParseIntegerOnly(false);
|
123 |
integerFormat.setMinimumFractionDigits(0);
|
124 |
integerFormat.setMaximumFractionDigits(3);
|
125 |
jTextFieldValue = new JFormattedTextField(new DefaultFormatterFactory( |
126 |
new NumberFormatter(integerFormat), |
127 |
new NumberFormatter(integerFormat), |
128 |
new NumberFormatter(integerFormat))); |
129 |
} |
130 |
return jTextFieldValue;
|
131 |
} |
132 |
|
133 |
private JLabel getLabelValue() { |
134 |
if (jLabelValue == null) { |
135 |
jLabelValue = new JLabel(); |
136 |
} |
137 |
return jLabelValue;
|
138 |
} |
139 |
|
140 |
protected void init() { |
141 |
GridBagConstraints gridBagConstraints;
|
142 |
|
143 |
setLayout(new GridBagLayout()); |
144 |
|
145 |
gridBagConstraints = new GridBagConstraints(); |
146 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
147 |
gridBagConstraints.weightx = 1.0;
|
148 |
add(getPanelUp(), gridBagConstraints); |
149 |
|
150 |
gridBagConstraints.gridy = 1;
|
151 |
add(getPanelCenter(), gridBagConstraints); |
152 |
|
153 |
gridBagConstraints.gridy = 2;
|
154 |
add(getPanelDown(), gridBagConstraints); |
155 |
} |
156 |
|
157 |
public JCheckBox getCheckBoxNoDataEnabled() { |
158 |
if (jCheckBoxNoDataEnabled == null) { |
159 |
jCheckBoxNoDataEnabled = new JCheckBox(); |
160 |
jCheckBoxNoDataEnabled.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); |
161 |
jCheckBoxNoDataEnabled.setMargin(new Insets(0, 0, 0, 0)); |
162 |
} |
163 |
return jCheckBoxNoDataEnabled;
|
164 |
} |
165 |
|
166 |
|
167 |
private void setCheckBoxValue(double d) { |
168 |
String text = d + ""; |
169 |
text = text.replaceAll("\\.", ","); |
170 |
getTextFieldValue().setText(text); |
171 |
} |
172 |
|
173 |
private String getCheckBoxValue() { |
174 |
String text = getTextFieldValue().getText();
|
175 |
text = text.replaceAll("\\.", ""); |
176 |
text = text.replaceAll(",", "\\."); |
177 |
return text;
|
178 |
} |
179 |
|
180 |
public void setEnabledComponents(boolean b) { |
181 |
getTextFieldValue().setEnabled(b); |
182 |
getLabelValue().setEnabled(b); |
183 |
getButtonDelete().setEnabled(b); |
184 |
getButtonRestore().setEnabled(b); |
185 |
getButtonSave().setEnabled(b); |
186 |
getButtonDefaultValue().setEnabled(b); |
187 |
} |
188 |
|
189 |
public NoData getNoDataValue() {
|
190 |
return noData;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Loads the NoData value using the selected text. If the text is
|
195 |
* null then it reads the text from the text field.
|
196 |
* @param text
|
197 |
*/
|
198 |
private void loadNoDataFromText(String text) { |
199 |
if(text == null) |
200 |
text = getCheckBoxValue(); |
201 |
|
202 |
Number number = null; |
203 |
try {
|
204 |
switch (dataType) {
|
205 |
case Buffer.TYPE_BYTE: |
206 |
number = new Byte(text); |
207 |
break;
|
208 |
case Buffer.TYPE_SHORT: |
209 |
number = new Short(text); |
210 |
break;
|
211 |
case Buffer.TYPE_INT: |
212 |
number = new Integer(text); |
213 |
break;
|
214 |
case Buffer.TYPE_FLOAT: |
215 |
number = new Float(text); |
216 |
break;
|
217 |
case Buffer.TYPE_DOUBLE: |
218 |
number = new Double(text); |
219 |
break;
|
220 |
} |
221 |
noData.setValue(number); |
222 |
noData.save(); |
223 |
}catch (NumberFormatException e1) { |
224 |
RasterToolsUtil.messageBoxError("error_creating_nodata", this); |
225 |
} |
226 |
} |
227 |
|
228 |
public void actionPerformed(ActionEvent e) { |
229 |
//Saves nodata value
|
230 |
if (e.getSource() == getButtonSave()) {
|
231 |
loadNoDataFromText(null);
|
232 |
} |
233 |
|
234 |
//Saves default nodata
|
235 |
if (e.getSource() == getButtonDefaultValue()) {
|
236 |
switch (dataType) {
|
237 |
case Buffer.TYPE_BYTE: |
238 |
loadNoDataFromText(RasterLibrary.defaultByteNoDataValue + "");
|
239 |
break;
|
240 |
case Buffer.TYPE_SHORT: |
241 |
loadNoDataFromText(RasterLibrary.defaultShortNoDataValue + "");
|
242 |
break;
|
243 |
case Buffer.TYPE_INT: |
244 |
loadNoDataFromText(RasterLibrary.defaultIntegerNoDataValue + "");
|
245 |
break;
|
246 |
case Buffer.TYPE_FLOAT: |
247 |
loadNoDataFromText(RasterLibrary.defaultFloatNoDataValue + "");
|
248 |
break;
|
249 |
case Buffer.TYPE_DOUBLE: |
250 |
loadNoDataFromText(RasterLibrary.defaultDoubleNoDataValue + "");
|
251 |
break;
|
252 |
} |
253 |
if(noData.isDefined()) {
|
254 |
setCheckBoxValue(noData.getValue().doubleValue()); |
255 |
} |
256 |
} |
257 |
|
258 |
//Deletes nodata value
|
259 |
if (e.getSource() == getButtonDelete()) {
|
260 |
noData.delete(); |
261 |
noData.save(); |
262 |
getTextFieldValue().setText("Not Defined");
|
263 |
} |
264 |
|
265 |
//Restores original nodata value
|
266 |
if (e.getSource() == getButtonRestore()) {
|
267 |
noData.restore(); |
268 |
noData.save(); |
269 |
if(noData.isDefined()) {
|
270 |
setCheckBoxValue(noData.getValue().doubleValue()); |
271 |
} else {
|
272 |
getTextFieldValue().setText("Not Defined");
|
273 |
} |
274 |
} |
275 |
} |
276 |
|
277 |
/**
|
278 |
* Assigns the data source
|
279 |
* @param t
|
280 |
*/
|
281 |
public void setLayer(FLyrRaster layer) { |
282 |
this.noData = layer.getNoDataValue();
|
283 |
this.dataType = layer.getDataStore().getDataType()[0]; |
284 |
if(noData.isDefined()) {
|
285 |
setCheckBoxValue(noData.getValue().doubleValue()); |
286 |
} else {
|
287 |
getTextFieldValue().setText("Not Defined");
|
288 |
} |
289 |
getCheckBoxNoDataEnabled().setSelected(noData.isNoDataTransparent()); |
290 |
} |
291 |
|
292 |
public JPanel getPanelCenter() { |
293 |
if(panelCenter == null) { |
294 |
panelCenter = new JPanel(); |
295 |
panelCenter.setLayout(new GridBagLayout()); |
296 |
|
297 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
298 |
gridBagConstraints.fill = GridBagConstraints.NONE;
|
299 |
gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
300 |
panelCenter.add(getLabelValue(), gridBagConstraints); |
301 |
|
302 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
303 |
gridBagConstraints.weightx = 1.0;
|
304 |
gridBagConstraints.gridx = 1;
|
305 |
panelCenter.add(getTextFieldValue(), gridBagConstraints); |
306 |
|
307 |
gridBagConstraints.fill = GridBagConstraints.NONE;
|
308 |
gridBagConstraints.weightx = 0.0;
|
309 |
gridBagConstraints.gridx = 2;
|
310 |
panelCenter.add(getButtonSave(), gridBagConstraints); |
311 |
|
312 |
gridBagConstraints.gridx = 3;
|
313 |
panelCenter.add(getButtonDelete(), gridBagConstraints); |
314 |
|
315 |
} |
316 |
return panelCenter;
|
317 |
} |
318 |
|
319 |
public JPanel getPanelUp() { |
320 |
if(panelUp == null) { |
321 |
panelUp = new JPanel(); |
322 |
panelUp.setLayout(new GridBagLayout()); |
323 |
|
324 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
325 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
326 |
gridBagConstraints.weightx = 1.0;
|
327 |
gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
328 |
panelUp.add(getCheckBoxNoDataEnabled(), gridBagConstraints); |
329 |
} |
330 |
return panelUp;
|
331 |
} |
332 |
|
333 |
public JPanel getPanelDown() { |
334 |
if(panelDown == null) { |
335 |
panelDown = new JPanel(); |
336 |
panelDown.setLayout(new GridBagLayout()); |
337 |
|
338 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
339 |
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
|
340 |
gridBagConstraints.weightx = 1.0;
|
341 |
gridBagConstraints.insets = new Insets(2, 2, 2, 2); |
342 |
panelDown.add(getButtonDefaultValue(), gridBagConstraints); |
343 |
|
344 |
gridBagConstraints.gridx = 1;
|
345 |
panelDown.add(getButtonRestore(), gridBagConstraints); |
346 |
} |
347 |
return panelDown;
|
348 |
} |
349 |
|
350 |
} |