Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / datainput / DataInputField.java @ 40561

History | View | Annotate | Download (5.76 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.gui.beans.datainput;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Container;
29
import java.awt.event.KeyListener;
30
import java.beans.PropertyChangeEvent;
31
import java.beans.PropertyChangeListener;
32
import java.text.NumberFormat;
33
import java.util.ArrayList;
34
import java.util.EventObject;
35
import java.util.Iterator;
36

    
37
import javax.swing.JFormattedTextField;
38
import javax.swing.text.DefaultFormatterFactory;
39
import javax.swing.text.NumberFormatter;
40
/**
41
 * Campo de texto que controla el contenido de datos del componente y solo
42
 * dispara un evento cuando realmente ha cambiado su valor.<p>
43
 * 
44
 * <b>RECOMENDABLE:</b> Usar JFormattedTextFields<p>
45
 * 
46
 * Ejemplo de Sun:<br>
47
 * &nbsp;&nbsp;<a href="http://java.sun.com/docs/books/tutorial/uiswing/examples/components/FormatterFactoryDemoProject/src/components/FormatterFactoryDemo.java">FormatterFactoryDemo.java</a>
48
 * 
49
 * @version 06/09/2007
50
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
51
 */
52
public class DataInputField extends Container implements PropertyChangeListener {
53
  private static final long serialVersionUID = 8633824284253287604L;
54

    
55
        private JFormattedTextField textField              = null;
56
        private ArrayList           actionChangedListeners = new ArrayList();
57

    
58
        private NumberFormat        doubleDisplayFormat;
59
        private NumberFormat        doubleEditFormat;
60
        private boolean             eventsEnabled = true;
61
        private int                 maximumFractionDigits = 3;
62

    
63
        /**
64
         * This is the default constructor
65
         */
66
        public DataInputField(int maximumFractionDigits) {
67
                this.maximumFractionDigits = maximumFractionDigits;
68
                setUpFormats();
69
                initialize();
70
        }
71

    
72
        /**
73
         * This is the default constructor
74
         */
75
        public DataInputField() {
76
                setUpFormats();
77
                initialize();
78
        }
79

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

    
91
        /**
92
         * This method initializes this
93
         * @return void
94
         */
95
        private void initialize() {
96
                setLayout(new BorderLayout());
97

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

    
120
        /**
121
         * Devuelve el valor del campo de texto.
122
         * @return
123
         */
124
        public String getValue(){
125
                double value = ((Number) getJFormattedTextField().getValue()).doubleValue();
126

    
127
                return Double.toString(value);
128
        }
129

    
130
        /**
131
         * Habilita o deshabilita el control
132
         * @param en
133
         */
134
        public void setControlEnabled(boolean en){
135
                getJFormattedTextField().setEnabled(en);
136
                if (en)
137
                        getJFormattedTextField().setBackground(Color.white);
138
                else
139
                        getJFormattedTextField().setBackground(getBackground());
140
        }
141

    
142
        /**
143
         * Asigna el valor al campo de texto.
144
         * @return
145
         */
146
        public void setValue(String value) {
147
                eventsEnabled = false;
148
                getJFormattedTextField().setValue(new Double(value));
149
                eventsEnabled = true;
150
        }
151

    
152
        /**
153
         * A?adir un listener a la lista de eventos
154
         * @param listener
155
         */
156
        public void addValueChangedListener(DataInputContainerListener listener) {
157
                if (!actionChangedListeners.contains(listener))
158
                        actionChangedListeners.add(listener);
159
        }
160

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

    
190
        public void propertyChange(PropertyChangeEvent evt) {
191
          callValueChangedListeners();
192
  }
193
}