Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / slidertext / ColorSliderTextContainer.java @ 12898

History | View | Annotate | Download (8.15 KB)

1 12085 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.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 12368 bsanchez
 *
42 12180 bsanchez
 * @version 15/06/2007
43 12368 bsanchez
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
44 12085 bsanchez
 */
45
public class ColorSliderTextContainer extends JPanel implements ChangeListener, DoubleSliderListener {
46
        private static final long serialVersionUID = 1876415954410511634L;
47 12121 bsanchez
        private ArrayList actionCommandListeners = new ArrayList();
48 12085 bsanchez
49 12107 bsanchez
        private JPanel       pText            = null;
50
        private DoubleSlider slider           = null;
51
        private JSpinner     jSpinner         = null;
52
        private int          min              = 0;
53
        private int          max              = 255;
54
        private int          defaultPos       = 0;
55 12085 bsanchez
56
        /**
57
         * Contructor
58
         * @param min Valor m?nimo de la barra
59
         * @param max Valor m?ximo de la barra
60 12368 bsanchez
         * @param defaultPos Posici?n por defecto
61 12085 bsanchez
         */
62
        public ColorSliderTextContainer(int min, int max, int defaultPos) {
63
                super();
64
                this.min = min;
65
                this.max = max;
66
                this.defaultPos = defaultPos;
67
68
                initialize();
69 12091 bsanchez
                slider.setTwoSliders(false);
70 12085 bsanchez
        }
71
72
        /**
73
         * Constructor vacio
74
         */
75
        public ColorSliderTextContainer() {
76
                this(0, 100, 0);
77
        }
78
79
        /**
80
         * This method initializes this
81 12368 bsanchez
         *
82 12085 bsanchez
         */
83
        private void initialize() {
84
                this.setLayout(new BorderLayout(5, 5));
85
                this.add(getSlider(), BorderLayout.CENTER);
86
                this.add(getPText(), BorderLayout.EAST);
87
        }
88
89
        /**
90 12368 bsanchez
         * This method initializes jPanel1
91
         *
92
         * @return javax.swing.JPanel
93 12085 bsanchez
         */
94
        private JPanel getPText() {
95
                if (pText == null) {
96
                        pText = new JPanel();
97
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
98
                        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 8, 0);
99
                        pText.setLayout(new GridBagLayout());
100
                        pText.add(getJSpinner(), gridBagConstraints1);
101
                }
102
                return pText;
103
        }
104
105
        /**
106 12368 bsanchez
         * This method initializes jSlider
107
         *
108
         * @return javax.swing.JSlider
109 12085 bsanchez
         */
110
        public DoubleSlider getSlider() {
111
                if (slider == null) {
112
                        slider = new DoubleSlider();
113
                        slider.setMinimum(min);
114
                        slider.setMaximum(max);
115
                        slider.setValue(defaultPos);
116
                        slider.addValueChangedListener(this);
117
                }
118
                return slider;
119
        }
120
121
        /**
122 12368 bsanchez
         * This method initializes jTextField
123
         *
124
         * @return javax.swing.JTextField
125 12085 bsanchez
         */
126
        public JSpinner getJSpinner() {
127
                if (jSpinner == null) {
128
                        jSpinner = new JSpinner();
129
                        jSpinner.setValue(new Integer(defaultPos));
130
                        jSpinner.setPreferredSize(new Dimension(50, 26));
131
                        jSpinner.setMinimumSize(new Dimension(50, 26));
132
                        jSpinner.addChangeListener(this);
133
                }
134
                return jSpinner;
135
        }
136
137
        public void setComponentSize(int w, int h){
138
        }
139
140
        /**
141
         * Obtiene el valor del control.
142
         * @return Valor del control en formato double.
143
         */
144
        public int getValue() {
145
                return new Integer(getJSpinner().getValue() + "").intValue();
146
        }
147
148
        /**
149
         * Asigna el valor del control.
150
         * @return Valor del control en formato double.
151
         */
152
        public void setValue(int value) {
153
                getJSpinner().setValue(new Integer(value));
154
                getSlider().setValue(value);
155
        }
156
157
        /**
158
         * Activa o desactiva el control del panel
159
         * @param active
160
         */
161
        public void setControlEnabled(boolean active){
162
                getSlider().setEnabled(active);
163
                getJSpinner().setEnabled(active);
164
        }
165
166
        /**
167
         * Obtiene el valor m?ximo del slider
168
         * @return Entero con el valor m?ximo
169
         */
170
        public int getMax() {
171
                return max;
172
        }
173
174
        /**
175
         * Asigna el valor m?ximo del slider
176
         * @param Entero con el valor m?ximo
177
         * @deprecated Usar setMaximum en su lugar
178
         */
179
        public void setMax(int max) {
180
                this.setMaximum(max);
181
        }
182
183
        /**
184
         * Asigna el valor m?ximo del slider
185
         * @param Entero con el valor m?ximo
186
         */
187
        public void setMaximum(int max) {
188
                this.max = max;
189
                updateInterval();
190
        }
191
192
        /**
193
         * Obtiene el valor m?nimo del slider
194
         * @return Entero con el valor m?nimo
195
         */
196
        public int getMin() {
197
                return min;
198
        }
199
200
        /**
201
         * Asigna el valor m?nimo del slider
202
         * @param Entero con el valor m?nimo
203
         * @deprecated Usar setMinimum
204
         */
205
        public void setMin(int min) {
206
                this.setMinimum(min);
207
        }
208
209
        /**
210
         * Asigna el valor m?nimo del slider
211
         * @param Entero con el valor m?nimo
212
         */
213
        public void setMinimum(int min) {
214
                this.min = min;
215
                updateInterval();
216
        }
217
218
        private void updateInterval() {
219
                int aux = this.getValue();
220
                getSlider().setMinimum(min);
221
                getSlider().setMaximum(max);
222
                setValue(aux);
223
        }
224
225
        /**
226
         * Especificar el color izquierdo del control
227
         * @param color
228
         */
229
        public void setColor1(Color color) {
230
                slider.setColor1(color);
231
        }
232
233
        /**
234
         * Especificar el color derecho del control
235
         * @param color
236
         */
237
        public void setColor2(Color color) {
238
                slider.setColor2(color);
239
        }
240
241
        /**
242 12368 bsanchez
         * Controla cuando cambia el spinner
243 12085 bsanchez
         */
244
        public void stateChanged(ChangeEvent e) {
245
                int value = new Integer(getJSpinner().getValue().toString()).intValue();
246
                getSlider().setValue(value);
247
                if (new Integer(getJSpinner().getValue().toString()).intValue() != getSlider().getValue())
248
                        getJSpinner().setValue(new Integer(getSlider().getValue()));
249
250 12138 bsanchez
                if (spinnerEvent)
251
                        callChangeValue();
252 12085 bsanchez
        }
253
254
        /**
255 12138 bsanchez
         * Dispara el evento del cambio del control
256 12085 bsanchez
         */
257 12138 bsanchez
        protected void callChangeValue() {
258
                Iterator acIterator = actionCommandListeners.iterator();
259
                while (acIterator.hasNext()) {
260
                        DoubleSliderListener listener = (DoubleSliderListener) acIterator.next();
261
                        listener.actionValueChanged(new DoubleSliderEvent(this));
262
                }
263 12085 bsanchez
        }
264
265
        /**
266
         * Dispara el evento del cambio del control
267
         */
268 12138 bsanchez
        protected void callDraggedValue() {
269 12085 bsanchez
                Iterator acIterator = actionCommandListeners.iterator();
270
                while (acIterator.hasNext()) {
271
                        DoubleSliderListener listener = (DoubleSliderListener) acIterator.next();
272 12138 bsanchez
                        listener.actionValueDragged(new DoubleSliderEvent(this));
273 12085 bsanchez
                }
274
        }
275
276
        /**
277
         * A?adir un listener a la lista de eventos
278
         * @param listener
279
         */
280
        public void addValueChangedListener(DoubleSliderListener listener) {
281
                if (!actionCommandListeners.contains(listener))
282
                        actionCommandListeners.add(listener);
283
        }
284
285
        /**
286
         * Borrar un listener de la lista de eventos
287
         * @param listener
288
         */
289
        public void removeValueChangedListener(DoubleSliderListener listener) {
290
                actionCommandListeners.remove(listener);
291
        }
292
293
        /* (non-Javadoc)
294
         * @see javax.swing.JComponent#setEnabled(boolean)
295
         */
296
        public void setEnabled(boolean enabled) {
297
                super.setEnabled(enabled);
298
                jSpinner.setEnabled(enabled);
299
                slider.setEnabled(enabled);
300
        }
301 12138 bsanchez
302
        /**
303 12368 bsanchez
         * Controla cuando cambia el slider
304 12138 bsanchez
         */
305
        public void actionValueChanged(DoubleSliderEvent e) {
306
                int value = getSlider().getValue();
307
                getJSpinner().setValue(new Integer(value));
308
309
                callChangeValue();
310
        }
311
312
        /*
313
         * (non-Javadoc)
314
         * @see org.gvsig.gui.beans.doubleslider.DoubleSliderListener#actionValueDragged(java.util.EventObject)
315
         */
316
        boolean spinnerEvent = true;
317
        public void actionValueDragged(DoubleSliderEvent e) {
318
                int value = getSlider().getValue();
319
                spinnerEvent = false;
320
                getJSpinner().setValue(new Integer(value));
321
                spinnerEvent = true;
322
323
                callDraggedValue();
324
        }
325 12085 bsanchez
}