Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libUIComponent / src / org / gvsig / gui / beans / controls / comboscale / ComboScale.java @ 37848

History | View | Annotate | Download (9.36 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
    // jaume
58
    private class ComboScaleItem {
59

    
60
        private long value;
61

    
62
        public ComboScaleItem(long itemScale) {
63
            this.value = itemScale;
64
        }
65

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

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

    
75
        public long getValue() {
76
            return value;
77
        }
78
    }
79

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

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

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

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

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

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

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

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

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

    
215
    private void callActionCommandListeners(long scale) {
216
        if (!bDoCallListeners)
217
            return;
218

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

    
228
    public void addActionListener(ActionListener listener) {
229
        if (!actionCommandListeners.contains(listener))
230
            actionCommandListeners.add(listener);
231
    }
232

    
233
    public void removeActionListener(ActionListener listener) {
234
        actionCommandListeners.remove(listener);
235
    }
236

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

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

    
254
    /**
255
     * Gets the label
256
     */
257
    public String getLabel() {
258
        return jLabel.getText();
259
    }
260

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

    
269
            if (scale < 0)
270
                return null;
271

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

    
283
    public void setEnabled(boolean enabled) {
284
        boolean oldEnabled = jComboBox.isEnabled();
285
        jComboBox.setEnabled(enabled);
286
        jComboBox.firePropertyChange("enabled", oldEnabled, enabled);
287
        if (enabled != oldEnabled) {
288
            jComboBox.repaint();
289
        }
290
    }
291

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