Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2055 / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / comboscale / ComboScale.java @ 38971

History | View | Annotate | Download (9.47 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.gui.beans.controls.comboscale;
23

    
24
import java.awt.FlowLayout;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.text.NumberFormat;
28
import java.util.ArrayList;
29
import java.util.BitSet;
30
import java.util.Iterator;
31
import java.util.List;
32

    
33
import javax.swing.DefaultComboBoxModel;
34
import javax.swing.JComboBox;
35
import javax.swing.JLabel;
36
import javax.swing.JPanel;
37

    
38
import org.gvsig.gui.beans.controls.IControl;
39

    
40
public class ComboScale extends JPanel implements IControl {
41

    
42
    private static final long serialVersionUID = 6483498713300082876L;
43

    
44
    private JLabel jLabel = null;
45

    
46
    private JComboBox jComboBox = null;
47

    
48
    private List<ActionListener> actionCommandListeners =
49
        new ArrayList<ActionListener>();
50

    
51
    private boolean bDoCallListeners = true;
52

    
53
    private boolean isScaleCombo;
54

    
55
    static private int eventId = Integer.MIN_VALUE;
56

    
57
    private Long lastValue = null;
58
    
59
    // jaume
60
    private class ComboScaleItem {
61

    
62
        private long value;
63

    
64
        public ComboScaleItem(long itemScale) {
65
            this.value = itemScale;
66
        }
67

    
68
        public String toString() {
69
            return NumberFormat.getNumberInstance().format(value);
70
        }
71

    
72
        public boolean equals(Object obj) {
73
            return obj instanceof ComboScaleItem
74
                && ((ComboScaleItem) obj).getValue() == value;
75
        }
76

    
77
        public long getValue() {
78
            return value;
79
        }
80
    }
81

    
82
    /**
83
     * This is the default constructor
84
     */
85
    public ComboScale() {
86
        super();
87
        initialize();
88
    }
89

    
90
    /**
91
     * This method initializes this
92
     * 
93
     * @return void
94
     */
95
    private void initialize() {
96
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 0, 0);
97
        jLabel = new JLabel();
98
        jLabel.setText("1:");
99
        this.setLayout(flowLayout);
100
        // this.setSize(155, 16);
101
        this.add(jLabel, null);
102
        this.add(getJComboBox(), null);
103
    }
104

    
105
    /**
106
     * This method initializes jComboBox
107
     * 
108
     * @return javax.swing.JComboBox
109
     */
110
    private JComboBox getJComboBox() {
111
        if (jComboBox == null) {
112
            jComboBox = new JComboBox();
113
            jComboBox.setEditable(true);
114
            jComboBox.setMaximumRowCount(5);
115
            jComboBox.setBackground(java.awt.SystemColor.window);
116
            jComboBox
117
                .setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
118
            jComboBox.addActionListener(new java.awt.event.ActionListener() {
119

    
120
                public void actionPerformed(java.awt.event.ActionEvent e) {
121
                    if (e.getActionCommand().equals("comboBoxChanged")) {
122

    
123
                        // callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
124
                        // setScale(((Long)jComboBox.getSelectedItem()).longValue());
125
                        Object item = jComboBox.getSelectedItem();
126
                        long scale = 0;
127
                        if (item instanceof String) {
128
                            StringBuffer sb = new StringBuffer((String) item);
129
                            // remove any point in the number
130
                            final String digits = "0123456789";
131
                            int i = sb.charAt(0) == '-' ? 1 : 0;
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
                            } catch (NumberFormatException e1) {
146
                            }
147
                        } else {
148
                            scale =
149
                                ((ComboScaleItem) jComboBox.getSelectedItem())
150
                                    .getValue();
151
                        }
152
                        insertScaleIfNotPresent(scale);
153
                        callActionCommandListeners(scale);
154
                    }
155
                }
156
            });
157
        }
158
        return jComboBox;
159
    }
160
    
161
        public Object getValue() {
162
                return lastValue;
163
        }
164

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

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

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

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

    
221
    private void callActionCommandListeners(long scale) {
222
        if (!bDoCallListeners)
223
            return;
224

    
225
        lastValue = new Long(scale);
226
        Iterator<ActionListener> acIterator = actionCommandListeners.iterator();
227
        while (acIterator.hasNext()) {
228
            ActionListener listener = acIterator.next();
229
            listener.actionPerformed(new ActionEvent(this, eventId,"view-change-scale"));
230
        }
231
        eventId++;
232
    }
233

    
234
    public void addActionListener(ActionListener listener) {
235
        if (!actionCommandListeners.contains(listener))
236
            actionCommandListeners.add(listener);
237
    }
238

    
239
    public void removeActionListener(ActionListener listener) {
240
        actionCommandListeners.remove(listener);
241
    }
242

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

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

    
260
    /**
261
     * Gets the label
262
     */
263
    public String getLabel() {
264
        return jLabel.getText();
265
    }
266

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

    
275
            if (scale < 0)
276
                return null;
277

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

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

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