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 / featuretype / FeatureAttributeTreeModel.java @ 44740
History | View | Annotate | Download (11 KB)
1 |
package org.gvsig.fmap.dal.swing.impl.featuretype; |
---|---|
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.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; |
19 |
import org.gvsig.fmap.dal.complements.Search; |
20 |
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute; |
21 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
22 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
23 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
25 |
import org.gvsig.fmap.dal.feature.FeatureType; |
26 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
27 |
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey; |
28 |
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel; |
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.dynobject.DynField; |
31 |
import org.gvsig.tools.util.LabeledValue; |
32 |
import org.gvsig.tools.util.LabeledValueImpl; |
33 |
import org.slf4j.Logger; |
34 |
import org.slf4j.LoggerFactory; |
35 |
|
36 |
/**
|
37 |
*
|
38 |
* @author jjdelcerro
|
39 |
*/
|
40 |
@SuppressWarnings("UseSpecificCatch") |
41 |
public class FeatureAttributeTreeModel |
42 |
implements TreeModel { |
43 |
|
44 |
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureAttributeTreeModel.class); |
45 |
|
46 |
|
47 |
public interface Node extends LabeledValue<FeatureAttributeDescriptor> { |
48 |
|
49 |
public FeatureStore getFeatureStore();
|
50 |
|
51 |
public boolean isRoot(); |
52 |
|
53 |
public boolean isLeaf(); |
54 |
|
55 |
public List<Node> getChildren(); |
56 |
} |
57 |
|
58 |
private class DefaultNode |
59 |
extends LabeledValueImpl<FeatureAttributeDescriptor>
|
60 |
implements Node {
|
61 |
|
62 |
private List<Node> childs; |
63 |
private final FeatureStore store; |
64 |
private final int type; |
65 |
|
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 |
} |
75 |
|
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; |
81 |
} |
82 |
|
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(); |
93 |
} |
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;
|
107 |
} |
108 |
|
109 |
@Override
|
110 |
public String getLabel() { |
111 |
return this.getLabel(this.type); |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public FeatureStore getFeatureStore() {
|
116 |
return this.store; |
117 |
} |
118 |
|
119 |
@Override
|
120 |
public boolean isRoot() { |
121 |
return this.getValue() == null; |
122 |
} |
123 |
|
124 |
@Override
|
125 |
public List<Node> getChildren() { |
126 |
if (showRelations && this.childs == null) { |
127 |
FeatureAttributeDescriptor descriptor = this.getValue();
|
128 |
try {
|
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 |
} |
160 |
} |
161 |
} else {
|
162 |
this.childs = Collections.EMPTY_LIST; |
163 |
} |
164 |
break;
|
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);
|
217 |
} |
218 |
} |
219 |
return this.childs; |
220 |
} |
221 |
|
222 |
@Override
|
223 |
public boolean isLeaf() { |
224 |
return this.getChildren().isEmpty(); |
225 |
} |
226 |
} |
227 |
|
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;
|
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 |
} |
257 |
|
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 |
|
294 |
} |
295 |
return 0; |
296 |
} |
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 |
|
310 |
} |