Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / libraries / libUIComponent / src / org / gvsig / gui / beans / datainput / DataInputField.java @ 39319

History | View | Annotate | Download (5.66 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
 * &nbsp;&nbsp;<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
        private int                 maximumFractionDigits = 3;
57

    
58
        /**
59
         * This is the default constructor
60
         */
61
        public DataInputField(int maximumFractionDigits) {
62
                this.maximumFractionDigits = maximumFractionDigits;
63
                setUpFormats();
64
                initialize();
65
        }
66

    
67
        /**
68
         * This is the default constructor
69
         */
70
        public DataInputField() {
71
                setUpFormats();
72
                initialize();
73
        }
74

    
75
  /**
76
         * Create and set up number formats. These objects also parse numbers input by
77
         * user.
78
         */
79
  private void setUpFormats() {
80
      doubleDisplayFormat = NumberFormat.getNumberInstance();
81
      doubleDisplayFormat.setMinimumFractionDigits(0);
82
      doubleDisplayFormat.setMaximumFractionDigits(maximumFractionDigits);
83
      doubleEditFormat = NumberFormat.getNumberInstance();
84
  }
85

    
86
        /**
87
         * This method initializes this
88
         * @return void
89
         */
90
        private void initialize() {
91
                setLayout(new BorderLayout());
92

    
93
                add(getJFormattedTextField(), BorderLayout.CENTER);
94
        }
95
        
96
        /**
97
         * This method initializes jTextField
98
         *
99
         * @return javax.swing.JTextField
100
         */
101
        private JFormattedTextField getJFormattedTextField() {
102
                if (textField == null) {
103
                        textField = new JFormattedTextField(new DefaultFormatterFactory(
104
          new NumberFormatter(doubleDisplayFormat),
105
          new NumberFormatter(doubleDisplayFormat),
106
          new NumberFormatter(doubleEditFormat)));
107
                        textField.setBackground(Color.white);
108
                        textField.setValue(new Double(0));
109
                        textField.setColumns(10);
110
                        textField.addPropertyChangeListener("value", this);
111
                }
112
                return textField;
113
        }
114

    
115
        /**
116
         * Devuelve el valor del campo de texto.
117
         * @return
118
         */
119
        public String getValue(){
120
                double value = ((Number) getJFormattedTextField().getValue()).doubleValue();
121

    
122
                return Double.toString(value);
123
        }
124

    
125
        /**
126
         * Habilita o deshabilita el control
127
         * @param en
128
         */
129
        public void setControlEnabled(boolean en){
130
                getJFormattedTextField().setEnabled(en);
131
                if (en)
132
                        getJFormattedTextField().setBackground(Color.white);
133
                else
134
                        getJFormattedTextField().setBackground(getBackground());
135
        }
136

    
137
        /**
138
         * Asigna el valor al campo de texto.
139
         * @return
140
         */
141
        public void setValue(String value) {
142
                eventsEnabled = false;
143
                getJFormattedTextField().setValue(new Double(value));
144
                eventsEnabled = true;
145
        }
146

    
147
        /**
148
         * A?adir un listener a la lista de eventos
149
         * @param listener
150
         */
151
        public void addValueChangedListener(DataInputContainerListener listener) {
152
                if (!actionChangedListeners.contains(listener))
153
                        actionChangedListeners.add(listener);
154
        }
155

    
156
        /**
157
         * A?adir un listener a la lista de eventos
158
         * @param listener
159
         */
160
        public void addKeyListener(KeyListener listener) {
161
                textField.addKeyListener(listener);
162
        }
163
        
164
        /**
165
         * Borrar un listener de la lista de eventos
166
         * @param listener
167
         */
168
        public void removeValueChangedListener(DataInputContainerListener listener) {
169
                actionChangedListeners.remove(listener);
170
        }
171
        
172
        /**
173
         * Invocar a los eventos asociados al componente
174
         */
175
        private void callValueChangedListeners() {
176
                if (eventsEnabled == false)
177
                        return;
178
                Iterator acIterator = actionChangedListeners.iterator();
179
                while (acIterator.hasNext()) {
180
                        DataInputContainerListener listener = (DataInputContainerListener) acIterator.next();
181
                        listener.actionValueChanged(new EventObject(this));
182
                }
183
        }
184

    
185
        public void propertyChange(PropertyChangeEvent evt) {
186
          callValueChangedListeners();
187
  }
188
}