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 @ 44744
History | View | Annotate | Download (10.9 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 |
this.childs = Collections.EMPTY_LIST; |
130 |
switch (descriptor.getRelationType()) {
|
131 |
case DynField.RELATION_TYPE_IDENTITY:
|
132 |
case DynField.RELATION_TYPE_COLLABORATION:
|
133 |
if (this.getValue().isForeingKey()) { |
134 |
ForeingKey foreingKey = this.getValue().getForeingKey();
|
135 |
ContextForeingKey context = foreingKey.createContext(); |
136 |
// Ojo, no liberamos el contexto para que no se destruya el store.
|
137 |
FeatureStore theStore = foreingKey.getFeatureStore(context); |
138 |
if (theStore == null) { |
139 |
this.childs = Collections.EMPTY_LIST; |
140 |
} else {
|
141 |
FeatureType featureType = foreingKey.getFeatureType(context); |
142 |
String fullName = theStore.getFullName();
|
143 |
if (stores.contains(fullName)) {
|
144 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
|
145 |
this.childs = Collections.EMPTY_LIST; |
146 |
} else {
|
147 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
148 |
Search.COMPLEMENT_MANE, featureType |
149 |
); |
150 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
|
151 |
filterByDataType, |
152 |
Search.LABEL_ORDER, |
153 |
-1
|
154 |
); |
155 |
this.childs = new ArrayList<>(); |
156 |
for (Search.OrderedAttribute attribute : attributes) {
|
157 |
this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType())); |
158 |
} |
159 |
stores.add(fullName); |
160 |
} |
161 |
} |
162 |
} |
163 |
break;
|
164 |
|
165 |
case DynField.RELATION_TYPE_COMPOSITION:
|
166 |
case DynField.RELATION_TYPE_AGGREGATE:
|
167 |
FeatureAttributeEmulator emulator = descriptor.getFeatureAttributeEmulator(); |
168 |
if( emulator instanceof FeatureAttributeEmulatorExpression) { |
169 |
FeatureAttributeEmulatorExpression emulatorExp = (FeatureAttributeEmulatorExpression) emulator; |
170 |
Code code = emulatorExp.getExpression().getCode(); |
171 |
if( code.code()==Code.CALLER ) {
|
172 |
// Function function = ((Code.Caller)code).function();
|
173 |
if( StringUtils.equalsIgnoreCase(((Code.Caller)code).name(), "SELECT") ) { |
174 |
Codes parameters = ((Code.Caller)code).parameters(); |
175 |
String tableName = (String) ((Code.Constant)(parameters.get("TABLE"))).value(); |
176 |
DataManager dataManager = DALLocator.getDataManager(); |
177 |
FeatureStore theStore = (FeatureStore) dataManager.getStoresRepository().getStore(tableName); |
178 |
if (theStore == null) { |
179 |
this.childs = Collections.EMPTY_LIST; |
180 |
} else {
|
181 |
FeatureType featureType = theStore.getDefaultFeatureType(); |
182 |
String fullName = theStore.getFullName();
|
183 |
if (stores.contains(fullName)) {
|
184 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
|
185 |
this.childs = Collections.EMPTY_LIST; |
186 |
} else {
|
187 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
188 |
Search.COMPLEMENT_MANE, featureType |
189 |
); |
190 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
|
191 |
filterByDataType, |
192 |
Search.LABEL_ORDER, |
193 |
-1
|
194 |
); |
195 |
this.childs = new ArrayList<>(); |
196 |
for (Search.OrderedAttribute attribute : attributes) {
|
197 |
this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType())); |
198 |
} |
199 |
stores.add(fullName); |
200 |
} |
201 |
} |
202 |
} |
203 |
} |
204 |
} |
205 |
break;
|
206 |
|
207 |
case DynField.RELATION_TYPE_NONE:
|
208 |
default:
|
209 |
break;
|
210 |
} |
211 |
} catch(Exception ex) { |
212 |
this.childs = Collections.EMPTY_LIST; |
213 |
LOGGER.warn("Can't get childs of "+descriptor.getName(),ex);
|
214 |
} |
215 |
} |
216 |
return this.childs; |
217 |
} |
218 |
|
219 |
@Override
|
220 |
public boolean isLeaf() { |
221 |
return this.getChildren().isEmpty(); |
222 |
} |
223 |
} |
224 |
|
225 |
private final Set<String> stores; |
226 |
private final DefaultNode root; |
227 |
private final Predicate<FeatureAttributeDescriptor> filterByDataType; |
228 |
private boolean showRelations; |
229 |
|
230 |
public FeatureAttributeTreeModel(FeatureStore store, boolean showRelations, Predicate<FeatureAttributeDescriptor> filterByDataType) { |
231 |
if (filterByDataType == null) { |
232 |
this.filterByDataType = Search.ALL_FILTER;
|
233 |
} else {
|
234 |
this.filterByDataType = filterByDataType;
|
235 |
} |
236 |
FeatureType featureType; |
237 |
try {
|
238 |
featureType = store.getDefaultFeatureType(); |
239 |
} catch (Exception ex) { |
240 |
throw new RuntimeException("Can't access attributes of store", ex); |
241 |
} |
242 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
243 |
Search.COMPLEMENT_MANE, featureType |
244 |
); |
245 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
|
246 |
filterByDataType, |
247 |
Search.LABEL_ORDER, |
248 |
-1
|
249 |
); |
250 |
this.root = new DefaultNode(store, attributes); |
251 |
this.stores = new HashSet<>(); |
252 |
this.showRelations = showRelations;
|
253 |
} |
254 |
|
255 |
@Override
|
256 |
public Object getRoot() { |
257 |
return this.root; |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public int getChildCount(Object parent) { |
262 |
DefaultNode node = (DefaultNode) parent; |
263 |
return node.getChildren().size();
|
264 |
} |
265 |
|
266 |
@Override
|
267 |
public Object getChild(Object parent, int index) { |
268 |
DefaultNode node = (DefaultNode) parent; |
269 |
return node.getChildren().get(index);
|
270 |
} |
271 |
|
272 |
@Override
|
273 |
public boolean isLeaf(Object node) { |
274 |
return ((DefaultNode) node).isLeaf();
|
275 |
} |
276 |
|
277 |
@Override
|
278 |
public int getIndexOfChild(Object parent, Object child) { |
279 |
try {
|
280 |
DefaultNode parantNode = (DefaultNode) parent; |
281 |
DefaultNode childNode = (DefaultNode) child; |
282 |
int index = 0; |
283 |
for (Node node : parantNode.getChildren()) {
|
284 |
if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) {
|
285 |
return index;
|
286 |
} |
287 |
index++; |
288 |
} |
289 |
} catch (Exception ex) { |
290 |
|
291 |
} |
292 |
return 0; |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public void valueForPathChanged(TreePath path, Object newValue) { |
297 |
} |
298 |
|
299 |
@Override
|
300 |
public void addTreeModelListener(TreeModelListener l) { |
301 |
} |
302 |
|
303 |
@Override
|
304 |
public void removeTreeModelListener(TreeModelListener l) { |
305 |
} |
306 |
|
307 |
} |