Revision 28501
trunk/libraries/libUIComponent/src/org/gvsig/gui/beans/simplecombobox/SimpleComboBox.java | ||
---|---|---|
1 |
package org.gvsig.gui.beans.simplecombobox; |
|
2 |
|
|
3 |
import javax.swing.JComboBox; |
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
/** |
|
8 |
* <p>Simple ComboBox component to add items with an associated code. |
|
9 |
* Typical usage would be adding Strings items and associated action |
|
10 |
* codes. Example</p> |
|
11 |
* <pre>SimpleComboBox combo = new SimpleComboBox(); |
|
12 |
* combo.addItem("Left", TextComponent.ALIGN_LEFT); |
|
13 |
* combo.addItem("Right", TextComponent.ALIGN_RIGHT; |
|
14 |
* combo.addItem("Center", TextComponent.ALIGN_CENTER); |
|
15 |
* combo.addItem("Justify", TextComponent.ALIGN_JUSTIFY); |
|
16 |
* . . . |
|
17 |
* int textAlign = combo.getSelectedCode(); |
|
18 |
* textComponent.align(textAlign); |
|
19 |
* </pre> |
|
20 |
* <p>This component provides a basic selection autocompletion when the user |
|
21 |
* presses a key (while the combo has the focus).</p> |
|
22 |
* |
|
23 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> |
|
24 |
* |
|
25 |
*/ |
|
26 |
public class SimpleComboBox extends JComboBox { |
|
27 |
private static final long serialVersionUID = 5805274299238455334L; |
|
28 |
|
|
29 |
public SimpleComboBox() { |
|
30 |
super(); |
|
31 |
} |
|
32 |
|
|
33 |
/** |
|
34 |
* <p>Invalid method, do not use it. Use |
|
35 |
* {@link #addItem(Object, int)} instead.</p> |
|
36 |
*/ |
|
37 |
public void addItem(Object anObject) { |
|
38 |
throw new RuntimeException("Invalid method: use addItem(Object anObject, int code) instead or regular Swing JComboBox"); |
|
39 |
} |
|
40 |
|
|
41 |
public void addItem(Object anObject, int code) { |
|
42 |
ComboItem item = new ComboItem(anObject, code); |
|
43 |
super.addItem(item); |
|
44 |
} |
|
45 |
|
|
46 |
public Object getItemAt(int index) { |
|
47 |
ComboItem item = (ComboItem) super.getItemAt(index); |
|
48 |
if (item!=null) { |
|
49 |
return item.getObject(); |
|
50 |
} |
|
51 |
return null; |
|
52 |
} |
|
53 |
|
|
54 |
public Object getSelectedItem() { |
|
55 |
ComboItem item = (ComboItem) super.getSelectedItem(); |
|
56 |
if (item!=null) { |
|
57 |
return item.getObject(); |
|
58 |
} |
|
59 |
return null; |
|
60 |
} |
|
61 |
|
|
62 |
public void setSelectedIndex(int anIndex) { |
|
63 |
int size = dataModel.getSize(); |
|
64 |
if ( anIndex == -1 ) { |
|
65 |
setSelectedItem( null ); |
|
66 |
} else if ( anIndex < -1 || anIndex >= size ) { |
|
67 |
throw new IllegalArgumentException("setSelectedIndex: " + anIndex + " out of bounds"); |
|
68 |
} else { |
|
69 |
Object item = dataModel.getElementAt(anIndex); |
|
70 |
super.setSelectedItem(item); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
public void setSelectedItem(Object anObject) { |
|
75 |
ComboItem item; |
|
76 |
for (int i=0; i<getItemCount(); i++) { |
|
77 |
item = (ComboItem) super.getItemAt(i); |
|
78 |
if (item.getObject().equals(anObject)) { |
|
79 |
super.setSelectedItem(item); |
|
80 |
} |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
public void setSelectedCode(int code) { |
|
85 |
ComboItem item; |
|
86 |
for (int i=0; i<getItemCount(); i++) { |
|
87 |
item = (ComboItem) super.getItemAt(i); |
|
88 |
if (item.getCode()==code) { |
|
89 |
super.setSelectedItem(item); |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
public Object[] getSelectedObjects() { |
|
95 |
Object obj = getSelectedItem(); |
|
96 |
if (obj!=null) { |
|
97 |
return new Object[]{obj}; |
|
98 |
} |
|
99 |
else { |
|
100 |
return new Object[0]; |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
/** |
|
105 |
* Gets the code associated to the selected item |
|
106 |
* @return |
|
107 |
*/ |
|
108 |
public int getSelectedCode() { |
|
109 |
ComboItem item = (ComboItem) super.getSelectedItem(); |
|
110 |
if (item!=null) { |
|
111 |
return item.getCode(); |
|
112 |
} |
|
113 |
return -1; |
|
114 |
} |
|
115 |
|
|
116 |
private class ComboItem { |
|
117 |
private Object theObject; |
|
118 |
private int code; |
|
119 |
|
|
120 |
public ComboItem(Object object, int code) { |
|
121 |
this.theObject = object; |
|
122 |
this.code = code; |
|
123 |
} |
|
124 |
|
|
125 |
public void setObject(Object theObject) { |
|
126 |
this.theObject = theObject; |
|
127 |
} |
|
128 |
public Object getObject() { |
|
129 |
return theObject; |
|
130 |
} |
|
131 |
public void setCode(int code) { |
|
132 |
this.code = code; |
|
133 |
} |
|
134 |
public int getCode() { |
|
135 |
return code; |
|
136 |
} |
|
137 |
|
|
138 |
public String toString() { |
|
139 |
return theObject.toString(); |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
0 | 143 |
Also available in: Unified diff