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