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

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.Code.Caller;
15
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
import org.gvsig.fmap.dal.complements.Search;
21
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute;
22
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
23
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
24
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
25
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
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel;
30
import org.gvsig.tools.ToolsLocator;
31
import org.gvsig.tools.dynobject.DynField;
32
import org.gvsig.tools.util.LabeledValue;
33
import org.gvsig.tools.util.LabeledValueImpl;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
/**
38
 *
39
 * @author jjdelcerro
40
 */
41
@SuppressWarnings("UseSpecificCatch")
42
public class FeatureAttributeTreeModel
43
        implements TreeModel {
44

    
45
  private static final Logger LOGGER = LoggerFactory.getLogger(FeatureAttributeTreeModel.class);
46
  
47
  
48
  public interface Node extends LabeledValue<FeatureAttributeDescriptor> {
49

    
50
    public FeatureStore getFeatureStore();
51

    
52
    public boolean isRoot();
53

    
54
    public boolean isLeaf();
55

    
56
    public List<Node> getChildren();
57
  }
58

    
59
  private class DefaultNode
60
          extends LabeledValueImpl<FeatureAttributeDescriptor>
61
          implements Node {
62

    
63
    private List<Node> childs;
64
    private final FeatureStore store;
65
    private final int type;
66

    
67
    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

    
77
    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
    }
83

    
84
    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
        }
95
        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
    }
109

    
110
    @Override
111
    public String getLabel() {
112
      return this.getLabel(this.type);
113
    }
114

    
115
    @Override
116
    public FeatureStore getFeatureStore() {
117
      return this.store;
118
    }
119

    
120
    @Override
121
    public boolean isRoot() {
122
      return this.getValue() == null;
123
    }
124

    
125
    @Override
126
    public List<Node> getChildren() {
127
      if (showRelations && this.childs == null) {
128
        FeatureAttributeDescriptor descriptor = this.getValue();
129
        try {
130
          this.childs = Collections.EMPTY_LIST;
131
          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
                }
163
              }
164
              break;
165

    
166
            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
                  Caller caller = (Caller) code;
174
                  if( StringUtils.equalsIgnoreCase(caller.name(), "SELECT") ) {
175
                    Codes parameters = caller.parameters();
176
                    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
        }
216
      }
217
      return this.childs;
218
    }
219

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

    
226
  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
    }
237
    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

    
256
  @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
    }
293
    return 0;
294
  }
295

    
296
  @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
}