Statistics
| Revision:

root / trunk / libraries / libCq CMS for java.old / src / org / cresques / ui / BrightnessContrast / SliderTextPanel.java @ 5788

History | View | Annotate | Download (7.82 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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

    
20
package org.cresques.ui.BrightnessContrast;
21

    
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.event.ActionEvent;
25
import java.awt.event.ActionListener;
26
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusListener;
28

    
29
import javax.swing.JLabel;
30
import javax.swing.JPanel;
31
import javax.swing.JSlider;
32
import javax.swing.JTextField;
33
import javax.swing.event.ChangeEvent;
34
import javax.swing.event.ChangeListener;
35

    
36

    
37
/**
38
 * Clase base para un componente con un slider y un campo de texto.
39
 * Contiene una etiqueta entre el slider y el campo de texto en la que se
40
 * pueden poner las unidades en las que se cuantifica el valor.
41
 * @author Miguel ?ngel Querol Carratal?  <querol_mig@gva.es>
42
 *
43
 */
44
public class SliderTextPanel extends JPanel implements ChangeListener, FocusListener, ActionListener{
45

    
46
        private JPanel pName = null;
47
        private JPanel pSliderText = null;
48
        private JSlider jSlider = null;
49
        private JTextField jTextField = null;
50
        private JLabel lPercent = null;
51
        private boolean decimal = false;
52
        /**
53
         * This is the default constructor
54
         */
55
        public SliderTextPanel() {
56
                super();
57
                initialize();
58
        }
59

    
60
        /**
61
         * This method initializes this
62
         * 
63
         * @return void
64
         */
65
        private void initialize() {
66
                GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
67
                gridBagConstraints1.gridx = 0;
68
                gridBagConstraints1.gridy = 1;
69
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
70
                gridBagConstraints.gridx = 0;
71
                gridBagConstraints.gridy = 0;
72
                this.setLayout(new GridBagLayout());
73
                this.setSize(180, 45);
74
                this.setPreferredSize(new java.awt.Dimension(180,45));
75
                this.add(getJPanel(), gridBagConstraints);
76
                this.add(getJPanel1(), gridBagConstraints1);
77
                this.getJSlider().addChangeListener(this);
78
                this.getJTextField().addFocusListener(this);
79
                this.getJTextField().addActionListener(this);
80
        }
81

    
82
        /**
83
         * This method initializes jPanel        
84
         *         
85
         * @return javax.swing.JPanel        
86
         */
87
        protected JPanel getJPanel() {
88
                if (pName == null) {
89
                        pName = new JPanel();
90
                        pName.setLayout(new GridBagLayout());
91
                        pName.setPreferredSize(new java.awt.Dimension(180,25));
92
                }
93
                return pName;
94
        }
95

    
96
        /**
97
         * This method initializes jPanel1        
98
         *         
99
         * @return javax.swing.JPanel        
100
         */
101
        private JPanel getJPanel1() {
102
                if (pSliderText == null) {
103
                        GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
104
                        gridBagConstraints6.gridx = 1;
105
                        gridBagConstraints6.gridy = 0;
106
                        lPercent = new JLabel();
107
                        lPercent.setText("");
108
                        GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
109
                        gridBagConstraints5.gridx = 1;
110
                        gridBagConstraints5.gridy = 0;
111
                        GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
112
                        gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL;
113
                        gridBagConstraints4.gridy = 0;
114
                        gridBagConstraints4.weightx = 1.0;
115
                        gridBagConstraints4.insets = new java.awt.Insets(0,6,0,6);
116
                        gridBagConstraints4.gridx = 2;
117
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
118
                        gridBagConstraints3.gridx = 1;
119
                        gridBagConstraints3.gridy = 0;
120
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
121
                        gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
122
                        gridBagConstraints2.gridy = 0;
123
                        gridBagConstraints2.weightx = 1.0;
124
                        gridBagConstraints2.gridx = 0;
125
                        pSliderText = new JPanel();
126
                        pSliderText.setLayout(new GridBagLayout());
127
                        pSliderText.setPreferredSize(new java.awt.Dimension(180,20));
128
                        pSliderText.add(getJSlider(), gridBagConstraints2);
129
                        pSliderText.add(getJTextField(), gridBagConstraints4);
130
                        pSliderText.add(lPercent, gridBagConstraints6);
131
                }
132
                return pSliderText;
133
        }
134

    
135
        /**
136
         * This method initializes jSlider        
137
         *         
138
         * @return javax.swing.JSlider        
139
         */
140
        public JSlider getJSlider() {
141
                if (jSlider == null) {
142
                        jSlider = new JSlider();
143
                        jSlider.setPreferredSize(new java.awt.Dimension(100,16));
144
                }
145
                return jSlider;
146
        }
147

    
148
        /**
149
         * This method initializes jTextField        
150
         *         
151
         * @return javax.swing.JTextField        
152
         */
153
        public JTextField getJTextField() {
154
                if (jTextField == null) {
155
                        jTextField = new JTextField();
156
                        jTextField.setPreferredSize(new java.awt.Dimension(20,19));
157
                        jTextField.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
158
                        jTextField.setText(String.valueOf(this.getJSlider().getValue()));
159
                }
160
                return jTextField;
161
        }
162

    
163
        /**
164
         * Pone una cadena con el tipo de unidad del cuadro de texto entre el slider
165
         * y el TextField.
166
         * @param cadena a poner entre el slider y el cudro de texto.
167
         */
168
        
169
        protected void setUnitText(String unit){
170
                lPercent.setText(unit);
171
        }
172
        
173
        /**
174
         * Activa o desactiva el control del panel
175
         * @param active
176
         */
177
        
178
        public void setControlEnabled(boolean active){
179
                this.jSlider.setEnabled(active);
180
                this.jTextField.setEnabled(active);
181
                this.lPercent.setEnabled(active);
182
                if(active == false){
183
                        this.jTextField.setForeground(this.getBackground());
184
                }else{
185
                        this.jTextField.setForeground(java.awt.Color.black);
186
                }
187
                
188
        }
189
        
190
        /**
191
         * Ponel el slider al valor que le pasamos
192
         * @param value
193
         */
194
        
195
        public void setSliderValue(int value){
196
                jSlider.setValue(value);
197
        }
198
        
199
        
200
        /**
201
         * Pone en el campo de texto a la derecha del slider el valor que le pasamos
202
         * como parametro
203
         * @param value: valor que aparecera en el campo de texto.
204
         */
205
        public void setTextValue(String value){
206
                this.getJTextField().setText(value);
207
        }
208

    
209
        /**
210
         * Devuelve el valor actual del campo de texto
211
         * @return: valor del campo de texto.
212
         */
213
        public String getTextValue(){
214
                return this.jTextField.getText();
215
        }
216
        
217
        public void stateChanged(ChangeEvent e) {
218
                if(e.getSource() == this.jSlider){
219
                        if(decimal == true)
220
                                this.jTextField.setText(String.valueOf(this.jSlider.getValue()) + ".0");
221
                        if(decimal == false)
222
                                this.jTextField.setText(String.valueOf(this.jSlider.getValue()));
223
                }
224
        }
225
        
226
        /**
227
         * Da el rango de valores en el que se mover? el slider
228
         * @param min
229
         * @param max
230
         */
231
        public void setSliderRange(int min, int max){
232
                this.jSlider.setMaximum(max);
233
                this.jSlider.setMinimum(min);
234
        }
235

    
236
        public void focusGained(FocusEvent e) {
237
                // TODO Auto-generated method stub
238
                
239
        }
240

    
241
        public void focusLost(FocusEvent e) {
242
                if(e.getSource() == this.jTextField)
243
                        checkTailText();
244
                
245
        }
246

    
247
        public void actionPerformed(ActionEvent e) {
248
                if(e.getSource() == this.jTextField)
249
                        checkTailText();
250
        }
251

    
252
         /**
253
     * Controla que si el formato introducido en el textfield
254
     * de Recorte de colas es numerico se actualiza la posici?n del
255
     * slider
256
     */
257
    private void checkTailText() {
258
        String text = getJTextField().getText();
259
        double value = 0;
260

    
261
        try {
262
                if (!text.equals(""))
263
                value = (Double.parseDouble(text) * 100) / 50;
264
                value = Double.valueOf(text).doubleValue();          
265
            getJSlider().setValue((int) Math.round(value));
266
            getJTextField().setText(text);
267
        } catch (NumberFormatException exc) {
268
            System.err.println("Formato no numerico");
269
            getJTextField().setText("0.0");
270
            getJSlider().setValue(0);
271
        }
272
    }
273
    
274
        public void setDecimal (boolean dec){
275
                decimal = dec;
276
        }
277
        
278
}