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