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