svn-gvsig-desktop / tags / v10_RC2c / libraries / libUI / src / org / gvsig / gui / beans / controls / comboscale / ComboScale.java @ 8745
History | View | Annotate | Download (6.23 KB)
1 | 5930 | caballero | package org.gvsig.gui.beans.controls.comboscale; |
---|---|---|---|
2 | |||
3 | 6582 | cesar | import java.awt.event.ActionEvent; |
4 | import java.awt.event.ActionListener; |
||
5 | 6937 | jaume | import java.text.NumberFormat; |
6 | 5930 | caballero | import java.util.ArrayList; |
7 | 6582 | cesar | import java.util.Iterator; |
8 | 5930 | caballero | |
9 | import javax.swing.DefaultComboBoxModel; |
||
10 | import javax.swing.JComboBox; |
||
11 | 6582 | cesar | import javax.swing.JLabel; |
12 | 5930 | caballero | import javax.swing.JPanel; |
13 | |||
14 | 6582 | cesar | import org.gvsig.gui.beans.controls.IControl; |
15 | 7341 | maquerol | import java.awt.FlowLayout; |
16 | 5930 | caballero | |
17 | 6582 | cesar | public class ComboScale extends JPanel implements IControl { |
18 | |||
19 | 5930 | caballero | private JLabel jLabel = null; |
20 | 6823 | fjp | |
21 | 5930 | caballero | private JComboBox jComboBox = null; |
22 | 6632 | caballero | |
23 | 6823 | fjp | private ArrayList actionCommandListeners = new ArrayList(); |
24 | |||
25 | // private Long[] scales;
|
||
26 | private boolean bDoCallListeners = true; |
||
27 | |||
28 | static private int eventId = Integer.MIN_VALUE; |
||
29 | |||
30 | 6937 | jaume | // jaume
|
31 | private class ComboScaleItem { |
||
32 | private long value; |
||
33 | |||
34 | public ComboScaleItem(long itemScale) { |
||
35 | this.value = itemScale;
|
||
36 | } |
||
37 | |||
38 | public String toString() { |
||
39 | return NumberFormat.getNumberInstance().format(value); |
||
40 | } |
||
41 | |||
42 | public boolean equals(Object obj) { |
||
43 | return obj instanceof ComboScaleItem && ((ComboScaleItem) obj).getValue() == value; |
||
44 | } |
||
45 | |||
46 | public long getValue() { |
||
47 | return value;
|
||
48 | } |
||
49 | } |
||
50 | 5930 | caballero | /**
|
51 | * This is the default constructor
|
||
52 | */
|
||
53 | public ComboScale() {
|
||
54 | super();
|
||
55 | initialize(); |
||
56 | } |
||
57 | |||
58 | /**
|
||
59 | * This method initializes this
|
||
60 | 6937 | jaume | *
|
61 | 5930 | caballero | * @return void
|
62 | */
|
||
63 | private void initialize() { |
||
64 | 7341 | maquerol | FlowLayout flowLayout = new FlowLayout(); |
65 | flowLayout.setHgap(0);
|
||
66 | flowLayout.setVgap(0);
|
||
67 | 5930 | caballero | jLabel = new JLabel(); |
68 | jLabel.setText("1:");
|
||
69 | 7341 | maquerol | this.setLayout(flowLayout);
|
70 | this.setSize(155, 16); |
||
71 | //this.setBorder(javax.swing.BorderFactory.createLineBorder(
|
||
72 | //java.awt.Color.gray, 1));
|
||
73 | 5930 | caballero | this.add(jLabel, null); |
74 | this.add(getJComboBox(), null); |
||
75 | } |
||
76 | |||
77 | /**
|
||
78 | * This method initializes jComboBox
|
||
79 | 6937 | jaume | *
|
80 | 5930 | caballero | * @return javax.swing.JComboBox
|
81 | */
|
||
82 | private JComboBox getJComboBox() { |
||
83 | if (jComboBox == null) { |
||
84 | jComboBox = new JComboBox(); |
||
85 | 6823 | fjp | jComboBox.setPreferredSize(new java.awt.Dimension(130, 16)); |
86 | 5930 | caballero | jComboBox.setEditable(true);
|
87 | jComboBox.setMaximumRowCount(5);
|
||
88 | 6823 | fjp | jComboBox.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, |
89 | 10));
|
||
90 | 5930 | caballero | jComboBox.setBackground(java.awt.SystemColor.window); |
91 | 6823 | fjp | jComboBox |
92 | .setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT); |
||
93 | 5930 | caballero | jComboBox.addActionListener(new java.awt.event.ActionListener() {
|
94 | public void actionPerformed(java.awt.event.ActionEvent e) { |
||
95 | 6582 | cesar | if (e.getActionCommand().equals("comboBoxChanged")) { |
96 | 6823 | fjp | |
97 | // callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
|
||
98 | // setScale(((Long)jComboBox.getSelectedItem()).longValue());
|
||
99 | 6937 | jaume | Object item = jComboBox.getSelectedItem();
|
100 | long scale;
|
||
101 | if (item instanceof String) { |
||
102 | 6940 | jaume | StringBuffer sb = new StringBuffer((String) item); |
103 | 7018 | jaume | // remove any point in the number
|
104 | final String digits = "0123456789"; |
||
105 | int i = sb.charAt(0) == '-' ? 1 : 0; |
||
106 | while (i < sb.length()-1) { |
||
107 | if (digits.indexOf(sb.charAt(i))==-1) |
||
108 | sb.deleteCharAt(i); |
||
109 | i++; |
||
110 | 6940 | jaume | } |
111 | 6937 | jaume | jComboBox.removeItem(item); |
112 | 6940 | jaume | scale = Long.parseLong(sb.toString());
|
113 | 6937 | jaume | } else {
|
114 | scale = ((ComboScaleItem) jComboBox.getSelectedItem()) |
||
115 | .getValue(); |
||
116 | } |
||
117 | 6823 | fjp | insertScaleIfNotPresent(scale); |
118 | callActionCommandListeners(scale); |
||
119 | 6582 | cesar | } |
120 | 5930 | caballero | } |
121 | }); |
||
122 | } |
||
123 | return jComboBox;
|
||
124 | } |
||
125 | |||
126 | public void setItems(long[] items) { |
||
127 | 6937 | jaume | ComboScaleItem[] scales = new ComboScaleItem[items.length]; |
128 | 6823 | fjp | for (int i = 0; i < items.length; i++) { |
129 | 6937 | jaume | scales[i] = new ComboScaleItem(items[i]);
|
130 | 5930 | caballero | } |
131 | 6823 | fjp | DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales); |
132 | getJComboBox().setModel(newModel); |
||
133 | 5930 | caballero | } |
134 | 6823 | fjp | |
135 | /**
|
||
136 | * This funcion ONLY sets the text in combo. It will NOT call listeners.
|
||
137 | 6937 | jaume | *
|
138 | 6823 | fjp | * @param scale
|
139 | */
|
||
140 | 5930 | caballero | public void setScale(long item) { |
141 | 6823 | fjp | bDoCallListeners = false;
|
142 | 6937 | jaume | getJComboBox().setSelectedItem(new ComboScaleItem(item));
|
143 | 7019 | jaume | bDoCallListeners = true;
|
144 | 6823 | fjp | } |
145 | |||
146 | /**
|
||
147 | * @param scale
|
||
148 | */
|
||
149 | private void insertScaleIfNotPresent(long scale) { |
||
150 | // Si viene de un setScale, no insertamos la escala en el combo
|
||
151 | 7286 | jaume | if (!bDoCallListeners)
|
152 | return;
|
||
153 | 6937 | jaume | |
154 | 6823 | fjp | DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBox |
155 | .getModel(); |
||
156 | // model=new DefaultComboBoxModel();
|
||
157 | boolean inserted = false; |
||
158 | for (int i = 0; i < model.getSize(); i++) { |
||
159 | 6937 | jaume | ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i); |
160 | if (scale == itemScale.getValue()) {
|
||
161 | 6823 | fjp | inserted = true;
|
162 | break;
|
||
163 | 5930 | caballero | } |
164 | 6823 | fjp | } |
165 | if (!inserted) {
|
||
166 | for (int i = 0; i < model.getSize(); i++) { |
||
167 | 6937 | jaume | ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i); |
168 | if (scale < itemScale.getValue()) {
|
||
169 | model.insertElementAt(new ComboScaleItem(scale), i);
|
||
170 | 6823 | fjp | inserted = true;
|
171 | break;
|
||
172 | } |
||
173 | 5930 | caballero | } |
174 | 6823 | fjp | if (!inserted)
|
175 | 6937 | jaume | model.addElement(new ComboScaleItem(scale));
|
176 | 5930 | caballero | } |
177 | 6823 | fjp | jComboBox.setSelectedItem(new Long(scale)); |
178 | 5930 | caballero | } |
179 | 6823 | fjp | |
180 | private void callActionCommandListeners(long scale) { |
||
181 | 7286 | jaume | if (!bDoCallListeners)
|
182 | 6823 | fjp | return;
|
183 | 6632 | caballero | |
184 | 6582 | cesar | Iterator acIterator = actionCommandListeners.iterator();
|
185 | while (acIterator.hasNext()) {
|
||
186 | ActionListener listener = (ActionListener) acIterator.next(); |
||
187 | 6823 | fjp | listener.actionPerformed(new ActionEvent(this, eventId, |
188 | "CHANGE_SCALE_" + scale));
|
||
189 | 6582 | cesar | } |
190 | eventId++; |
||
191 | } |
||
192 | 6632 | caballero | |
193 | 6582 | cesar | public void addActionListener(ActionListener listener) { |
194 | if (!actionCommandListeners.contains(listener))
|
||
195 | actionCommandListeners.add(listener); |
||
196 | } |
||
197 | 6632 | caballero | |
198 | 6582 | cesar | public void removeActionListener(ActionListener listener) { |
199 | actionCommandListeners.remove(listener); |
||
200 | } |
||
201 | 6632 | caballero | |
202 | 6618 | cesar | /**
|
203 | * Returns the current selected item.
|
||
204 | 6937 | jaume | *
|
205 | 6823 | fjp | * @return The value of the selected scale, or -1 if there was an invalid
|
206 | * value (ie. not long value).
|
||
207 | 6618 | cesar | */
|
208 | public long getScale() { |
||
209 | 6937 | jaume | return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
|
210 | 6618 | cesar | } |
211 | 6632 | caballero | |
212 | 6618 | cesar | /**
|
213 | * Sets the label to be displayed on the left of the combo
|
||
214 | */
|
||
215 | public void setLabel(String label) { |
||
216 | jLabel.setText(label); |
||
217 | } |
||
218 | 6632 | caballero | |
219 | 6618 | cesar | /**
|
220 | * Gets the label
|
||
221 | */
|
||
222 | public String getLabel() { |
||
223 | return jLabel.getText();
|
||
224 | } |
||
225 | 6632 | caballero | |
226 | 6937 | jaume | public Object setValue(Object value) { |
227 | 6622 | cesar | try {
|
228 | 6937 | jaume | long scale = Long.parseLong((String)value); |
229 | |||
230 | 6938 | jaume | if (scale < 0) |
231 | 6937 | jaume | return null; |
232 | |||
233 | ComboScaleItem item = new ComboScaleItem(scale);
|
||
234 | if (item.equals(jComboBox.getSelectedItem()))
|
||
235 | return item;
|
||
236 | 6622 | cesar | this.setScale(scale);
|
237 | 6937 | jaume | return item;
|
238 | 6823 | fjp | } catch (NumberFormatException ex) { |
239 | 6632 | caballero | // don't change the status if the provided value was not valid
|
240 | 6622 | cesar | return null; |
241 | } |
||
242 | } |
||
243 | 6937 | jaume | |
244 | 6823 | fjp | } // @jve:decl-index=0:visual-constraint="10,10" |