Statistics
| Revision:

root / trunk / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / comboscale / ComboScale.java @ 14390

History | View | Annotate | Download (7.49 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
        // jaume
53
        private class ComboScaleItem {
54
                private long value;
55

    
56
                public ComboScaleItem(long itemScale) {
57
                        this.value = itemScale;
58
                }
59

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

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

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

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

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

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

    
155
        public void setItems(long[] items) {
156
                ComboScaleItem[] scales = new ComboScaleItem[items.length];
157
                for (int i = 0; i < items.length; i++) {
158
                        scales[i] = new ComboScaleItem(items[i]);
159
                }
160
                DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales);
161
                getJComboBox().setModel(newModel);
162
        }
163

    
164
        /**
165
         * This funcion ONLY sets the text in combo. It will NOT call listeners.
166
         *
167
         * @param scale
168
         */
169
        public void setScale(long item) {
170
                bDoCallListeners = false;
171
                getJComboBox().setSelectedItem(new ComboScaleItem(item));
172
                bDoCallListeners = true;
173
        }
174

    
175
        /**
176
         * @param scale
177
         */
178
        private void insertScaleIfNotPresent(long scale) {
179
                // Si viene de un setScale, no insertamos la escala en el combo
180
                if (!bDoCallListeners)
181
                        return;
182

    
183
                DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBox
184
                                .getModel();
185
                // model=new DefaultComboBoxModel();
186
                boolean inserted = false;
187
                for (int i = 0; i < model.getSize(); i++) {
188
                        ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
189
                        if (scale == itemScale.getValue()) {
190
                                inserted = true;
191
                                break;
192
                        }
193
                }
194
                if (!inserted) {
195
                        for (int i = 0; i < model.getSize(); i++) {
196
                                ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
197
                                if (scale < itemScale.getValue()) {
198
                                        model.insertElementAt(new ComboScaleItem(scale), i);
199
                                        inserted = true;
200
                                        break;
201
                                }
202
                        }
203
                        if (!inserted)
204
                                model.addElement(new ComboScaleItem(scale));
205
                }
206
                jComboBox.setSelectedItem(new ComboScaleItem(scale));
207
                isScaleCombo=true;
208
        }
209

    
210
        private void callActionCommandListeners(long scale) {
211
                if (!bDoCallListeners)
212
                        return;
213

    
214
                Iterator acIterator = actionCommandListeners.iterator();
215
                while (acIterator.hasNext()) {
216
                        ActionListener listener = (ActionListener) acIterator.next();
217
                        listener.actionPerformed(new ActionEvent(this, eventId,
218
                                        "CHANGE_SCALE_" + scale));
219
                }
220
                eventId++;
221
        }
222

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

    
228
        public void removeActionListener(ActionListener listener) {
229
                actionCommandListeners.remove(listener);
230
        }
231

    
232
        /**
233
         * Returns the current selected item.
234
         *
235
         * @return The value of the selected scale, or -1 if there was an invalid
236
         *         value (ie. not long value).
237
         */
238
        public long getScale() {
239
                return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
240
        }
241

    
242
        /**
243
         * Sets the label to be displayed on the left of the combo
244
         */
245
        public void setLabel(String label) {
246
                jLabel.setText(label);
247
        }
248

    
249
        /**
250
         * Gets the label
251
         */
252
        public String getLabel() {
253
                return jLabel.getText();
254
        }
255

    
256
        public Object setValue(Object value) {
257
                if (isScaleCombo) {
258
                        isScaleCombo=false;
259
                        return null;
260
                }
261
                try {
262
                        long scale = Long.parseLong((String)value);
263

    
264
                        if (scale < 0)
265
                                return null;
266

    
267
                        ComboScaleItem item = new ComboScaleItem(scale);
268
                        if (item.equals(jComboBox.getSelectedItem()))
269
                                return item;
270
                        this.setScale(scale);
271
                        return item;
272
                } catch (NumberFormatException ex) {
273
                        // don't change the status if the provided value was not valid
274
                        return null;
275
                }
276
        }
277

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