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 |
package org.gvsig.fmap.dal.swing.impl.expressionevaluator; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.HashMap; |
5 |
import java.util.Iterator; |
6 |
import java.util.List; |
7 |
import java.util.Map; |
8 |
import java.util.Objects; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
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 |
import org.gvsig.fmap.dal.swing.impl.searchpanel.SearchUtils; |
22 |
import org.gvsig.tools.ToolsLocator; |
23 |
import org.gvsig.tools.exception.BaseException; |
24 |
import org.gvsig.tools.i18n.I18nManager; |
25 |
import org.gvsig.tools.util.LabeledValue; |
26 |
import org.gvsig.tools.visitor.VisitCanceledException; |
27 |
import org.gvsig.tools.visitor.Visitor; |
28 |
|
29 |
/**
|
30 |
*
|
31 |
* @author jjdelcerro
|
32 |
*/
|
33 |
public class FeatureAttributeElement |
34 |
extends AbstractElement
|
35 |
implements
|
36 |
GroupElement, |
37 |
SimpleElement |
38 |
{ |
39 |
|
40 |
final FeatureStore store;
|
41 |
final FeatureAttributeDescriptor attrdesc;
|
42 |
List<Element> elements; |
43 |
boolean hasMoreElements;
|
44 |
|
45 |
FeatureAttributeElement(FeatureStore store, FeatureAttributeDescriptor attrdesc) { |
46 |
super(attrdesc.getName(), attrdesc.getName(), "expressionbuilder-element-table-field"); |
47 |
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 |
|
58 |
@Override
|
59 |
public boolean isEnabled() { |
60 |
if (this.getConfig().getPreferences().isSQLCompatible()) { |
61 |
if (this.attrdesc.isComputed()) { |
62 |
return false; |
63 |
} |
64 |
} |
65 |
return true; |
66 |
} |
67 |
|
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 |
public void reload() { |
90 |
this.elements = null; |
91 |
hasMoreElements = false;
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
public List<Element> getElements() { |
96 |
if (this.elements == null) { |
97 |
hasMoreElements = false;
|
98 |
ExpressionEvaluatorSwingManager manager = ExpressionEvaluatorSwingLocator.getManager(); |
99 |
final List<Object> values = new ArrayList<>(); |
100 |
final int limit = this.getConfig() == null ? 60 : this.getConfig().getPreferences().getSimpleElementsLimit(); |
101 |
final long timeLimit = System.currentTimeMillis() + limit * 1000; |
102 |
try {
|
103 |
FeatureSet set = this.store.getFeatureSet();
|
104 |
set.accept(new Visitor() {
|
105 |
@Override
|
106 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
107 |
Object value = ((Feature) o).get(attrdesc.getName());
|
108 |
if (!values.contains(value)) {
|
109 |
values.add(value); |
110 |
} |
111 |
if (System.currentTimeMillis() > timeLimit) { |
112 |
hasMoreElements = true;
|
113 |
throw new VisitCanceledException(); |
114 |
} |
115 |
} |
116 |
}); |
117 |
} catch (VisitCanceledException ex) {
|
118 |
|
119 |
} catch (Exception ex) { |
120 |
// FIXME
|
121 |
} |
122 |
this.elements = new ArrayList<>(); |
123 |
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 |
} |
144 |
} |
145 |
return this.elements; |
146 |
} |
147 |
|
148 |
@Override
|
149 |
public boolean hasSubgroups() { |
150 |
return false; |
151 |
} |
152 |
|
153 |
@Override
|
154 |
public boolean hasMoreElements() { |
155 |
return this.hasMoreElements; |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public String getName() { |
160 |
return attrdesc.getName();
|
161 |
} |
162 |
|
163 |
@Override
|
164 |
public String getLabel() { |
165 |
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 |
} |
172 |
|
173 |
@Override
|
174 |
public String getRenderedValue() { |
175 |
return " "+attrdesc.getName()+" "; |
176 |
} |
177 |
|
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 |
html.append("<b>").append(i18n.getTranslation("_Source")).append("</b> ").append(this.store.getName()).append("<br>\n"); |
187 |
html.append("<b>").append(i18n.getTranslation("_Name")).append("</b> ").append(this.getName()).append("<br>\n"); |
188 |
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 |
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 |
|
198 |
@Override
|
199 |
@SuppressWarnings("empty-statement") |
200 |
public void used() { |
201 |
SearchUtils.getRecentUseds().add(attrdesc); |
202 |
} |
203 |
} |