Revision 44740 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/featuretype/FeatureAttributeTreeModel.java
FeatureAttributeTreeModel.java | ||
---|---|---|
10 | 10 |
import javax.swing.tree.TreeModel; |
11 | 11 |
import javax.swing.tree.TreePath; |
12 | 12 |
import org.apache.commons.lang3.StringUtils; |
13 |
import org.gvsig.expressionevaluator.Code; |
|
14 |
import org.gvsig.expressionevaluator.Codes; |
|
15 |
import org.gvsig.expressionevaluator.Function; |
|
16 |
import org.gvsig.fmap.dal.DALLocator; |
|
17 |
import org.gvsig.fmap.dal.DataManager; |
|
18 |
import org.gvsig.fmap.dal.DataStore; |
|
13 | 19 |
import org.gvsig.fmap.dal.complements.Search; |
14 | 20 |
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute; |
21 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
|
15 | 22 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
23 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
|
16 | 24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
17 | 25 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 | 26 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
19 | 27 |
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey; |
20 | 28 |
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel; |
21 | 29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.dynobject.DynField; |
|
22 | 31 |
import org.gvsig.tools.util.LabeledValue; |
23 | 32 |
import org.gvsig.tools.util.LabeledValueImpl; |
33 |
import org.slf4j.Logger; |
|
34 |
import org.slf4j.LoggerFactory; |
|
24 | 35 |
|
25 | 36 |
/** |
26 | 37 |
* |
... | ... | |
30 | 41 |
public class FeatureAttributeTreeModel |
31 | 42 |
implements TreeModel { |
32 | 43 |
|
33 |
public interface Node extends LabeledValue<FeatureAttributeDescriptor> { |
|
44 |
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureAttributeTreeModel.class); |
|
45 |
|
|
46 |
|
|
47 |
public interface Node extends LabeledValue<FeatureAttributeDescriptor> { |
|
34 | 48 |
|
35 |
public FeatureStore getFeatureStore(); |
|
36 |
public boolean isRoot(); |
|
37 |
public boolean isLeaf(); |
|
38 |
public List<Node> getChildren(); |
|
39 |
} |
|
49 |
public FeatureStore getFeatureStore(); |
|
40 | 50 |
|
41 |
private class DefaultNode |
|
42 |
extends LabeledValueImpl<FeatureAttributeDescriptor> |
|
43 |
implements Node { |
|
51 |
public boolean isRoot(); |
|
44 | 52 |
|
45 |
private List<Node> childs; |
|
46 |
private final FeatureStore store; |
|
47 |
private final int type; |
|
53 |
public boolean isLeaf(); |
|
48 | 54 |
|
49 |
public DefaultNode(FeatureStore store, List<OrderedAttribute> attributes) { |
|
50 |
super(store==null? "":store.getName(), null); |
|
51 |
this.store = store; |
|
52 |
this.type = OrderedAttribute.TYPE_REGURAL; |
|
53 |
this.childs = new ArrayList<>(); |
|
54 |
for (OrderedAttribute attribute : attributes) { |
|
55 |
this.childs.add(new DefaultNode(store, attribute.getDescriptor(), attribute.getType())); |
|
56 |
} |
|
57 |
} |
|
55 |
public List<Node> getChildren(); |
|
56 |
} |
|
58 | 57 |
|
59 |
private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute, int type) { |
|
60 |
super(attribute.getLocalizedLabel(), attribute); |
|
61 |
this.store = store; |
|
62 |
this.type = type; |
|
63 |
this.childs = null; |
|
64 |
} |
|
58 |
private class DefaultNode |
|
59 |
extends LabeledValueImpl<FeatureAttributeDescriptor> |
|
60 |
implements Node { |
|
65 | 61 |
|
66 |
@Override |
|
67 |
public String getLabel() { |
|
68 |
String theLabel; |
|
69 |
FeatureAttributeDescriptor attrdesc = this.getValue(); |
|
70 |
if( attrdesc==null ) { |
|
71 |
theLabel = super.getLabel(); |
|
72 |
} else { |
|
73 |
String tableName = null; |
|
74 |
if( attrdesc.isForeingKey() ) { |
|
75 |
ForeingKey foreingKey = attrdesc.getForeingKey(); |
|
76 |
tableName = foreingKey.getTableName(); |
|
77 |
} |
|
78 |
theLabel = getAttributeDescriptorLabel(attrdesc, tableName); |
|
79 |
} |
|
80 |
switch(this.type) { |
|
81 |
case Search.OrderedAttribute.TYPE_REGURAL: |
|
82 |
break; |
|
83 |
case Search.OrderedAttribute.TYPE_FAVORITE: |
|
84 |
theLabel = "<html><b>"+theLabel+"</b></html>"; |
|
85 |
break; |
|
86 |
case Search.OrderedAttribute.TYPE_RECENT: |
|
87 |
theLabel = "<html><i><b>"+theLabel+"</b></i></html>"; |
|
88 |
break; |
|
89 |
} |
|
90 |
return theLabel; |
|
91 |
} |
|
62 |
private List<Node> childs; |
|
63 |
private final FeatureStore store; |
|
64 |
private final int type; |
|
92 | 65 |
|
93 |
@Override |
|
94 |
public FeatureStore getFeatureStore() { |
|
95 |
return this.store; |
|
96 |
} |
|
66 |
public DefaultNode(FeatureStore store, List<OrderedAttribute> attributes) { |
|
67 |
super(store == null ? "" : store.getName(), null); |
|
68 |
this.store = store; |
|
69 |
this.type = OrderedAttribute.TYPE_REGURAL; |
|
70 |
this.childs = new ArrayList<>(); |
|
71 |
for (OrderedAttribute attribute : attributes) { |
|
72 |
this.childs.add(new DefaultNode(store, attribute.getDescriptor(), attribute.getType())); |
|
73 |
} |
|
74 |
} |
|
97 | 75 |
|
98 |
@Override |
|
99 |
public boolean isRoot() { |
|
100 |
return this.getValue() == null; |
|
101 |
} |
|
102 |
|
|
103 |
@Override |
|
104 |
public List<Node> getChildren() { |
|
105 |
if (showRelations && this.childs == null) { |
|
106 |
if (this.getValue().isForeingKey()) { |
|
107 |
ForeingKey foreingKey = this.getValue().getForeingKey(); |
|
108 |
ContextForeingKey context = foreingKey.createContext(); |
|
109 |
// Ojo, no liberamos el contexto para que no se destruya el store. |
|
110 |
FeatureStore theStore = foreingKey.getFeatureStore(context); |
|
111 |
if( theStore==null ) { |
|
112 |
this.childs = Collections.EMPTY_LIST; |
|
113 |
} else { |
|
114 |
FeatureType featureType = foreingKey.getFeatureType(context); |
|
115 |
String fullName = theStore.getFullName(); |
|
116 |
if (stores.contains(fullName)) { |
|
117 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir. |
|
118 |
this.childs = Collections.EMPTY_LIST; |
|
119 |
} else { |
|
120 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
|
121 |
Search.COMPLEMENT_MANE, featureType |
|
122 |
); |
|
123 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes( |
|
124 |
filterByDataType, |
|
125 |
Search.LABEL_ORDER, |
|
126 |
-1 |
|
127 |
); |
|
128 |
this.childs = new ArrayList<>(); |
|
129 |
for (Search.OrderedAttribute attribute : attributes) { |
|
130 |
this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType())); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
} else { |
|
135 |
this.childs = Collections.EMPTY_LIST; |
|
136 |
} |
|
137 |
} |
|
138 |
return this.childs; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public boolean isLeaf() { |
|
143 |
return this.getChildren().isEmpty(); |
|
144 |
} |
|
76 |
private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute, int type) { |
|
77 |
super(attribute.getLocalizedLabel(), attribute); |
|
78 |
this.store = store; |
|
79 |
this.type = type; |
|
80 |
this.childs = null; |
|
145 | 81 |
} |
146 | 82 |
|
147 |
private final Set<String> stores;
|
|
148 |
private final DefaultNode root;
|
|
149 |
private final Predicate<FeatureAttributeDescriptor> filterByDataType;
|
|
150 |
private boolean showRelations;
|
|
151 |
|
|
152 |
public FeatureAttributeTreeModel(FeatureStore store, boolean showRelations, Predicate<FeatureAttributeDescriptor> filterByDataType) {
|
|
153 |
if( filterByDataType == null ) {
|
|
154 |
this.filterByDataType = Search.ALL_FILTER;
|
|
155 |
} else {
|
|
156 |
this.filterByDataType = filterByDataType;
|
|
83 |
public String getLabel(int type) {
|
|
84 |
String theLabel;
|
|
85 |
FeatureAttributeDescriptor attrdesc = this.getValue();
|
|
86 |
if (attrdesc == null) {
|
|
87 |
theLabel = super.getLabel(); |
|
88 |
} else {
|
|
89 |
String tableName = null;
|
|
90 |
if (attrdesc.isForeingKey()) {
|
|
91 |
ForeingKey foreingKey = attrdesc.getForeingKey();
|
|
92 |
tableName = foreingKey.getTableName();
|
|
157 | 93 |
} |
158 |
FeatureType featureType; |
|
159 |
try { |
|
160 |
featureType = store.getDefaultFeatureType(); |
|
161 |
} catch (Exception ex) { |
|
162 |
throw new RuntimeException("Can't access attributes of store", ex); |
|
163 |
} |
|
164 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
|
165 |
Search.COMPLEMENT_MANE, featureType |
|
166 |
); |
|
167 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes( |
|
168 |
filterByDataType, |
|
169 |
Search.LABEL_ORDER, |
|
170 |
-1 |
|
171 |
); |
|
172 |
this.root = new DefaultNode(store, attributes); |
|
173 |
this.stores = new HashSet<>(); |
|
174 |
this.showRelations = showRelations; |
|
94 |
theLabel = getAttributeDescriptorLabel(attrdesc, tableName); |
|
95 |
} |
|
96 |
switch (type) { |
|
97 |
case Search.OrderedAttribute.TYPE_REGURAL: |
|
98 |
break; |
|
99 |
case Search.OrderedAttribute.TYPE_FAVORITE: |
|
100 |
theLabel = "<html><b>" + theLabel + "</b></html>"; |
|
101 |
break; |
|
102 |
case Search.OrderedAttribute.TYPE_RECENT: |
|
103 |
theLabel = "<html><i><b>" + theLabel + "</b></i></html>"; |
|
104 |
break; |
|
105 |
} |
|
106 |
return theLabel; |
|
175 | 107 |
} |
176 | 108 |
|
177 | 109 |
@Override |
178 |
public Object getRoot() {
|
|
179 |
return this.root;
|
|
110 |
public String getLabel() {
|
|
111 |
return this.getLabel(this.type);
|
|
180 | 112 |
} |
181 | 113 |
|
182 | 114 |
@Override |
183 |
public int getChildCount(Object parent) { |
|
184 |
DefaultNode node = (DefaultNode) parent; |
|
185 |
return node.getChildren().size(); |
|
115 |
public FeatureStore getFeatureStore() { |
|
116 |
return this.store; |
|
186 | 117 |
} |
187 | 118 |
|
188 | 119 |
@Override |
189 |
public Object getChild(Object parent, int index) { |
|
190 |
DefaultNode node = (DefaultNode) parent; |
|
191 |
return node.getChildren().get(index); |
|
120 |
public boolean isRoot() { |
|
121 |
return this.getValue() == null; |
|
192 | 122 |
} |
193 | 123 |
|
194 | 124 |
@Override |
195 |
public boolean isLeaf(Object node) { |
|
196 |
return ((DefaultNode) node).isLeaf(); |
|
197 |
} |
|
198 |
|
|
199 |
@Override |
|
200 |
public int getIndexOfChild(Object parent, Object child) { |
|
125 |
public List<Node> getChildren() { |
|
126 |
if (showRelations && this.childs == null) { |
|
127 |
FeatureAttributeDescriptor descriptor = this.getValue(); |
|
201 | 128 |
try { |
202 |
DefaultNode parantNode = (DefaultNode) parent; |
|
203 |
DefaultNode childNode = (DefaultNode) child; |
|
204 |
int index = 0; |
|
205 |
for (Node node : parantNode.getChildren()) { |
|
206 |
if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) { |
|
207 |
return index; |
|
129 |
switch (descriptor.getRelationType()) { |
|
130 |
case DynField.RELATION_TYPE_IDENTITY: |
|
131 |
case DynField.RELATION_TYPE_COLLABORATION: |
|
132 |
if (this.getValue().isForeingKey()) { |
|
133 |
ForeingKey foreingKey = this.getValue().getForeingKey(); |
|
134 |
ContextForeingKey context = foreingKey.createContext(); |
|
135 |
// Ojo, no liberamos el contexto para que no se destruya el store. |
|
136 |
FeatureStore theStore = foreingKey.getFeatureStore(context); |
|
137 |
if (theStore == null) { |
|
138 |
this.childs = Collections.EMPTY_LIST; |
|
139 |
} else { |
|
140 |
FeatureType featureType = foreingKey.getFeatureType(context); |
|
141 |
String fullName = theStore.getFullName(); |
|
142 |
if (stores.contains(fullName)) { |
|
143 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir. |
|
144 |
this.childs = Collections.EMPTY_LIST; |
|
145 |
} else { |
|
146 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
|
147 |
Search.COMPLEMENT_MANE, featureType |
|
148 |
); |
|
149 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes( |
|
150 |
filterByDataType, |
|
151 |
Search.LABEL_ORDER, |
|
152 |
-1 |
|
153 |
); |
|
154 |
this.childs = new ArrayList<>(); |
|
155 |
for (Search.OrderedAttribute attribute : attributes) { |
|
156 |
this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType())); |
|
157 |
} |
|
158 |
stores.add(fullName); |
|
159 |
} |
|
208 | 160 |
} |
209 |
index++; |
|
210 |
} |
|
211 |
} catch (Exception ex) { |
|
161 |
} else { |
|
162 |
this.childs = Collections.EMPTY_LIST; |
|
163 |
} |
|
164 |
break; |
|
212 | 165 |
|
166 |
case DynField.RELATION_TYPE_COMPOSITION: |
|
167 |
case DynField.RELATION_TYPE_AGGREGATE: |
|
168 |
FeatureAttributeEmulator emulator = descriptor.getFeatureAttributeEmulator(); |
|
169 |
if( emulator instanceof FeatureAttributeEmulatorExpression) { |
|
170 |
FeatureAttributeEmulatorExpression emulatorExp = (FeatureAttributeEmulatorExpression) emulator; |
|
171 |
Code code = emulatorExp.getExpression().getCode(); |
|
172 |
if( code.code()==Code.CALLER ) { |
|
173 |
Function function = ((Code.Caller)code).function(); |
|
174 |
if( function != null && StringUtils.equalsIgnoreCase(function.name(), "SELECT") ) { |
|
175 |
Codes parameters = ((Code.Caller)code).parameters(); |
|
176 |
String tableName = (String) ((Code.Constant)(parameters.get("TABLE"))).value(); |
|
177 |
DataManager dataManager = DALLocator.getDataManager(); |
|
178 |
FeatureStore theStore = (FeatureStore) dataManager.getStoresRepository().getStore(tableName); |
|
179 |
if (theStore == null) { |
|
180 |
this.childs = Collections.EMPTY_LIST; |
|
181 |
} else { |
|
182 |
FeatureType featureType = theStore.getDefaultFeatureType(); |
|
183 |
String fullName = theStore.getFullName(); |
|
184 |
if (stores.contains(fullName)) { |
|
185 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir. |
|
186 |
this.childs = Collections.EMPTY_LIST; |
|
187 |
} else { |
|
188 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
|
189 |
Search.COMPLEMENT_MANE, featureType |
|
190 |
); |
|
191 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes( |
|
192 |
filterByDataType, |
|
193 |
Search.LABEL_ORDER, |
|
194 |
-1 |
|
195 |
); |
|
196 |
this.childs = new ArrayList<>(); |
|
197 |
for (Search.OrderedAttribute attribute : attributes) { |
|
198 |
this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType())); |
|
199 |
} |
|
200 |
stores.add(fullName); |
|
201 |
} |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
this.childs = Collections.EMPTY_LIST; |
|
207 |
break; |
|
208 |
|
|
209 |
case DynField.RELATION_TYPE_NONE: |
|
210 |
default: |
|
211 |
this.childs = Collections.EMPTY_LIST; |
|
212 |
break; |
|
213 |
} |
|
214 |
} catch(Exception ex) { |
|
215 |
this.childs = Collections.EMPTY_LIST; |
|
216 |
LOGGER.warn("Can't get childs of "+descriptor.getName(),ex); |
|
213 | 217 |
} |
214 |
return 0; |
|
218 |
} |
|
219 |
return this.childs; |
|
215 | 220 |
} |
216 | 221 |
|
217 | 222 |
@Override |
218 |
public void valueForPathChanged(TreePath path, Object newValue) { |
|
223 |
public boolean isLeaf() { |
|
224 |
return this.getChildren().isEmpty(); |
|
219 | 225 |
} |
226 |
} |
|
220 | 227 |
|
221 |
@Override |
|
222 |
public void addTreeModelListener(TreeModelListener l) { |
|
228 |
private final Set<String> stores; |
|
229 |
private final DefaultNode root; |
|
230 |
private final Predicate<FeatureAttributeDescriptor> filterByDataType; |
|
231 |
private boolean showRelations; |
|
232 |
|
|
233 |
public FeatureAttributeTreeModel(FeatureStore store, boolean showRelations, Predicate<FeatureAttributeDescriptor> filterByDataType) { |
|
234 |
if (filterByDataType == null) { |
|
235 |
this.filterByDataType = Search.ALL_FILTER; |
|
236 |
} else { |
|
237 |
this.filterByDataType = filterByDataType; |
|
223 | 238 |
} |
239 |
FeatureType featureType; |
|
240 |
try { |
|
241 |
featureType = store.getDefaultFeatureType(); |
|
242 |
} catch (Exception ex) { |
|
243 |
throw new RuntimeException("Can't access attributes of store", ex); |
|
244 |
} |
|
245 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
|
246 |
Search.COMPLEMENT_MANE, featureType |
|
247 |
); |
|
248 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes( |
|
249 |
filterByDataType, |
|
250 |
Search.LABEL_ORDER, |
|
251 |
-1 |
|
252 |
); |
|
253 |
this.root = new DefaultNode(store, attributes); |
|
254 |
this.stores = new HashSet<>(); |
|
255 |
this.showRelations = showRelations; |
|
256 |
} |
|
224 | 257 |
|
225 |
@Override |
|
226 |
public void removeTreeModelListener(TreeModelListener l) { |
|
258 |
@Override |
|
259 |
public Object getRoot() { |
|
260 |
return this.root; |
|
261 |
} |
|
262 |
|
|
263 |
@Override |
|
264 |
public int getChildCount(Object parent) { |
|
265 |
DefaultNode node = (DefaultNode) parent; |
|
266 |
return node.getChildren().size(); |
|
267 |
} |
|
268 |
|
|
269 |
@Override |
|
270 |
public Object getChild(Object parent, int index) { |
|
271 |
DefaultNode node = (DefaultNode) parent; |
|
272 |
return node.getChildren().get(index); |
|
273 |
} |
|
274 |
|
|
275 |
@Override |
|
276 |
public boolean isLeaf(Object node) { |
|
277 |
return ((DefaultNode) node).isLeaf(); |
|
278 |
} |
|
279 |
|
|
280 |
@Override |
|
281 |
public int getIndexOfChild(Object parent, Object child) { |
|
282 |
try { |
|
283 |
DefaultNode parantNode = (DefaultNode) parent; |
|
284 |
DefaultNode childNode = (DefaultNode) child; |
|
285 |
int index = 0; |
|
286 |
for (Node node : parantNode.getChildren()) { |
|
287 |
if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) { |
|
288 |
return index; |
|
289 |
} |
|
290 |
index++; |
|
291 |
} |
|
292 |
} catch (Exception ex) { |
|
293 |
|
|
227 | 294 |
} |
295 |
return 0; |
|
296 |
} |
|
228 | 297 |
|
298 |
@Override |
|
299 |
public void valueForPathChanged(TreePath path, Object newValue) { |
|
300 |
} |
|
301 |
|
|
302 |
@Override |
|
303 |
public void addTreeModelListener(TreeModelListener l) { |
|
304 |
} |
|
305 |
|
|
306 |
@Override |
|
307 |
public void removeTreeModelListener(TreeModelListener l) { |
|
308 |
} |
|
309 |
|
|
229 | 310 |
} |
Also available in: Unified diff