svn-gvsig-desktop / tags / v1_0_2_Build_894 / extensions / extRasterTools / src / org / gvsig / rasterTools / brightnessContrast / ui / SliderTextPanel.java @ 10309
History | View | Annotate | Download (7.46 KB)
1 | 5497 | maquerol | /* 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 | 5159 | nacho | package org.gvsig.rasterTools.brightnessContrast.ui; |
21 | |||
22 | import java.awt.GridBagConstraints; |
||
23 | import java.awt.GridBagLayout; |
||
24 | 5358 | maquerol | import java.awt.event.ActionEvent; |
25 | import java.awt.event.ActionListener; |
||
26 | import java.awt.event.FocusEvent; |
||
27 | import java.awt.event.FocusListener; |
||
28 | 5159 | nacho | |
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 | 5358 | maquerol | /**
|
38 | 5497 | maquerol | * 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 | 5358 | maquerol | * @author Miguel ?ngel Querol Carratal? <querol_mig@gva.es>
|
42 | *
|
||
43 | */
|
||
44 | public class SliderTextPanel extends JPanel implements ChangeListener, FocusListener, ActionListener{ |
||
45 | |||
46 | 5159 | nacho | 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 | 5497 | maquerol | private boolean decimal = false; |
52 | 5159 | nacho | /**
|
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 | 5358 | maquerol | this.getJTextField().addFocusListener(this); |
79 | this.getJTextField().addActionListener(this); |
||
80 | 5159 | nacho | } |
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 | 5250 | nacho | public JSlider getJSlider() { |
141 | 5159 | nacho | 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 | 5358 | maquerol | public JTextField getJTextField() { |
154 | 5159 | nacho | if (jTextField == null) { |
155 | jTextField = new JTextField(); |
||
156 | 5358 | maquerol | jTextField.setPreferredSize(new java.awt.Dimension(20,19)); |
157 | 5159 | nacho | jTextField.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT); |
158 | 5497 | maquerol | jTextField.setText(String.valueOf(this.getJSlider().getValue())); |
159 | 5159 | nacho | } |
160 | return jTextField;
|
||
161 | } |
||
162 | |||
163 | /**
|
||
164 | * Pone una cadena con el tipo de unidad del cuadro de texto entre el slider
|
||
165 | 5247 | nacho | * y el TextField.
|
166 | 5159 | nacho | * @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 | 5247 | nacho | public void setTextValue(String value){ |
206 | this.getJTextField().setText(value);
|
||
207 | 5159 | nacho | } |
208 | |||
209 | 5247 | nacho | /**
|
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 | 5159 | nacho | public void stateChanged(ChangeEvent e) { |
218 | if(e.getSource() == this.jSlider){ |
||
219 | 5497 | maquerol | 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 | 5358 | maquerol | } |
224 | 5159 | nacho | } |
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 | 5358 | maquerol | public void focusGained(FocusEvent e) { |
237 | 5159 | nacho | // TODO Auto-generated method stub
|
238 | |||
239 | } |
||
240 | |||
241 | 5358 | maquerol | public void focusLost(FocusEvent e) { |
242 | if(e.getSource() == this.jTextField){ |
||
243 | String text = this.getJTextField().getText(); |
||
244 | double value = Double.valueOf(text).doubleValue(); |
||
245 | this.getJSlider().setValue((int) Math.round(value)); |
||
246 | this.getJTextField().setText(text);
|
||
247 | } |
||
248 | 5159 | nacho | |
249 | } |
||
250 | |||
251 | 5358 | maquerol | public void actionPerformed(ActionEvent e) { |
252 | 5159 | nacho | if(e.getSource() == this.jTextField){ |
253 | 5358 | maquerol | String text = this.getJTextField().getText(); |
254 | double value = Double.valueOf(text).doubleValue(); |
||
255 | this.getJSlider().setValue((int) Math.round(value)); |
||
256 | this.getJTextField().setText(text);
|
||
257 | 5497 | maquerol | } |
258 | 5159 | nacho | } |
259 | 5358 | maquerol | |
260 | 5497 | maquerol | public void setDecimal (boolean dec){ |
261 | decimal = dec; |
||
262 | } |
||
263 | 5159 | nacho | |
264 | } |