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 @ 44752

History | View | Annotate | Download (10.8 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
                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
              }
162
              break;
163

    
164
            case DynField.RELATION_TYPE_COMPOSITION:
165
            case DynField.RELATION_TYPE_AGGREGATE:
166
              FeatureAttributeEmulator emulator = descriptor.getFeatureAttributeEmulator();
167
              if( emulator instanceof FeatureAttributeEmulatorExpression) {
168
                FeatureAttributeEmulatorExpression emulatorExp = (FeatureAttributeEmulatorExpression) emulator;
169
                Code code = emulatorExp.getExpression().getCode();
170
                if( code.code()==Code.CALLABLE ) {
171
                  Callable caller = (Callable) code;
172
                  if( StringUtils.equalsIgnoreCase(caller.name(), "SELECT") ) {
173
                    Codes parameters = caller.parameters();
174
                    String tableName = (String) ((Code.Identifier)(parameters.get(1))).name();
175
                    DataManager dataManager = DALLocator.getDataManager();
176
                    FeatureStore theStore = (FeatureStore) dataManager.getStoresRepository().getStore(tableName);
177
                    if (theStore == null) {
178
                      this.childs = Collections.EMPTY_LIST;
179
                    } else {
180
                      FeatureType featureType = theStore.getDefaultFeatureType();
181
                      String fullName = theStore.getFullName();
182
                      if (stores.contains(fullName)) {
183
                        // Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
184
                        this.childs = Collections.EMPTY_LIST;
185
                      } else {
186
                        Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get(
187
                                Search.COMPLEMENT_MANE, featureType
188
                        );
189
                        List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
190
                                filterByDataType,
191
                                Search.LABEL_ORDER,
192
                                -1
193
                        );
194
                        this.childs = new ArrayList<>();
195
                        for (Search.OrderedAttribute attribute : attributes) {
196
                          this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType()));
197
                        }
198
                        stores.add(fullName);
199
                      }
200
                    }
201
                  }
202
                }
203
              }
204
              break;
205

    
206
            case DynField.RELATION_TYPE_NONE: 
207
            default:
208
              break;
209
          }
210
        } catch(Exception ex) {
211
          this.childs = Collections.EMPTY_LIST;
212
          LOGGER.warn("Can't get childs of "+descriptor.getName(),ex);
213
        }
214
      }
215
      return this.childs;
216
    }
217

    
218
    @Override
219
    public boolean isLeaf() {
220
      return this.getChildren().isEmpty();
221
    }
222
  }
223

    
224
  private final Set<String> stores;
225
  private final DefaultNode root;
226
  private final Predicate<FeatureAttributeDescriptor> filterByDataType;
227
  private boolean showRelations;
228

    
229
  public FeatureAttributeTreeModel(FeatureStore store, boolean showRelations, Predicate<FeatureAttributeDescriptor> filterByDataType) {
230
    if (filterByDataType == null) {
231
      this.filterByDataType = Search.ALL_FILTER;
232
    } else {
233
      this.filterByDataType = filterByDataType;
234
    }
235
    FeatureType featureType;
236
    try {
237
      featureType = store.getDefaultFeatureType();
238
    } catch (Exception ex) {
239
      throw new RuntimeException("Can't access attributes of store", ex);
240
    }
241
    Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get(
242
            Search.COMPLEMENT_MANE, featureType
243
    );
244
    List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
245
            filterByDataType,
246
            Search.LABEL_ORDER,
247
            -1
248
    );
249
    this.root = new DefaultNode(store, attributes);
250
    this.stores = new HashSet<>();
251
    this.showRelations = showRelations;
252
  }
253

    
254
  @Override
255
  public Object getRoot() {
256
    return this.root;
257
  }
258

    
259
  @Override
260
  public int getChildCount(Object parent) {
261
    DefaultNode node = (DefaultNode) parent;
262
    return node.getChildren().size();
263
  }
264

    
265
  @Override
266
  public Object getChild(Object parent, int index) {
267
    DefaultNode node = (DefaultNode) parent;
268
    return node.getChildren().get(index);
269
  }
270

    
271
  @Override
272
  public boolean isLeaf(Object node) {
273
    return ((DefaultNode) node).isLeaf();
274
  }
275

    
276
  @Override
277
  public int getIndexOfChild(Object parent, Object child) {
278
    try {
279
      DefaultNode parantNode = (DefaultNode) parent;
280
      DefaultNode childNode = (DefaultNode) child;
281
      int index = 0;
282
      for (Node node : parantNode.getChildren()) {
283
        if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) {
284
          return index;
285
        }
286
        index++;
287
      }
288
    } catch (Exception ex) {
289

    
290
    }
291
    return 0;
292
  }
293

    
294
  @Override
295
  public void valueForPathChanged(TreePath path, Object newValue) {
296
  }
297

    
298
  @Override
299
  public void addTreeModelListener(TreeModelListener l) {
300
  }
301

    
302
  @Override
303
  public void removeTreeModelListener(TreeModelListener l) {
304
  }
305

    
306
}