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 @ 40561
History | View | Annotate | Download (9.51 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 |
|
66 |
public ComboScaleItem(long itemScale) { |
67 |
this.value = itemScale;
|
68 |
} |
69 |
|
70 |
public String toString() { |
71 |
return NumberFormat.getNumberInstance().format(value); |
72 |
} |
73 |
|
74 |
public boolean equals(Object obj) { |
75 |
return obj instanceof ComboScaleItem |
76 |
&& ((ComboScaleItem) obj).getValue() == value; |
77 |
} |
78 |
|
79 |
public long getValue() { |
80 |
return value;
|
81 |
} |
82 |
} |
83 |
|
84 |
/**
|
85 |
* This is the default constructor
|
86 |
*/
|
87 |
public ComboScale() {
|
88 |
super();
|
89 |
initialize(); |
90 |
} |
91 |
|
92 |
/**
|
93 |
* This method initializes this
|
94 |
*
|
95 |
* @return void
|
96 |
*/
|
97 |
private void initialize() { |
98 |
FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 0, 0); |
99 |
jLabel = new JLabel(); |
100 |
jLabel.setText("1:");
|
101 |
this.setLayout(flowLayout);
|
102 |
// this.setSize(155, 16);
|
103 |
this.add(jLabel, null); |
104 |
this.add(getJComboBox(), null); |
105 |
} |
106 |
|
107 |
/**
|
108 |
* This method initializes jComboBox
|
109 |
*
|
110 |
* @return javax.swing.JComboBox
|
111 |
*/
|
112 |
private JComboBox getJComboBox() { |
113 |
if (jComboBox == null) { |
114 |
jComboBox = new JComboBox(); |
115 |
jComboBox.setEditable(true);
|
116 |
jComboBox.setMaximumRowCount(5);
|
117 |
jComboBox.setBackground(java.awt.SystemColor.window); |
118 |
jComboBox |
119 |
.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT); |
120 |
jComboBox.addActionListener(new java.awt.event.ActionListener() {
|
121 |
|
122 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
123 |
if (e.getActionCommand().equals("comboBoxChanged")) { |
124 |
|
125 |
// callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
|
126 |
// setScale(((Long)jComboBox.getSelectedItem()).longValue());
|
127 |
Object item = jComboBox.getSelectedItem();
|
128 |
long scale = 0; |
129 |
if (item instanceof String) { |
130 |
StringBuffer sb = new StringBuffer((String) item); |
131 |
// remove any point in the number
|
132 |
final String digits = "0123456789"; |
133 |
int i = sb.charAt(0) == '-' ? 1 : 0; |
134 |
BitSet deleteChars = new BitSet(); |
135 |
while (i < sb.length()) {
|
136 |
if (digits.indexOf(sb.charAt(i)) == -1) |
137 |
deleteChars.set(i); |
138 |
i++; |
139 |
} |
140 |
for (int k = deleteChars.size(); k >= 0; k--) { |
141 |
if (deleteChars.get(k))
|
142 |
sb.deleteCharAt(k); |
143 |
} |
144 |
jComboBox.removeItem(item); |
145 |
try {
|
146 |
scale = Long.parseLong(sb.toString());
|
147 |
} catch (NumberFormatException e1) { |
148 |
} |
149 |
} else {
|
150 |
scale = |
151 |
((ComboScaleItem) jComboBox.getSelectedItem()) |
152 |
.getValue(); |
153 |
} |
154 |
insertScaleIfNotPresent(scale); |
155 |
callActionCommandListeners(scale); |
156 |
} |
157 |
} |
158 |
}); |
159 |
} |
160 |
return jComboBox;
|
161 |
} |
162 |
|
163 |
public Object getValue() { |
164 |
return lastValue;
|
165 |
} |
166 |
|
167 |
public void setItems(long[] items) { |
168 |
ComboScaleItem[] scales = new ComboScaleItem[items.length]; |
169 |
for (int i = 0; i < items.length; i++) { |
170 |
scales[i] = new ComboScaleItem(items[i]);
|
171 |
} |
172 |
DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales); |
173 |
getJComboBox().setModel(newModel); |
174 |
} |
175 |
|
176 |
/**
|
177 |
* This funcion ONLY sets the text in combo. It will NOT call listeners.
|
178 |
*
|
179 |
* @param scale
|
180 |
*/
|
181 |
public void setScale(long item) { |
182 |
bDoCallListeners = false;
|
183 |
getJComboBox().setSelectedItem(new ComboScaleItem(item));
|
184 |
bDoCallListeners = true;
|
185 |
} |
186 |
|
187 |
/**
|
188 |
* @param scale
|
189 |
*/
|
190 |
private void insertScaleIfNotPresent(long scale) { |
191 |
// Si viene de un setScale, no insertamos la escala en el combo
|
192 |
if (!bDoCallListeners)
|
193 |
return;
|
194 |
|
195 |
DefaultComboBoxModel model =
|
196 |
(DefaultComboBoxModel) jComboBox.getModel();
|
197 |
// model=new DefaultComboBoxModel();
|
198 |
boolean inserted = false; |
199 |
for (int i = 0; i < model.getSize(); i++) { |
200 |
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i); |
201 |
if (scale == itemScale.getValue()) {
|
202 |
inserted = true;
|
203 |
break;
|
204 |
} |
205 |
} |
206 |
if (!inserted) {
|
207 |
for (int i = 0; i < model.getSize(); i++) { |
208 |
ComboScaleItem itemScale = |
209 |
(ComboScaleItem) model.getElementAt(i); |
210 |
if (scale < itemScale.getValue()) {
|
211 |
model.insertElementAt(new ComboScaleItem(scale), i);
|
212 |
inserted = true;
|
213 |
break;
|
214 |
} |
215 |
} |
216 |
if (!inserted)
|
217 |
model.addElement(new ComboScaleItem(scale));
|
218 |
} |
219 |
jComboBox.setSelectedItem(new ComboScaleItem(scale));
|
220 |
isScaleCombo = true;
|
221 |
} |
222 |
|
223 |
private void callActionCommandListeners(long scale) { |
224 |
if (!bDoCallListeners)
|
225 |
return;
|
226 |
|
227 |
lastValue = new Long(scale); |
228 |
Iterator<ActionListener> acIterator = actionCommandListeners.iterator(); |
229 |
while (acIterator.hasNext()) {
|
230 |
ActionListener listener = acIterator.next();
|
231 |
listener.actionPerformed(new ActionEvent(this, eventId,"view-change-scale")); |
232 |
} |
233 |
eventId++; |
234 |
} |
235 |
|
236 |
public void addActionListener(ActionListener listener) { |
237 |
if (!actionCommandListeners.contains(listener))
|
238 |
actionCommandListeners.add(listener); |
239 |
} |
240 |
|
241 |
public void removeActionListener(ActionListener listener) { |
242 |
actionCommandListeners.remove(listener); |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Returns the current selected item.
|
247 |
*
|
248 |
* @return The value of the selected scale, or -1 if there was an invalid
|
249 |
* value (ie. not long value).
|
250 |
*/
|
251 |
public long getScale() { |
252 |
return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* Sets the label to be displayed on the left of the combo
|
257 |
*/
|
258 |
public void setLabel(String label) { |
259 |
jLabel.setText(label); |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Gets the label
|
264 |
*/
|
265 |
public String getLabel() { |
266 |
return jLabel.getText();
|
267 |
} |
268 |
|
269 |
public Object setValue(Object value) { |
270 |
if (isScaleCombo) {
|
271 |
isScaleCombo = false;
|
272 |
return null; |
273 |
} |
274 |
try {
|
275 |
long scale = Long.parseLong((String) value); |
276 |
|
277 |
if (scale < 0) |
278 |
return null; |
279 |
|
280 |
ComboScaleItem item = new ComboScaleItem(scale);
|
281 |
if (item.equals(jComboBox.getSelectedItem()))
|
282 |
return item;
|
283 |
this.setScale(scale);
|
284 |
return item;
|
285 |
} catch (NumberFormatException ex) { |
286 |
// don't change the status if the provided value was not valid
|
287 |
return null; |
288 |
} |
289 |
} |
290 |
|
291 |
public void setEnabled(boolean enabled) { |
292 |
boolean oldEnabled = jComboBox.isEnabled();
|
293 |
jComboBox.setEnabled(enabled); |
294 |
jComboBox.firePropertyChange("enabled", oldEnabled, enabled);
|
295 |
if (enabled != oldEnabled) {
|
296 |
jComboBox.repaint(); |
297 |
} |
298 |
} |
299 |
|
300 |
} // @jve:decl-index=0:visual-constraint="10,10"
|