Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / NoDataPanel.java @ 18000

History | View | Annotate | Download (8.17 KB)

1 17045 bsanchez
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.rastertools.properties.panels;
20
21
import java.awt.GridBagConstraints;
22
import java.awt.GridBagLayout;
23
import java.awt.Insets;
24 17666 bsanchez
import java.awt.event.ActionEvent;
25
import java.awt.event.ActionListener;
26 17045 bsanchez
import java.awt.event.ItemEvent;
27
import java.awt.event.ItemListener;
28 17666 bsanchez
import java.io.FileNotFoundException;
29
import java.io.IOException;
30
import java.text.NumberFormat;
31 17045 bsanchez
32
import javax.swing.DefaultComboBoxModel;
33 17666 bsanchez
import javax.swing.JButton;
34 17045 bsanchez
import javax.swing.JComboBox;
35
import javax.swing.JFormattedTextField;
36
import javax.swing.JLabel;
37 17666 bsanchez
import javax.swing.JOptionPane;
38
import javax.swing.text.DefaultFormatterFactory;
39
import javax.swing.text.NumberFormatter;
40 17045 bsanchez
41 17666 bsanchez
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
42
import org.gvsig.raster.dataset.io.rmf.RmfBlocksManager;
43
import org.gvsig.raster.datastruct.serializer.NoDataRmfSerializer;
44
import org.gvsig.raster.hierarchy.IRasterProperties;
45 17045 bsanchez
import org.gvsig.raster.util.PanelBase;
46 18000 bsanchez
import org.gvsig.rastertools.Configuration;
47
import org.gvsig.rastertools.ConfigurationEvent;
48
import org.gvsig.rastertools.ConfigurationListener;
49 17045 bsanchez
import org.gvsig.rastertools.properties.panels.interfaces.INoDataPanel;
50 17666 bsanchez
/**
51
 * Panel para la gestion del valor NoData en el panel de propiedades
52
 *
53
 * @version 18/12/2007
54
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
55
 */
56
public class NoDataPanel extends PanelBase implements ItemListener, ConfigurationListener, ActionListener {
57 17045 bsanchez
        private static final long serialVersionUID = 7645641060812458944L;
58
59 17666 bsanchez
        private JLabel              jLabelValue       = null;
60
        private JComboBox           jComboBoxSetup    = null;
61
        private JButton             buttonSaveDefault = null;
62
        private JFormattedTextField jTextFieldValue   = null;
63
        private INoDataPanel        noDataPanel       = null;
64
        private IRasterProperties   layer             = null;
65 17045 bsanchez
66
        public NoDataPanel(INoDataPanel noDataPanel) {
67
                initialize();
68 17666 bsanchez
                translate();
69 17045 bsanchez
                this.noDataPanel = noDataPanel;
70 17666 bsanchez
                Configuration.addValueChangedListener(this);
71
                setEnabledComponents(false);
72 17045 bsanchez
        }
73
74
        private JComboBox getComboBoxSetup() {
75
                if (jComboBoxSetup == null) {
76
                        jComboBoxSetup = new JComboBox();
77
                        jComboBoxSetup.addItemListener(this);
78
                }
79
                return jComboBoxSetup;
80
        }
81
82 17666 bsanchez
        private void translate() {
83
                getButtonSaveDefault().setText(getText(this, "guardar_predeterminado"));
84
                getComboBoxSetup().setModel(new DefaultComboBoxModel(new String[] { getText(this, "global"), getText(this, "capa"), getText(this, "personalizado") }));
85
                getLabelValue().setText(getText(this, "value") + ":");
86
        }
87
88
        private JButton getButtonSaveDefault() {
89
                if (buttonSaveDefault == null) {
90
                        buttonSaveDefault = new JButton();
91
                        buttonSaveDefault.addActionListener(this);
92
                }
93
                return buttonSaveDefault;
94
        }
95
96 17045 bsanchez
        private JFormattedTextField getTextFieldValue() {
97
                if (jTextFieldValue == null) {
98 17666 bsanchez
                        NumberFormat integerFormat = NumberFormat.getNumberInstance();
99
                        integerFormat.setParseIntegerOnly(false);
100
                        integerFormat.setMinimumFractionDigits(0);
101
                        integerFormat.setMaximumFractionDigits(3);
102
                        jTextFieldValue = new JFormattedTextField(new DefaultFormatterFactory(
103
                                        new NumberFormatter(integerFormat),
104
                                        new NumberFormatter(integerFormat),
105
                                        new NumberFormatter(integerFormat)));
106 17045 bsanchez
                }
107
                return jTextFieldValue;
108
        }
109
110
        private JLabel getLabelValue() {
111
                if (jLabelValue == null) {
112
                        jLabelValue = new JLabel();
113
                }
114
                return jLabelValue;
115
        }
116
117
        private void initialize() {
118
                GridBagConstraints gridBagConstraints;
119
120
                getPanel().setLayout(new GridBagLayout());
121
122
                int col = 0;
123
                gridBagConstraints = new GridBagConstraints();
124
                gridBagConstraints.gridx = col;
125
                gridBagConstraints.gridy = 0;
126
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
127
                gridBagConstraints.weightx = 1.0;
128
                gridBagConstraints.insets = new Insets(5, 5, 5, 2);
129
                getPanel().add(getComboBoxSetup(), gridBagConstraints);
130
131
                col++;
132
                gridBagConstraints = new GridBagConstraints();
133
                gridBagConstraints.gridx = col;
134
                gridBagConstraints.gridy = 0;
135
                gridBagConstraints.insets = new Insets(5, 2, 5, 2);
136
                getPanel().add(getLabelValue(), gridBagConstraints);
137
138
                col++;
139
                gridBagConstraints = new GridBagConstraints();
140
                gridBagConstraints.gridx = col;
141
                gridBagConstraints.gridy = 0;
142
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
143
                gridBagConstraints.weightx = 1.0;
144
                gridBagConstraints.insets = new Insets(5, 2, 5, 2);
145
                getPanel().add(getTextFieldValue(), gridBagConstraints);
146
147
                col++;
148
                gridBagConstraints = new GridBagConstraints();
149
                gridBagConstraints.gridx = col;
150
                gridBagConstraints.gridy = 0;
151
                gridBagConstraints.insets = new Insets(5, 2, 5, 5);
152 17666 bsanchez
                getPanel().add(getButtonSaveDefault(), gridBagConstraints);
153
154
                getPanel().setVisible(Configuration.getValue("nodata_transparency_enabled", Boolean.FALSE).booleanValue());
155 17045 bsanchez
        }
156
157 17666 bsanchez
        /*
158
         * (non-Javadoc)
159
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
160
         */
161 17045 bsanchez
        public void itemStateChanged(ItemEvent e) {
162
                if (e.getStateChange() == ItemEvent.SELECTED) {
163
                        if (e.getSource() == getComboBoxSetup()) {
164
                                noDataPanel.SourceStateChanged(this, getComboBoxSetup().getSelectedIndex());
165
                                return;
166
                        }
167
                }
168
        }
169
170
        public void setComboValueSetup(int pos) {
171
                getComboBoxSetup().setSelectedIndex(pos);
172 17666 bsanchez
                setEnabledComponents(pos == 2);
173 17045 bsanchez
        }
174
175
        public void setEnabledComponents(boolean b) {
176
                getTextFieldValue().setEnabled(b);
177
                getLabelValue().setEnabled(b);
178
        }
179
180
        public void setNoDataValue(double value) {
181
                getTextFieldValue().setValue(Double.valueOf(value));
182
        }
183
184 17666 bsanchez
        public double getNoDataValue() {
185
                return Double.valueOf(getTextFieldValue().getValue().toString()).doubleValue();
186 17045 bsanchez
        }
187
188
        public int getComboSetupIndex() {
189
                return getComboBoxSetup().getSelectedIndex();
190
        }
191 17666 bsanchez
192
        /*
193
         * (non-Javadoc)
194
         * @see org.gvsig.fmap.raster.util.ConfigurationListener#actionConfigurationChanged(org.gvsig.fmap.raster.util.ConfigurationEvent)
195
         */
196
        public void actionConfigurationChanged(ConfigurationEvent e) {
197
                if (e.getKey().equals("nodata_transparency_enabled")) {
198
                        getPanel().setVisible(((Boolean) e.getValue()).booleanValue());
199
                }
200
201
                if (e.getKey().equals("nodata_value")) {
202
                        if (getComboSetupIndex() == 0)
203
                                setNoDataValue(((Double) e.getValue()).doubleValue());
204
                }
205
        }
206
207
        public void actionPerformed(ActionEvent e) {
208 17881 bsanchez
                if (e.getSource() == getButtonSaveDefault()) {
209
                        saveToRmf();
210
                        return;
211
                }
212 17666 bsanchez
        }
213
214
        /**
215
         * Aqui se definen los parametros iniciales para la transparencia del panel
216
         * @param t
217
         */
218
        public void setLayer(IRasterProperties layer) {
219
                this.layer = layer;
220
221 17723 bsanchez
                setComboValueSetup(((FLyrRasterSE) layer).getNoDataType());
222 17666 bsanchez
                setNoDataValue(((FLyrRasterSE) layer).getNoDataValue());
223
        }
224
225
        /**
226
         * Salva el valor NoData al fichero rmf.
227
         * @param fName
228
         * @throws IOException
229
         */
230
        private void saveToRmf() {
231
                if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(getPanel(), getText(this, "nodata_modificado_continuar"))) {
232
233
                        RmfBlocksManager manager = ((FLyrRasterSE) layer).getDataSource().getDataset(0)[0].getRmfBlocksManager();
234
                        NoDataRmfSerializer ser;
235
                        ser = new NoDataRmfSerializer(Double.valueOf(getNoDataValue()));
236
237
                        if (!manager.checkRmf())
238
                                return;
239
                        manager.addClient(ser);
240
                        try {
241
                                manager.write(true);
242
                        } catch (FileNotFoundException e) {
243
                        } catch (IOException e) {
244
245
                        }
246
                        manager.removeClient(ser.getClass());
247
                }
248
        }
249 17045 bsanchez
}