Revision 1424
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.api/src/main/java/org/gvsig/tools/swing/api/ToolsSwingManager.java | ||
---|---|---|
1 | 1 |
|
2 | 2 |
package org.gvsig.tools.swing.api; |
3 | 3 |
|
4 |
import javax.swing.ComboBoxModel; |
|
4 | 5 |
import javax.swing.JComboBox; |
5 | 6 |
import javax.swing.JList; |
6 | 7 |
import javax.swing.tree.TreeModel; |
... | ... | |
13 | 14 |
public JListWithCheckbox createJListWithCheckbox(JList wrappedList); |
14 | 15 |
|
15 | 16 |
public void setTreeModel(JComboBox comboBox, TreeModel aTreeModel); |
16 |
|
|
17 |
|
|
18 |
public ComboBoxModel createComboBoxModel(TreeModel treeModel); |
|
17 | 19 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/TreeComboUtils.java | ||
---|---|---|
1 | 1 |
package org.gvsig.tools.swing.impl; |
2 | 2 |
|
3 |
import java.awt.Component; |
|
4 | 3 |
import java.util.ArrayList; |
5 | 4 |
import java.util.List; |
6 | 5 |
import javax.swing.AbstractListModel; |
7 | 6 |
import javax.swing.ComboBoxModel; |
8 |
import javax.swing.Icon; |
|
9 | 7 |
import javax.swing.JComboBox; |
10 |
import javax.swing.JLabel; |
|
11 |
import javax.swing.JList; |
|
12 |
import javax.swing.ListCellRenderer; |
|
13 |
import javax.swing.UIManager; |
|
14 |
import javax.swing.border.Border; |
|
15 |
import javax.swing.border.EmptyBorder; |
|
16 | 8 |
import javax.swing.event.TreeModelEvent; |
17 | 9 |
import javax.swing.event.TreeModelListener; |
18 | 10 |
import javax.swing.tree.DefaultMutableTreeNode; |
... | ... | |
20 | 12 |
|
21 | 13 |
public class TreeComboUtils { |
22 | 14 |
|
23 |
static final int OFFSET = 16; |
|
24 |
|
|
25 | 15 |
public static void setTreeModel(JComboBox comboBox, TreeModel aTreeModel) { |
26 |
comboBox.setModel(new TreeToListModel(comboBox, aTreeModel)); |
|
27 |
//comboBox.setRenderer(new ListEntryRenderer()); |
|
16 |
ComboBoxModel model = createComboBoxModel(aTreeModel); |
|
17 |
comboBox.setModel(model); |
|
18 |
comboBox.setRenderer(new TreeComboCellRenderer(model)); |
|
28 | 19 |
} |
29 | 20 |
|
30 |
public static class TreeToListModel extends AbstractListModel implements ComboBoxModel, TreeModelListener { |
|
21 |
public static ComboBoxModel createComboBoxModel(TreeModel aTreeModel) { |
|
22 |
return new TreeModelToComboBoxModelAdapter(aTreeModel); |
|
23 |
} |
|
31 | 24 |
|
25 |
public static class TreeModelToComboBoxModelAdapter extends AbstractListModel implements ComboBoxModel, TreeModelListener { |
|
26 |
|
|
27 |
private static final long serialVersionUID = 922222366298284635L; |
|
28 |
|
|
32 | 29 |
TreeModel source; |
33 | 30 |
boolean invalid = true; |
34 |
ListEntry currentValue;
|
|
35 |
List<ListEntry> cache = new ArrayList<>();
|
|
31 |
DefaultMutableTreeNode currentValue;
|
|
32 |
List<DefaultMutableTreeNode> cache = new ArrayList<>();
|
|
36 | 33 |
|
37 | 34 |
@SuppressWarnings("LeakingThisInConstructor") |
38 |
public TreeToListModel(JComboBox comboBox, TreeModel aTreeModel) {
|
|
35 |
public TreeModelToComboBoxModelAdapter(TreeModel aTreeModel) {
|
|
39 | 36 |
source = aTreeModel; |
40 | 37 |
aTreeModel.addTreeModelListener(this); |
41 |
comboBox.setRenderer(new ListEntryRenderer()); |
|
42 | 38 |
} |
43 | 39 |
|
44 | 40 |
@Override |
45 | 41 |
public void setSelectedItem(Object anObject) { |
46 | 42 |
if ((currentValue != null && !currentValue.equals(anObject)) |
47 | 43 |
|| currentValue == null && anObject != null) { |
48 |
currentValue = this.getEntry(anObject);
|
|
44 |
currentValue = this.getNode(anObject);
|
|
49 | 45 |
} |
50 | 46 |
fireContentsChanged(this, -1, -1); |
51 | 47 |
} |
... | ... | |
55 | 51 |
if (currentValue == null) { |
56 | 52 |
return null; |
57 | 53 |
} |
58 |
return currentValue.object();
|
|
54 |
return currentValue.getUserObject();
|
|
59 | 55 |
} |
60 | 56 |
|
61 | 57 |
@Override |
... | ... | |
66 | 62 |
|
67 | 63 |
@Override |
68 | 64 |
public Object getElementAt(int index) { |
69 |
return cache.get(index).object();
|
|
65 |
return cache.get(index).getUserObject();
|
|
70 | 66 |
} |
71 | 67 |
|
72 |
public ListEntry getEntry(int index) {
|
|
68 |
public DefaultMutableTreeNode getNode(int index) {
|
|
73 | 69 |
return cache.get(index); |
74 | 70 |
} |
75 | 71 |
|
76 |
public ListEntry getEntry(Object anObject) {
|
|
77 |
for (ListEntry entry : cache) {
|
|
78 |
if (entry.object().equals(anObject)) {
|
|
79 |
return entry;
|
|
72 |
public DefaultMutableTreeNode getNode(Object anObject) {
|
|
73 |
for (DefaultMutableTreeNode node : cache) {
|
|
74 |
if (node.getUserObject().equals(anObject)) {
|
|
75 |
return node;
|
|
80 | 76 |
} |
81 | 77 |
} |
82 | 78 |
return null; |
... | ... | |
116 | 112 |
|
117 | 113 |
void cacheTree(Object anObject, int level) { |
118 | 114 |
if (source.isLeaf(anObject)) { |
119 |
addListEntry(anObject, level, false);
|
|
115 |
addNode(anObject, level, false);
|
|
120 | 116 |
} else { |
121 | 117 |
int c = source.getChildCount(anObject); |
122 | 118 |
int i; |
123 | 119 |
Object child; |
124 | 120 |
|
125 |
addListEntry(anObject, level, true);
|
|
121 |
addNode(anObject, level, true);
|
|
126 | 122 |
level++; |
127 | 123 |
|
128 | 124 |
for (i = 0; i < c; i++) { |
... | ... | |
134 | 130 |
} |
135 | 131 |
} |
136 | 132 |
|
137 |
void addListEntry(Object anObject, int level, boolean isNode) {
|
|
133 |
void addNode(Object anObject, int level, boolean isNode) {
|
|
138 | 134 |
cache.add(new ListEntry(anObject, level, isNode)); |
139 | 135 |
} |
140 | 136 |
|
141 |
static Border emptyBorder = new EmptyBorder(0, 0, 0, 0);
|
|
137 |
private static class ListEntry extends DefaultMutableTreeNode {
|
|
142 | 138 |
|
143 |
class ListEntryRenderer extends JLabel implements ListCellRenderer {
|
|
139 |
private static final long serialVersionUID = 304579876188493086L;
|
|
144 | 140 |
|
145 |
Icon leafIcon = (Icon) UIManager.get("Tree.leafIcon"); |
|
146 |
Icon nodeIcon = (Icon) UIManager.get("Tree.openIcon"); |
|
141 |
int level; |
|
147 | 142 |
|
148 |
public ListEntryRenderer() { |
|
149 |
this.setOpaque(true); |
|
143 |
public ListEntry(Object anObject, int aLevel, boolean isNode) { |
|
144 |
super(anObject, isNode); |
|
145 |
this.setUserObject(anObject); |
|
146 |
level = aLevel; |
|
150 | 147 |
} |
151 | 148 |
|
152 | 149 |
@Override |
153 |
public Component getListCellRendererComponent( |
|
154 |
JList listbox, |
|
155 |
Object value, |
|
156 |
int index, |
|
157 |
boolean isSelected, |
|
158 |
boolean cellHasFocus) { |
|
159 |
ListEntry listEntry = getEntry(value); |
|
160 |
if (listEntry != null) { |
|
161 |
Border border; |
|
162 |
setText(listEntry.object().toString()); |
|
163 |
setIcon(listEntry.isNode() ? nodeIcon : leafIcon); |
|
164 |
if (index != -1) { |
|
165 |
border = new EmptyBorder(0, OFFSET * listEntry.level(), 0, 0); |
|
166 |
} else { |
|
167 |
border = emptyBorder; |
|
168 |
} |
|
169 |
|
|
170 |
if (UIManager.getLookAndFeel().getName().equals("CDE/Motif")) { |
|
171 |
if (index == -1) { |
|
172 |
this.setOpaque(false); |
|
173 |
} else { |
|
174 |
this.setOpaque(true); |
|
175 |
} |
|
176 |
} else { |
|
177 |
this.setOpaque(true); |
|
178 |
} |
|
179 |
|
|
180 |
this.setBorder(border); |
|
181 |
if (isSelected) { |
|
182 |
this.setBackground(UIManager.getColor("ComboBox.selectionBackground")); |
|
183 |
this.setForeground(UIManager.getColor("ComboBox.selectionForeground")); |
|
184 |
} else { |
|
185 |
this.setBackground(UIManager.getColor("ComboBox.background")); |
|
186 |
this.setForeground(UIManager.getColor("ComboBox.foreground")); |
|
187 |
} |
|
188 |
} else { |
|
189 |
setText(""); |
|
190 |
} |
|
191 |
return this; |
|
150 |
public int getLevel() { |
|
151 |
return level; |
|
192 | 152 |
} |
193 |
} |
|
194 |
} |
|
195 | 153 |
|
196 |
public static class ListEntry extends DefaultMutableTreeNode { |
|
197 |
|
|
198 |
Object object; |
|
199 |
int level; |
|
200 |
boolean isNode; |
|
201 |
|
|
202 |
public ListEntry(Object anObject, int aLevel, boolean isNode) { |
|
203 |
super(anObject, isNode); |
|
204 |
object = anObject; |
|
205 |
level = aLevel; |
|
206 |
this.isNode = isNode; |
|
207 | 154 |
} |
208 |
|
|
209 |
public Object object() { |
|
210 |
return object; |
|
211 |
} |
|
212 |
|
|
213 |
public int level() { |
|
214 |
return level; |
|
215 |
} |
|
216 |
|
|
217 |
public boolean isNode() { |
|
218 |
return isNode; |
|
219 |
} |
|
220 | 155 |
} |
221 |
|
|
222 | 156 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/TreeComboCellRenderer.java | ||
---|---|---|
1 |
package org.gvsig.tools.swing.impl; |
|
2 |
|
|
3 |
import java.awt.Component; |
|
4 |
import javax.swing.ComboBoxModel; |
|
5 |
import javax.swing.Icon; |
|
6 |
import javax.swing.JLabel; |
|
7 |
import javax.swing.JList; |
|
8 |
import javax.swing.ListCellRenderer; |
|
9 |
import javax.swing.UIManager; |
|
10 |
import javax.swing.border.Border; |
|
11 |
import javax.swing.border.EmptyBorder; |
|
12 |
import javax.swing.tree.DefaultMutableTreeNode; |
|
13 |
import org.gvsig.tools.swing.impl.TreeComboUtils.TreeModelToComboBoxModelAdapter; |
|
14 |
|
|
15 |
public class TreeComboCellRenderer extends JLabel implements ListCellRenderer { |
|
16 |
|
|
17 |
private static final long serialVersionUID = 3732251042879578480L; |
|
18 |
private static final Border emptyBorder = new EmptyBorder(0, 0, 0, 0); |
|
19 |
private static final int OFFSET = 16; |
|
20 |
|
|
21 |
private final Icon leafIcon = (Icon) UIManager.get("Tree.leafIcon"); |
|
22 |
private final Icon nodeIcon = (Icon) UIManager.get("Tree.openIcon"); |
|
23 |
private final TreeModelToComboBoxModelAdapter model; |
|
24 |
|
|
25 |
public TreeComboCellRenderer(ComboBoxModel model) { |
|
26 |
this.setOpaque(true); |
|
27 |
this.model = (TreeModelToComboBoxModelAdapter) model; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public Component getListCellRendererComponent( |
|
32 |
JList listbox, |
|
33 |
Object value, |
|
34 |
int index, |
|
35 |
boolean isSelected, |
|
36 |
boolean cellHasFocus) { |
|
37 |
DefaultMutableTreeNode entry = model.getNode(value); |
|
38 |
if (entry != null) { |
|
39 |
Border border; |
|
40 |
setText(getCellText(entry)); |
|
41 |
setIcon(getCellIcon(entry)); |
|
42 |
if (index != -1) { |
|
43 |
border = new EmptyBorder(0, OFFSET * entry.getLevel(), 0, 0); |
|
44 |
} else { |
|
45 |
border = emptyBorder; |
|
46 |
} |
|
47 |
|
|
48 |
if (UIManager.getLookAndFeel().getName().equals("CDE/Motif")) { |
|
49 |
if (index == -1) { |
|
50 |
this.setOpaque(false); |
|
51 |
} else { |
|
52 |
this.setOpaque(true); |
|
53 |
} |
|
54 |
} else { |
|
55 |
this.setOpaque(true); |
|
56 |
} |
|
57 |
|
|
58 |
this.setBorder(border); |
|
59 |
if (isSelected) { |
|
60 |
this.setBackground(UIManager.getColor("ComboBox.selectionBackground")); |
|
61 |
this.setForeground(UIManager.getColor("ComboBox.selectionForeground")); |
|
62 |
} else { |
|
63 |
this.setBackground(UIManager.getColor("ComboBox.background")); |
|
64 |
this.setForeground(UIManager.getColor("ComboBox.foreground")); |
|
65 |
} |
|
66 |
} else { |
|
67 |
setText(""); |
|
68 |
} |
|
69 |
return this; |
|
70 |
} |
|
71 |
|
|
72 |
public Icon getCellIcon(DefaultMutableTreeNode node) { |
|
73 |
return node.getAllowsChildren() ? nodeIcon : leafIcon; |
|
74 |
} |
|
75 |
|
|
76 |
public String getCellText(DefaultMutableTreeNode node) { |
|
77 |
return node.getUserObject().toString(); |
|
78 |
} |
|
79 |
} |
org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultToolsSwingManager.java | ||
---|---|---|
1 | 1 |
|
2 | 2 |
package org.gvsig.tools.swing.impl; |
3 | 3 |
|
4 |
import javax.swing.ComboBoxModel; |
|
4 | 5 |
import javax.swing.JComboBox; |
5 | 6 |
import javax.swing.JList; |
6 | 7 |
import javax.swing.tree.TreeModel; |
... | ... | |
26 | 27 |
TreeComboUtils.setTreeModel(comboBox, aTreeModel); |
27 | 28 |
} |
28 | 29 |
|
30 |
public ComboBoxModel createComboBoxModel(TreeModel treeModel) { |
|
31 |
return TreeComboUtils.createComboBoxModel(treeModel); |
|
32 |
} |
|
29 | 33 |
|
30 | 34 |
} |
Also available in: Unified diff