Statistics
| Revision:

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 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
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 44262 jjdelcerro
import org.gvsig.fmap.dal.complements.Search;
20 44337 jjdelcerro
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute;
21 44740 jjdelcerro
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
22 44262 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
23 44740 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
24 44262 jjdelcerro
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 44351 jjdelcerro
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel;
29 44262 jjdelcerro
import org.gvsig.tools.ToolsLocator;
30 44740 jjdelcerro
import org.gvsig.tools.dynobject.DynField;
31 44262 jjdelcerro
import org.gvsig.tools.util.LabeledValue;
32
import org.gvsig.tools.util.LabeledValueImpl;
33 44740 jjdelcerro
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
35 44262 jjdelcerro
36
/**
37
 *
38
 * @author jjdelcerro
39
 */
40 44263 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
41 44713 jjdelcerro
public class FeatureAttributeTreeModel
42 44262 jjdelcerro
        implements TreeModel {
43
44 44740 jjdelcerro
  private static final Logger LOGGER = LoggerFactory.getLogger(FeatureAttributeTreeModel.class);
45
46
47
  public interface Node extends LabeledValue<FeatureAttributeDescriptor> {
48 44262 jjdelcerro
49 44740 jjdelcerro
    public FeatureStore getFeatureStore();
50 44262 jjdelcerro
51 44740 jjdelcerro
    public boolean isRoot();
52 44262 jjdelcerro
53 44740 jjdelcerro
    public boolean isLeaf();
54 44262 jjdelcerro
55 44740 jjdelcerro
    public List<Node> getChildren();
56
  }
57 44262 jjdelcerro
58 44740 jjdelcerro
  private class DefaultNode
59
          extends LabeledValueImpl<FeatureAttributeDescriptor>
60
          implements Node {
61 44262 jjdelcerro
62 44740 jjdelcerro
    private List<Node> childs;
63
    private final FeatureStore store;
64
    private final int type;
65 44263 jjdelcerro
66 44740 jjdelcerro
    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 44262 jjdelcerro
76 44740 jjdelcerro
    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 44262 jjdelcerro
    }
82
83 44740 jjdelcerro
    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 44263 jjdelcerro
        }
94 44740 jjdelcerro
        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 44262 jjdelcerro
    }
108
109
    @Override
110 44740 jjdelcerro
    public String getLabel() {
111
      return this.getLabel(this.type);
112 44262 jjdelcerro
    }
113
114
    @Override
115 44740 jjdelcerro
    public FeatureStore getFeatureStore() {
116
      return this.store;
117 44262 jjdelcerro
    }
118
119
    @Override
120 44740 jjdelcerro
    public boolean isRoot() {
121
      return this.getValue() == null;
122 44262 jjdelcerro
    }
123
124
    @Override
125 44740 jjdelcerro
    public List<Node> getChildren() {
126
      if (showRelations && this.childs == null) {
127
        FeatureAttributeDescriptor descriptor = this.getValue();
128 44262 jjdelcerro
        try {
129 44744 jjdelcerro
          this.childs = Collections.EMPTY_LIST;
130 44740 jjdelcerro
          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 44262 jjdelcerro
                }
162 44740 jjdelcerro
              }
163
              break;
164 44262 jjdelcerro
165 44740 jjdelcerro
            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 44744 jjdelcerro
//                  Function function = ((Code.Caller)code).function();
173
                  if( StringUtils.equalsIgnoreCase(((Code.Caller)code).name(), "SELECT") ) {
174 44740 jjdelcerro
                    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 44262 jjdelcerro
        }
215 44740 jjdelcerro
      }
216
      return this.childs;
217 44262 jjdelcerro
    }
218
219
    @Override
220 44740 jjdelcerro
    public boolean isLeaf() {
221
      return this.getChildren().isEmpty();
222 44262 jjdelcerro
    }
223 44740 jjdelcerro
  }
224 44262 jjdelcerro
225 44740 jjdelcerro
  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 44262 jjdelcerro
    }
236 44740 jjdelcerro
    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 44262 jjdelcerro
255 44740 jjdelcerro
  @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 44262 jjdelcerro
    }
292 44740 jjdelcerro
    return 0;
293
  }
294 44262 jjdelcerro
295 44740 jjdelcerro
  @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 44262 jjdelcerro
}