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 / searchpanel / AdvancedAttributeSelectionTreeModel.java @ 44644

History | View | Annotate | Download (8.01 KB)

1 44262 jjdelcerro
package org.gvsig.fmap.dal.swing.impl.searchpanel;
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 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
import org.gvsig.fmap.dal.complements.Search;
14 44337 jjdelcerro
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute;
15 44262 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
16
import org.gvsig.fmap.dal.feature.FeatureStore;
17
import org.gvsig.fmap.dal.feature.FeatureType;
18
import org.gvsig.fmap.dal.feature.ForeingKey;
19
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey;
20 44351 jjdelcerro
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel;
21 44262 jjdelcerro
import org.gvsig.tools.ToolsLocator;
22
import org.gvsig.tools.util.LabeledValue;
23
import org.gvsig.tools.util.LabeledValueImpl;
24
25
/**
26
 *
27
 * @author jjdelcerro
28
 */
29 44263 jjdelcerro
@SuppressWarnings("UseSpecificCatch")
30 44262 jjdelcerro
public class AdvancedAttributeSelectionTreeModel
31
        implements TreeModel {
32
33
    public interface Node extends LabeledValue<FeatureAttributeDescriptor> {
34
35
        public FeatureStore getFeatureStore();
36
        public boolean isRoot();
37
        public boolean isLeaf();
38 44263 jjdelcerro
        public List<Node> getChildren();
39 44262 jjdelcerro
    }
40
41
    private class DefaultNode
42
            extends LabeledValueImpl<FeatureAttributeDescriptor>
43
            implements Node {
44
45
        private List<Node> childs;
46
        private final FeatureStore store;
47 44337 jjdelcerro
        private final int type;
48 44262 jjdelcerro
49 44337 jjdelcerro
        public DefaultNode(FeatureStore store, List<OrderedAttribute> attributes) {
50 44262 jjdelcerro
            super(store==null? "":store.getName(), null);
51
            this.store = store;
52 44337 jjdelcerro
            this.type = OrderedAttribute.TYPE_REGURAL;
53 44262 jjdelcerro
            this.childs = new ArrayList<>();
54 44337 jjdelcerro
            for (OrderedAttribute attribute : attributes) {
55 44338 jjdelcerro
                this.childs.add(new DefaultNode(store, attribute.getDescriptor(), attribute.getType()));
56 44262 jjdelcerro
            }
57
        }
58
59 44337 jjdelcerro
        private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute, int type) {
60 44338 jjdelcerro
            super(attribute.getLocalizedLabel(), attribute);
61 44262 jjdelcerro
            this.store = store;
62 44337 jjdelcerro
            this.type = type;
63 44262 jjdelcerro
            this.childs = null;
64
        }
65
66
        @Override
67 44263 jjdelcerro
        public String getLabel() {
68 44337 jjdelcerro
            String theLabel;
69 44263 jjdelcerro
            FeatureAttributeDescriptor attrdesc = this.getValue();
70 44351 jjdelcerro
            if( attrdesc==null ) {
71
                theLabel = super.getLabel();
72 44337 jjdelcerro
            } else {
73 44351 jjdelcerro
                String tableName = null;
74
                if( attrdesc.isForeingKey() ) {
75
                    ForeingKey foreingKey = attrdesc.getForeingKey();
76
                    tableName = foreingKey.getTableName();
77
                }
78
                theLabel = getAttributeDescriptorLabel(attrdesc, tableName);
79 44263 jjdelcerro
            }
80 44337 jjdelcerro
            switch(this.type) {
81
                case Search.OrderedAttribute.TYPE_REGURAL:
82
                    break;
83
                case Search.OrderedAttribute.TYPE_FAVORITE:
84 44338 jjdelcerro
                    theLabel = "<html><b>"+theLabel+"</b></html>";
85 44337 jjdelcerro
                    break;
86
                case Search.OrderedAttribute.TYPE_RECENT:
87 44338 jjdelcerro
                    theLabel = "<html><i><b>"+theLabel+"</b></i></html>";
88 44337 jjdelcerro
                    break;
89
            }
90
            return theLabel;
91 44263 jjdelcerro
        }
92
93
        @Override
94 44262 jjdelcerro
        public FeatureStore getFeatureStore() {
95
            return this.store;
96
        }
97
98
        @Override
99
        public boolean isRoot() {
100
            return this.getValue() == null;
101
        }
102
103
        @Override
104 44263 jjdelcerro
        public List<Node> getChildren() {
105 44262 jjdelcerro
            if (this.childs == null) {
106
                if (this.getValue().isForeingKey()) {
107
                    ForeingKey foreingKey = this.getValue().getForeingKey();
108
                    ContextForeingKey context = foreingKey.createContext();
109
                    // Ojo, no liberamos el contexto para que no se destruya el store.
110
                    FeatureStore theStore = foreingKey.getFeatureStore(context);
111 44338 jjdelcerro
                    if( theStore==null ) {
112 44262 jjdelcerro
                        this.childs = Collections.EMPTY_LIST;
113
                    } else {
114 44338 jjdelcerro
                        FeatureType featureType = foreingKey.getFeatureType(context);
115
                        String fullName = theStore.getFullName();
116
                        if (stores.contains(fullName)) {
117
                            // Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
118
                            this.childs = Collections.EMPTY_LIST;
119
                        } else {
120
                            Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get(
121
                                    Search.COMPLEMENT_MANE, featureType
122
                            );
123
                            List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
124
                                    filterByDataType,
125
                                    Search.LABEL_ORDER,
126
                                    -1
127
                            );
128
                            this.childs = new ArrayList<>();
129
                            for (Search.OrderedAttribute attribute : attributes) {
130
                                this.childs.add(new DefaultNode(theStore, attribute.getDescriptor(), attribute.getType()));
131
                            }
132 44262 jjdelcerro
                        }
133
                    }
134
                } else {
135
                    this.childs = Collections.EMPTY_LIST;
136
                }
137
            }
138
            return this.childs;
139
        }
140
141
        @Override
142
        public boolean isLeaf() {
143 44263 jjdelcerro
            return this.getChildren().isEmpty();
144 44262 jjdelcerro
        }
145
    }
146
147
    private final Set<String> stores;
148
    private final DefaultNode root;
149 44263 jjdelcerro
    private final Predicate<FeatureAttributeDescriptor> filterByDataType;
150 44262 jjdelcerro
151 44263 jjdelcerro
    public AdvancedAttributeSelectionTreeModel(FeatureStore store, Predicate<FeatureAttributeDescriptor> filterByDataType) {
152
        if( filterByDataType == null ) {
153
            this.filterByDataType = Search.ALL_FILTER;
154
        } else {
155
            this.filterByDataType = filterByDataType;
156
        }
157
        FeatureType featureType;
158
        try {
159
            featureType = store.getDefaultFeatureType();
160
        } catch (Exception ex) {
161
            throw new RuntimeException("Can't access attributes of store", ex);
162
        }
163
        Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get(
164
                Search.COMPLEMENT_MANE, featureType
165
        );
166 44337 jjdelcerro
        List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
167 44263 jjdelcerro
                filterByDataType,
168
                Search.LABEL_ORDER,
169
                -1
170
        );
171 44262 jjdelcerro
        this.root = new DefaultNode(store, attributes);
172
        this.stores = new HashSet<>();
173
    }
174
175
    @Override
176
    public Object getRoot() {
177
        return this.root;
178
    }
179
180
    @Override
181
    public int getChildCount(Object parent) {
182
        DefaultNode node = (DefaultNode) parent;
183 44263 jjdelcerro
        return node.getChildren().size();
184 44262 jjdelcerro
    }
185
186
    @Override
187
    public Object getChild(Object parent, int index) {
188
        DefaultNode node = (DefaultNode) parent;
189 44263 jjdelcerro
        return node.getChildren().get(index);
190 44262 jjdelcerro
    }
191
192
    @Override
193
    public boolean isLeaf(Object node) {
194
        return ((DefaultNode) node).isLeaf();
195
    }
196
197
    @Override
198
    public int getIndexOfChild(Object parent, Object child) {
199
        try {
200
            DefaultNode parantNode = (DefaultNode) parent;
201
            DefaultNode childNode = (DefaultNode) child;
202
            int index = 0;
203 44263 jjdelcerro
            for (Node node : parantNode.getChildren()) {
204 44262 jjdelcerro
                if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) {
205
                    return index;
206
                }
207
                index++;
208
            }
209
        } catch (Exception ex) {
210
211
        }
212
        return 0;
213
    }
214
215
    @Override
216
    public void valueForPathChanged(TreePath path, Object newValue) {
217
    }
218
219
    @Override
220
    public void addTreeModelListener(TreeModelListener l) {
221
    }
222
223
    @Override
224
    public void removeTreeModelListener(TreeModelListener l) {
225
    }
226
227
}