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

History | View | Annotate | Download (5.51 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 javax.swing.event.TreeModelListener;
9
import javax.swing.tree.TreeModel;
10
import javax.swing.tree.TreePath;
11
import org.apache.commons.lang3.StringUtils;
12
import org.gvsig.fmap.dal.complements.Search;
13
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
14
import org.gvsig.fmap.dal.feature.FeatureStore;
15
import org.gvsig.fmap.dal.feature.FeatureType;
16
import org.gvsig.fmap.dal.feature.ForeingKey;
17
import org.gvsig.fmap.dal.feature.ForeingKey.ContextForeingKey;
18
import org.gvsig.tools.ToolsLocator;
19
import org.gvsig.tools.util.LabeledValue;
20
import org.gvsig.tools.util.LabeledValueImpl;
21

    
22
/**
23
 *
24
 * @author jjdelcerro
25
 */
26
public class AdvancedAttributeSelectionTreeModel
27
        implements TreeModel {
28

    
29
    public interface Node extends LabeledValue<FeatureAttributeDescriptor> {
30

    
31
        public FeatureStore getFeatureStore();
32
        public boolean isRoot();
33
        public boolean isLeaf();
34
        public List<Node> getChilds();
35
    }
36

    
37
    private class DefaultNode
38
            extends LabeledValueImpl<FeatureAttributeDescriptor>
39
            implements Node {
40

    
41
        private List<Node> childs;
42
        private final FeatureStore store;
43

    
44
        public DefaultNode(FeatureStore store, List<FeatureAttributeDescriptor> attributes) {
45
            super(store==null? "":store.getName(), null);
46
            this.store = store;
47
            this.childs = new ArrayList<>();
48
            for (FeatureAttributeDescriptor attribute : attributes) {
49
                this.childs.add(new DefaultNode(store, attribute));
50
            }
51
        }
52

    
53
        private DefaultNode(FeatureStore store, FeatureAttributeDescriptor attribute) {
54
            super(attribute.getLabel(), attribute);
55
            this.store = store;
56
            this.childs = null;
57
        }
58

    
59
        @Override
60
        public FeatureStore getFeatureStore() {
61
            return this.store;
62
        }
63

    
64
        @Override
65
        public boolean isRoot() {
66
            return this.getValue() == null;
67
        }
68

    
69
        @Override
70
        public List<Node> getChilds() {
71
            if (this.childs == null) {
72
                if (this.getValue().isForeingKey()) {
73
                    ForeingKey foreingKey = this.getValue().getForeingKey();
74
                    ContextForeingKey context = foreingKey.createContext();
75
                    // Ojo, no liberamos el contexto para que no se destruya el store.
76
                    FeatureStore theStore = foreingKey.getFeatureStore(context);
77
                    FeatureType featureType = foreingKey.getFeatureType(context);
78
                    String fullName = theStore.getFullName();
79
                    if (stores.contains(fullName)) {
80
                        // Si ya hemos a?adido el store al arbol no lo volvemos a a?adir.
81
                        this.childs = Collections.EMPTY_LIST;
82
                    } else {
83
                        Search featureTypeSearch = (Search) ToolsLocator.getComplementsManager().get(
84
                                Search.COMPLEMENT_MANE, featureType
85
                        );
86
                        List<FeatureAttributeDescriptor> attributes = featureTypeSearch.getOrderedAttributes(
87
                                Search.BASIC_TYPES_FILTER,
88
                                Search.LABEL_ORDER,
89
                                -1
90
                        );
91
                        this.childs = new ArrayList<>();
92
                        for (FeatureAttributeDescriptor attribute : attributes) {
93
                            this.childs.add(new DefaultNode(theStore, attribute));
94
                        }
95
                    }
96
                } else {
97
                    this.childs = Collections.EMPTY_LIST;
98
                }
99
            }
100
            return this.childs;
101
        }
102

    
103
        @Override
104
        public boolean isLeaf() {
105
            return this.getChilds().isEmpty();
106
        }
107
    }
108

    
109
    private final Set<String> stores;
110
    private final DefaultNode root;
111

    
112
    public AdvancedAttributeSelectionTreeModel(FeatureStore store, List<FeatureAttributeDescriptor> attributes) {
113
        this.root = new DefaultNode(store, attributes);
114
        this.stores = new HashSet<>();
115
    }
116

    
117
    @Override
118
    public Object getRoot() {
119
        return this.root;
120
    }
121

    
122
    @Override
123
    public int getChildCount(Object parent) {
124
        DefaultNode node = (DefaultNode) parent;
125
        return node.getChilds().size();
126
    }
127

    
128
    @Override
129
    public Object getChild(Object parent, int index) {
130
        DefaultNode node = (DefaultNode) parent;
131
        return node.getChilds().get(index);
132
    }
133

    
134
    @Override
135
    public boolean isLeaf(Object node) {
136
        return ((DefaultNode) node).isLeaf();
137
    }
138

    
139
    @Override
140
    public int getIndexOfChild(Object parent, Object child) {
141
        try {
142
            DefaultNode parantNode = (DefaultNode) parent;
143
            DefaultNode childNode = (DefaultNode) child;
144
            int index = 0;
145
            for (Node node : parantNode.getChilds()) {
146
                if (StringUtils.equalsIgnoreCase(childNode.getValue().getName(), node.getValue().getName())) {
147
                    return index;
148
                }
149
                index++;
150
            }
151
        } catch (Exception ex) {
152

    
153
        }
154
        return 0;
155
    }
156

    
157
    @Override
158
    public void valueForPathChanged(TreePath path, Object newValue) {
159
    }
160

    
161
    @Override
162
    public void addTreeModelListener(TreeModelListener l) {
163
    }
164

    
165
    @Override
166
    public void removeTreeModelListener(TreeModelListener l) {
167
    }
168

    
169
}