root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / textincreaser / TextIncreaserContainer.java @ 10741
History | View | Annotate | Download (6.49 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 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.textincreaser; |
20 |
|
21 |
import java.awt.FlowLayout; |
22 |
import java.awt.GridBagConstraints; |
23 |
import java.awt.GridBagLayout; |
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
|
27 |
import javax.swing.ImageIcon; |
28 |
import javax.swing.JButton; |
29 |
import javax.swing.JPanel; |
30 |
import javax.swing.JTextField; |
31 |
|
32 |
import org.gvsig.gui.beans.BaseComponent; |
33 |
|
34 |
/**
|
35 |
*
|
36 |
* Nacho Brodin (brodin_ign@gva.es)
|
37 |
*/
|
38 |
|
39 |
public class TextIncreaserContainer extends BaseComponent implements ActionListener{ |
40 |
|
41 |
private int width = 70, height = 25; |
42 |
private JTextField tText = null; |
43 |
private JPanel pButtons = null; |
44 |
private JButton bmas = null; |
45 |
private JButton bmenos = null; |
46 |
private JPanel pGeneral = null; |
47 |
private double minValue = 0; |
48 |
private double maxValue = 100; |
49 |
private double init = 0; |
50 |
private String pathToImages = "images/";//"/com/iver/cit/gvsig/gui/panels/images/"; |
51 |
/**
|
52 |
* Variable que est? a true si los controles est?n a la derecha
|
53 |
*/
|
54 |
private boolean isRight = true; |
55 |
|
56 |
/**
|
57 |
* Creaci?n de un componente TextIncrearserContainer
|
58 |
* @param width Ancho del componente
|
59 |
* @param minValue M?nimo valor que puede tomar el texto del control
|
60 |
* @param maxValue M?ximo valor que puede tomar el texto del control
|
61 |
* @param right a true si queremos que los controles se muestren a la derecha del TextBox
|
62 |
* y false si los queremos a la izquierda
|
63 |
*/
|
64 |
public TextIncreaserContainer(int width, double minValue, double maxValue, double init, boolean right) { |
65 |
super();
|
66 |
this.width = width;
|
67 |
this.minValue = minValue;
|
68 |
this.maxValue = maxValue;
|
69 |
this.isRight = right;
|
70 |
this.init = init;
|
71 |
initialize(); |
72 |
} |
73 |
|
74 |
/**
|
75 |
* This method initializes this
|
76 |
*
|
77 |
*/
|
78 |
private void initialize() { |
79 |
FlowLayout flowLayout = new FlowLayout(); |
80 |
flowLayout.setHgap(0);
|
81 |
flowLayout.setVgap(0);
|
82 |
FlowLayout flowLayout5 = new FlowLayout(); |
83 |
flowLayout5.setHgap(0);
|
84 |
flowLayout5.setVgap(0);
|
85 |
this.setLayout(flowLayout5);
|
86 |
//this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
|
87 |
this.setSize(new java.awt.Dimension(width, width)); |
88 |
this.setPreferredSize(new java.awt.Dimension(width, width)); |
89 |
this.add(getPGeneral(), null); |
90 |
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* Asigna un valor al textBox
|
95 |
* @param value
|
96 |
*/
|
97 |
public void setValue(double value){ |
98 |
getTText().setText(Double.toString(value));
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Obtiene el valor del textBox
|
103 |
* @return value
|
104 |
*/
|
105 |
public double getValue(){ |
106 |
try{
|
107 |
Double.valueOf(getTText().getText()).doubleValue();
|
108 |
}catch(NumberFormatException exc){ |
109 |
getTText().setText("0.0");
|
110 |
return 0; |
111 |
} |
112 |
return Double.valueOf(getTText().getText()).doubleValue(); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* This method initializes jTextField
|
117 |
*
|
118 |
* @return javax.swing.JTextField
|
119 |
*/
|
120 |
public JTextField getTText() { |
121 |
if (tText == null) { |
122 |
tText = new JTextField(); |
123 |
tText.setText(Double.toString(init));
|
124 |
tText.setPreferredSize(new java.awt.Dimension(width - 25,25)); |
125 |
} |
126 |
return tText;
|
127 |
} |
128 |
|
129 |
/**
|
130 |
* This method initializes jPanel
|
131 |
*
|
132 |
* @return javax.swing.JPanel
|
133 |
*/
|
134 |
private JPanel getPButtons() { |
135 |
if (pButtons == null) { |
136 |
GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); |
137 |
gridBagConstraints3.insets = new java.awt.Insets(0,0,0,0); |
138 |
gridBagConstraints3.gridy = 1;
|
139 |
gridBagConstraints3.gridx = 0;
|
140 |
GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); |
141 |
gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0); |
142 |
gridBagConstraints2.gridy = 0;
|
143 |
gridBagConstraints2.gridx = 0;
|
144 |
pButtons = new JPanel(); |
145 |
pButtons.setLayout(new GridBagLayout()); |
146 |
pButtons.setPreferredSize(new java.awt.Dimension(25,25)); |
147 |
pButtons.add(getBmas(), gridBagConstraints2); |
148 |
pButtons.add(getBmenos(), gridBagConstraints3); |
149 |
} |
150 |
return pButtons;
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* This method initializes jButton
|
155 |
*
|
156 |
* @return javax.swing.JButton
|
157 |
*/
|
158 |
public JButton getBmas() { |
159 |
if (bmas == null) { |
160 |
bmas = new JButton(); |
161 |
bmas.setPreferredSize(new java.awt.Dimension(25,12)); |
162 |
bmas.addActionListener(this);
|
163 |
bmas.setIcon(new ImageIcon(getClass().getResource(pathToImages + "mas.png"))); |
164 |
} |
165 |
return bmas;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* This method initializes jButton1
|
170 |
*
|
171 |
* @return javax.swing.JButton
|
172 |
*/
|
173 |
public JButton getBmenos() { |
174 |
if (bmenos == null) { |
175 |
bmenos = new JButton(); |
176 |
bmenos.setPreferredSize(new java.awt.Dimension(25,12)); |
177 |
bmenos.addActionListener(this);
|
178 |
bmenos.setIcon(new ImageIcon(getClass().getResource(pathToImages + "menos.png"))); |
179 |
} |
180 |
return bmenos;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* This method initializes jPanel
|
185 |
*
|
186 |
* @return javax.swing.JPanel
|
187 |
*/
|
188 |
private JPanel getPGeneral() { |
189 |
if (pGeneral == null) { |
190 |
GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); |
191 |
|
192 |
GridBagConstraints gridBagConstraints = new GridBagConstraints(); |
193 |
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; |
194 |
if(isRight){
|
195 |
gridBagConstraints1.gridx = 1;
|
196 |
gridBagConstraints.gridx = 0;
|
197 |
}else{
|
198 |
gridBagConstraints1.gridx = 0;
|
199 |
gridBagConstraints.gridx = 1;
|
200 |
} |
201 |
gridBagConstraints.gridy = 0;
|
202 |
gridBagConstraints1.gridy = 0;
|
203 |
gridBagConstraints.weightx = 1.0;
|
204 |
pGeneral = new JPanel(); |
205 |
pGeneral.setLayout(new GridBagLayout()); |
206 |
pGeneral.setPreferredSize(new java.awt.Dimension(width, 25)); |
207 |
pGeneral.add(getTText(), gridBagConstraints); |
208 |
pGeneral.add(getPButtons(), gridBagConstraints1); |
209 |
} |
210 |
return pGeneral;
|
211 |
} |
212 |
|
213 |
public void actionPerformed(ActionEvent e) { |
214 |
double value = Double.parseDouble(getTText().getText()); |
215 |
if(e.getSource() == bmas){
|
216 |
value ++; |
217 |
if(value <= maxValue)
|
218 |
tText.setText(Double.toString(value));
|
219 |
} |
220 |
|
221 |
if(e.getSource() == bmenos){
|
222 |
value --; |
223 |
if(value >= minValue)
|
224 |
tText.setText(Double.toString(value));
|
225 |
} |
226 |
} |
227 |
|
228 |
} |