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