Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / ScalePanel.java @ 15804

History | View | Annotate | Download (11.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.BorderLayout;
22
import java.awt.Color;
23
import java.awt.Dimension;
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.JCheckBox;
33
import javax.swing.JFormattedTextField;
34
import javax.swing.JLabel;
35
import javax.swing.JPanel;
36
import javax.swing.text.DefaultFormatterFactory;
37
import javax.swing.text.NumberFormatter;
38

    
39
import org.gvsig.gui.beans.panelGroup.panels.AbstractPanel;
40
import org.gvsig.raster.hierarchy.IRasterProperties;
41

    
42
import com.iver.andami.PluginServices;
43
import com.iver.cit.gvsig.fmap.layers.FLayer;
44
/**
45
 *
46
 * @version 04/10/2007
47
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
48
 */
49
public class ScalePanel extends AbstractPanel implements ActionListener {
50
        private static final long serialVersionUID = -4761218260868307869L;
51
        private FLayer              fLayer              = null;
52

    
53
        private JLabel              labelNotShowLayer   = null;
54
        private JLabel              labelMinim          = null;
55
        private JLabel              labelMaxim          = null;
56
        private JCheckBox           checkBoxMinim       = null;
57
        private JCheckBox           checkBoxMaxim       = null;
58
        private JFormattedTextField textFieldMinim      = null;
59
        private JFormattedTextField textFieldMaxim      = null;
60

    
61
        private JPanel              panel               = null;
62

    
63
        private NumberFormat        doubleDisplayFormat = null;
64
        private NumberFormat        doubleEditFormat    = null;
65
        private double              initMaxScale        = -1;
66
        private double              initMinScale        = -1;
67

    
68
        public ScalePanel() {
69
                super();
70
                setLabel(PluginServices.getText(this, "scale_panel"));
71
                setUpFormats();
72
                initialize();
73
                translate();
74
        }
75

    
76
        /**
77
         * Create and set up number formats. These objects also parse numbers input by
78
         * user.
79
         */
80
        private void setUpFormats() {
81
                doubleDisplayFormat = NumberFormat.getNumberInstance();
82
                doubleDisplayFormat.setMinimumFractionDigits(0);
83
                doubleEditFormat = NumberFormat.getNumberInstance();
84
        }
85

    
86
        private void translate() {
87
                getJLabelNotShowLayer().setText(PluginServices.getText(this, "no_mostrar_la_capa_cuando_la_escala_sea") + ":");
88
                getJLabelMinim().setText("(" + PluginServices.getText(this, "escala_minima") + ")");
89
                getJLabelMaxim().setText("(" + PluginServices.getText(this, "escala_maxima") + ")");
90
                getJCheckBoxMinim().setText(PluginServices.getText(this, "mayor_de") + " 1:");
91
                getJCheckBoxMaxim().setText(PluginServices.getText(this, "menor_de") + " 1:");
92
                getJPanel().setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "rango_de_escalas")));
93
        }
94

    
95
        protected void initialize() {
96
                setLayout(new BorderLayout());
97

    
98
                GridBagConstraints gridBagConstraints;
99

    
100
                JPanel jPanel1 = new JPanel();
101
                jPanel1.setLayout(new GridBagLayout());
102

    
103
                int y = 0;
104
                gridBagConstraints = new GridBagConstraints();
105
                gridBagConstraints.gridx = 0;
106
                gridBagConstraints.gridy = y;
107
                gridBagConstraints.gridwidth = 3;
108
                gridBagConstraints.anchor = GridBagConstraints.WEST;
109
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
110
                jPanel1.add(getJLabelNotShowLayer(), gridBagConstraints);
111

    
112
                y++;
113
                gridBagConstraints = new GridBagConstraints();
114
                gridBagConstraints.gridx = 0;
115
                gridBagConstraints.gridy = y;
116
                gridBagConstraints.insets = new Insets(2, 2, 2, 0);
117
                gridBagConstraints.anchor = GridBagConstraints.WEST;
118
                jPanel1.add(getJCheckBoxMinim(), gridBagConstraints);
119

    
120
                gridBagConstraints = new GridBagConstraints();
121
                gridBagConstraints.gridx = 1;
122
                gridBagConstraints.gridy = y;
123
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
124
                gridBagConstraints.weightx = 1.0;
125
                gridBagConstraints.insets = new Insets(2, 0, 2, 2);
126
                jPanel1.add(getJTextFieldMinim(), gridBagConstraints);
127

    
128
                gridBagConstraints = new GridBagConstraints();
129
                gridBagConstraints.gridx = 2;
130
                gridBagConstraints.gridy = y;
131
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
132
                gridBagConstraints.anchor = GridBagConstraints.WEST;
133
                jPanel1.add(getJLabelMinim(), gridBagConstraints);
134

    
135
                y++;
136
                gridBagConstraints = new GridBagConstraints();
137
                gridBagConstraints.gridx = 0;
138
                gridBagConstraints.gridy = y;
139
                gridBagConstraints.insets = new Insets(2, 2, 2, 0);
140
                gridBagConstraints.anchor = GridBagConstraints.WEST;
141
                jPanel1.add(getJCheckBoxMaxim(), gridBagConstraints);
142

    
143
                gridBagConstraints = new GridBagConstraints();
144
                gridBagConstraints.gridx = 1;
145
                gridBagConstraints.gridy = y;
146
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
147
                gridBagConstraints.weightx = 1.0;
148
                gridBagConstraints.insets = new Insets(2, 0, 2, 2);
149
                jPanel1.add(getJTextFieldMaxim(), gridBagConstraints);
150

    
151
                gridBagConstraints = new GridBagConstraints();
152
                gridBagConstraints.gridx = 2;
153
                gridBagConstraints.gridy = y;
154
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
155
                gridBagConstraints.anchor = GridBagConstraints.WEST;
156
                jPanel1.add(getJLabelMaxim(), gridBagConstraints);
157

    
158
                y++;
159
                gridBagConstraints = new GridBagConstraints();
160
                gridBagConstraints.gridx = 0;
161
                gridBagConstraints.gridy = y;
162
                gridBagConstraints.gridwidth = 3;
163
                gridBagConstraints.fill = GridBagConstraints.BOTH;
164
                gridBagConstraints.weightx = 1.0;
165
                gridBagConstraints.weighty = 1.0;
166
                JPanel emptyPanel = new JPanel();
167
                emptyPanel.setMinimumSize(new Dimension(0, 0));
168
                jPanel1.add(emptyPanel, gridBagConstraints);
169

    
170
                getJPanel().add(jPanel1, BorderLayout.CENTER);
171
                add(getJPanel(), BorderLayout.CENTER);
172
        }
173

    
174
        private JPanel getJPanel() {
175
                if (panel == null) {
176
                        panel = new JPanel();
177
                        panel.setLayout(new BorderLayout());
178
                }
179
                return panel;
180
        }
181

    
182
        private JLabel getJLabelNotShowLayer() {
183
                if (labelNotShowLayer == null) {
184
                        labelNotShowLayer = new JLabel();
185
                }
186
                return labelNotShowLayer;
187
        }
188

    
189
        private JFormattedTextField getJTextFieldMinim() {
190
                if (textFieldMinim == null) {
191
                        textFieldMinim = new JFormattedTextField(new DefaultFormatterFactory(
192
                                        new NumberFormatter(doubleDisplayFormat),
193
                                        new NumberFormatter(doubleDisplayFormat),
194
                                        new NumberFormatter(doubleEditFormat)));
195
                        textFieldMinim.setEnabled(false);
196
                        textFieldMinim.setBackground(getBackground());
197
                }
198
                return textFieldMinim;
199
        }
200

    
201
        private JFormattedTextField getJTextFieldMaxim() {
202
                if (textFieldMaxim == null) {
203
                        textFieldMaxim = new JFormattedTextField(new DefaultFormatterFactory(
204
                                        new NumberFormatter(doubleDisplayFormat),
205
                                        new NumberFormatter(doubleDisplayFormat),
206
                                        new NumberFormatter(doubleEditFormat)));
207
                        textFieldMaxim.setEnabled(false);
208
                        textFieldMaxim.setBackground(getBackground());
209
                }
210
                return textFieldMaxim;
211
        }
212

    
213
        private JCheckBox getJCheckBoxMinim() {
214
                if (checkBoxMinim == null) {
215
                        checkBoxMinim = new JCheckBox();
216
                        checkBoxMinim.addActionListener(this);
217
                        checkBoxMinim.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
218
                        checkBoxMinim.setMargin(new java.awt.Insets(0, 0, 0, 0));
219
                }
220
                return checkBoxMinim;
221
        }
222

    
223
        private JLabel getJLabelMinim() {
224
                if (labelMinim == null) {
225
                        labelMinim = new JLabel();
226
                        labelMinim.setEnabled(false);
227
                }
228
                return labelMinim;
229
        }
230

    
231
        private JCheckBox getJCheckBoxMaxim() {
232
                if (checkBoxMaxim == null) {
233
                        checkBoxMaxim = new JCheckBox();
234
                        checkBoxMaxim.addActionListener(this);
235
                        checkBoxMaxim.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
236
                        checkBoxMaxim.setMargin(new java.awt.Insets(0, 0, 0, 0));
237
                }
238
                return checkBoxMaxim;
239
        }
240

    
241
        private JLabel getJLabelMaxim() {
242
                if (labelMaxim == null) {
243
                        labelMaxim = new JLabel();
244
                        labelMaxim.setEnabled(false);
245
                }
246
                return labelMaxim;
247
        }
248

    
249
        /*
250
         * (non-Javadoc)
251
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
252
         */
253
        public void actionPerformed(ActionEvent e) {
254
                if (e.getSource() == getJCheckBoxMinim()) {
255
                        boolean enabled = getJCheckBoxMinim().isSelected();
256
                        getJLabelMinim().setEnabled(enabled);
257
                        getJTextFieldMinim().setEnabled(enabled);
258
                        getJTextFieldMinim().setBackground(enabled?Color.white: getBackground());
259
                }
260

    
261
                if (e.getSource() == getJCheckBoxMaxim()) {
262
                        boolean enabled = getJCheckBoxMaxim().isSelected();
263
                        getJLabelMaxim().setEnabled(enabled);
264
                        getJTextFieldMaxim().setEnabled(enabled);
265
                        getJTextFieldMaxim().setBackground(enabled?Color.white: getBackground());
266
                }
267
        }
268

    
269
        public void setReference(Object ref) {
270
                super.setReference(ref);
271

    
272
                if (!(ref instanceof FLayer))
273
                        return;
274

    
275
                FLayer lyr = (FLayer) ref;
276

    
277
                if (lyr instanceof IRasterProperties) {
278
                        fLayer = lyr;
279

    
280
                        if (fLayer.getMaxScale() != -1) {
281
                                initMaxScale = fLayer.getMaxScale();
282
                                getJTextFieldMaxim().setValue(
283
                                                Double.valueOf(fLayer.getMaxScale()));
284
                                getJCheckBoxMaxim().setSelected(true);
285
                                getJLabelMaxim().setEnabled(true);
286
                                getJTextFieldMaxim().setEnabled(true);
287
                                getJTextFieldMaxim().setBackground(Color.WHITE);
288
                        }
289

    
290
                        if (fLayer.getMinScale() != -1) {
291
                                initMinScale = fLayer.getMinScale();
292
                                getJTextFieldMinim().setValue(
293
                                                Double.valueOf(fLayer.getMinScale()));
294
                                getJCheckBoxMinim().setSelected(true);
295
                                getJLabelMinim().setEnabled(true);
296
                                getJTextFieldMinim().setEnabled(true);
297
                                getJTextFieldMinim().setBackground(Color.WHITE);
298
                        }
299
                }
300
        }
301

    
302
        /*
303
         * (non-Javadoc)
304
         * @see org.gvsig.raster.gui.properties.dialog.IRegistrablePanel#accept()
305
         */
306
        public void accept() {
307
                if (fLayer == null)
308
                        return;
309

    
310
                double maxScale = -1;
311
                double minScale = -1;
312

    
313
                if (getJCheckBoxMaxim().isSelected())
314
                        maxScale = ((Number) getJTextFieldMaxim().getValue()).doubleValue();
315

    
316
                if (getJCheckBoxMinim().isSelected())
317
                        minScale = ((Number) getJTextFieldMinim().getValue()).doubleValue();
318

    
319
                fLayer.setMaxScale(maxScale);
320
                fLayer.setMinScale(minScale);
321
        }
322

    
323
        /*
324
         * (non-Javadoc)
325
         * @see org.gvsig.raster.gui.properties.dialog.IRegistrablePanel#apply()
326
         */
327
        public void apply() {
328
                accept();
329
        }
330

    
331
        /*
332
         * (non-Javadoc)
333
         * @see org.gvsig.raster.gui.properties.dialog.IRegistrablePanel#cancel()
334
         */
335
        public void cancel() {
336
                fLayer.setMaxScale(initMaxScale);
337
                fLayer.setMinScale(initMinScale);
338
        }
339

    
340
        public void selected() {
341
        }
342
}