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 |
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 |
static private int eventId = Integer.MIN_VALUE; |
29 |
|
30 |
// 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 |
/**
|
51 |
* This is the default constructor
|
52 |
*/
|
53 |
public ComboScale() {
|
54 |
super();
|
55 |
initialize(); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* This method initializes this
|
60 |
*
|
61 |
* @return void
|
62 |
*/
|
63 |
private void initialize() { |
64 |
FlowLayout flowLayout = new FlowLayout(); |
65 |
flowLayout.setHgap(0);
|
66 |
flowLayout.setVgap(0);
|
67 |
jLabel = new JLabel(); |
68 |
jLabel.setText("1:");
|
69 |
this.setLayout(flowLayout);
|
70 |
this.setSize(155, 16); |
71 |
//this.setBorder(javax.swing.BorderFactory.createLineBorder(
|
72 |
//java.awt.Color.gray, 1));
|
73 |
this.add(jLabel, null); |
74 |
this.add(getJComboBox(), null); |
75 |
} |
76 |
|
77 |
/**
|
78 |
* This method initializes jComboBox
|
79 |
*
|
80 |
* @return javax.swing.JComboBox
|
81 |
*/
|
82 |
private JComboBox getJComboBox() { |
83 |
if (jComboBox == null) { |
84 |
jComboBox = new JComboBox(); |
85 |
jComboBox.setPreferredSize(new java.awt.Dimension(130, 16)); |
86 |
jComboBox.setEditable(true);
|
87 |
jComboBox.setMaximumRowCount(5);
|
88 |
jComboBox.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD, |
89 |
10));
|
90 |
jComboBox.setBackground(java.awt.SystemColor.window); |
91 |
jComboBox |
92 |
.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT); |
93 |
jComboBox.addActionListener(new java.awt.event.ActionListener() {
|
94 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
95 |
if (e.getActionCommand().equals("comboBoxChanged")) { |
96 |
|
97 |
// callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
|
98 |
// setScale(((Long)jComboBox.getSelectedItem()).longValue());
|
99 |
Object item = jComboBox.getSelectedItem();
|
100 |
long scale;
|
101 |
if (item instanceof String) { |
102 |
StringBuffer sb = new StringBuffer((String) item); |
103 |
// 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 |
} |
111 |
jComboBox.removeItem(item); |
112 |
scale = Long.parseLong(sb.toString());
|
113 |
} else {
|
114 |
scale = ((ComboScaleItem) jComboBox.getSelectedItem()) |
115 |
.getValue(); |
116 |
} |
117 |
insertScaleIfNotPresent(scale); |
118 |
callActionCommandListeners(scale); |
119 |
} |
120 |
} |
121 |
}); |
122 |
} |
123 |
return jComboBox;
|
124 |
} |
125 |
|
126 |
public void setItems(long[] items) { |
127 |
ComboScaleItem[] scales = new ComboScaleItem[items.length]; |
128 |
for (int i = 0; i < items.length; i++) { |
129 |
scales[i] = new ComboScaleItem(items[i]);
|
130 |
} |
131 |
DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales); |
132 |
getJComboBox().setModel(newModel); |
133 |
} |
134 |
|
135 |
/**
|
136 |
* This funcion ONLY sets the text in combo. It will NOT call listeners.
|
137 |
*
|
138 |
* @param scale
|
139 |
*/
|
140 |
public void setScale(long item) { |
141 |
bDoCallListeners = false;
|
142 |
getJComboBox().setSelectedItem(new ComboScaleItem(item));
|
143 |
bDoCallListeners = true;
|
144 |
} |
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 |
if (!bDoCallListeners)
|
152 |
return;
|
153 |
|
154 |
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBox |
155 |
.getModel(); |
156 |
// model=new DefaultComboBoxModel();
|
157 |
boolean inserted = false; |
158 |
for (int i = 0; i < model.getSize(); i++) { |
159 |
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i); |
160 |
if (scale == itemScale.getValue()) {
|
161 |
inserted = true;
|
162 |
break;
|
163 |
} |
164 |
} |
165 |
if (!inserted) {
|
166 |
for (int i = 0; i < model.getSize(); i++) { |
167 |
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i); |
168 |
if (scale < itemScale.getValue()) {
|
169 |
model.insertElementAt(new ComboScaleItem(scale), i);
|
170 |
inserted = true;
|
171 |
break;
|
172 |
} |
173 |
} |
174 |
if (!inserted)
|
175 |
model.addElement(new ComboScaleItem(scale));
|
176 |
} |
177 |
jComboBox.setSelectedItem(new Long(scale)); |
178 |
} |
179 |
|
180 |
private void callActionCommandListeners(long scale) { |
181 |
if (!bDoCallListeners)
|
182 |
return;
|
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"
|