Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / ui / library / AddLibrary.java @ 19129

History | View | Annotate | Download (11.8 KB)

1
/* 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.colortable.ui.library;
20

    
21
import java.awt.Component;
22
import java.awt.Dimension;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.Insets;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.util.ArrayList;
29
import java.util.EventObject;
30

    
31
import javax.swing.BorderFactory;
32
import javax.swing.ButtonGroup;
33
import javax.swing.JLabel;
34
import javax.swing.JOptionPane;
35
import javax.swing.JPanel;
36
import javax.swing.JRadioButton;
37
import javax.swing.JTextField;
38

    
39
import org.gvsig.gui.beans.datainput.DataInputContainerListener;
40
import org.gvsig.gui.beans.datainput.DataInputField;
41
import org.gvsig.raster.datastruct.ColorItem;
42
import org.gvsig.raster.datastruct.ColorTable;
43

    
44
import com.iver.andami.PluginServices;
45
/**
46
 * Ventana para a?adir una nueva libreria. En ella se especificara si queremos
47
 * una tabla de color por intervalos o el numero de cortes. La generar? con el
48
 * nombre seleccionado y todos los colores seran negros inicialmente.
49
 * 
50
 * @version 01/10/2007
51
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
52
 */
53
public class AddLibrary extends JOptionPane implements ActionListener, DataInputContainerListener {
54
  private static final long serialVersionUID = 1638303379491319120L;
55

    
56
  private ButtonGroup    buttonGroup1   = null;
57
        private JLabel         jLabelName     = null;
58
        private JLabel         jLabelMinim    = null;
59
        private JLabel         jLabelMaxim    = null;
60
        private JPanel         jPanel1        = null;
61
        private JRadioButton   jRadioInterval = null;
62
        private JRadioButton   jRadioCount    = null;
63
        private JTextField     jTextName      = null;
64
        private DataInputField jTextMinim     = null;
65
        private DataInputField jTextMaxim     = null;
66
        private DataInputField jTextValue     = null;
67

    
68
        private String         interval       = "255";
69
        private String         count          = "1";
70

    
71
        /**
72
         * Si supera el numero de particiones estimado, se le preguntar? al usuario si
73
         * quiere continuar
74
         */
75
        private int          limit_question = 500;
76

    
77
        /**
78
         * Devuelve el componente interno del JOptionPane
79
         * @return
80
         */
81
  private Object getMessageComponent() {
82
    GridBagConstraints gridBagConstraints;
83

    
84
          JPanel panel = new JPanel();
85
    panel.setLayout(new GridBagLayout());
86
    jPanel1 = new JPanel();
87
    jPanel1.setLayout(new GridBagLayout());
88
    jPanel1.setBorder(BorderFactory.createTitledBorder(""));
89

    
90
    int y = 0;
91
    
92
    jLabelName = new JLabel(PluginServices.getText(this, "nombre") + ":");
93
    gridBagConstraints = new GridBagConstraints();
94
    gridBagConstraints.gridx = 0;
95
    gridBagConstraints.gridy = y;
96
    gridBagConstraints.anchor = GridBagConstraints.EAST;
97
    gridBagConstraints.insets = new Insets(5, 5, 2, 2);
98
    panel.add(jLabelName, gridBagConstraints);
99

    
100
    jTextName = new JTextField(PluginServices.getText(this, "nueva_libreria"));
101
    gridBagConstraints = new GridBagConstraints();
102
    gridBagConstraints.gridx = 1;
103
    gridBagConstraints.gridy = y;
104
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
105
    gridBagConstraints.insets = new Insets(5, 2, 2, 5);
106
    panel.add(jTextName, gridBagConstraints);
107

    
108
    y++;
109
    jLabelMinim = new JLabel(PluginServices.getText(this, "minimo") + ":");
110
    gridBagConstraints = new GridBagConstraints();
111
    gridBagConstraints.gridx = 0;
112
    gridBagConstraints.gridy = y;
113
    gridBagConstraints.anchor = GridBagConstraints.EAST;
114
    gridBagConstraints.insets = new Insets(2, 5, 2, 2);
115
    panel.add(jLabelMinim, gridBagConstraints);
116

    
117
    jTextMinim = new DataInputField();
118
    jTextMinim.setValue(Double.valueOf("0").toString());
119
    jTextMinim.addValueChangedListener(this);
120
    gridBagConstraints = new GridBagConstraints();
121
    gridBagConstraints.gridx = 1;
122
    gridBagConstraints.gridy = y;
123
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
124
    gridBagConstraints.insets = new Insets(2, 2, 2, 5);
125
    panel.add(jTextMinim, gridBagConstraints);
126

    
127
    y++;
128
    jLabelMaxim = new JLabel(PluginServices.getText(this, "maximo") + ":");
129
    gridBagConstraints = new GridBagConstraints();
130
    gridBagConstraints.gridx = 0;
131
    gridBagConstraints.gridy = y;
132
    gridBagConstraints.anchor = GridBagConstraints.EAST;
133
    gridBagConstraints.insets = new Insets(2, 5, 2, 2);
134
    panel.add(jLabelMaxim, gridBagConstraints);
135

    
136
    jTextMaxim = new DataInputField();
137
    jTextMaxim.setValue(Double.valueOf("255").toString());
138
    jTextMaxim.addValueChangedListener(this);
139
    gridBagConstraints = new GridBagConstraints();
140
    gridBagConstraints.gridx = 1;
141
    gridBagConstraints.gridy = y;
142
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
143
    gridBagConstraints.insets = new Insets(2, 2, 2, 5);
144
    panel.add(jTextMaxim, gridBagConstraints);
145
    
146
    y++;
147
    gridBagConstraints = new GridBagConstraints();
148
    gridBagConstraints.gridx = 0;
149
    gridBagConstraints.gridy = y;
150
    gridBagConstraints.gridwidth = 2;
151
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
152
    gridBagConstraints.weightx = 1.0;
153
    gridBagConstraints.insets = new Insets(2, 5, 0, 5);
154
    panel.add(jPanel1, gridBagConstraints);
155

    
156
    // Panel vacio
157
    y++;
158
    gridBagConstraints = new GridBagConstraints();
159
    gridBagConstraints.gridx = 0;
160
    gridBagConstraints.gridy = y;
161
    gridBagConstraints.gridwidth = 2;
162
    gridBagConstraints.fill = GridBagConstraints.BOTH;
163
    gridBagConstraints.weightx = 1.0;
164
    gridBagConstraints.weighty = 1.0;
165
    JPanel panel2 = new JPanel();
166
    panel2.setPreferredSize(new Dimension(0, 0));
167
    panel.add(panel2, gridBagConstraints);
168
    
169
    jRadioInterval = new JRadioButton(PluginServices.getText(this, "tamano_intervalo"), true);
170
    jRadioInterval.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
171
    jRadioInterval.setMargin(new Insets(0, 0, 0, 0));
172
    jRadioInterval.addActionListener(this);
173
    gridBagConstraints = new GridBagConstraints();
174
    gridBagConstraints.anchor = GridBagConstraints.WEST;
175
    gridBagConstraints.insets = new Insets(5, 5, 2, 5);
176
    jPanel1.add(jRadioInterval, gridBagConstraints);
177

    
178
    jRadioCount = new JRadioButton(PluginServices.getText(this, "n_intervalos"));
179
    jRadioCount.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
180
    jRadioCount.setMargin(new Insets(0, 0, 0, 0));
181
    jRadioCount.addActionListener(this);
182
    gridBagConstraints = new GridBagConstraints();
183
    gridBagConstraints.gridx = 0;
184
    gridBagConstraints.gridy = 1;
185
    gridBagConstraints.anchor = GridBagConstraints.WEST;
186
    gridBagConstraints.insets = new Insets(2, 5, 2, 5);
187
    jPanel1.add(jRadioCount, gridBagConstraints);
188

    
189
    jTextValue = new DataInputField();
190
    jTextValue.setValue(Double.valueOf(interval).toString());
191
    jTextValue.addValueChangedListener(this);
192
    gridBagConstraints = new GridBagConstraints();
193
    gridBagConstraints.gridx = 0;
194
    gridBagConstraints.gridy = 2;
195
    gridBagConstraints.weightx = 1.0;
196
    gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
197
    gridBagConstraints.insets = new Insets(2, 5, 5, 5);
198
    jPanel1.add(jTextValue, gridBagConstraints);
199

    
200
    buttonGroup1 = new ButtonGroup();
201
    buttonGroup1.add(jRadioInterval);
202
    buttonGroup1.add(jRadioCount);
203
    
204
    return panel;
205
  }
206
  
207
  /**
208
         * Muestra el di?logo de la creaci?n de la tabla de color. Devolver?
209
         * JOptionPane.OK_OPTION si el usuario le ha dado a aceptar y otro valor en
210
         * caso contrario.
211
         * @param parentComponent
212
         * @return
213
         */
214
  public int showConfirm(Component parentComponent) {
215
          return showConfirmDialog(
216
                          parentComponent,
217
                          getMessageComponent(),
218
                          PluginServices.getText(this, "nueva_libreria_title"),
219
                          JOptionPane.OK_CANCEL_OPTION,
220
                          JOptionPane.PLAIN_MESSAGE);
221
  }
222
  
223
  /**
224
   * Devuelve la tabla de color creada segun los parametros especificados en el
225
   * panel.
226
   * @return
227
   */
228
  public ColorTable getColorTable() {
229
                ColorTable colorTable = new ColorTable();
230
                colorTable.setName(getNameLibrary());
231
                ArrayList items = new ArrayList();
232
                
233
                double start = getMinim();
234
                double end = getMaxim();
235
                
236
                if (start > end) {
237
                        double aux = start;
238
                        start = end;
239
                        end = aux;
240
                }
241

    
242
                double num = getInterval();
243
                boolean quest = false;
244
                if (jRadioCount.isSelected())
245
                        quest = (num > limit_question);
246
                else
247
                        quest = (((end - start) / num) > limit_question);
248
                if (quest && JOptionPane.showConfirmDialog(this, PluginServices.getText(this, "addlibrary_supera_limite"), PluginServices.getText(this, "confirmar"), JOptionPane.YES_NO_OPTION) == NO_OPTION)
249
                        return null;
250
                if (jRadioCount.isSelected()) {
251
                        for (double i = 0; i <= num; i++) {
252
                                ColorItem colorItem = new ColorItem();
253
                                colorItem.setValue(start + (((end - start) / num) * i));
254
                                items.add(colorItem);
255
                        }
256
                } else {
257
                        for (double i = start; i <= end; i += num) {
258
                                ColorItem colorItem = new ColorItem();
259
                                colorItem.setValue(i);
260
                                items.add(colorItem);
261
                        }
262
                }
263
                
264
                colorTable.createPaletteFromColorItems(items, false);
265
                return colorTable;
266
  }
267

    
268
  /**
269
   * Devuelve el minimo
270
   * @return
271
   */
272
  private double getMinim() {
273
                return Double.parseDouble(jTextMinim.getValue());
274
  }
275

    
276
  /**
277
   * Devuelve el maximo
278
   * @return
279
   */
280
  private double getMaxim() {
281
                return Double.parseDouble(jTextMaxim.getValue());
282
  }
283
  
284
        /**
285
         * Devuelve un intervalo valido
286
         * @return
287
         */
288
        private double getInterval() {
289
                if (jRadioCount.isSelected())
290
                        count = jTextValue.getValue();
291
                else
292
                        interval = jTextValue.getValue();
293
                double value = Double.parseDouble(jTextValue.getValue());
294
                double value2 = value;
295
                if (jRadioInterval.isSelected()) {
296
                        if (value > Math.abs(getMaxim() - getMinim()))
297
                                value = Math.abs(getMaxim() - getMinim());
298
                        if (value <= 0)
299
                                value = 1;
300
                } else {
301
                        if (value < 1)
302
                                value = 1;
303
                }
304
                value = Math.abs(value);
305
                
306
                if (value != value2) {
307
                        jTextValue.setValue(Double.valueOf(value).toString());
308
                        
309
                        if (jRadioCount.isSelected())
310
                                count = jTextValue.getValue();
311
                        else
312
                                interval = jTextValue.getValue();
313
                }
314
                return value;
315
        }
316
        
317
        /**
318
         * Devuelve el nombre de la nueva libreria
319
         */
320
        private String getNameLibrary() {
321
                if (jTextName.getText().length() == 0)
322
                        return PluginServices.getText(this, "nueva_libreria");
323

    
324
                return jTextName.getText();
325
        }
326

    
327
        boolean selectedCount = false;
328
        /*
329
         * (non-Javadoc)
330
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
331
         */
332
        public void actionPerformed(ActionEvent e) {
333
          if (jRadioCount.isSelected()) {
334
                  if (!selectedCount) {
335
                          interval = jTextValue.getValue();
336
                          jTextValue.setValue(count);
337
                          selectedCount = true;
338
                  }
339
          } else {
340
                  if (selectedCount) {
341
                          count = jTextValue.getValue();
342
                          jTextValue.setValue(interval);
343
                          selectedCount = false;
344
                  }
345
          }
346
  }
347

    
348
        /*
349
         * (non-Javadoc)
350
         * @see org.gvsig.gui.beans.datainput.DataInputContainerListener#actionValueChanged(java.util.EventObject)
351
         */
352
        public void actionValueChanged(EventObject e) {
353
                getInterval();
354
  }
355
}