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 @ 45180
History | View | Annotate | Download (11.1 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.fmap.dal.DALLocator; |
16 |
import org.gvsig.fmap.dal.DataManager; |
17 |
import org.gvsig.fmap.dal.complements.Search; |
18 |
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute; |
19 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
20 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
21 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
22 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
23 |
import org.gvsig.fmap.dal.feature.FeatureType; |
24 |
import org.gvsig.fmap.dal.feature.ForeingKey; |
25 |
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey; |
26 |
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel; |
27 |
import org.gvsig.tools.ToolsLocator; |
28 |
import org.gvsig.tools.dynobject.DynField; |
29 |
import org.gvsig.tools.util.LabeledValue; |
30 |
import org.gvsig.tools.util.LabeledValueImpl; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
import org.gvsig.expressionevaluator.Code.Callable; |
34 |
|
35 |
/**
|
36 |
*
|
37 |
* @author jjdelcerro
|
38 |
*/
|
39 |
@SuppressWarnings("UseSpecificCatch") |
40 |
public class FeatureAttributeTreeModel |
41 |
implements TreeModel { |
42 |
|
43 |
private static final Logger LOGGER = LoggerFactory.getLogger(FeatureAttributeTreeModel.class); |
44 |
|
45 |
|
46 |
public interface Node extends LabeledValue<FeatureAttributeDescriptor> { |
47 |
|
48 |
public FeatureStore getFeatureStore();
|
49 |
|
50 |
public boolean isRoot(); |
51 |
|
52 |
public boolean isLeaf(); |
53 |
|
54 |
public List<Node> getChildren(); |
55 |
} |
56 |
|
57 |
private class DefaultNode |
58 |
extends LabeledValueImpl<FeatureAttributeDescriptor>
|
59 |
implements Node {
|
60 |
|
61 |
private List<Node> childs; |
62 |
private final FeatureStore store; |
63 |
private final int type; |
64 |
|
65 |
public DefaultNode(FeatureStore store, List<OrderedAttribute> attributes) { |
66 |
super(store == null ? "" : store.getName(), null); |
67 |
this.store = store;
|
68 |
this.type = OrderedAttribute.TYPE_REGURAL;
|
69 |
this.childs = new ArrayList<>(); |
70 |
for (OrderedAttribute attribute : attributes) {
|
71 |
this.childs.add(new DefaultNode(store, attribute.getDescriptor(), attribute.getType())); |
72 |
} |
73 |
} |
74 |
|
75 |
private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute, int type) { |
76 |
super(attribute.getLocalizedLabel(), attribute);
|
77 |
this.store = store;
|
78 |
this.type = type;
|
79 |
this.childs = null; |
80 |
} |
81 |
|
82 |
public String getLabel(int type) { |
83 |
String theLabel;
|
84 |
FeatureAttributeDescriptor attrdesc = this.getValue();
|
85 |
if (attrdesc == null) { |
86 |
theLabel = super.getLabel();
|
87 |
} else {
|
88 |
String tableName = null; |
89 |
if (attrdesc.isForeingKey()) {
|
90 |
ForeingKey foreingKey = attrdesc.getForeingKey(); |
91 |
tableName = foreingKey.getTableName(); |
92 |
} |
93 |
theLabel = getAttributeDescriptorLabel(attrdesc, tableName); |
94 |
} |
95 |
switch (type) {
|
96 |
case Search.OrderedAttribute.TYPE_REGURAL:
|
97 |
break;
|
98 |
case Search.OrderedAttribute.TYPE_FAVORITE:
|
99 |
theLabel = "<html><b>" + theLabel + "</b></html>"; |
100 |
break;
|
101 |
case Search.OrderedAttribute.TYPE_RECENT:
|
102 |
theLabel = "<html><i><b>" + theLabel + "</b></i></html>"; |
103 |
break;
|
104 |
} |
105 |
return theLabel;
|
106 |
} |
107 |
|
108 |
@Override
|
109 |
public String getLabel() { |
110 |
return this.getLabel(this.type); |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public FeatureStore getFeatureStore() {
|
115 |
return this.store; |
116 |
} |
117 |
|
118 |
@Override
|
119 |
public boolean isRoot() { |
120 |
return this.getValue() == null; |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public List<Node> getChildren() { |
125 |
if (showRelations && this.childs == null) { |
126 |
FeatureAttributeDescriptor descriptor = this.getValue();
|
127 |
try {
|
128 |
this.childs = Collections.EMPTY_LIST; |
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 |
try {
|
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 |
} finally {
|
163 |
context.dispose(); |
164 |
} |
165 |
} |
166 |
break;
|
167 |
|
168 |
case DynField.RELATION_TYPE_COMPOSITION:
|
169 |
case DynField.RELATION_TYPE_AGGREGATE:
|
170 |
FeatureAttributeEmulator emulator = descriptor.getFeatureAttributeEmulator(); |
171 |
if( emulator instanceof FeatureAttributeEmulatorExpression) { |
172 |
FeatureAttributeEmulatorExpression emulatorExp = (FeatureAttributeEmulatorExpression) emulator; |
173 |
Code code = emulatorExp.getExpression().getCode(); |
174 |
if( code.code()==Code.CALLABLE ) {
|
175 |
Callable caller = (Callable) code; |
176 |
if( StringUtils.equalsIgnoreCase(caller.name(), "SELECT") ) { |
177 |
Codes parameters = caller.parameters(); |
178 |
String tableName = (String) ((Code.Identifier)(parameters.get(1))).name(); |
179 |
DataManager dataManager = DALLocator.getDataManager(); |
180 |
FeatureStore theStore = (FeatureStore) dataManager.getStoresRepository().getStore(tableName); |
181 |
if (theStore == null) { |
182 |
this.childs = Collections.EMPTY_LIST; |
183 |
} else {
|
184 |
FeatureType featureType = theStore.getDefaultFeatureType(); |
185 |
String fullName = theStore.getFullName();
|
186 |
if (stores.contains(fullName)) {
|
187 |
// Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
|
188 |
this.childs = Collections.EMPTY_LIST; |
189 |
} else {
|
190 |
Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get( |
191 |
Search.COMPLEMENT_MANE, featureType |
192 |
); |
193 |
List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
|
194 |
filterByDataType, |
195 |
Search.LABEL_ORDER, |
196 |
-1
|
197 |
); |
198 |
this.childs = new ArrayList<>(); |
199 |
for (Search.OrderedAttribute attribute : attributes) {
|
200 |
this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType())); |
201 |
} |
202 |
stores.add(fullName); |
203 |
} |
204 |
} |
205 |
} |
206 |
} |
207 |
} |
208 |
break;
|
209 |
|
210 |
case DynField.RELATION_TYPE_NONE:
|
211 |
default:
|
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 |
if(node.getChildren()==null) { |
273 |
return null; |
274 |
} |
275 |
return node.getChildren().get(index);
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
public boolean isLeaf(Object node) { |
280 |
return ((DefaultNode) node).isLeaf();
|
281 |
} |
282 |
|
283 |
@Override
|
284 |
public int getIndexOfChild(Object parent, Object child) { |
285 |
try {
|
286 |
DefaultNode parantNode = (DefaultNode) parent; |
287 |
DefaultNode childNode = (DefaultNode) child; |
288 |
int index = 0; |
289 |
for (Node node : parantNode.getChildren()) {
|
290 |
if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) {
|
291 |
return index;
|
292 |
} |
293 |
index++; |
294 |
} |
295 |
} catch (Exception ex) { |
296 |
|
297 |
} |
298 |
return 0; |
299 |
} |
300 |
|
301 |
@Override
|
302 |
public void valueForPathChanged(TreePath path, Object newValue) { |
303 |
} |
304 |
|
305 |
@Override
|
306 |
public void addTreeModelListener(TreeModelListener l) { |
307 |
} |
308 |
|
309 |
@Override
|
310 |
public void removeTreeModelListener(TreeModelListener l) { |
311 |
} |
312 |
|
313 |
} |