Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / textincreaser / TextIncreaserContainer.java @ 11004

History | View | Annotate | Download (7.75 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
import java.awt.event.KeyEvent;
27
import java.awt.event.KeyListener;
28
import java.util.ArrayList;
29
import java.util.Iterator;
30

    
31
import javax.swing.ImageIcon;
32
import javax.swing.JButton;
33
import javax.swing.JPanel;
34
import javax.swing.JTextField;
35

    
36
import org.gvsig.gui.beans.BaseComponent;
37

    
38
/**
39
 * 
40
 * Nacho Brodin (brodin_ign@gva.es)
41
 */
42

    
43
public class TextIncreaserContainer extends BaseComponent implements ActionListener, KeyListener{
44
        private static final long serialVersionUID = 7570162018139822874L;
45
        private int                                 width = 70;
46
        private JTextField                         tText = null;
47
        private JPanel                                pButtons = null;
48
        private JButton                         bmas = null;
49
        private JButton                         bmenos = null;
50
        private JPanel                                pGeneral = null;
51
        private        double                                 minValue = 0;
52
        private        double                                 maxValue = 100;
53
        private double                                value = 0.0; 
54
        private String                                pathToImages = "images/";//"/com/iver/cit/gvsig/gui/panels/images/";
55
        /**
56
         * Variable que est? a true si los controles est?n a la derecha
57
         */
58
        private boolean isRight = true;
59
        
60
        private ArrayList actionCommandListeners = new ArrayList();
61
        private boolean bDoCallListeners = true;
62
        static private int eventId = Integer.MIN_VALUE;
63
        
64
        /**
65
         * Creaci?n de un componente TextIncrearserContainer
66
         * @param width Ancho del componente
67
         * @param minValue M?nimo valor que puede tomar el texto del control
68
         * @param maxValue M?ximo valor que puede tomar el texto del control
69
         * @param right        a true si queremos que los controles se muestren a la derecha del TextBox
70
         * y false si los queremos a la izquierda
71
         */
72
        public TextIncreaserContainer(int width, double minValue, double maxValue, double init, boolean right) {
73
                super();
74
                this.width = width;
75
                this.minValue = minValue;
76
                this.maxValue = maxValue;
77
                this.isRight = right;
78
                this.value = init;
79
                initialize();
80
        }
81

    
82
        /**
83
         * This method initializes this
84
         * 
85
         */
86
        private void initialize() {
87
        FlowLayout flowLayout = new FlowLayout();
88
        flowLayout.setHgap(0);
89
        flowLayout.setVgap(0);
90
        FlowLayout flowLayout5 = new FlowLayout();
91
        flowLayout5.setHgap(0);
92
        flowLayout5.setVgap(0);
93
        this.setLayout(flowLayout5);
94
        //this.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.gray,1));
95
        this.setSize(new java.awt.Dimension(width, width));
96
        this.setPreferredSize(new java.awt.Dimension(width, width));
97
        this.add(getPGeneral(), null);
98
                        
99
        }
100

    
101
        /**
102
         * Asigna un valor al textBox
103
         * @param value
104
         */
105
        public void setValue(double value){
106
                this.value = value; 
107
                getTText().setText(Double.toString(value));
108
        }
109
        
110
        /**
111
         * Obtiene el valor del textBox
112
         * @return value
113
         */
114
        public double getValue(){
115
                return value;
116
        }
117
        
118
        /**
119
         * This method initializes jTextField        
120
         *         
121
         * @return javax.swing.JTextField        
122
         */
123
        private JTextField getTText() {
124
                if (tText == null) {
125
                        tText = new JTextField();
126
                        tText.setText(Double.toString(value));
127
                        tText.setPreferredSize(new java.awt.Dimension(width - 25,25));
128
                        tText.addKeyListener(this);
129
                }
130
                return tText;
131
        }
132

    
133
        /**
134
         * This method initializes jPanel        
135
         *         
136
         * @return javax.swing.JPanel        
137
         */
138
        private JPanel getPButtons() {
139
                if (pButtons == null) {
140
                        GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
141
                        gridBagConstraints3.insets = new java.awt.Insets(0,0,0,0);
142
                        gridBagConstraints3.gridy = 1;
143
                        gridBagConstraints3.gridx = 0;
144
                        GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
145
                        gridBagConstraints2.insets = new java.awt.Insets(0,0,0,0);
146
                        gridBagConstraints2.gridy = 0;
147
                        gridBagConstraints2.gridx = 0;
148
                        pButtons = new JPanel();
149
                        pButtons.setLayout(new GridBagLayout());
150
                        pButtons.setPreferredSize(new java.awt.Dimension(25,25));
151
                        pButtons.add(getBmas(), gridBagConstraints2);
152
                        pButtons.add(getBmenos(), gridBagConstraints3);
153
                }
154
                return pButtons;
155
        }
156

    
157
        /**
158
         * This method initializes jButton        
159
         *         
160
         * @return javax.swing.JButton        
161
         */
162
        private JButton getBmas() {
163
                if (bmas == null) {
164
                        bmas = new JButton();
165
                        bmas.setPreferredSize(new java.awt.Dimension(25,12));
166
                        bmas.addActionListener(this);
167
                        bmas.setIcon(new ImageIcon(getClass().getResource(pathToImages + "mas.png")));
168
                }
169
                return bmas;
170
        }
171

    
172
        /**
173
         * This method initializes jButton1        
174
         *         
175
         * @return javax.swing.JButton        
176
         */
177
        private JButton getBmenos() {
178
                if (bmenos == null) {
179
                        bmenos = new JButton();
180
                        bmenos.setPreferredSize(new java.awt.Dimension(25,12));
181
                        bmenos.addActionListener(this);
182
                        bmenos.setIcon(new ImageIcon(getClass().getResource(pathToImages + "menos.png")));
183
                }
184
                return bmenos;
185
        }
186

    
187
        /**
188
         * This method initializes jPanel        
189
         *         
190
         * @return javax.swing.JPanel        
191
         */
192
        private JPanel getPGeneral() {
193
                if (pGeneral == null) {
194
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
195

    
196
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
197
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
198
                        if(isRight){
199
                                gridBagConstraints1.gridx = 1;
200
                                gridBagConstraints.gridx = 0;
201
                        }else{
202
                                gridBagConstraints1.gridx = 0;
203
                                gridBagConstraints.gridx = 1;
204
                        }
205
                        gridBagConstraints.gridy = 0;
206
                        gridBagConstraints1.gridy = 0;
207
                        gridBagConstraints.weightx = 1.0;
208
                        pGeneral = new JPanel();
209
                        pGeneral.setLayout(new GridBagLayout());
210
                        pGeneral.setPreferredSize(new java.awt.Dimension(width, 25));
211
                        pGeneral.add(getTText(), gridBagConstraints);
212
                        pGeneral.add(getPButtons(), gridBagConstraints1);
213
                }
214
                return pGeneral;
215
        }
216

    
217
        public void actionPerformed(ActionEvent e) {
218
                if (e.getSource() == bmas) {
219
                        value ++;
220
                        checkValues();
221
                }
222
                
223
                if (e.getSource() == bmenos) {
224
                        value --;
225
                        checkValues();
226
                }
227
                callValueChangedListeners();
228
        }
229
        
230
        public void addValueChangedListener(TextIncreaserListener listener) {
231
                if (!actionCommandListeners.contains(listener))
232
                        actionCommandListeners.add(listener);
233
        }
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
        
278
}