svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.ui / src / main / java / org / gvsig / gui / beans / controls / comboscale / ComboScale.java @ 47489
History | View | Annotate | Download (10.8 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.gui.beans.controls.comboscale; |
25 |
|
26 |
import java.awt.FlowLayout; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.text.NumberFormat; |
30 |
import java.util.ArrayList; |
31 |
import java.util.BitSet; |
32 |
import java.util.Iterator; |
33 |
import java.util.List; |
34 |
|
35 |
import javax.swing.DefaultComboBoxModel; |
36 |
import javax.swing.JComboBox; |
37 |
import javax.swing.JLabel; |
38 |
import javax.swing.JPanel; |
39 |
|
40 |
import org.gvsig.gui.beans.controls.IControl; |
41 |
|
42 |
public class ComboScale extends JPanel implements IControl { |
43 |
|
44 |
private static final long serialVersionUID = 6483498713300082876L; |
45 |
|
46 |
private JLabel jLabel = null; |
47 |
|
48 |
private JComboBox jComboBox = null; |
49 |
|
50 |
private List<ActionListener> actionCommandListeners = |
51 |
new ArrayList<ActionListener>(); |
52 |
|
53 |
private boolean bDoCallListeners = true; |
54 |
|
55 |
// private boolean isScaleCombo;
|
56 |
|
57 |
static private int eventId = Integer.MIN_VALUE; |
58 |
|
59 |
private Long lastValue = null; |
60 |
|
61 |
// jaume
|
62 |
private class ComboScaleItem { |
63 |
|
64 |
private long value; |
65 |
private boolean temporal; |
66 |
|
67 |
public ComboScaleItem(long itemScale) { |
68 |
this.value = itemScale;
|
69 |
this.temporal=false; |
70 |
} |
71 |
|
72 |
public ComboScaleItem(long itemScale, boolean temporal) { |
73 |
this.value = itemScale;
|
74 |
this.temporal=temporal;
|
75 |
} |
76 |
|
77 |
public String toString() { |
78 |
return NumberFormat.getNumberInstance().format(value); |
79 |
} |
80 |
|
81 |
public boolean equals(Object obj) { |
82 |
return obj instanceof ComboScaleItem |
83 |
&& ((ComboScaleItem) obj).getValue() == value; |
84 |
} |
85 |
|
86 |
public long getValue() { |
87 |
return value;
|
88 |
} |
89 |
|
90 |
public boolean isTemporal(){ |
91 |
return temporal;
|
92 |
} |
93 |
} |
94 |
|
95 |
/**
|
96 |
* This is the default constructor
|
97 |
*/
|
98 |
public ComboScale() {
|
99 |
super();
|
100 |
initialize(); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* This method initializes this
|
105 |
*
|
106 |
* @return void
|
107 |
*/
|
108 |
private void initialize() { |
109 |
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 0, 0); |
110 |
jLabel = new JLabel(); |
111 |
jLabel.setText("1:");
|
112 |
this.setLayout(flowLayout);
|
113 |
// this.setSize(155, 16);
|
114 |
this.add(jLabel, null); |
115 |
this.add(getJComboBox(), null); |
116 |
} |
117 |
|
118 |
/**
|
119 |
* This method initializes jComboBox
|
120 |
*
|
121 |
* @return javax.swing.JComboBox
|
122 |
*/
|
123 |
private JComboBox getJComboBox() { |
124 |
if (jComboBox == null) { |
125 |
jComboBox = new JComboBox(); |
126 |
jComboBox.setEditable(true);
|
127 |
jComboBox.setMaximumRowCount(5);
|
128 |
jComboBox.setBackground(java.awt.SystemColor.window); |
129 |
jComboBox |
130 |
.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT); |
131 |
jComboBox.addActionListener(new java.awt.event.ActionListener() {
|
132 |
|
133 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
134 |
if (e.getActionCommand().equals("comboBoxChanged")) { |
135 |
|
136 |
// callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
|
137 |
// setScale(((Long)jComboBox.getSelectedItem()).longValue());
|
138 |
Object item = jComboBox.getSelectedItem();
|
139 |
long scale = 0; |
140 |
if (item instanceof String) { |
141 |
StringBuffer sb = new StringBuffer((String) item); |
142 |
// remove any point in the number
|
143 |
final String digits = "0123456789"; |
144 |
int i = sb.charAt(0) == '-' ? 1 : 0; |
145 |
BitSet deleteChars = new BitSet(); |
146 |
while (i < sb.length()) {
|
147 |
if (digits.indexOf(sb.charAt(i)) == -1) |
148 |
deleteChars.set(i); |
149 |
i++; |
150 |
} |
151 |
for (int k = deleteChars.size(); k >= 0; k--) { |
152 |
if (deleteChars.get(k))
|
153 |
sb.deleteCharAt(k); |
154 |
} |
155 |
jComboBox.removeItem(item); |
156 |
try {
|
157 |
scale = Long.parseLong(sb.toString());
|
158 |
} catch (NumberFormatException e1) { |
159 |
} |
160 |
} else {
|
161 |
scale = |
162 |
((ComboScaleItem) jComboBox.getSelectedItem()) |
163 |
.getValue(); |
164 |
} |
165 |
setScale(scale, false);
|
166 |
// insertScaleIfNotPresent(scale);
|
167 |
callActionCommandListeners(scale); |
168 |
} |
169 |
} |
170 |
}); |
171 |
} |
172 |
return jComboBox;
|
173 |
} |
174 |
|
175 |
public Object getValue() { |
176 |
return lastValue;
|
177 |
} |
178 |
|
179 |
public void setItems(long[] items) { |
180 |
ComboScaleItem[] scales = new ComboScaleItem[items.length]; |
181 |
for (int i = 0; i < items.length; i++) { |
182 |
scales[i] = new ComboScaleItem(items[i]);
|
183 |
} |
184 |
DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales); |
185 |
getJComboBox().setModel(newModel); |
186 |
} |
187 |
|
188 |
public void setScale(long scale) { |
189 |
setScale(scale, true);
|
190 |
|
191 |
} |
192 |
/**
|
193 |
* This funcion ONLY sets the text in combo. It will NOT call listeners.
|
194 |
*
|
195 |
* @param scale
|
196 |
*/
|
197 |
public synchronized void setScale(long scale, boolean temporal) { |
198 |
bDoCallListeners = false;
|
199 |
try {
|
200 |
DefaultComboBoxModel model =
|
201 |
(DefaultComboBoxModel) jComboBox.getModel();
|
202 |
|
203 |
//The item is inserted in the data model but marked as temporal
|
204 |
//Only one temporal can be in the data model
|
205 |
for (int i = 0; i < model.getSize(); i++) { |
206 |
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i); |
207 |
if (itemScale.isTemporal()) {
|
208 |
model.removeElement(itemScale); |
209 |
break;
|
210 |
} |
211 |
} |
212 |
getJComboBox().setSelectedItem(new ComboScaleItem(scale, temporal));
|
213 |
} finally {
|
214 |
bDoCallListeners = true;
|
215 |
} |
216 |
// insertScaleIfNotPresent(scale, true);
|
217 |
} |
218 |
|
219 |
// private void insertScaleIfNotPresent(long scale, boolean isTemporal) {
|
220 |
// // Si viene de un setScale, no insertamos la escala en el combo
|
221 |
// if (!bDoCallListeners)
|
222 |
// return;
|
223 |
//
|
224 |
// DefaultComboBoxModel model =
|
225 |
// (DefaultComboBoxModel) jComboBox.getModel();
|
226 |
// // model=new DefaultComboBoxModel();
|
227 |
// boolean inserted = false;
|
228 |
// for (int i = 0; i < model.getSize(); i++) {
|
229 |
// ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
|
230 |
// if (scale == itemScale.getValue()) {
|
231 |
// inserted = true;
|
232 |
// break;
|
233 |
// }
|
234 |
// }
|
235 |
// ComboScaleItem newScaleItem = new ComboScaleItem(scale, isTemporal);
|
236 |
// if (!inserted) {
|
237 |
// for (int i = 0; i < model.getSize(); i++) {
|
238 |
// ComboScaleItem itemScale =
|
239 |
// (ComboScaleItem) model.getElementAt(i);
|
240 |
// if (scale < itemScale.getValue()) {
|
241 |
// model.insertElementAt(newScaleItem, i);
|
242 |
// inserted = true;
|
243 |
// break;
|
244 |
// }
|
245 |
// }
|
246 |
// if (!inserted)
|
247 |
// model.addElement(newScaleItem);
|
248 |
// }
|
249 |
// jComboBox.setSelectedItem(newScaleItem);
|
250 |
//
|
251 |
//// isScaleCombo = true;
|
252 |
// }
|
253 |
|
254 |
// /**
|
255 |
// * @param scale
|
256 |
// */
|
257 |
// private void insertScaleIfNotPresent(long scale) {
|
258 |
// insertScaleIfNotPresent(scale, false);
|
259 |
// }
|
260 |
|
261 |
private void callActionCommandListeners(long scale) { |
262 |
if (!bDoCallListeners)
|
263 |
return;
|
264 |
|
265 |
lastValue = new Long(scale); |
266 |
Iterator<ActionListener> acIterator = actionCommandListeners.iterator(); |
267 |
while (acIterator.hasNext()) {
|
268 |
ActionListener listener = acIterator.next();
|
269 |
listener.actionPerformed(new ActionEvent(this, eventId,"view-change-scale")); |
270 |
} |
271 |
eventId++; |
272 |
} |
273 |
|
274 |
public void addActionListener(ActionListener listener) { |
275 |
if (!actionCommandListeners.contains(listener))
|
276 |
actionCommandListeners.add(listener); |
277 |
} |
278 |
|
279 |
public void removeActionListener(ActionListener listener) { |
280 |
actionCommandListeners.remove(listener); |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Returns the current selected item.
|
285 |
*
|
286 |
* @return The value of the selected scale, or -1 if there was an invalid
|
287 |
* value (ie. not long value).
|
288 |
*/
|
289 |
public long getScale() { |
290 |
return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
|
291 |
} |
292 |
|
293 |
/**
|
294 |
* Sets the label to be displayed on the left of the combo
|
295 |
*/
|
296 |
public void setLabel(String label) { |
297 |
jLabel.setText(label); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Gets the label
|
302 |
*/
|
303 |
public String getLabel() { |
304 |
return jLabel.getText();
|
305 |
} |
306 |
|
307 |
public Object setValue(Object value) { |
308 |
// if (isScaleCombo) {
|
309 |
// isScaleCombo = false;
|
310 |
// return null;
|
311 |
// }
|
312 |
try {
|
313 |
long scale = Long.parseLong((String) value); |
314 |
|
315 |
if (scale < 0) |
316 |
return null; |
317 |
|
318 |
ComboScaleItem item = new ComboScaleItem(scale);
|
319 |
if (item.equals(jComboBox.getSelectedItem()))
|
320 |
return item;
|
321 |
this.setScale(scale);
|
322 |
return item;
|
323 |
} catch (NumberFormatException ex) { |
324 |
// don't change the status if the provided value was not valid
|
325 |
return null; |
326 |
} |
327 |
} |
328 |
|
329 |
public void setEnabled(boolean enabled) { |
330 |
boolean oldEnabled = jComboBox.isEnabled();
|
331 |
jComboBox.setEnabled(enabled); |
332 |
jComboBox.firePropertyChange("enabled", oldEnabled, enabled);
|
333 |
if (enabled != oldEnabled) {
|
334 |
jComboBox.repaint(); |
335 |
} |
336 |
} |
337 |
|
338 |
} // @jve:decl-index=0:visual-constraint="10,10"
|