svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / datainput / DataInputField.java @ 15679
History | View | Annotate | Download (5.32 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 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.datainput; |
20 |
|
21 |
import java.awt.BorderLayout; |
22 |
import java.awt.Color; |
23 |
import java.awt.Container; |
24 |
import java.awt.event.KeyListener; |
25 |
import java.beans.PropertyChangeEvent; |
26 |
import java.beans.PropertyChangeListener; |
27 |
import java.text.NumberFormat; |
28 |
import java.util.ArrayList; |
29 |
import java.util.EventObject; |
30 |
import java.util.Iterator; |
31 |
|
32 |
import javax.swing.JFormattedTextField; |
33 |
import javax.swing.text.DefaultFormatterFactory; |
34 |
import javax.swing.text.NumberFormatter; |
35 |
/**
|
36 |
* Campo de texto que controla el contenido de datos del componente y solo
|
37 |
* dispara un evento cuando realmente ha cambiado su valor.<p>
|
38 |
*
|
39 |
* <b>RECOMENDABLE:</b> Usar JFormattedTextFields<p>
|
40 |
*
|
41 |
* Ejemplo de Sun:<br>
|
42 |
* <a href="http://java.sun.com/docs/books/tutorial/uiswing/examples/components/FormatterFactoryDemoProject/src/components/FormatterFactoryDemo.java">FormatterFactoryDemo.java</a>
|
43 |
*
|
44 |
* @version 06/09/2007
|
45 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
46 |
*/
|
47 |
public class DataInputField extends Container implements PropertyChangeListener { |
48 |
private static final long serialVersionUID = 8633824284253287604L; |
49 |
|
50 |
private JFormattedTextField textField = null; |
51 |
private ArrayList actionChangedListeners = new ArrayList(); |
52 |
|
53 |
private NumberFormat doubleDisplayFormat; |
54 |
private NumberFormat doubleEditFormat; |
55 |
private boolean eventsEnabled = true; |
56 |
|
57 |
|
58 |
/**
|
59 |
* This is the default constructor
|
60 |
*/
|
61 |
public DataInputField() {
|
62 |
setUpFormats(); |
63 |
initialize(); |
64 |
} |
65 |
|
66 |
/**
|
67 |
* Create and set up number formats. These objects also parse numbers input by
|
68 |
* user.
|
69 |
*/
|
70 |
private void setUpFormats() { |
71 |
doubleDisplayFormat = NumberFormat.getNumberInstance();
|
72 |
doubleDisplayFormat.setMinimumFractionDigits(0);
|
73 |
doubleEditFormat = NumberFormat.getNumberInstance();
|
74 |
} |
75 |
|
76 |
/**
|
77 |
* This method initializes this
|
78 |
* @return void
|
79 |
*/
|
80 |
private void initialize() { |
81 |
setLayout(new BorderLayout()); |
82 |
|
83 |
add(getJFormattedTextField(), BorderLayout.CENTER);
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* This method initializes jTextField
|
88 |
*
|
89 |
* @return javax.swing.JTextField
|
90 |
*/
|
91 |
private JFormattedTextField getJFormattedTextField() { |
92 |
if (textField == null) { |
93 |
textField = new JFormattedTextField(new DefaultFormatterFactory( |
94 |
new NumberFormatter(doubleDisplayFormat), |
95 |
new NumberFormatter(doubleDisplayFormat), |
96 |
new NumberFormatter(doubleEditFormat))); |
97 |
textField.setBackground(Color.white);
|
98 |
textField.setValue(new Double(0)); |
99 |
textField.setColumns(10);
|
100 |
textField.addPropertyChangeListener("value", this); |
101 |
} |
102 |
return textField;
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* Devuelve el valor del campo de texto.
|
107 |
* @return
|
108 |
*/
|
109 |
public String getValue(){ |
110 |
double value = ((Number) getJFormattedTextField().getValue()).doubleValue(); |
111 |
|
112 |
return Double.toString(value); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Habilita o deshabilita el control
|
117 |
* @param en
|
118 |
*/
|
119 |
public void setControlEnabled(boolean en){ |
120 |
getJFormattedTextField().setEnabled(en); |
121 |
if (en)
|
122 |
getJFormattedTextField().setBackground(Color.white);
|
123 |
else
|
124 |
getJFormattedTextField().setBackground(getBackground()); |
125 |
} |
126 |
|
127 |
/**
|
128 |
* Asigna el valor al campo de texto.
|
129 |
* @return
|
130 |
*/
|
131 |
public void setValue(String value) { |
132 |
eventsEnabled = false;
|
133 |
getJFormattedTextField().setValue(new Double(value)); |
134 |
eventsEnabled = true;
|
135 |
} |
136 |
|
137 |
/**
|
138 |
* A?adir un listener a la lista de eventos
|
139 |
* @param listener
|
140 |
*/
|
141 |
public void addValueChangedListener(DataInputContainerListener listener) { |
142 |
if (!actionChangedListeners.contains(listener))
|
143 |
actionChangedListeners.add(listener); |
144 |
} |
145 |
|
146 |
/**
|
147 |
* A?adir un listener a la lista de eventos
|
148 |
* @param listener
|
149 |
*/
|
150 |
public void addKeyListener(KeyListener listener) { |
151 |
textField.addKeyListener(listener); |
152 |
} |
153 |
|
154 |
/**
|
155 |
* Borrar un listener de la lista de eventos
|
156 |
* @param listener
|
157 |
*/
|
158 |
public void removeValueChangedListener(DataInputContainerListener listener) { |
159 |
actionChangedListeners.remove(listener); |
160 |
} |
161 |
|
162 |
/**
|
163 |
* Invocar a los eventos asociados al componente
|
164 |
*/
|
165 |
private void callValueChangedListeners() { |
166 |
if (eventsEnabled == false) |
167 |
return;
|
168 |
Iterator acIterator = actionChangedListeners.iterator();
|
169 |
while (acIterator.hasNext()) {
|
170 |
DataInputContainerListener listener = (DataInputContainerListener) acIterator.next(); |
171 |
listener.actionValueChanged(new EventObject(this)); |
172 |
} |
173 |
} |
174 |
|
175 |
public void propertyChange(PropertyChangeEvent evt) { |
176 |
callValueChangedListeners(); |
177 |
} |
178 |
} |