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

History | View | Annotate | Download (8.01 KB)

1
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
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.fmap.dal.complements.Search;
14
import org.gvsig.fmap.dal.complements.Search.OrderedAttribute;
15
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
import static org.gvsig.fmap.dal.swing.impl.searchpanel.DefaultSearchPanel.getAttributeDescriptorLabel;
21
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
@SuppressWarnings("UseSpecificCatch")
30
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
        public List<Node> getChildren();
39
    }
40

    
41
    private class DefaultNode
42
            extends LabeledValueImpl<FeatureAttributeDescriptor>
43
            implements Node {
44

    
45
        private List<Node> childs;
46
        private final FeatureStore store;
47
        private final int type;
48

    
49
        public DefaultNode(FeatureStore store, List<OrderedAttribute> attributes) {
50
            super(store==null? "":store.getName(), null);
51
            this.store = store;
52
            this.type = OrderedAttribute.TYPE_REGURAL;
53
            this.childs = new ArrayList<>();
54
            for (OrderedAttribute attribute : attributes) {
55
                this.childs.add(new DefaultNode(store, attribute.getDescriptor(), attribute.getType()));
56
            }
57
        }
58

    
59
        private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute, int type) {
60
            super(attribute.getLocalizedLabel(), attribute);
61
            this.store = store;
62
            this.type = type;
63
            this.childs = null;
64
        }
65

    
66
        @Override
67
        public String getLabel() {
68
            String theLabel;
69
            FeatureAttributeDescriptor attrdesc = this.getValue();
70
            if( attrdesc==null ) {
71
                theLabel = super.getLabel();
72
            } else {
73
                String tableName = null;
74
                if( attrdesc.isForeingKey() ) {
75
                    ForeingKey foreingKey = attrdesc.getForeingKey();
76
                    tableName = foreingKey.getTableName();
77
                }
78
                theLabel = getAttributeDescriptorLabel(attrdesc, tableName);
79
            }
80
            switch(this.type) {
81
                case Search.OrderedAttribute.TYPE_REGURAL:
82
                    break;
83
                case Search.OrderedAttribute.TYPE_FAVORITE:
84
                    theLabel = "<html><b>"+theLabel+"</b></html>";
85
                    break;
86
                case Search.OrderedAttribute.TYPE_RECENT:
87
                    theLabel = "<html><i><b>"+theLabel+"</b></i></html>";
88
                    break;
89
            }
90
            return theLabel;
91
        }
92

    
93
        @Override
94
        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
        public List<Node> getChildren() {
105
            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
                    if( theStore==null ) {
112
                        this.childs = Collections.EMPTY_LIST;
113
                    } else {
114
                        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
                        }
133
                    }
134
                } else {
135
                    this.childs = Collections.EMPTY_LIST;
136
                }
137
            }
138
            return this.childs;
139
        }
140

    
141
        @Override
142
        public boolean isLeaf() {
143
            return this.getChildren().isEmpty();
144
        }
145
    }
146

    
147
    private final Set<String> stores;
148
    private final DefaultNode root;
149
    private final Predicate<FeatureAttributeDescriptor> filterByDataType;
150

    
151
    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
        List<Search.OrderedAttribute> attributes = featureTypeSearch.getOrderedAttributes(
167
                filterByDataType,
168
                Search.LABEL_ORDER,
169
                -1
170
        );
171
        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
        return node.getChildren().size();
184
    }
185

    
186
    @Override
187
    public Object getChild(Object parent, int index) {
188
        DefaultNode node = (DefaultNode) parent;
189
        return node.getChildren().get(index);
190
    }
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
            for (Node node : parantNode.getChildren()) {
204
                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
}