Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.buffer / org.gvsig.raster.swing.buffer.impl / src / main / java / org / gvsig / raster / swing / buffer / impl / histogram / textincreaser / TextIncreaserContainer.java @ 43805

History | View | Annotate | Download (7.56 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.raster.swing.buffer.impl.histogram.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
        /**
56
         * Variable que est? a true si los controles est?n a la derecha
57
         */
58
        private boolean    isRight          = true;
59

    
60
        private boolean    bDoCallListeners = true;
61
        static private int eventId          = Integer.MIN_VALUE;
62

    
63
        /**
64
         * Creaci?n de un componente TextIncrearserContainer
65
         * @param width Ancho del componente
66
         * @param minValue M?nimo valor que puede tomar el texto del control
67
         * @param maxValue M?ximo valor que puede tomar el texto del control
68
         * @param right        a true si queremos que los controles se muestren a la derecha del TextBox
69
         * y false si los queremos a la izquierda
70
         */
71
        public TextIncreaserContainer(int width, double minValue, double maxValue, double init, boolean right) {
72
                super();
73
                getTText().setPreferredSize(new Dimension(width - 25, (int) getTText().getPreferredSize().getHeight()));
74
                this.minValue = minValue;
75
                this.maxValue = maxValue;
76
                this.isRight = right;
77
                setValue(init);
78
                initialize();
79
        }
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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