svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / searchpanel / AdvancedAttributeSelectionTreeModel.java @ 44263
History | View | Annotate | Download (6.8 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.searchpanel; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
5 |
import java.util.HashSet; |
6 |
import java.util.List; |
7 |
import java.util.Set; |
8 |
import java.util.function.Predicate; |
9 |
import javax.swing.event.TreeModelListener; |
10 |
import javax.swing.tree.TreeModel; |
11 |
import javax.swing.tree.TreePath; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.fmap.dal.complements.Search; |
14 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
16 |
import org.gvsig.fmap.dal.feature.FeatureType; |
17 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
18 |
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey; |
19 |
import org.gvsig.tools.ToolsLocator; |
20 |
import org.gvsig.tools.util.LabeledValue; |
21 |
import org.gvsig.tools.util.LabeledValueImpl; |
22 |
|
23 |
/**
|
24 |
*
|
25 |
* @author jjdelcerro
|
26 |
*/
|
27 |
@SuppressWarnings("UseSpecificCatch") |
28 |
public class AdvancedAttributeSelectionTreeModel |
29 |
implements TreeModel { |
30 |
|
31 |
public interface Node extends LabeledValue<FeatureAttributeDescriptor> { |
32 |
|
33 |
public FeatureStore getFeatureStore();
|
34 |
public boolean isRoot(); |
35 |
public boolean isLeaf(); |
36 |
public List<Node> getChildren(); |
37 |
} |
38 |
|
39 |
private class DefaultNode |
40 |
extends LabeledValueImpl<FeatureAttributeDescriptor>
|
41 |
implements Node {
|
42 |
|
43 |
private List<Node> childs; |
44 |
private final FeatureStore store; |
45 |
|
46 |
public DefaultNode(FeatureStore store, List<FeatureAttributeDescriptor> attributes) { |
47 |
super(store==null? "":store.getName(), null); |
48 |
this.store = store;
|
49 |
this.childs = new ArrayList<>(); |
50 |
for (FeatureAttributeDescriptor attribute : attributes) {
|
51 |
this.childs.add(new DefaultNode(store, attribute)); |
52 |
} |
53 |
} |
54 |
|
55 |
private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute) {
|
56 |
super(attribute.getLabel(), attribute);
|
57 |
this.store = store;
|
58 |
this.childs = null; |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public String getLabel() { |
63 |
FeatureAttributeDescriptor attrdesc = this.getValue();
|
64 |
if( attrdesc!=null && attrdesc.isForeingKey() ) { |
65 |
ForeingKey foreingKey = attrdesc.getForeingKey(); |
66 |
return String.format("%s [%s]", super.getLabel(), foreingKey.getTableName()); |
67 |
} |
68 |
return super.getLabel(); |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public FeatureStore getFeatureStore() {
|
73 |
return this.store; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public boolean isRoot() { |
78 |
return this.getValue() == null; |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public List<Node> getChildren() { |
83 |
if (this.childs == null) { |
84 |
if (this.getValue().isForeingKey()) { |
85 |
ForeingKey foreingKey = this.getValue().getForeingKey();
|
86 |
ContextForeingKey context = foreingKey.createContext(); |
87 |
// Ojo, no liberamos el contexto para que no se destruya el store.
|
88 |
FeatureStore theStore = foreingKey.getFeatureStore(context); |
89 |
FeatureType featureType = foreingKey.getFeatureType(context); |
90 |
String fullName = theStore.getFullName();
|
91 |
if (stores.contains(fullName)) {
|
92 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
|
93 |
this.childs = Collections.EMPTY_LIST; |
94 |
} else {
|
95 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
96 |
Search.COMPLEMENT_MANE, featureType |
97 |
); |
98 |
List<FeatureAttributeDescriptor> attributes = featureTypeSearch.getOrderedAttributes(
|
99 |
filterByDataType, |
100 |
Search.LABEL_ORDER, |
101 |
-1
|
102 |
); |
103 |
this.childs = new ArrayList<>(); |
104 |
for (FeatureAttributeDescriptor attribute : attributes) {
|
105 |
this.childs.add(new DefaultNode(theStore, attribute)); |
106 |
} |
107 |
} |
108 |
} else {
|
109 |
this.childs = Collections.EMPTY_LIST; |
110 |
} |
111 |
} |
112 |
return this.childs; |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public boolean isLeaf() { |
117 |
return this.getChildren().isEmpty(); |
118 |
} |
119 |
} |
120 |
|
121 |
private final Set<String> stores; |
122 |
private final DefaultNode root; |
123 |
private final Predicate<FeatureAttributeDescriptor> filterByDataType; |
124 |
|
125 |
public AdvancedAttributeSelectionTreeModel(FeatureStore store, Predicate<FeatureAttributeDescriptor> filterByDataType) { |
126 |
if( filterByDataType == null ) { |
127 |
this.filterByDataType = Search.ALL_FILTER;
|
128 |
} else {
|
129 |
this.filterByDataType = filterByDataType;
|
130 |
} |
131 |
FeatureType featureType; |
132 |
try {
|
133 |
featureType = store.getDefaultFeatureType(); |
134 |
} catch (Exception ex) { |
135 |
throw new RuntimeException("Can't access attributes of store", ex); |
136 |
} |
137 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
138 |
Search.COMPLEMENT_MANE, featureType |
139 |
); |
140 |
List<FeatureAttributeDescriptor> attributes = featureTypeSearch.getOrderedAttributes(
|
141 |
filterByDataType, |
142 |
Search.LABEL_ORDER, |
143 |
-1
|
144 |
); |
145 |
this.root = new DefaultNode(store, attributes); |
146 |
this.stores = new HashSet<>(); |
147 |
} |
148 |
|
149 |
@Override
|
150 |
public Object getRoot() { |
151 |
return this.root; |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public int getChildCount(Object parent) { |
156 |
DefaultNode node = (DefaultNode) parent; |
157 |
return node.getChildren().size();
|
158 |
} |
159 |
|
160 |
@Override
|
161 |
public Object getChild(Object parent, int index) { |
162 |
DefaultNode node = (DefaultNode) parent; |
163 |
return node.getChildren().get(index);
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
public boolean isLeaf(Object node) { |
168 |
return ((DefaultNode) node).isLeaf();
|
169 |
} |
170 |
|
171 |
@Override
|
172 |
public int getIndexOfChild(Object parent, Object child) { |
173 |
try {
|
174 |
DefaultNode parantNode = (DefaultNode) parent; |
175 |
DefaultNode childNode = (DefaultNode) child; |
176 |
int index = 0; |
177 |
for (Node node : parantNode.getChildren()) {
|
178 |
if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) {
|
179 |
return index;
|
180 |
} |
181 |
index++; |
182 |
} |
183 |
} catch (Exception ex) { |
184 |
|
185 |
} |
186 |
return 0; |
187 |
} |
188 |
|
189 |
@Override
|
190 |
public void valueForPathChanged(TreePath path, Object newValue) { |
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void addTreeModelListener(TreeModelListener l) { |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public void removeTreeModelListener(TreeModelListener l) { |
199 |
} |
200 |
|
201 |
} |