Statistics
| Revision:

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

History | View | Annotate | Download (7.65 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.textincreaser;
25

    
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.awt.GridBagConstraints;
29
import java.awt.GridBagLayout;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.KeyEvent;
33
import java.awt.event.KeyListener;
34
import java.util.ArrayList;
35
import java.util.Iterator;
36

    
37
import javax.swing.ImageIcon;
38
import javax.swing.JButton;
39
import javax.swing.JPanel;
40
import javax.swing.JTextField;
41
/**
42
 * Nacho Brodin (brodin_ign@gva.es)
43
 */
44
public class TextIncreaserContainer extends JPanel implements ActionListener, KeyListener{
45
        private static final long serialVersionUID = 7570162018139822874L;
46
        private ArrayList actionCommandListeners = new ArrayList();
47
        private JTextField tText            = null;
48
        private JPanel     pButtons         = null;
49
        private JButton    bmas             = null;
50
        private JButton    bmenos           = null;
51
        private JPanel     pGeneral         = null;
52
        private double     minValue         = 0;
53
        private double     maxValue         = 100;
54
        private double     value            = 0.0;
55
        private String     pathToImages     = "images/"; // "/com/iver/cit/gvsig/gui/panels/images/";
56
        /**
57
         * Variable que est? a true si los controles est?n a la derecha
58
         */
59
        private boolean    isRight          = true;
60

    
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
                getTText().setPreferredSize(new Dimension(width - 25, (int) getTText().getPreferredSize().getHeight()));
75
                this.minValue = minValue;
76
                this.maxValue = maxValue;
77
                this.isRight = right;
78
                setValue(init);
79
                initialize();
80
        }
81

    
82
        /**
83
         * This method initializes this
84
         */
85
        private void initialize() {
86
                FlowLayout flowLayout = new FlowLayout();
87
                flowLayout.setHgap(0);
88
                flowLayout.setVgap(0);
89
                FlowLayout flowLayout5 = new FlowLayout();
90
                flowLayout5.setHgap(0);
91
                flowLayout5.setVgap(0);
92
                this.setLayout(flowLayout5);
93
                this.add(getPGeneral(), null);
94
        }
95

    
96
        /**
97
         * Asigna un valor al textBox
98
         * @param value
99
         */
100
        public void setValue(double value){
101
                this.value = value;
102
                getTText().setText(Double.toString(value));
103
        }
104

    
105
        /**
106
         * Obtiene el valor del textBox
107
         * @return value
108
         */
109
        public double getValue(){
110
                return value;
111
        }
112

    
113
        /**
114
         * This method initializes jTextField
115
         *
116
         * @return javax.swing.JTextField
117
         */
118
        private JTextField getTText() {
119
                if (tText == null) {
120
                        tText = new JTextField();
121
                        tText.setText(Double.toString(value));
122
                        tText.setPreferredSize(new java.awt.Dimension(45, (int) tText.getPreferredSize().getHeight()));
123
                        tText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
124
                        tText.addKeyListener(this);
125
                }
126
                return tText;
127
        }
128

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

    
152
        /**
153
         * This method initializes jButton
154
         *
155
         * @return javax.swing.JButton
156
         */
157
        private JButton getBmas() {
158
                if (bmas == null) {
159
                        bmas = new JButton();
160
                        bmas.setPreferredSize(new java.awt.Dimension(16, (int) getTText().getPreferredSize().getHeight()/2));
161
                        bmas.addActionListener(this);
162
                        bmas.setIcon(new ImageIcon(getClass().getResource(pathToImages + "mas.png")));
163
                }
164
                return bmas;
165
        }
166

    
167
        /**
168
         * This method initializes jButton1
169
         *
170
         * @return javax.swing.JButton
171
         */
172
        private JButton getBmenos() {
173
                if (bmenos == null) {
174
                        bmenos = new JButton();
175
                        bmenos.setPreferredSize(new java.awt.Dimension(16, (int) getTText().getPreferredSize().getHeight()/2));
176
                        bmenos.addActionListener(this);
177
                        bmenos.setIcon(new ImageIcon(getClass().getResource(pathToImages + "menos.png")));
178
                }
179
                return bmenos;
180
        }
181

    
182
        /**
183
         * This method initializes jPanel
184
         *
185
         * @return javax.swing.JPanel
186
         */
187
        private JPanel getPGeneral() {
188
                if (pGeneral == null) {
189
                        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
190

    
191
                        GridBagConstraints gridBagConstraints = new GridBagConstraints();
192
                        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
193
                        if(isRight){
194
                                gridBagConstraints1.gridx = 1;
195
                                gridBagConstraints.gridx = 0;
196
                        }else{
197
                                gridBagConstraints1.gridx = 0;
198
                                gridBagConstraints.gridx = 1;
199
                        }
200
                        gridBagConstraints.gridy = 0;
201
                        gridBagConstraints1.gridy = 0;
202
                        gridBagConstraints.weightx = 1.0;
203
                        pGeneral = new JPanel();
204
                        pGeneral.setLayout(new GridBagLayout());
205
                        pGeneral.add(getTText(), gridBagConstraints);
206
                        pGeneral.add(getPButtons(), gridBagConstraints1);
207
                }
208
                return pGeneral;
209
        }
210

    
211
        public void actionPerformed(ActionEvent e) {
212
                if (e.getSource() == bmas) {
213
                        value ++;
214
                        checkValues();
215
                }
216

    
217
                if (e.getSource() == bmenos) {
218
                        value --;
219
                        checkValues();
220
                }
221
                callValueChangedListeners();
222
        }
223

    
224
        public void addValueChangedListener(TextIncreaserListener listener) {
225
                if (!actionCommandListeners.contains(listener))
226
                        actionCommandListeners.add(listener);
227
        }
228

    
229
        public void removeValueChangedListener(TextIncreaserListener listener) {
230
                actionCommandListeners.remove(listener);
231
        }
232

    
233
        private void callValueChangedListeners() {
234
                if (!bDoCallListeners)
235
                        return;
236
                Iterator acIterator = actionCommandListeners.iterator();
237
                while (acIterator.hasNext()) {
238
                        TextIncreaserListener listener = (TextIncreaserListener) acIterator.next();
239
                        listener.actionValueChanged(new TextIncreaserEvent(this));
240
                }
241
                eventId++;
242
        }
243

    
244
        private void checkValues() {
245
                if (value >= maxValue) value = maxValue;
246
                if (value <= minValue) value = minValue;
247
                getTText().setText(Double.toString(value));
248
        }
249

    
250
        public void keyPressed(KeyEvent e) {
251
                if (e.getKeyCode() == 10) {
252
                        try {
253
                                value = new Double(getTText().getText()).doubleValue();
254
                        } catch (NumberFormatException exc) {
255
                        }
256

    
257
                        checkValues();
258
                        callValueChangedListeners();
259
                }
260
        }
261

    
262
        public void keyReleased(KeyEvent e) {}
263
        public void keyTyped(KeyEvent e) {}
264
}