Statistics
| Revision:

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

History | View | Annotate | Download (7.58 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 18014 bsanchez
import org.gvsig.raster.ConfigurationEvent;
43 17666 bsanchez
import org.gvsig.raster.dataset.io.rmf.RmfBlocksManager;
44
import org.gvsig.raster.datastruct.serializer.NoDataRmfSerializer;
45
import org.gvsig.raster.hierarchy.IRasterProperties;
46 17045 bsanchez
import org.gvsig.raster.util.PanelBase;
47 17666 bsanchez
/**
48
 * Panel para la gestion del valor NoData en el panel de propiedades
49
 *
50
 * @version 18/12/2007
51
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
52
 */
53 20117 bsanchez
public class NoDataPanel extends PanelBase implements ItemListener, ActionListener {
54 17045 bsanchez
        private static final long serialVersionUID = 7645641060812458944L;
55
56 17666 bsanchez
        private JLabel              jLabelValue       = null;
57
        private JComboBox           jComboBoxSetup    = null;
58
        private JButton             buttonSaveDefault = null;
59
        private JFormattedTextField jTextFieldValue   = null;
60
        private INoDataPanel        noDataPanel       = null;
61
        private IRasterProperties   layer             = null;
62 17045 bsanchez
63
        public NoDataPanel(INoDataPanel noDataPanel) {
64
                initialize();
65 17666 bsanchez
                translate();
66 17045 bsanchez
                this.noDataPanel = noDataPanel;
67 17666 bsanchez
                setEnabledComponents(false);
68 17045 bsanchez
        }
69
70
        private JComboBox getComboBoxSetup() {
71
                if (jComboBoxSetup == null) {
72
                        jComboBoxSetup = new JComboBox();
73
                        jComboBoxSetup.addItemListener(this);
74
                }
75
                return jComboBoxSetup;
76
        }
77
78 17666 bsanchez
        private void translate() {
79
                getButtonSaveDefault().setText(getText(this, "guardar_predeterminado"));
80 20117 bsanchez
                getComboBoxSetup().setModel(new DefaultComboBoxModel(new String[] { getText(this, "desactivado"), getText(this, "capa"), getText(this, "personalizado") }));
81 17666 bsanchez
                getLabelValue().setText(getText(this, "value") + ":");
82
        }
83
84
        private JButton getButtonSaveDefault() {
85
                if (buttonSaveDefault == null) {
86
                        buttonSaveDefault = new JButton();
87
                        buttonSaveDefault.addActionListener(this);
88
                }
89
                return buttonSaveDefault;
90
        }
91
92 17045 bsanchez
        private JFormattedTextField getTextFieldValue() {
93
                if (jTextFieldValue == null) {
94 17666 bsanchez
                        NumberFormat integerFormat = NumberFormat.getNumberInstance();
95
                        integerFormat.setParseIntegerOnly(false);
96
                        integerFormat.setMinimumFractionDigits(0);
97
                        integerFormat.setMaximumFractionDigits(3);
98
                        jTextFieldValue = new JFormattedTextField(new DefaultFormatterFactory(
99
                                        new NumberFormatter(integerFormat),
100
                                        new NumberFormatter(integerFormat),
101
                                        new NumberFormatter(integerFormat)));
102 17045 bsanchez
                }
103
                return jTextFieldValue;
104
        }
105
106
        private JLabel getLabelValue() {
107
                if (jLabelValue == null) {
108
                        jLabelValue = new JLabel();
109
                }
110
                return jLabelValue;
111
        }
112
113
        private void initialize() {
114
                GridBagConstraints gridBagConstraints;
115
116
                getPanel().setLayout(new GridBagLayout());
117
118
                int col = 0;
119
                gridBagConstraints = new GridBagConstraints();
120
                gridBagConstraints.gridx = col;
121
                gridBagConstraints.gridy = 0;
122
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
123
                gridBagConstraints.weightx = 1.0;
124
                gridBagConstraints.insets = new Insets(5, 5, 5, 2);
125
                getPanel().add(getComboBoxSetup(), gridBagConstraints);
126
127
                col++;
128
                gridBagConstraints = new GridBagConstraints();
129
                gridBagConstraints.gridx = col;
130
                gridBagConstraints.gridy = 0;
131
                gridBagConstraints.insets = new Insets(5, 2, 5, 2);
132
                getPanel().add(getLabelValue(), gridBagConstraints);
133
134
                col++;
135
                gridBagConstraints = new GridBagConstraints();
136
                gridBagConstraints.gridx = col;
137
                gridBagConstraints.gridy = 0;
138
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
139
                gridBagConstraints.weightx = 1.0;
140
                gridBagConstraints.insets = new Insets(5, 2, 5, 2);
141
                getPanel().add(getTextFieldValue(), gridBagConstraints);
142
143
                col++;
144
                gridBagConstraints = new GridBagConstraints();
145
                gridBagConstraints.gridx = col;
146
                gridBagConstraints.gridy = 0;
147
                gridBagConstraints.insets = new Insets(5, 2, 5, 5);
148 17666 bsanchez
                getPanel().add(getButtonSaveDefault(), gridBagConstraints);
149 17045 bsanchez
        }
150
151 17666 bsanchez
        /*
152
         * (non-Javadoc)
153
         * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
154
         */
155 17045 bsanchez
        public void itemStateChanged(ItemEvent e) {
156
                if (e.getStateChange() == ItemEvent.SELECTED) {
157
                        if (e.getSource() == getComboBoxSetup()) {
158
                                noDataPanel.SourceStateChanged(this, getComboBoxSetup().getSelectedIndex());
159
                                return;
160
                        }
161
                }
162
        }
163
164
        public void setComboValueSetup(int pos) {
165
                getComboBoxSetup().setSelectedIndex(pos);
166 17666 bsanchez
                setEnabledComponents(pos == 2);
167 17045 bsanchez
        }
168
169
        public void setEnabledComponents(boolean b) {
170
                getTextFieldValue().setEnabled(b);
171
                getLabelValue().setEnabled(b);
172
        }
173
174
        public void setNoDataValue(double value) {
175
                getTextFieldValue().setValue(Double.valueOf(value));
176
        }
177
178 17666 bsanchez
        public double getNoDataValue() {
179
                return Double.valueOf(getTextFieldValue().getValue().toString()).doubleValue();
180 17045 bsanchez
        }
181
182
        public int getComboSetupIndex() {
183
                return getComboBoxSetup().getSelectedIndex();
184
        }
185 17666 bsanchez
186
        /*
187
         * (non-Javadoc)
188
         * @see org.gvsig.fmap.raster.util.ConfigurationListener#actionConfigurationChanged(org.gvsig.fmap.raster.util.ConfigurationEvent)
189
         */
190
        public void actionConfigurationChanged(ConfigurationEvent e) {
191
        }
192
193
        public void actionPerformed(ActionEvent e) {
194 17881 bsanchez
                if (e.getSource() == getButtonSaveDefault()) {
195
                        saveToRmf();
196
                        return;
197
                }
198 17666 bsanchez
        }
199
200
        /**
201
         * Aqui se definen los parametros iniciales para la transparencia del panel
202
         * @param t
203
         */
204
        public void setLayer(IRasterProperties layer) {
205
                this.layer = layer;
206
207 17723 bsanchez
                setComboValueSetup(((FLyrRasterSE) layer).getNoDataType());
208 17666 bsanchez
                setNoDataValue(((FLyrRasterSE) layer).getNoDataValue());
209
        }
210
211
        /**
212
         * Salva el valor NoData al fichero rmf.
213
         * @param fName
214
         * @throws IOException
215
         */
216
        private void saveToRmf() {
217
                if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(getPanel(), getText(this, "nodata_modificado_continuar"))) {
218
219
                        RmfBlocksManager manager = ((FLyrRasterSE) layer).getDataSource().getDataset(0)[0].getRmfBlocksManager();
220
                        NoDataRmfSerializer ser;
221 20867 bsanchez
                        ser = new NoDataRmfSerializer(getNoDataValue(), getComboSetupIndex(), layer.getDataType()[0]);
222 17666 bsanchez
223
                        if (!manager.checkRmf())
224
                                return;
225
                        manager.addClient(ser);
226
                        try {
227
                                manager.write(true);
228
                        } catch (FileNotFoundException e) {
229
                        } catch (IOException e) {
230
231
                        }
232
                        manager.removeClient(ser.getClass());
233
                }
234
        }
235 17045 bsanchez
}