1 |
|
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
|
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
2 |
2 |
*
|
3 |
|
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
|
*
|
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
|
4 |
* of the Valencian Government (CIT)
|
|
5 |
*
|
5 |
6 |
* This program is free software; you can redistribute it and/or
|
6 |
7 |
* modify it under the terms of the GNU General Public License
|
7 |
8 |
* as published by the Free Software Foundation; either version 2
|
8 |
9 |
* of the License, or (at your option) any later version.
|
9 |
|
*
|
|
10 |
*
|
10 |
11 |
* This program is distributed in the hope that it will be useful,
|
11 |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
14 |
* GNU General Public License for more details.
|
14 |
|
*
|
|
15 |
*
|
15 |
16 |
* You should have received a copy of the GNU General Public License
|
16 |
17 |
* along with this program; if not, write to the Free Software
|
17 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
19 |
* MA 02110-1301, USA.
|
|
20 |
*
|
18 |
21 |
*/
|
19 |
22 |
package org.gvsig.gui.beans.controls.comboscale;
|
20 |
23 |
|
... | ... | |
25 |
28 |
import java.util.ArrayList;
|
26 |
29 |
import java.util.BitSet;
|
27 |
30 |
import java.util.Iterator;
|
|
31 |
import java.util.List;
|
28 |
32 |
|
29 |
33 |
import javax.swing.DefaultComboBoxModel;
|
30 |
34 |
import javax.swing.JComboBox;
|
... | ... | |
34 |
38 |
import org.gvsig.gui.beans.controls.IControl;
|
35 |
39 |
|
36 |
40 |
public class ComboScale extends JPanel implements IControl {
|
37 |
|
private static final long serialVersionUID = 6483498713300082876L;
|
38 |
41 |
|
39 |
|
private JLabel jLabel = null;
|
|
42 |
private static final long serialVersionUID = 6483498713300082876L;
|
40 |
43 |
|
41 |
|
private JComboBox jComboBox = null;
|
|
44 |
private JLabel jLabel = null;
|
42 |
45 |
|
43 |
|
private ArrayList actionCommandListeners = new ArrayList();
|
|
46 |
private JComboBox jComboBox = null;
|
44 |
47 |
|
45 |
|
// private Long[] scales;
|
46 |
|
private boolean bDoCallListeners = true;
|
|
48 |
private List<ActionListener> actionCommandListeners =
|
|
49 |
new ArrayList<ActionListener>();
|
47 |
50 |
|
48 |
|
private boolean isScaleCombo;
|
|
51 |
private boolean bDoCallListeners = true;
|
49 |
52 |
|
50 |
|
static private int eventId = Integer.MIN_VALUE;
|
|
53 |
private boolean isScaleCombo;
|
51 |
54 |
|
52 |
|
// jaume
|
53 |
|
private class ComboScaleItem {
|
54 |
|
private long value;
|
|
55 |
static private int eventId = Integer.MIN_VALUE;
|
55 |
56 |
|
56 |
|
public ComboScaleItem(long itemScale) {
|
57 |
|
this.value = itemScale;
|
58 |
|
}
|
|
57 |
// jaume
|
|
58 |
private class ComboScaleItem {
|
59 |
59 |
|
60 |
|
public String toString() {
|
61 |
|
return NumberFormat.getNumberInstance().format(value);
|
62 |
|
}
|
|
60 |
private long value;
|
63 |
61 |
|
64 |
|
public boolean equals(Object obj) {
|
65 |
|
return obj instanceof ComboScaleItem && ((ComboScaleItem) obj).getValue() == value;
|
66 |
|
}
|
|
62 |
public ComboScaleItem(long itemScale) {
|
|
63 |
this.value = itemScale;
|
|
64 |
}
|
67 |
65 |
|
68 |
|
public long getValue() {
|
69 |
|
return value;
|
70 |
|
}
|
71 |
|
}
|
72 |
|
/**
|
73 |
|
* This is the default constructor
|
74 |
|
*/
|
75 |
|
public ComboScale() {
|
76 |
|
super();
|
77 |
|
initialize();
|
78 |
|
}
|
|
66 |
public String toString() {
|
|
67 |
return NumberFormat.getNumberInstance().format(value);
|
|
68 |
}
|
79 |
69 |
|
80 |
|
/**
|
81 |
|
* This method initializes this
|
82 |
|
*
|
83 |
|
* @return void
|
84 |
|
*/
|
85 |
|
private void initialize() {
|
86 |
|
FlowLayout flowLayout = new FlowLayout();
|
87 |
|
flowLayout.setHgap(0);
|
88 |
|
flowLayout.setVgap(0);
|
89 |
|
jLabel = new JLabel();
|
90 |
|
jLabel.setText("1:");
|
91 |
|
this.setLayout(flowLayout);
|
92 |
|
this.setSize(155, 16);
|
93 |
|
//this.setBorder(javax.swing.BorderFactory.createLineBorder(
|
94 |
|
this.add(jLabel, null);
|
95 |
|
this.add(getJComboBox(), null);
|
96 |
|
//java.awt.Color.gray, 1));
|
97 |
|
}
|
|
70 |
public boolean equals(Object obj) {
|
|
71 |
return obj instanceof ComboScaleItem
|
|
72 |
&& ((ComboScaleItem) obj).getValue() == value;
|
|
73 |
}
|
98 |
74 |
|
99 |
|
/**
|
100 |
|
* This method initializes jComboBox
|
101 |
|
*
|
102 |
|
* @return javax.swing.JComboBox
|
103 |
|
*/
|
104 |
|
private JComboBox getJComboBox() {
|
105 |
|
if (jComboBox == null) {
|
106 |
|
jComboBox = new JComboBox();
|
107 |
|
jComboBox.setPreferredSize(new java.awt.Dimension(130, 16));
|
108 |
|
jComboBox.setEditable(true);
|
109 |
|
jComboBox.setMaximumRowCount(5);
|
110 |
|
jComboBox.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD,
|
111 |
|
10));
|
112 |
|
jComboBox.setBackground(java.awt.SystemColor.window);
|
113 |
|
jComboBox.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
|
114 |
|
jComboBox.addActionListener(new java.awt.event.ActionListener() {
|
115 |
|
public void actionPerformed(java.awt.event.ActionEvent e) {
|
116 |
|
if (e.getActionCommand().equals("comboBoxChanged")) {
|
|
75 |
public long getValue() {
|
|
76 |
return value;
|
|
77 |
}
|
|
78 |
}
|
117 |
79 |
|
118 |
|
// callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
|
119 |
|
// setScale(((Long)jComboBox.getSelectedItem()).longValue());
|
120 |
|
Object item = jComboBox.getSelectedItem();
|
121 |
|
long scale=0;
|
122 |
|
if (item instanceof String) {
|
123 |
|
StringBuffer sb = new StringBuffer((String) item);
|
124 |
|
// remove any point in the number
|
125 |
|
final String digits = "0123456789";
|
126 |
|
int i = sb.charAt(0) == '-' ? 1 : 0;
|
127 |
|
BitSet deleteChars=new BitSet();
|
128 |
|
while (i < sb.length()) {
|
129 |
|
if (digits.indexOf(sb.charAt(i))==-1)
|
130 |
|
deleteChars.set(i);
|
131 |
|
i++;
|
132 |
|
}
|
133 |
|
for (int k=deleteChars.size();k>=0;k--){
|
134 |
|
if (deleteChars.get(k))
|
135 |
|
sb.deleteCharAt(k);
|
136 |
|
}
|
137 |
|
jComboBox.removeItem(item);
|
138 |
|
try{
|
139 |
|
scale = Long.parseLong(sb.toString());
|
140 |
|
}catch (NumberFormatException e1) {
|
141 |
|
}
|
142 |
|
} else {
|
143 |
|
scale = ((ComboScaleItem) jComboBox.getSelectedItem())
|
144 |
|
.getValue();
|
145 |
|
}
|
146 |
|
insertScaleIfNotPresent(scale);
|
147 |
|
callActionCommandListeners(scale);
|
148 |
|
}
|
149 |
|
}
|
150 |
|
});
|
151 |
|
}
|
152 |
|
return jComboBox;
|
153 |
|
}
|
|
80 |
/**
|
|
81 |
* This is the default constructor
|
|
82 |
*/
|
|
83 |
public ComboScale() {
|
|
84 |
super();
|
|
85 |
initialize();
|
|
86 |
}
|
154 |
87 |
|
155 |
|
public void setItems(long[] items) {
|
156 |
|
ComboScaleItem[] scales = new ComboScaleItem[items.length];
|
157 |
|
for (int i = 0; i < items.length; i++) {
|
158 |
|
scales[i] = new ComboScaleItem(items[i]);
|
159 |
|
}
|
160 |
|
DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales);
|
161 |
|
getJComboBox().setModel(newModel);
|
162 |
|
}
|
|
88 |
/**
|
|
89 |
* This method initializes this
|
|
90 |
*
|
|
91 |
* @return void
|
|
92 |
*/
|
|
93 |
private void initialize() {
|
|
94 |
FlowLayout flowLayout = new FlowLayout();
|
|
95 |
flowLayout.setHgap(0);
|
|
96 |
flowLayout.setVgap(0);
|
|
97 |
jLabel = new JLabel();
|
|
98 |
jLabel.setText("1:");
|
|
99 |
this.setLayout(flowLayout);
|
|
100 |
this.setSize(155, 16);
|
|
101 |
// this.setBorder(javax.swing.BorderFactory.createLineBorder(
|
|
102 |
this.add(jLabel, null);
|
|
103 |
this.add(getJComboBox(), null);
|
|
104 |
// java.awt.Color.gray, 1));
|
|
105 |
}
|
163 |
106 |
|
164 |
|
/**
|
165 |
|
* This funcion ONLY sets the text in combo. It will NOT call listeners.
|
166 |
|
*
|
167 |
|
* @param scale
|
168 |
|
*/
|
169 |
|
public void setScale(long item) {
|
170 |
|
bDoCallListeners = false;
|
171 |
|
getJComboBox().setSelectedItem(new ComboScaleItem(item));
|
172 |
|
bDoCallListeners = true;
|
173 |
|
}
|
|
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.setPreferredSize(new java.awt.Dimension(130, 16));
|
|
116 |
jComboBox.setEditable(true);
|
|
117 |
jComboBox.setMaximumRowCount(5);
|
|
118 |
jComboBox.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD,
|
|
119 |
10));
|
|
120 |
jComboBox.setBackground(java.awt.SystemColor.window);
|
|
121 |
jComboBox
|
|
122 |
.setComponentOrientation(java.awt.ComponentOrientation.LEFT_TO_RIGHT);
|
|
123 |
jComboBox.addActionListener(new java.awt.event.ActionListener() {
|
174 |
124 |
|
175 |
|
/**
|
176 |
|
* @param scale
|
177 |
|
*/
|
178 |
|
private void insertScaleIfNotPresent(long scale) {
|
179 |
|
// Si viene de un setScale, no insertamos la escala en el combo
|
180 |
|
if (!bDoCallListeners)
|
181 |
|
return;
|
|
125 |
public void actionPerformed(java.awt.event.ActionEvent e) {
|
|
126 |
if (e.getActionCommand().equals("comboBoxChanged")) {
|
182 |
127 |
|
183 |
|
DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBox
|
184 |
|
.getModel();
|
185 |
|
// model=new DefaultComboBoxModel();
|
186 |
|
boolean inserted = false;
|
187 |
|
for (int i = 0; i < model.getSize(); i++) {
|
188 |
|
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
|
189 |
|
if (scale == itemScale.getValue()) {
|
190 |
|
inserted = true;
|
191 |
|
break;
|
192 |
|
}
|
193 |
|
}
|
194 |
|
if (!inserted) {
|
195 |
|
for (int i = 0; i < model.getSize(); i++) {
|
196 |
|
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
|
197 |
|
if (scale < itemScale.getValue()) {
|
198 |
|
model.insertElementAt(new ComboScaleItem(scale), i);
|
199 |
|
inserted = true;
|
200 |
|
break;
|
201 |
|
}
|
202 |
|
}
|
203 |
|
if (!inserted)
|
204 |
|
model.addElement(new ComboScaleItem(scale));
|
205 |
|
}
|
206 |
|
jComboBox.setSelectedItem(new ComboScaleItem(scale));
|
207 |
|
isScaleCombo=true;
|
208 |
|
}
|
|
128 |
// callActionCommandListeners(((Long)jComboBox.getSelectedItem()).longValue());
|
|
129 |
// setScale(((Long)jComboBox.getSelectedItem()).longValue());
|
|
130 |
Object item = jComboBox.getSelectedItem();
|
|
131 |
long scale = 0;
|
|
132 |
if (item instanceof String) {
|
|
133 |
StringBuffer sb = new StringBuffer((String) item);
|
|
134 |
// remove any point in the number
|
|
135 |
final String digits = "0123456789";
|
|
136 |
int i = sb.charAt(0) == '-' ? 1 : 0;
|
|
137 |
BitSet deleteChars = new BitSet();
|
|
138 |
while (i < sb.length()) {
|
|
139 |
if (digits.indexOf(sb.charAt(i)) == -1)
|
|
140 |
deleteChars.set(i);
|
|
141 |
i++;
|
|
142 |
}
|
|
143 |
for (int k = deleteChars.size(); k >= 0; k--) {
|
|
144 |
if (deleteChars.get(k))
|
|
145 |
sb.deleteCharAt(k);
|
|
146 |
}
|
|
147 |
jComboBox.removeItem(item);
|
|
148 |
try {
|
|
149 |
scale = Long.parseLong(sb.toString());
|
|
150 |
} catch (NumberFormatException e1) {
|
|
151 |
}
|
|
152 |
} else {
|
|
153 |
scale =
|
|
154 |
((ComboScaleItem) jComboBox.getSelectedItem())
|
|
155 |
.getValue();
|
|
156 |
}
|
|
157 |
insertScaleIfNotPresent(scale);
|
|
158 |
callActionCommandListeners(scale);
|
|
159 |
}
|
|
160 |
}
|
|
161 |
});
|
|
162 |
}
|
|
163 |
return jComboBox;
|
|
164 |
}
|
209 |
165 |
|
210 |
|
private void callActionCommandListeners(long scale) {
|
211 |
|
if (!bDoCallListeners)
|
212 |
|
return;
|
|
166 |
public void setItems(long[] items) {
|
|
167 |
ComboScaleItem[] scales = new ComboScaleItem[items.length];
|
|
168 |
for (int i = 0; i < items.length; i++) {
|
|
169 |
scales[i] = new ComboScaleItem(items[i]);
|
|
170 |
}
|
|
171 |
DefaultComboBoxModel newModel = new DefaultComboBoxModel(scales);
|
|
172 |
getJComboBox().setModel(newModel);
|
|
173 |
}
|
213 |
174 |
|
214 |
|
Iterator acIterator = actionCommandListeners.iterator();
|
215 |
|
while (acIterator.hasNext()) {
|
216 |
|
ActionListener listener = (ActionListener) acIterator.next();
|
217 |
|
listener.actionPerformed(new ActionEvent(this, eventId,
|
218 |
|
"CHANGE_SCALE_" + scale));
|
219 |
|
}
|
220 |
|
eventId++;
|
221 |
|
}
|
|
175 |
/**
|
|
176 |
* This funcion ONLY sets the text in combo. It will NOT call listeners.
|
|
177 |
*
|
|
178 |
* @param scale
|
|
179 |
*/
|
|
180 |
public void setScale(long item) {
|
|
181 |
bDoCallListeners = false;
|
|
182 |
getJComboBox().setSelectedItem(new ComboScaleItem(item));
|
|
183 |
bDoCallListeners = true;
|
|
184 |
}
|
222 |
185 |
|
223 |
|
public void addActionListener(ActionListener listener) {
|
224 |
|
if (!actionCommandListeners.contains(listener))
|
225 |
|
actionCommandListeners.add(listener);
|
226 |
|
}
|
|
186 |
/**
|
|
187 |
* @param scale
|
|
188 |
*/
|
|
189 |
private void insertScaleIfNotPresent(long scale) {
|
|
190 |
// Si viene de un setScale, no insertamos la escala en el combo
|
|
191 |
if (!bDoCallListeners)
|
|
192 |
return;
|
227 |
193 |
|
228 |
|
public void removeActionListener(ActionListener listener) {
|
229 |
|
actionCommandListeners.remove(listener);
|
230 |
|
}
|
|
194 |
DefaultComboBoxModel model =
|
|
195 |
(DefaultComboBoxModel) jComboBox.getModel();
|
|
196 |
// model=new DefaultComboBoxModel();
|
|
197 |
boolean inserted = false;
|
|
198 |
for (int i = 0; i < model.getSize(); i++) {
|
|
199 |
ComboScaleItem itemScale = (ComboScaleItem) model.getElementAt(i);
|
|
200 |
if (scale == itemScale.getValue()) {
|
|
201 |
inserted = true;
|
|
202 |
break;
|
|
203 |
}
|
|
204 |
}
|
|
205 |
if (!inserted) {
|
|
206 |
for (int i = 0; i < model.getSize(); i++) {
|
|
207 |
ComboScaleItem itemScale =
|
|
208 |
(ComboScaleItem) model.getElementAt(i);
|
|
209 |
if (scale < itemScale.getValue()) {
|
|
210 |
model.insertElementAt(new ComboScaleItem(scale), i);
|
|
211 |
inserted = true;
|
|
212 |
break;
|
|
213 |
}
|
|
214 |
}
|
|
215 |
if (!inserted)
|
|
216 |
model.addElement(new ComboScaleItem(scale));
|
|
217 |
}
|
|
218 |
jComboBox.setSelectedItem(new ComboScaleItem(scale));
|
|
219 |
isScaleCombo = true;
|
|
220 |
}
|
231 |
221 |
|
232 |
|
/**
|
233 |
|
* Returns the current selected item.
|
234 |
|
*
|
235 |
|
* @return The value of the selected scale, or -1 if there was an invalid
|
236 |
|
* value (ie. not long value).
|
237 |
|
*/
|
238 |
|
public long getScale() {
|
239 |
|
return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
|
240 |
|
}
|
|
222 |
private void callActionCommandListeners(long scale) {
|
|
223 |
if (!bDoCallListeners)
|
|
224 |
return;
|
241 |
225 |
|
242 |
|
/**
|
243 |
|
* Sets the label to be displayed on the left of the combo
|
244 |
|
*/
|
245 |
|
public void setLabel(String label) {
|
246 |
|
jLabel.setText(label);
|
247 |
|
}
|
|
226 |
Iterator<ActionListener> acIterator = actionCommandListeners.iterator();
|
|
227 |
while (acIterator.hasNext()) {
|
|
228 |
ActionListener listener = acIterator.next();
|
|
229 |
listener.actionPerformed(new ActionEvent(this, eventId,
|
|
230 |
"CHANGE_SCALE_" + scale));
|
|
231 |
}
|
|
232 |
eventId++;
|
|
233 |
}
|
248 |
234 |
|
249 |
|
/**
|
250 |
|
* Gets the label
|
251 |
|
*/
|
252 |
|
public String getLabel() {
|
253 |
|
return jLabel.getText();
|
254 |
|
}
|
|
235 |
public void addActionListener(ActionListener listener) {
|
|
236 |
if (!actionCommandListeners.contains(listener))
|
|
237 |
actionCommandListeners.add(listener);
|
|
238 |
}
|
255 |
239 |
|
256 |
|
public Object setValue(Object value) {
|
257 |
|
if (isScaleCombo) {
|
258 |
|
isScaleCombo=false;
|
259 |
|
return null;
|
260 |
|
}
|
261 |
|
try {
|
262 |
|
long scale = Long.parseLong((String)value);
|
|
240 |
public void removeActionListener(ActionListener listener) {
|
|
241 |
actionCommandListeners.remove(listener);
|
|
242 |
}
|
263 |
243 |
|
264 |
|
if (scale < 0)
|
265 |
|
return null;
|
|
244 |
/**
|
|
245 |
* Returns the current selected item.
|
|
246 |
*
|
|
247 |
* @return The value of the selected scale, or -1 if there was an invalid
|
|
248 |
* value (ie. not long value).
|
|
249 |
*/
|
|
250 |
public long getScale() {
|
|
251 |
return ((ComboScaleItem) jComboBox.getSelectedItem()).getValue();
|
|
252 |
}
|
266 |
253 |
|
267 |
|
ComboScaleItem item = new ComboScaleItem(scale);
|
268 |
|
if (item.equals(jComboBox.getSelectedItem()))
|
269 |
|
return item;
|
270 |
|
this.setScale(scale);
|
271 |
|
return item;
|
272 |
|
} catch (NumberFormatException ex) {
|
273 |
|
// don't change the status if the provided value was not valid
|
274 |
|
return null;
|
275 |
|
}
|
276 |
|
}
|
|
254 |
/**
|
|
255 |
* Sets the label to be displayed on the left of the combo
|
|
256 |
*/
|
|
257 |
public void setLabel(String label) {
|
|
258 |
jLabel.setText(label);
|
|
259 |
}
|
277 |
260 |
|
|
261 |
/**
|
|
262 |
* Gets the label
|
|
263 |
*/
|
|
264 |
public String getLabel() {
|
|
265 |
return jLabel.getText();
|
|
266 |
}
|
278 |
267 |
|
279 |
|
public void setEnabled(boolean enabled) {
|
|
268 |
public Object setValue(Object value) {
|
|
269 |
if (isScaleCombo) {
|
|
270 |
isScaleCombo = false;
|
|
271 |
return null;
|
|
272 |
}
|
|
273 |
try {
|
|
274 |
long scale = Long.parseLong((String) value);
|
|
275 |
|
|
276 |
if (scale < 0)
|
|
277 |
return null;
|
|
278 |
|
|
279 |
ComboScaleItem item = new ComboScaleItem(scale);
|
|
280 |
if (item.equals(jComboBox.getSelectedItem()))
|
|
281 |
return item;
|
|
282 |
this.setScale(scale);
|
|
283 |
return item;
|
|
284 |
} catch (NumberFormatException ex) {
|
|
285 |
// don't change the status if the provided value was not valid
|
|
286 |
return null;
|
|
287 |
}
|
|
288 |
}
|
|
289 |
|
|
290 |
public void setEnabled(boolean enabled) {
|
280 |
291 |
boolean oldEnabled = jComboBox.isEnabled();
|
281 |
292 |
jComboBox.setEnabled(enabled);
|
282 |
293 |
jComboBox.firePropertyChange("enabled", oldEnabled, enabled);
|