Statistics
| Revision:

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

History | View | Annotate | Download (11.8 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.FlowLayout;
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.util.ArrayList;
27
import java.util.EventObject;
28
import java.util.Iterator;
29

    
30
import javax.swing.DefaultListModel;
31
import javax.swing.JButton;
32
import javax.swing.JCheckBox;
33
import javax.swing.JList;
34
import javax.swing.JPanel;
35
import javax.swing.JScrollPane;
36
import javax.swing.ListSelectionModel;
37

    
38
import org.gvsig.raster.datastruct.TransparencyRange;
39
import org.gvsig.rastertools.properties.control.TranspByPixelEventListener;
40
import org.gvsig.rastertools.properties.control.TranspByPixelListener;
41

    
42
import com.iver.andami.PluginServices;
43
/**
44
 * Panel con los controles para la transparencia por pixel. Incluye los textbox
45
 * para a?adir un RGB a la lista.
46
 *
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class TranspByPixelPanel extends JPanel {
50
        private static final long               serialVersionUID = -1131297200332579683L;
51
        private boolean                         controlEnabled = false;
52
        private JCheckBox                       activeCheck    = null;
53
        private JList                           jList          = null;
54
        private JPanel                          pixelSelector  = null;
55
        private JPanel                          activeControl  = null;
56
        private JPanel                          pEast          = null;
57
        private JPanel                          pCenter        = null;
58
        private JPanel                          pButtons       = null;
59
        private JButton                         bAdd           = null;
60
        private JButton                         bRemove        = null;
61
        private JButton                         bSelectColor   = null;
62
        private JScrollPane                     jScrollList    = null;
63
        private TranspByPixelRGBInputPanel      pWest          = null;
64
        private TranspByPixelAndOrSelectorPanel pOperation     = null;
65
        private TranspByPixelListener           listener       = null;
66
        private ArrayList                       actionCommandListeners = new ArrayList();
67

    
68
        /**
69
         * This is the default constructor
70
         */
71
        public TranspByPixelPanel() {
72
                initialize();
73
                // Inicializa el TranspByPixelListener
74
                getListener();
75
                setControlEnabled(false);
76
        }
77

    
78
        /**
79
         * This method initializes this
80
         */
81
        private void initialize() {
82
                setLayout(new BorderLayout());
83
                add(getActiveControl(), BorderLayout.NORTH);
84
                add(getPixelSelector(), BorderLayout.CENTER);
85
        }
86

    
87
        /**
88
         * Obtiene el panel con los controles principales para introducir valores RGB
89
         * en la lista
90
         * @return JPanel
91
         */
92
        private JPanel getPixelSelector() {
93
                if (pixelSelector == null) {
94

    
95
                        pixelSelector = new JPanel();
96
                        pixelSelector.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 11, 5, 11));
97

    
98
                        pixelSelector.setLayout(new BorderLayout(5, 5));
99

    
100
                        JPanel jpanel1 = new JPanel();
101
                        jpanel1.setLayout(new BorderLayout(5, 5));
102
                        jpanel1.add(getPRGBInput(), BorderLayout.CENTER);
103
                        jpanel1.add(getPCenter(), BorderLayout.EAST);
104

    
105
                        pixelSelector.add(jpanel1, BorderLayout.CENTER);
106
                        pixelSelector.add(getPList(), BorderLayout.EAST);
107
                }
108
                return pixelSelector;
109
        }
110

    
111
        /**
112
         * Obtiene el panel con el control de activaci?n
113
         * @return JPanel
114
         */
115
        private JPanel getActiveControl() {
116
                if (activeControl == null){
117
                        activeControl = new JPanel();
118
                        FlowLayout fl = new FlowLayout();
119
                        fl.setAlignment(FlowLayout.LEFT);
120
                        activeControl.setLayout(fl);
121
                        activeControl.add(getActiveCheck());
122
                }
123
                return activeControl;
124
        }
125

    
126
        /**
127
         * Obtiene el check que activa y desactiva la transparencia por pixel.
128
         * @return JCheckBox
129
         */
130
        public JCheckBox getActiveCheck() {
131
                if(activeCheck == null) {
132
                        activeCheck = new JCheckBox(PluginServices.getText(this, "activar"));
133
                        activeCheck.setSelected(false);
134
                }
135
                return activeCheck;
136
        }
137

    
138
        /**
139
         * This method initializes jPanel
140
         * @return
141
         */
142
        public TranspByPixelRGBInputPanel getPRGBInput() {
143
                if (pWest == null) {
144
                        pWest = new TranspByPixelRGBInputPanel();
145
                }
146
                return pWest;
147
        }
148

    
149
        /**
150
         * This method initializes jPanel1
151
         * @return javax.swing.JPanel
152
         */
153
        public JPanel getPList() {
154
                if (pEast == null) {
155
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
156
                        gridBagConstraints3.fill = java.awt.GridBagConstraints.BOTH;
157
                        gridBagConstraints3.gridx = 0;
158
                        gridBagConstraints3.gridy = 0;
159
                        gridBagConstraints3.weightx = 1.0;
160
                        gridBagConstraints3.weighty = 1.0;
161
                        gridBagConstraints3.insets = new java.awt.Insets(0,0,0,0);
162
                        pEast = new JPanel();
163
                        pEast.setLayout(new GridBagLayout());
164
                        pEast.add(getJScrollList(), gridBagConstraints3);
165
                }
166
                return pEast;
167
        }
168

    
169
        /**
170
         * This method initializes pCenter
171
         * @return javax.swing.JPanel
172
         */
173
        private JPanel getPCenter() {
174
                if (pCenter == null) {
175
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
176
                        gridBagConstraints1.insets = new java.awt.Insets(0,0,0,0);
177
                        gridBagConstraints1.gridy = 1;
178
                        gridBagConstraints1.gridx = 0;
179
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
180
                        gridBagConstraints.insets = new java.awt.Insets(0,0,0,0);
181
                        gridBagConstraints.gridy = 0;
182
                        gridBagConstraints.gridx = 0;
183
                        pCenter = new JPanel();
184
                        pCenter.setLayout(new GridBagLayout());
185
                        pCenter.add(getPButtons(), gridBagConstraints);
186
                        pCenter.add(getPOperation(), gridBagConstraints1);
187
                }
188
                return pCenter;
189
        }
190

    
191

    
192
        /**
193
         * This method initializes bAdd
194
         *
195
         * @return javax.swing.JButton
196
         */
197
        public JButton getBAdd() {
198
                if (bAdd == null) {
199
                        bAdd = new JButton("A?adir");
200
                        bAdd.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
201
                        bAdd.setIcon(PluginServices.getIconTheme().get("forward-icon"));
202
                }
203
                return bAdd;
204
        }
205

    
206
        /**
207
         * This method initializes bRemove
208
         *
209
         * @return javax.swing.JButton
210
         */
211
        public JButton getBRemove() {
212
                if (bRemove == null) {
213
                        bRemove = new JButton("Quitar");
214
                        bRemove.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
215
                        bRemove.setIcon(PluginServices.getIconTheme().get("backward-icon"));
216
                        bRemove.setEnabled(false);
217
                }
218
                return bRemove;
219
        }
220

    
221
        public JButton getBSelectColor() {
222
                if (bSelectColor == null) {
223
                        bSelectColor = new JButton("Color");
224
                }
225
                return bSelectColor;
226
        }
227

    
228
        /**
229
         * This method initializes jPanel
230
         *
231
         * @return javax.swing.JPanel
232
         */
233
        private JPanel getPButtons() {
234
                if (pButtons == null) {
235
                        pButtons = new JPanel();
236
                        pButtons.setLayout(new java.awt.GridBagLayout());
237

    
238
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
239
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
240
                        gridBagConstraints.gridy = 0;
241
                        gridBagConstraints.gridx = 0;
242
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
243
                        pButtons.add(getBAdd(), gridBagConstraints);
244

    
245
                        gridBagConstraints = new GridBagConstraints();
246
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
247
                        gridBagConstraints.gridy = 1;
248
                        gridBagConstraints.gridx = 0;
249
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
250
                        pButtons.add(getBRemove(), gridBagConstraints);
251

    
252
                        gridBagConstraints = new GridBagConstraints();
253
                        gridBagConstraints.insets = new java.awt.Insets(3, 3, 3, 3);
254
                        gridBagConstraints.gridy = 2;
255
                        gridBagConstraints.gridx = 0;
256
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
257
                        pButtons.add(getBSelectColor(), gridBagConstraints);
258
                }
259
                return pButtons;
260
        }
261

    
262
        /**
263
         * This method initializes jPanel1
264
         *
265
         * @return javax.swing.JPanel
266
         */
267
        public TranspByPixelAndOrSelectorPanel getPOperation() {
268
                if (pOperation == null) {
269
                        pOperation = new TranspByPixelAndOrSelectorPanel();
270
                        pOperation.setPreferredSize(new java.awt.Dimension(60,60));
271
                }
272
                return pOperation;
273
        }
274

    
275
        /**
276
         * This method initializes jList
277
         *
278
         * @return javax.swing.JList
279
         */
280
        public JList getJList() {
281
                if (jList == null) {
282
                        jList = new JList(new DefaultListModel());
283
                        jList.setFont(new java.awt.Font("Monospaced", java.awt.Font.PLAIN, 10));
284
                        jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
285
                }
286
                return jList;
287
        }
288

    
289
        /**
290
         * Inicializa el scroll para el jList
291
         * @return
292
         */
293
        public JScrollPane getJScrollList() {
294
                if (jScrollList == null) {
295
                        jScrollList = new JScrollPane();
296
                        jScrollList.setViewportView(getJList());
297
                        jScrollList.setPreferredSize(new java.awt.Dimension(160,100));
298
                }
299
                return jScrollList;
300
        }
301

    
302
        /**
303
         * Obtiene el ListModel de la lista
304
         * @return DefaultListModel
305
         */
306
        public DefaultListModel getListModel(){
307
                return (DefaultListModel) getJList().getModel();
308
        }
309

    
310
        /**
311
         * Activa o desactiva el control
312
         * @param enable True activa el control y false lo desactiva
313
         */
314
        public void setControlEnabled(boolean enabled){
315
                this.getActiveCheck().setSelected(enabled);
316
                getJList().setEnabled(enabled);
317
                if (enabled)
318
                        getJList().setBackground(Color.WHITE);
319
                else
320
                        getJList().setBackground(this.getBackground());
321
                getPRGBInput().setControlEnabled(enabled);
322
                getBAdd().setEnabled(enabled);
323
                getBSelectColor().setEnabled(enabled);
324

    
325
                if (enabled)
326
                        getBRemove().setEnabled(getJList().getModel().getSize()>0);
327
                else
328
                        getBRemove().setEnabled(false);
329

    
330
                getPOperation().setControlEnabled(enabled);
331
                controlEnabled = enabled;
332
        }
333

    
334
        /**
335
         * Obtiene true si el control est? activo y false si no lo est?
336
         * @return
337
         */
338
        public boolean isControlEnabled(){
339
                return controlEnabled;
340
        }
341

    
342
        /**
343
         * Asigna el n?mero de bandas activas
344
         * @param n N?mero de bandas
345
         */
346
        public void setActiveBands(int n){
347
                ((TranspByPixelRGBInputPanel)getPRGBInput()).setActiveBands(n);
348
        }
349

    
350
        /**
351
         * Obtiene el array de entradas de valores a?adidos a la lista
352
         * @return ArrayList
353
         */
354
        public ArrayList getEntries(){
355
                return getListener().getEntries();
356
        }
357

    
358
        /**
359
         * Obtiene la clase manejadora de eventos asociada a esta
360
         * @return TransparencyByPixelListener
361
         */
362
        public TranspByPixelListener getListener() {
363
                if (listener == null) {
364
                        listener = new TranspByPixelListener(this);
365
                }
366
                return listener;
367
        }
368

    
369
        /**
370
         * A?ade una entrada a la tabla
371
         * @param entry objeto TransparencyRange
372
         */
373
        public void addStringElement(TransparencyRange entry){
374
                getEntries().add(entry);
375
                getListModel().addElement(entry.getStrEntry());
376
                getBRemove().setEnabled(true);
377
        }
378

    
379
        /**
380
         * Borra las entradas de la tabla
381
         */
382
        public void clear(){
383
                getPRGBInput().clear();
384
                getListModel().clear();
385
                getEntries().clear();
386
                getBRemove().setEnabled(false);
387
        }
388

    
389
        /**
390
         * A?adir un listener a la lista de eventos
391
         * @param listener
392
         */
393
        public void addValueChangedListener(TranspByPixelEventListener listener) {
394
                if (!actionCommandListeners.contains(listener))
395
                        actionCommandListeners.add(listener);
396
        }
397

    
398
        /**
399
         * Borrar un listener de la lista de eventos
400
         * @param listener
401
         */
402
        public void removeValueChangedListener(TranspByPixelEventListener listener) {
403
                actionCommandListeners.remove(listener);
404
        }
405

    
406
        /**
407
         * Dispara todos los manejadores que estan apuntando a este evento
408
         */
409
        public void callValueChanged() {
410
                Iterator acIterator = actionCommandListeners.iterator();
411
                while (acIterator.hasNext()) {
412
                        TranspByPixelEventListener listener = (TranspByPixelEventListener) acIterator.next();
413
                        listener.actionPixelListChanged(new EventObject(this));
414
                }
415
        }
416
}