Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / comboscale / ComboScale.java @ 34390

History | View | Annotate | Download (7.95 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.controls.comboscale;
20

    
21
import java.awt.FlowLayout;
22
import java.awt.event.ActionEvent;
23
import java.awt.event.ActionListener;
24
import java.text.NumberFormat;
25
import java.util.ArrayList;
26
import java.util.BitSet;
27
import java.util.Iterator;
28

    
29
import javax.swing.DefaultComboBoxModel;
30
import javax.swing.JComboBox;
31
import javax.swing.JLabel;
32
import javax.swing.JPanel;
33

    
34
import org.gvsig.gui.beans.controls.IControl;
35

    
36
public class ComboScale extends JPanel implements IControl {
37
  private static final long serialVersionUID = 6483498713300082876L;
38

    
39
        private JLabel jLabel = null;
40

    
41
        private JComboBox jComboBox = null;
42

    
43
        private ArrayList actionCommandListeners = new ArrayList();
44

    
45
        // private Long[] scales;
46
        private boolean bDoCallListeners = true;
47

    
48
        private boolean isScaleCombo;
49

    
50
        static private int eventId = Integer.MIN_VALUE;
51
        
52
        private long lastScaleValue=0;
53

    
54
        // jaume
55
        private class ComboScaleItem {
56
                private long value;
57

    
58
                public ComboScaleItem(long itemScale) {
59
                        this.value = itemScale;
60
                }
61

    
62
                public String toString() {
63
                        return NumberFormat.getNumberInstance().format(value);
64
                }
65

    
66
                public boolean equals(Object obj) {
67
                        return obj instanceof ComboScaleItem && ((ComboScaleItem) obj).getValue() == value;
68
                }
69

    
70
                public long getValue() {
71
                        return value;
72
                }
73
        }
74
        /**
75
         * This is the default constructor
76
         */
77
        public ComboScale() {
78
                super();
79
                initialize();
80
        }
81

    
82
        /**
83
         * This method initializes this
84
         *
85
         * @return void
86
         */
87
        private void initialize() {
88
                FlowLayout flowLayout = new FlowLayout();
89
                flowLayout.setHgap(0);
90
                flowLayout.setVgap(0);
91
                jLabel = new JLabel();
92
                jLabel.setText("1:");
93
                this.setLayout(flowLayout);
94
                this.setSize(155, 16);
95
                //this.setBorder(javax.swing.BorderFactory.createLineBorder(
96
                this.add(jLabel, null);
97
                this.add(getJComboBox(), null);
98
                                //java.awt.Color.gray, 1));
99
        }
100

    
101
        /**
102
         * This method initializes jComboBox
103
         *
104
         * @return javax.swing.JComboBox
105
         */
106
        private JComboBox getJComboBox() {
107
                if (jComboBox == null) {
108
                        jComboBox = new JComboBox();
109
                        jComboBox.setPreferredSize(new java.awt.Dimension(130, 16));
110
                        jComboBox.setEditable(true);
111
                        jComboBox.setMaximumRowCount(5);
112
                        jComboBox.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD,
113
                                        10));
114
                        jComboBox.setBackground(java.awt.SystemColor.window);
115
                        jComboBox.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
116
                        jComboBox.addActionListener(new java.awt.event.ActionListener() {
117
                                public void actionPerformed(java.awt.event.ActionEvent e) {
118
                                        if (e.getActionCommand().equals("comboBoxChanged")) {
119

    
120
                                                // callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
121
                                                // setScale(((Long)jComboBox.getSelectedItem()).longValue());
122
                                                Object item = jComboBox.getSelectedItem();
123
                                                long scale=0;
124
                                                if (item instanceof String) {
125
                                                        StringBuffer sb = new StringBuffer((String) item);
126
                                                        // remove any point in the number
127
                                                        final String digits = "0123456789";
128
                                                        int i = 0;
129
                                                        if ((sb.length() > 0) && (sb.charAt(0) == '-')){
130
                                                                i = 1;
131
                                                        }
132
                                                        BitSet deleteChars=new BitSet();
133
                                                        while (i < sb.length()) {
134
                                                                if (digits.indexOf(sb.charAt(i))==-1)
135
                                                                        deleteChars.set(i);
136
                                                                i++;
137
                                                        }
138
                                                        for (int k=deleteChars.size();k>=0;k--){
139
                                                                if (deleteChars.get(k))
140
                                                                        sb.deleteCharAt(k);
141
                                                        }
142
                                                        jComboBox.removeItem(item);
143
                                                        try{
144
                                                                scale = Long.parseLong(sb.toString());
145
                                                                lastScaleValue = scale;
146
                                                        }catch (NumberFormatException e1) {
147
                                                                scale = lastScaleValue;
148
                                                        }
149
                                                } else {
150
                                                        scale = ((ComboScaleItem) jComboBox.getSelectedItem())
151
                                                        .getValue();
152
                                                }
153
                                                insertScaleIfNotPresent(scale);
154
                                                callActionCommandListeners(scale);
155
                                        }
156
                                }
157
                        });
158
                }
159
                return jComboBox;
160
        }
161

    
162
        public void setItems(long[] items) {
163
                ComboScaleItem[] scales = new ComboScaleItem[items.length];
164
                for (int i = 0; i < items.length; i++) {
165
                        scales[i] = new ComboScaleItem(items[i]);
166
                }
167
                DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales);
168
                getJComboBox().setModel(newModel);
169
        }
170

    
171
        /**
172
         * This funcion ONLY sets the text in combo. It will NOT call listeners.
173
         *
174
         * @param scale
175
         */
176
        public void setScale(long item) {
177
                bDoCallListeners = false;
178
                getJComboBox().setSelectedItem(new ComboScaleItem(item));
179
                bDoCallListeners = true;
180
                this.lastScaleValue=item;
181
        }
182

    
183
        /**
184
         * @param scale
185
         */
186
        private void insertScaleIfNotPresent(long scale) {
187
                // Si viene de un setScale, no insertamos la escala en el combo
188
                if (!bDoCallListeners)
189
                        return;
190

    
191
                DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBox
192
                                .getModel();
193
                // model=new DefaultComboBoxModel();
194
                boolean inserted = false;
195
                for (int i = 0; i < model.getSize(); i++) {
196
                        ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
197
                        if (scale == itemScale.getValue()) {
198
                                inserted = true;
199
                                break;
200
                        }
201
                }
202
                if (!inserted) {
203
                        for (int i = 0; i < model.getSize(); i++) {
204
                                ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
205
                                if (scale < itemScale.getValue()) {
206
                                        model.insertElementAt(new ComboScaleItem(scale), i);
207
                                        inserted = true;
208
                                        break;
209
                                }
210
                        }
211
                        if (!inserted)
212
                                model.addElement(new ComboScaleItem(scale));
213
                }
214
                jComboBox.setSelectedItem(new ComboScaleItem(scale));
215
                isScaleCombo=true;
216
        }
217

    
218
        private void callActionCommandListeners(long scale) {
219
                if (!bDoCallListeners)
220
                        return;
221

    
222
                Iterator acIterator = actionCommandListeners.iterator();
223
                while (acIterator.hasNext()) {
224
                        ActionListener listener = (ActionListener) acIterator.next();
225
                        listener.actionPerformed(new ActionEvent(this, eventId,
226
                                        "CHANGE_SCALE_" + scale));
227
                }
228
                eventId++;
229
        }
230

    
231
        public void addActionListener(ActionListener listener) {
232
                if (!actionCommandListeners.contains(listener))
233
                        actionCommandListeners.add(listener);
234
        }
235

    
236
        public void removeActionListener(ActionListener listener) {
237
                actionCommandListeners.remove(listener);
238
        }
239

    
240
        /**
241
         * Returns the current selected item.
242
         *
243
         * @return The value of the selected scale, or -1 if there was an invalid
244
         *         value (ie. not long value).
245
         */
246
        public long getScale() {
247
                return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
248
        }
249

    
250
        /**
251
         * Sets the label to be displayed on the left of the combo
252
         */
253
        public void setLabel(String label) {
254
                jLabel.setText(label);
255
        }
256

    
257
        /**
258
         * Gets the label
259
         */
260
        public String getLabel() {
261
                return jLabel.getText();
262
        }
263

    
264
        public Object setValue(Object value) {
265
                if (isScaleCombo) {
266
                        isScaleCombo=false;
267
                        return null;
268
                }
269
                try {
270
                        long scale = Long.parseLong((String)value);
271

    
272
                        if (scale < 0)
273
                                return null;
274

    
275
                        ComboScaleItem item = new ComboScaleItem(scale);
276
                        if (item.equals(jComboBox.getSelectedItem()))
277
                                return item;
278
                        this.setScale(scale);
279
                        return item;
280
                } catch (NumberFormatException ex) {
281
                        // don't change the status if the provided value was not valid
282
                        return null;
283
                }
284
        }
285

    
286

    
287
        public void setEnabled(boolean enabled) {
288
        boolean oldEnabled = jComboBox.isEnabled();
289
        jComboBox.setEnabled(enabled);
290
        jComboBox.firePropertyChange("enabled", oldEnabled, enabled);
291
        if (enabled != oldEnabled) {
292
            jComboBox.repaint();
293
        }
294
    }
295

    
296
} // @jve:decl-index=0:visual-constraint="10,10"