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 / expressionevaluator / FeatureAttributeElement.java @ 44205
History | View | Annotate | Download (5.73 KB)
1 | 43983 | jjdelcerro | package org.gvsig.fmap.dal.swing.impl.expressionevaluator; |
---|---|---|---|
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.Iterator; |
||
5 | import java.util.List; |
||
6 | 44128 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
7 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.Function; |
8 | 44205 | jjdelcerro | import org.gvsig.expressionevaluator.SymbolTable; |
9 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.swing.Element; |
10 | import org.gvsig.expressionevaluator.swing.Element.GroupElement; |
||
11 | import org.gvsig.expressionevaluator.swing.Element.SimpleElement; |
||
12 | import org.gvsig.expressionevaluator.swing.Element.VariableElement; |
||
13 | import org.gvsig.expressionevaluator.swing.ExpressionBuilderConfig; |
||
14 | import org.gvsig.expressionevaluator.swing.spi.AbstractElement; |
||
15 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
||
16 | import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
||
17 | import org.gvsig.fmap.dal.feature.Feature; |
||
18 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
19 | import org.gvsig.fmap.dal.feature.FeatureSet; |
||
20 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
21 | 44128 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
22 | 43983 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
23 | 44128 | jjdelcerro | import org.gvsig.tools.i18n.I18nManager; |
24 | 43983 | jjdelcerro | import org.gvsig.tools.visitor.VisitCanceledException; |
25 | import org.gvsig.tools.visitor.Visitor; |
||
26 | |||
27 | /**
|
||
28 | *
|
||
29 | * @author jjdelcerro
|
||
30 | */
|
||
31 | public class FeatureAttributeElement |
||
32 | extends AbstractElement
|
||
33 | implements
|
||
34 | GroupElement, |
||
35 | SimpleElement, |
||
36 | VariableElement |
||
37 | { |
||
38 | |||
39 | final FeatureStore store;
|
||
40 | final FeatureAttributeDescriptor attrdesc;
|
||
41 | List<Element> elements; |
||
42 | 44205 | jjdelcerro | final ExpressionBuilderConfig config;
|
43 | 43983 | jjdelcerro | boolean hasMoreElements;
|
44 | |||
45 | FeatureAttributeElement(ExpressionBuilderConfig configPanel, FeatureStore store, FeatureAttributeDescriptor attrdesc) { |
||
46 | 44128 | jjdelcerro | super(attrdesc.getName(), "expressionbuilder-element-table-field"); |
47 | 44205 | jjdelcerro | this.config = configPanel;
|
48 | 43983 | jjdelcerro | this.store = store;
|
49 | this.attrdesc = attrdesc;
|
||
50 | this.elements = null; |
||
51 | this.hasMoreElements = false; |
||
52 | } |
||
53 | |||
54 | @Override
|
||
55 | public String getTypeName() { |
||
56 | return this.attrdesc.getDataTypeName(); |
||
57 | } |
||
58 | |||
59 | @Override
|
||
60 | public Element get(int index) { |
||
61 | return this.getElements().get(index); |
||
62 | } |
||
63 | 44205 | jjdelcerro | |
64 | @Override
|
||
65 | public boolean isEnabled() { |
||
66 | if( this.config.isSQLCompatible() ) { |
||
67 | if( this.attrdesc.isComputed() ) { |
||
68 | return false; |
||
69 | } |
||
70 | } |
||
71 | return true; |
||
72 | } |
||
73 | 43983 | jjdelcerro | |
74 | @Override
|
||
75 | public int size() { |
||
76 | return this.getElements().size(); |
||
77 | } |
||
78 | |||
79 | @Override
|
||
80 | public Iterator<Element> iterator() { |
||
81 | return this.getElements().iterator(); |
||
82 | } |
||
83 | |||
84 | @Override
|
||
85 | public void addElement(Element element) { |
||
86 | |||
87 | } |
||
88 | |||
89 | @Override
|
||
90 | public void addElement(Function function) { |
||
91 | |||
92 | } |
||
93 | |||
94 | @Override
|
||
95 | 43987 | jjdelcerro | public void reload() { |
96 | this.elements=null; |
||
97 | hasMoreElements = false;
|
||
98 | } |
||
99 | |||
100 | @Override
|
||
101 | 43983 | jjdelcerro | public List<Element> getElements() { |
102 | if( this.elements==null ) { |
||
103 | hasMoreElements = false;
|
||
104 | ExpressionEvaluatorSwingManager manager = ExpressionEvaluatorSwingLocator.getManager(); |
||
105 | final List<Object> values = new ArrayList<>(); |
||
106 | 44205 | jjdelcerro | final int limit = this.config==null? 60 : this.config.getSimpleElementsLimit(); |
107 | 43987 | jjdelcerro | final long timeLimit = System.currentTimeMillis() + limit*1000; |
108 | 43983 | jjdelcerro | try {
|
109 | FeatureSet set = this.store.getFeatureSet();
|
||
110 | set.accept(new Visitor() {
|
||
111 | @Override
|
||
112 | public void visit(Object o) throws VisitCanceledException, BaseException { |
||
113 | Object value = ((Feature)o).get(attrdesc.getName());
|
||
114 | if( !values.contains(value ) ) {
|
||
115 | values.add(value); |
||
116 | } |
||
117 | 43987 | jjdelcerro | if( System.currentTimeMillis() > timeLimit ) { |
118 | 43983 | jjdelcerro | hasMoreElements = true;
|
119 | throw new VisitCanceledException(); |
||
120 | } |
||
121 | } |
||
122 | }); |
||
123 | } catch(VisitCanceledException ex) {
|
||
124 | |||
125 | } catch (Exception ex) { |
||
126 | // FIXME
|
||
127 | } |
||
128 | this.elements = new ArrayList<>(); |
||
129 | for (Object value : values) { |
||
130 | 44205 | jjdelcerro | this.elements.add(manager.createConstantElement(this.config, value)); |
131 | 43983 | jjdelcerro | } |
132 | } |
||
133 | return this.elements; |
||
134 | } |
||
135 | |||
136 | @Override
|
||
137 | public boolean hasSubgroups() { |
||
138 | return false; |
||
139 | } |
||
140 | |||
141 | @Override
|
||
142 | public String getSource() { |
||
143 | return this.store.getName(); |
||
144 | } |
||
145 | |||
146 | @Override
|
||
147 | public String getLabel() { |
||
148 | return "Field"; |
||
149 | } |
||
150 | |||
151 | @Override
|
||
152 | public boolean hasMoreElements() { |
||
153 | return this.hasMoreElements; |
||
154 | } |
||
155 | 44128 | jjdelcerro | |
156 | @Override
|
||
157 | public String getDescription() { |
||
158 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
159 | |||
160 | StringBuilder html = new StringBuilder(); |
||
161 | html.append("<html>\n");
|
||
162 | |||
163 | html.append("<b>").append(this.getLabel()).append("</b><br>\n<br>\n"); |
||
164 | html.append("<b>").append(i18n.getTranslation("_Source")).append("</b> ").append(this.getSource()).append("<br>\n"); |
||
165 | html.append("<b>").append(i18n.getTranslation("_Name")).append("</b> ").append(this.getName()).append("<br>\n"); |
||
166 | html.append("<b>").append(i18n.getTranslation("_Type")).append("</b> ").append(this.getTypeName()).append("<br>\n"); |
||
167 | if( !StringUtils.isBlank(this.attrdesc.getDescription()) && |
||
168 | StringUtils.equalsAnyIgnoreCase(this.getName(), this.attrdesc.getDescription()) ) { |
||
169 | html.append("<b>").append(i18n.getTranslation("_Description")).append("</b><br>\n"); |
||
170 | html.append(this.attrdesc.getDescription().replace("\n", "<br>")).append("<br>\n"); |
||
171 | } |
||
172 | html.append("</html>\n");
|
||
173 | return html.toString();
|
||
174 | } |
||
175 | 43983 | jjdelcerro | |
176 | 44128 | jjdelcerro | |
177 | 43983 | jjdelcerro | } |