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