Revision 11004 trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/textincreaser/TextIncreaserContainer.java
TextIncreaserContainer.java | ||
---|---|---|
23 | 23 |
import java.awt.GridBagLayout; |
24 | 24 |
import java.awt.event.ActionEvent; |
25 | 25 |
import java.awt.event.ActionListener; |
26 |
import java.awt.event.KeyEvent; |
|
27 |
import java.awt.event.KeyListener; |
|
28 |
import java.util.ArrayList; |
|
29 |
import java.util.Iterator; |
|
26 | 30 |
|
27 | 31 |
import javax.swing.ImageIcon; |
28 | 32 |
import javax.swing.JButton; |
... | ... | |
36 | 40 |
* Nacho Brodin (brodin_ign@gva.es) |
37 | 41 |
*/ |
38 | 42 |
|
39 |
public class TextIncreaserContainer extends BaseComponent implements ActionListener{ |
|
43 |
public class TextIncreaserContainer extends BaseComponent implements ActionListener, KeyListener{
|
|
40 | 44 |
private static final long serialVersionUID = 7570162018139822874L; |
41 | 45 |
private int width = 70; |
42 | 46 |
private JTextField tText = null; |
... | ... | |
46 | 50 |
private JPanel pGeneral = null; |
47 | 51 |
private double minValue = 0; |
48 | 52 |
private double maxValue = 100; |
49 |
private double init = 0;
|
|
53 |
private double value = 0.0;
|
|
50 | 54 |
private String pathToImages = "images/";//"/com/iver/cit/gvsig/gui/panels/images/"; |
51 | 55 |
/** |
52 | 56 |
* Variable que est? a true si los controles est?n a la derecha |
53 | 57 |
*/ |
54 | 58 |
private boolean isRight = true; |
55 | 59 |
|
60 |
private ArrayList actionCommandListeners = new ArrayList(); |
|
61 |
private boolean bDoCallListeners = true; |
|
62 |
static private int eventId = Integer.MIN_VALUE; |
|
63 |
|
|
56 | 64 |
/** |
57 | 65 |
* Creaci?n de un componente TextIncrearserContainer |
58 | 66 |
* @param width Ancho del componente |
... | ... | |
67 | 75 |
this.minValue = minValue; |
68 | 76 |
this.maxValue = maxValue; |
69 | 77 |
this.isRight = right; |
70 |
this.init = init;
|
|
78 |
this.value = init;
|
|
71 | 79 |
initialize(); |
72 | 80 |
} |
73 | 81 |
|
... | ... | |
95 | 103 |
* @param value |
96 | 104 |
*/ |
97 | 105 |
public void setValue(double value){ |
106 |
this.value = value; |
|
98 | 107 |
getTText().setText(Double.toString(value)); |
99 | 108 |
} |
100 | 109 |
|
... | ... | |
103 | 112 |
* @return value |
104 | 113 |
*/ |
105 | 114 |
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(); |
|
115 |
return value; |
|
113 | 116 |
} |
114 | 117 |
|
115 | 118 |
/** |
... | ... | |
117 | 120 |
* |
118 | 121 |
* @return javax.swing.JTextField |
119 | 122 |
*/ |
120 |
public JTextField getTText() {
|
|
123 |
private JTextField getTText() {
|
|
121 | 124 |
if (tText == null) { |
122 | 125 |
tText = new JTextField(); |
123 |
tText.setText(Double.toString(init));
|
|
126 |
tText.setText(Double.toString(value));
|
|
124 | 127 |
tText.setPreferredSize(new java.awt.Dimension(width - 25,25)); |
128 |
tText.addKeyListener(this); |
|
125 | 129 |
} |
126 | 130 |
return tText; |
127 | 131 |
} |
... | ... | |
155 | 159 |
* |
156 | 160 |
* @return javax.swing.JButton |
157 | 161 |
*/ |
158 |
public JButton getBmas() {
|
|
162 |
private JButton getBmas() {
|
|
159 | 163 |
if (bmas == null) { |
160 | 164 |
bmas = new JButton(); |
161 | 165 |
bmas.setPreferredSize(new java.awt.Dimension(25,12)); |
... | ... | |
170 | 174 |
* |
171 | 175 |
* @return javax.swing.JButton |
172 | 176 |
*/ |
173 |
public JButton getBmenos() {
|
|
177 |
private JButton getBmenos() {
|
|
174 | 178 |
if (bmenos == null) { |
175 | 179 |
bmenos = new JButton(); |
176 | 180 |
bmenos.setPreferredSize(new java.awt.Dimension(25,12)); |
... | ... | |
211 | 215 |
} |
212 | 216 |
|
213 | 217 |
public void actionPerformed(ActionEvent e) { |
214 |
double value = Double.parseDouble(getTText().getText()); |
|
215 |
if(e.getSource() == bmas){ |
|
218 |
if (e.getSource() == bmas) { |
|
216 | 219 |
value ++; |
217 |
if(value <= maxValue) |
|
218 |
tText.setText(Double.toString(value)); |
|
220 |
checkValues(); |
|
219 | 221 |
} |
220 | 222 |
|
221 |
if(e.getSource() == bmenos){
|
|
223 |
if (e.getSource() == bmenos) {
|
|
222 | 224 |
value --; |
223 |
if(value >= minValue) |
|
224 |
tText.setText(Double.toString(value)); |
|
225 |
checkValues(); |
|
225 | 226 |
} |
227 |
callValueChangedListeners(); |
|
226 | 228 |
} |
229 |
|
|
230 |
public void addValueChangedListener(TextIncreaserListener listener) { |
|
231 |
if (!actionCommandListeners.contains(listener)) |
|
232 |
actionCommandListeners.add(listener); |
|
233 |
} |
|
227 | 234 |
|
235 |
public void removeValueChangedListener(TextIncreaserListener listener) { |
|
236 |
actionCommandListeners.remove(listener); |
|
237 |
} |
|
238 |
|
|
239 |
private void callValueChangedListeners() { |
|
240 |
if (!bDoCallListeners) |
|
241 |
return; |
|
242 |
Iterator acIterator = actionCommandListeners.iterator(); |
|
243 |
while (acIterator.hasNext()) { |
|
244 |
TextIncreaserListener listener = (TextIncreaserListener) acIterator.next(); |
|
245 |
listener.actionValueChanged(new TextIncreaserEvent(this)); |
|
246 |
} |
|
247 |
eventId++; |
|
248 |
} |
|
249 |
|
|
250 |
private void checkValues() { |
|
251 |
if (value >= maxValue) value = maxValue; |
|
252 |
if (value <= minValue) value = minValue; |
|
253 |
tText.setText(Double.toString(value)); |
|
254 |
} |
|
255 |
|
|
256 |
public void keyPressed(KeyEvent e) { |
|
257 |
if (e.getKeyCode() == 10) { |
|
258 |
try { |
|
259 |
value = Double.valueOf(getTText().getText()).doubleValue(); |
|
260 |
} catch(NumberFormatException exc) { |
|
261 |
} |
|
262 |
|
|
263 |
checkValues(); |
|
264 |
callValueChangedListeners(); |
|
265 |
} |
|
266 |
} |
|
267 |
|
|
268 |
public void keyReleased(KeyEvent e) { |
|
269 |
// TODO Auto-generated method stub |
|
270 |
|
|
271 |
} |
|
272 |
|
|
273 |
public void keyTyped(KeyEvent e) { |
|
274 |
// TODO Auto-generated method stub |
|
275 |
|
|
276 |
} |
|
277 |
|
|
228 | 278 |
} |
Also available in: Unified diff