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 @ 44263
History | View | Annotate | Download (7.86 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.List; |
6 |
import java.util.Map; |
7 |
import java.util.Objects; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
10 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
11 |
import org.gvsig.expressionevaluator.swing.Element; |
12 |
import org.gvsig.expressionevaluator.swing.Element.SimpleElement; |
13 |
import org.gvsig.expressionevaluator.swing.spi.AbstractElement; |
14 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator; |
15 |
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager; |
16 |
import org.gvsig.fmap.dal.DataManager; |
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.AdvancedAttributeSelectionTreeModel; |
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 |
SimpleElement |
37 |
{ |
38 |
|
39 |
final FeatureStore store;
|
40 |
final FeatureAttributeDescriptor attrdesc;
|
41 |
List<Element> values; |
42 |
boolean hasMoreValues;
|
43 |
private final Element parent; |
44 |
|
45 |
FeatureAttributeElement(Element parent, FeatureStore store, FeatureAttributeDescriptor attrdesc) {
|
46 |
this(parent, store, attrdesc, "featurestore-column"); |
47 |
} |
48 |
|
49 |
FeatureAttributeElement(Element parent, FeatureStore store, FeatureAttributeDescriptor attrdesc, String iconName) { |
50 |
super(attrdesc.getName(), attrdesc.getName(), iconName);
|
51 |
this.parent = parent;
|
52 |
this.store = store;
|
53 |
this.attrdesc = attrdesc;
|
54 |
this.hasMoreValues = false; |
55 |
} |
56 |
|
57 |
public Element getParent() { |
58 |
return this.parent; |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public String getRenderedValue() { |
63 |
if( this.parent instanceof DefaultFeatureStoreElement2 ) { |
64 |
return super.getRenderedValue(); |
65 |
} |
66 |
List<String> fieldNames = new ArrayList<>(); |
67 |
fieldNames.add(attrdesc.getName()); |
68 |
Element theParent = this.parent; |
69 |
while( theParent!=null ) { |
70 |
if( theParent instanceof DefaultFeatureStoreElement2 ) { |
71 |
break;
|
72 |
} |
73 |
if( !(theParent instanceof FeatureAttributeElement) ) { |
74 |
break; // Esto no deberia pasar |
75 |
} |
76 |
fieldNames.add(0, theParent.getName());
|
77 |
theParent = ((FeatureAttributeElement)theParent).getParent(); |
78 |
} |
79 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
80 |
ExpressionBuilder.Function list = builder.list(); |
81 |
for (String fieldName : fieldNames) { |
82 |
list.parameter(builder.constant(fieldName)); |
83 |
} |
84 |
String formula = builder.function(DataManager.FUNCTION_FOREING_VALUE, list).toString();
|
85 |
return formula;
|
86 |
} |
87 |
|
88 |
|
89 |
@Override
|
90 |
public void reload() { |
91 |
this.values = null; |
92 |
hasMoreValues = false;
|
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean isEnabled() { |
97 |
if (this.getConfig().getPreferences().isSQLCompatible()) { |
98 |
if (this.attrdesc.isComputed()) { |
99 |
return false; |
100 |
} |
101 |
} |
102 |
return true; |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public boolean hasMoreValues() { |
107 |
return this.hasMoreValues; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public List<Element> getValues() { |
112 |
if (this.values == null) { |
113 |
hasMoreValues = false;
|
114 |
ExpressionEvaluatorSwingManager manager = ExpressionEvaluatorSwingLocator.getManager(); |
115 |
final List<Object> values = new ArrayList<>(); |
116 |
final int limit = this.getConfig() == null ? 60 : this.getConfig().getPreferences().getSimpleElementsLimit(); |
117 |
final long timeLimit = System.currentTimeMillis() + limit * 1000; |
118 |
try {
|
119 |
FeatureSet set = this.store.getFeatureSet();
|
120 |
set.accept(new Visitor() {
|
121 |
@Override
|
122 |
public void visit(Object o) throws VisitCanceledException, BaseException { |
123 |
Object value = ((Feature) o).get(attrdesc.getName());
|
124 |
if (!values.contains(value)) {
|
125 |
values.add(value); |
126 |
} |
127 |
if (System.currentTimeMillis() > timeLimit) { |
128 |
hasMoreValues = true;
|
129 |
throw new VisitCanceledException(); |
130 |
} |
131 |
} |
132 |
}); |
133 |
} catch (VisitCanceledException ex) {
|
134 |
|
135 |
} catch (Exception ex) { |
136 |
// FIXME
|
137 |
} |
138 |
this.values = new ArrayList<>(); |
139 |
if (!values.isEmpty()) {
|
140 |
LabeledValue[] availableValues = this.attrdesc.getAvailableValues(); |
141 |
Map<String, String> availableValuesMap = new HashMap<>(); |
142 |
if( availableValues!=null ) { |
143 |
for (LabeledValue availableValue : availableValues) {
|
144 |
availableValuesMap.put( |
145 |
Objects.toString(availableValue.getValue()), |
146 |
availableValue.getLabel() |
147 |
); |
148 |
} |
149 |
} |
150 |
for (Object value : values) { |
151 |
String key = Objects.toString(value);
|
152 |
String label = availableValuesMap.getOrDefault(key, null); |
153 |
ConstantElement e = manager.createConstantElement( |
154 |
value, |
155 |
label |
156 |
); |
157 |
this.values.add(e);
|
158 |
e.setConfig(this.getConfig());
|
159 |
} |
160 |
} |
161 |
} |
162 |
return this.values; |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public String getName() { |
167 |
return attrdesc.getName();
|
168 |
} |
169 |
|
170 |
@Override
|
171 |
public String getLabel() { |
172 |
if (getConfig().getPreferences().getShowLabelsOfElements()) {
|
173 |
if (!StringUtils.equals(attrdesc.getName(), attrdesc.getLabel())) {
|
174 |
return "<html>" + attrdesc.getLabel() + "<br><font size=\"2\"><i>(" + attrdesc.getName() + ")</i></font></html>"; |
175 |
} |
176 |
} |
177 |
return "<html>" + attrdesc.getName() + "</html>"; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public String getDescription() { |
182 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
183 |
|
184 |
StringBuilder html = new StringBuilder(); |
185 |
html.append("<html>\n");
|
186 |
|
187 |
html.append("<b>").append(this.attrdesc.getName()).append("</b><br>"); |
188 |
if( !StringUtils.equalsIgnoreCase(attrdesc.getName(), attrdesc.getLabel()) ) {
|
189 |
html.append(this.attrdesc.getLabel()).append("<br>\n"); |
190 |
} |
191 |
html.append("<br>\n");
|
192 |
html.append("<b>").append(i18n.getTranslation("_Source")).append("</b> ").append(this.store.getName()).append("<br>\n"); |
193 |
html.append("<b>").append(i18n.getTranslation("_Name")).append("</b> ").append(this.getName()).append("<br>\n"); |
194 |
html.append("<b>").append(i18n.getTranslation("_Type")).append("</b> ").append(this.attrdesc.getDataTypeName()).append("<br>\n"); |
195 |
if (!StringUtils.isBlank(this.attrdesc.getDescription()) |
196 |
&& StringUtils.equalsAnyIgnoreCase(this.getName(), this.attrdesc.getDescription())) { |
197 |
html.append("<b>").append(i18n.getTranslation("_Description")).append("</b><br>\n"); |
198 |
html.append(this.attrdesc.getDescription().replace("\n", "<br>")).append("<br>\n"); |
199 |
} |
200 |
html.append("</html>\n");
|
201 |
return html.toString();
|
202 |
} |
203 |
|
204 |
@Override
|
205 |
@SuppressWarnings("empty-statement") |
206 |
public void used() { |
207 |
attrdesc.recentUsed(); |
208 |
} |
209 |
} |