Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / slidertext / ColorSliderTextContainer.java @ 12107

History | View | Annotate | Download (7.54 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.gui.beans.slidertext;
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.util.ArrayList;
27
import java.util.Iterator;
28

    
29
import javax.swing.JPanel;
30
import javax.swing.JSpinner;
31
import javax.swing.event.ChangeEvent;
32
import javax.swing.event.ChangeListener;
33

    
34
import org.gvsig.gui.beans.doubleslider.DoubleSlider;
35
import org.gvsig.gui.beans.doubleslider.DoubleSliderEvent;
36
import org.gvsig.gui.beans.doubleslider.DoubleSliderListener;
37
/**
38
 * Barra de deslizamiento con una ventana de texto que tiene el valor de la
39
 * posici?n de la barra. En este control podr? controlarse mediante la entrada
40
 * de datos por la caja de texto la posibilidad de introducir valores decimales.
41
 * 
42
 * Nacho Brodin (nachobrodin@gmail.com)
43
 */
44
public class ColorSliderTextContainer extends JPanel implements ChangeListener, DoubleSliderListener {
45
        private static final long serialVersionUID = 1876415954410511634L;
46
        private ArrayList<DoubleSliderListener> actionCommandListeners = new ArrayList<DoubleSliderListener>();
47

    
48
        private JPanel       pText            = null;
49
        private DoubleSlider slider           = null;
50
        private JSpinner     jSpinner         = null;
51
        private int          min              = 0;
52
        private int          max              = 255;
53
        private int          defaultPos       = 0;
54

    
55
        private boolean      bDoCallListeners = true;
56

    
57
        /**
58
         * Contructor
59
         * @param min Valor m?nimo de la barra
60
         * @param max Valor m?ximo de la barra
61
         * @param defaultPos Posici?n por defecto 
62
         */
63
        public ColorSliderTextContainer(int min, int max, int defaultPos) {
64
                super();
65
                this.min = min;
66
                this.max = max;
67
                this.defaultPos = defaultPos;
68

    
69
                initialize();
70
                slider.setTwoSliders(false);
71
        }
72

    
73
        /**
74
         * Constructor vacio
75
         */
76
        public ColorSliderTextContainer() {
77
                this(0, 100, 0);
78
        }
79

    
80
        /**
81
         * This method initializes this
82
         * 
83
         */
84
        private void initialize() {
85
                this.setLayout(new BorderLayout(5, 5));
86
                this.add(getSlider(), BorderLayout.CENTER);
87
                this.add(getPText(), BorderLayout.EAST);
88
        }
89

    
90
        /**
91
         * This method initializes jPanel1        
92
         *         
93
         * @return javax.swing.JPanel        
94
         */
95
        private JPanel getPText() {
96
                if (pText == null) {
97
                        pText = new JPanel();
98
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
99
                        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 8, 0);
100
                        pText.setLayout(new GridBagLayout());
101
                        pText.add(getJSpinner(), gridBagConstraints1);
102
                }
103
                return pText;
104
        }
105

    
106
        /**
107
         * This method initializes jSlider        
108
         *         
109
         * @return javax.swing.JSlider        
110
         */
111
        public DoubleSlider getSlider() {
112
                if (slider == null) {
113
                        slider = new DoubleSlider();
114
                        slider.setMinimum(min);
115
                        slider.setMaximum(max);
116
                        slider.setValue(defaultPos);
117
                        slider.addValueChangedListener(this);
118
                }
119
                return slider;
120
        }
121

    
122
        /**
123
         * This method initializes jTextField        
124
         *         
125
         * @return javax.swing.JTextField        
126
         */
127
        public JSpinner getJSpinner() {
128
                if (jSpinner == null) {
129
                        jSpinner = new JSpinner();
130
                        jSpinner.setValue(new Integer(defaultPos));
131
                        jSpinner.setPreferredSize(new Dimension(50, 26));
132
                        jSpinner.setMinimumSize(new Dimension(50, 26));
133
                        jSpinner.addChangeListener(this);
134
                }
135
                return jSpinner;
136
        }
137

    
138
        public void setComponentSize(int w, int h){
139
        }
140

    
141
        /**
142
         * Obtiene el valor del control.
143
         * @return Valor del control en formato double.
144
         */
145
        public int getValue() {
146
                return new Integer(getJSpinner().getValue() + "").intValue();
147
        }
148

    
149
        /**
150
         * Asigna el valor del control.
151
         * @return Valor del control en formato double.
152
         */
153
        public void setValue(int value) {
154
                getJSpinner().setValue(new Integer(value));
155
                getSlider().setValue(value);
156
        }
157

    
158
        /**
159
         * Activa o desactiva el control del panel
160
         * @param active
161
         */
162
        public void setControlEnabled(boolean active){
163
                getSlider().setEnabled(active);
164
                getJSpinner().setEnabled(active);
165
        }
166

    
167
        /**
168
         * Obtiene el valor m?ximo del slider
169
         * @return Entero con el valor m?ximo
170
         */
171
        public int getMax() {
172
                return max;
173
        }
174

    
175
        /**
176
         * Asigna el valor m?ximo del slider
177
         * @param Entero con el valor m?ximo
178
         * @deprecated Usar setMaximum en su lugar
179
         */
180
        public void setMax(int max) {
181
                this.setMaximum(max);
182
        }
183

    
184
        /**
185
         * Asigna el valor m?ximo del slider
186
         * @param Entero con el valor m?ximo
187
         */
188
        public void setMaximum(int max) {
189
                this.max = max;
190
                updateInterval();
191
        }
192

    
193
        /**
194
         * Obtiene el valor m?nimo del slider
195
         * @return Entero con el valor m?nimo
196
         */
197
        public int getMin() {
198
                return min;
199
        }
200

    
201
        /**
202
         * Asigna el valor m?nimo del slider
203
         * @param Entero con el valor m?nimo
204
         * @deprecated Usar setMinimum
205
         */
206
        public void setMin(int min) {
207
                this.setMinimum(min);
208
        }
209

    
210
        /**
211
         * Asigna el valor m?nimo del slider
212
         * @param Entero con el valor m?nimo
213
         */
214
        public void setMinimum(int min) {
215
                this.min = min;
216
                updateInterval();
217
        }
218

    
219
        private void updateInterval() {
220
                int aux = this.getValue();
221
                getSlider().setMinimum(min);
222
                getSlider().setMaximum(max);
223
                setValue(aux);
224
        }
225

    
226
        /**
227
         * Especificar el color izquierdo del control
228
         * @param color
229
         */
230
        public void setColor1(Color color) {
231
                slider.setColor1(color);
232
        }
233

    
234
        /**
235
         * Especificar el color derecho del control
236
         * @param color
237
         */
238
        public void setColor2(Color color) {
239
                slider.setColor2(color);
240
        }
241

    
242
        /**
243
         * Controla cuando cambia el spinner 
244
         */
245
        public void stateChanged(ChangeEvent e) {
246
                int value = new Integer(getJSpinner().getValue().toString()).intValue();
247
                getSlider().setValue(value);
248
                if (new Integer(getJSpinner().getValue().toString()).intValue() != getSlider().getValue())
249
                        getJSpinner().setValue(new Integer(getSlider().getValue()));
250

    
251
                callChangeValue();
252
        }
253

    
254
        /**
255
         * Controla cuando cambia el slider 
256
         */
257
        public void actionValueChanged(DoubleSliderEvent e) {
258
                int value = getSlider().getValue();
259
                getJSpinner().setValue(new Integer(value));
260

    
261
                callChangeValue();
262
        }
263

    
264
        /**
265
         * Dispara el evento del cambio del control
266
         */
267
        protected void callChangeValue() {
268
                if (!bDoCallListeners)
269
                        return;
270
                Iterator acIterator = actionCommandListeners.iterator();
271
                while (acIterator.hasNext()) {
272
                        DoubleSliderListener listener = (DoubleSliderListener) acIterator.next();
273
                        listener.actionValueChanged(new DoubleSliderEvent(this));
274
                }
275
        }
276

    
277
        /**
278
         * A?adir un listener a la lista de eventos
279
         * @param listener
280
         */
281
        public void addValueChangedListener(DoubleSliderListener listener) {
282
                if (!actionCommandListeners.contains(listener))
283
                        actionCommandListeners.add(listener);
284
        }
285

    
286
        /**
287
         * Borrar un listener de la lista de eventos
288
         * @param listener
289
         */
290
        public void removeValueChangedListener(DoubleSliderListener listener) {
291
                actionCommandListeners.remove(listener);
292
        }
293

    
294
        /* (non-Javadoc)
295
         * @see javax.swing.JComponent#setEnabled(boolean)
296
         */
297
        public void setEnabled(boolean enabled) {
298
                super.setEnabled(enabled);
299
                jSpinner.setEnabled(enabled);
300
                slider.setEnabled(enabled);
301
        }
302
}