svn-gvsig-desktop / tags / v1_1_Build_1013 / libraries / libCq_CMS_praster / src / org / cresques / ui / BrightnessContrast / SliderTextPanel.java @ 13521
History | View | Annotate | Download (9.44 KB)
1 | 8026 | nacho | /* 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 | 10645 | nacho | import java.awt.FlowLayout; |
36 | 8026 | nacho | |
37 | |||
38 | /**
|
||
39 | * Clase base para un componente con un slider y un campo de texto.
|
||
40 | * Contiene una etiqueta entre el slider y el campo de texto en la que se
|
||
41 | * pueden poner las unidades en las que se cuantifica el valor.
|
||
42 | * @author Miguel ?ngel Querol Carratal? <querol_mig@gva.es>
|
||
43 | *
|
||
44 | */
|
||
45 | public class SliderTextPanel extends JPanel implements ChangeListener, FocusListener, ActionListener{ |
||
46 | |||
47 | 10645 | nacho | |
48 | private int wComp = 180, hComp = 50; |
||
49 | private int wSlider = wComp - 85, hSlider = 16; |
||
50 | |||
51 | 8026 | nacho | private JPanel pName = null; |
52 | private JPanel pSliderText = null; |
||
53 | private JSlider jSlider = null; |
||
54 | private JTextField jTextField = null; |
||
55 | private JLabel lPercent = null; |
||
56 | private boolean decimal = false; |
||
57 | /**
|
||
58 | * This is the default constructor
|
||
59 | */
|
||
60 | public SliderTextPanel() {
|
||
61 | super();
|
||
62 | initialize(); |
||
63 | } |
||
64 | |||
65 | /**
|
||
66 | * This method initializes this
|
||
67 | *
|
||
68 | * @return void
|
||
69 | */
|
||
70 | private void initialize() { |
||
71 | GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
||
72 | gridBagConstraints1.gridx = 0;
|
||
73 | gridBagConstraints1.gridy = 1;
|
||
74 | GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
||
75 | gridBagConstraints.gridx = 0;
|
||
76 | gridBagConstraints.gridy = 0;
|
||
77 | this.setLayout(new GridBagLayout()); |
||
78 | 10645 | nacho | this.setSize(wComp, hComp);
|
79 | this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
||
80 | 8026 | nacho | this.add(getJPanel(), gridBagConstraints);
|
81 | this.add(getJPanel1(), gridBagConstraints1);
|
||
82 | this.getJSlider().addChangeListener(this); |
||
83 | this.getJTextField().addFocusListener(this); |
||
84 | this.getJTextField().addActionListener(this); |
||
85 | } |
||
86 | |||
87 | /**
|
||
88 | * This method initializes jPanel
|
||
89 | *
|
||
90 | * @return javax.swing.JPanel
|
||
91 | */
|
||
92 | protected JPanel getJPanel() { |
||
93 | if (pName == null) { |
||
94 | 10645 | nacho | FlowLayout flowLayout = new FlowLayout(); |
95 | flowLayout.setAlignment(java.awt.FlowLayout.LEFT); |
||
96 | 8026 | nacho | pName = new JPanel(); |
97 | 10645 | nacho | pName.setLayout(flowLayout); |
98 | pName.setPreferredSize(new java.awt.Dimension(wComp,22)); |
||
99 | 8026 | nacho | } |
100 | return pName;
|
||
101 | } |
||
102 | |||
103 | /**
|
||
104 | * This method initializes jPanel1
|
||
105 | *
|
||
106 | * @return javax.swing.JPanel
|
||
107 | */
|
||
108 | private JPanel getJPanel1() { |
||
109 | if (pSliderText == null) { |
||
110 | GridBagConstraints gridBagConstraints6 = new GridBagConstraints(); |
||
111 | gridBagConstraints6.gridx = 1;
|
||
112 | gridBagConstraints6.gridy = 0;
|
||
113 | lPercent = new JLabel(); |
||
114 | lPercent.setText("");
|
||
115 | GridBagConstraints gridBagConstraints5 = new GridBagConstraints(); |
||
116 | gridBagConstraints5.gridx = 1;
|
||
117 | gridBagConstraints5.gridy = 0;
|
||
118 | GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); |
||
119 | gridBagConstraints4.fill = java.awt.GridBagConstraints.HORIZONTAL; |
||
120 | gridBagConstraints4.gridy = 0;
|
||
121 | gridBagConstraints4.weightx = 1.0;
|
||
122 | gridBagConstraints4.insets = new java.awt.Insets(0,6,0,6); |
||
123 | gridBagConstraints4.gridx = 2;
|
||
124 | GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
||
125 | gridBagConstraints3.gridx = 1;
|
||
126 | gridBagConstraints3.gridy = 0;
|
||
127 | GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
||
128 | gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL; |
||
129 | gridBagConstraints2.gridy = 0;
|
||
130 | gridBagConstraints2.weightx = 1.0;
|
||
131 | gridBagConstraints2.gridx = 0;
|
||
132 | pSliderText = new JPanel(); |
||
133 | pSliderText.setLayout(new GridBagLayout()); |
||
134 | 10645 | nacho | pSliderText.setPreferredSize(new java.awt.Dimension(wComp,23)); |
135 | 8026 | nacho | pSliderText.add(getJSlider(), gridBagConstraints2); |
136 | pSliderText.add(getJTextField(), gridBagConstraints4); |
||
137 | pSliderText.add(lPercent, gridBagConstraints6); |
||
138 | } |
||
139 | return pSliderText;
|
||
140 | } |
||
141 | |||
142 | /**
|
||
143 | * This method initializes jSlider
|
||
144 | *
|
||
145 | * @return javax.swing.JSlider
|
||
146 | */
|
||
147 | public JSlider getJSlider() { |
||
148 | if (jSlider == null) { |
||
149 | jSlider = new JSlider(); |
||
150 | 10645 | nacho | jSlider.setPreferredSize(new java.awt.Dimension(wSlider, hSlider));
|
151 | 8026 | nacho | } |
152 | return jSlider;
|
||
153 | } |
||
154 | |||
155 | /**
|
||
156 | * This method initializes jTextField
|
||
157 | *
|
||
158 | * @return javax.swing.JTextField
|
||
159 | */
|
||
160 | public JTextField getJTextField() { |
||
161 | if (jTextField == null) { |
||
162 | jTextField = new JTextField(); |
||
163 | 10645 | nacho | jTextField.setPreferredSize(new java.awt.Dimension(20,17)); |
164 | 8026 | nacho | jTextField.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT); |
165 | jTextField.setText(String.valueOf(this.getJSlider().getValue())); |
||
166 | } |
||
167 | return jTextField;
|
||
168 | } |
||
169 | |||
170 | /**
|
||
171 | 10645 | nacho | * Ajusta los tama?os de todos los componentes a partir de un tama?o asignado
|
172 | * para la clase.
|
||
173 | * @param w
|
||
174 | * @param h
|
||
175 | */
|
||
176 | public void setComponentSize(int w, int h){ |
||
177 | wComp = w; hComp = 50;
|
||
178 | wSlider = wComp - 85; hSlider = 16; |
||
179 | |||
180 | this.setSize(wComp, hComp);
|
||
181 | this.setPreferredSize(new java.awt.Dimension(wComp, hComp)); |
||
182 | pName.setPreferredSize(new java.awt.Dimension(wComp,22)); |
||
183 | pSliderText.setPreferredSize(new java.awt.Dimension(wComp,23)); |
||
184 | jSlider.setPreferredSize(new java.awt.Dimension(wSlider, hSlider));
|
||
185 | } |
||
186 | |||
187 | |||
188 | /**
|
||
189 | 8026 | nacho | * Pone una cadena con el tipo de unidad del cuadro de texto entre el slider
|
190 | * y el TextField.
|
||
191 | * @param cadena a poner entre el slider y el cudro de texto.
|
||
192 | */
|
||
193 | |||
194 | protected void setUnitText(String unit){ |
||
195 | lPercent.setText(unit); |
||
196 | } |
||
197 | |||
198 | /**
|
||
199 | * Activa o desactiva el control del panel
|
||
200 | * @param active
|
||
201 | */
|
||
202 | |||
203 | public void setControlEnabled(boolean active){ |
||
204 | this.jSlider.setEnabled(active);
|
||
205 | this.jTextField.setEnabled(active);
|
||
206 | this.lPercent.setEnabled(active);
|
||
207 | if(active == false){ |
||
208 | this.jTextField.setForeground(this.getBackground()); |
||
209 | }else{
|
||
210 | this.jTextField.setForeground(java.awt.Color.black);
|
||
211 | } |
||
212 | |||
213 | } |
||
214 | |||
215 | /**
|
||
216 | * Ponel el slider al valor que le pasamos
|
||
217 | * @param value
|
||
218 | */
|
||
219 | |||
220 | public void setSliderValue(int value){ |
||
221 | jSlider.setValue(value); |
||
222 | } |
||
223 | |||
224 | |||
225 | /**
|
||
226 | * Pone en el campo de texto a la derecha del slider el valor que le pasamos
|
||
227 | * como parametro
|
||
228 | * @param value: valor que aparecera en el campo de texto.
|
||
229 | */
|
||
230 | public void setTextValue(String value){ |
||
231 | this.getJTextField().setText(value);
|
||
232 | } |
||
233 | |||
234 | /**
|
||
235 | * Devuelve el valor actual del campo de texto
|
||
236 | * @return: valor del campo de texto.
|
||
237 | */
|
||
238 | public String getTextValue(){ |
||
239 | return this.jTextField.getText(); |
||
240 | } |
||
241 | |||
242 | public void stateChanged(ChangeEvent e) { |
||
243 | if(e.getSource() == this.jSlider){ |
||
244 | if(decimal == true) |
||
245 | this.jTextField.setText(String.valueOf(this.jSlider.getValue()) + ".0"); |
||
246 | if(decimal == false) |
||
247 | this.jTextField.setText(String.valueOf(this.jSlider.getValue())); |
||
248 | } |
||
249 | } |
||
250 | |||
251 | /**
|
||
252 | * Da el rango de valores en el que se mover? el slider
|
||
253 | * @param min
|
||
254 | * @param max
|
||
255 | */
|
||
256 | public void setSliderRange(int min, int max){ |
||
257 | this.jSlider.setMaximum(max);
|
||
258 | this.jSlider.setMinimum(min);
|
||
259 | } |
||
260 | |||
261 | public void checkRangeText(){ |
||
262 | int valor = Math.round(Float.parseFloat(this.getJTextField().getText())); |
||
263 | if((valor > this.getJSlider().getMaximum())){ |
||
264 | if(decimal == true) |
||
265 | this.setTextValue(String.valueOf(this.getJSlider().getMaximum()) + ".0"); |
||
266 | if(decimal == false) |
||
267 | this.setTextValue(String.valueOf(this.getJSlider().getMaximum())); |
||
268 | this.setSliderValue(this.getJSlider().getMaximum()); |
||
269 | } |
||
270 | |||
271 | if((valor < this.getJSlider().getMinimum())){ |
||
272 | if(decimal == true) |
||
273 | this.setTextValue(String.valueOf(this.getJSlider().getMinimum()) + ".0"); |
||
274 | if(decimal == false) |
||
275 | this.setTextValue(String.valueOf(this.getJSlider().getMinimum())); |
||
276 | this.setSliderValue(this.getJSlider().getMinimum()); |
||
277 | } |
||
278 | |||
279 | } |
||
280 | |||
281 | public void focusGained(FocusEvent e) { |
||
282 | // TODO Auto-generated method stub
|
||
283 | |||
284 | } |
||
285 | |||
286 | public void focusLost(FocusEvent e) { |
||
287 | if(e.getSource() == this.jTextField){ |
||
288 | checkTailText(); |
||
289 | checkRangeText(); |
||
290 | } |
||
291 | } |
||
292 | |||
293 | public void actionPerformed(ActionEvent e) { |
||
294 | if(e.getSource() == this.jTextField){ |
||
295 | checkTailText(); |
||
296 | checkRangeText(); |
||
297 | } |
||
298 | } |
||
299 | |||
300 | /**
|
||
301 | * Controla que si el formato introducido en el textfield
|
||
302 | * de Recorte de colas es numerico se actualiza la posici?n del
|
||
303 | * slider
|
||
304 | */
|
||
305 | private void checkTailText() { |
||
306 | String text = getJTextField().getText();
|
||
307 | double value = 0; |
||
308 | |||
309 | try {
|
||
310 | if (!text.equals("")) |
||
311 | value = (Double.parseDouble(text) * 100) / 50; |
||
312 | value = Double.valueOf(text).doubleValue();
|
||
313 | getJSlider().setValue((int) Math.round(value)); |
||
314 | getJTextField().setText(text); |
||
315 | } catch (NumberFormatException exc) { |
||
316 | System.err.println("Formato no numerico"); |
||
317 | if(decimal == true) |
||
318 | getJTextField().setText("0.0");
|
||
319 | else
|
||
320 | getJTextField().setText("0");
|
||
321 | getJSlider().setValue(0);
|
||
322 | } |
||
323 | } |
||
324 | |||
325 | public void setDecimal (boolean dec){ |
||
326 | decimal = dec; |
||
327 | } |
||
328 | |||
329 | } |