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 / expressionevaluator / FeatureAttributeElement.java @ 44376

History | View | Annotate | Download (9.07 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 javax.swing.SwingUtilities;
9
import org.apache.commons.lang3.StringUtils;
10
import org.gvsig.expressionevaluator.ExpressionBuilder;
11
import org.gvsig.expressionevaluator.ExpressionBuilder.Constant;
12
import org.gvsig.expressionevaluator.ExpressionUtils;
13
import org.gvsig.expressionevaluator.swing.Element;
14
import org.gvsig.expressionevaluator.swing.Element.SimpleElement;
15
import org.gvsig.expressionevaluator.swing.spi.AbstractElement;
16
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingLocator;
17
import org.gvsig.expressionevaluator.swing.ExpressionEvaluatorSwingManager;
18
import org.gvsig.fmap.dal.DataManager;
19
import org.gvsig.fmap.dal.feature.Feature;
20
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
21
import org.gvsig.fmap.dal.feature.FeatureSet;
22
import org.gvsig.fmap.dal.feature.FeatureStore;
23
import org.gvsig.tools.ToolsLocator;
24
import org.gvsig.tools.exception.BaseException;
25
import org.gvsig.tools.i18n.I18nManager;
26
import org.gvsig.tools.swing.api.ToolsSwingLocator;
27
import org.gvsig.tools.swing.icontheme.IconTheme;
28
import org.gvsig.tools.util.LabeledValue;
29
import org.gvsig.tools.visitor.VisitCanceledException;
30
import org.gvsig.tools.visitor.Visitor;
31

    
32
/**
33
 *
34
 * @author jjdelcerro
35
 */
36
public class FeatureAttributeElement
37
        extends AbstractElement
38
        implements
39
        SimpleElement
40
    {
41

    
42
    final FeatureStore store;
43
    final FeatureAttributeDescriptor attrdesc;
44
    List<Element> values;
45
    boolean hasMoreValues;
46
    private final Element parent;
47

    
48
    FeatureAttributeElement(Element parent, FeatureStore store, FeatureAttributeDescriptor attrdesc) {
49
        this(parent, store, attrdesc, "featurestore-column");
50
    }
51
    
52
    FeatureAttributeElement(Element parent, FeatureStore store, FeatureAttributeDescriptor attrdesc, String iconName) {
53
        super(attrdesc.getName(), attrdesc.getName(), iconName);
54
        this.parent = parent;
55
        this.store = store;
56
        this.attrdesc = attrdesc;
57
        this.hasMoreValues = false;
58

    
59
        IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
60
        String theIconName = attrdesc.getDataType().getIconName();
61
        IconTheme.Icon theIcon = iconTheme.getThemeIcon(theIconName);
62
        if( theIcon!=null && theIcon.getURL()!=null ) {
63
            this.icon = ToolsSwingLocator.getToolsSwingManager().createCompoundIcon(
64
                    SwingUtilities.HORIZONTAL, 
65
                    1, 
66
                    SwingUtilities.LEFT, 
67
                    SwingUtilities.TOP,
68
                    this.getIcon(),
69
                    theIcon.getImageIcon()
70
            );
71
        }
72
    }
73

    
74
    public Element getParent() {
75
        return this.parent;
76
    }
77

    
78
    @Override
79
    public String getRenderedValue() {
80
        if( this.parent instanceof DefaultFeatureStoreElement2 ) {
81
            return super.getRenderedValue();
82
        }
83
        List<String> fieldNames = new ArrayList<>();
84
        fieldNames.add(attrdesc.getName());
85
        Element theParent = this.parent;
86
        while( theParent!=null ) {
87
            if( theParent instanceof DefaultFeatureStoreElement2 ) {
88
                break;
89
            }
90
            if( !(theParent instanceof FeatureAttributeElement) ) {
91
                break; // Esto no deberia pasar
92
            }
93
            fieldNames.add(0, theParent.getName());
94
            theParent = ((FeatureAttributeElement)theParent).getParent();
95
        }
96
        ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
97

    
98
        Constant[] params = new Constant[fieldNames.size()];
99
        for (int i = 0; i < fieldNames.size(); i++) {
100
            params[i] = builder.constant(fieldNames.get(i));
101
        }
102
        ExpressionBuilder.Function tuple = builder.tuple(params);
103

    
104
        String formula = builder.function(DataManager.FUNCTION_FOREING_VALUE, tuple).toString();
105
        return formula;
106
    }
107
    
108
    
109
    @Override
110
    public void reload() {
111
        this.values = null;
112
        hasMoreValues = false;
113
    }
114

    
115
    @Override
116
    public boolean isEnabled() {
117
        if (this.getConfig().getPreferences().isSQLCompatible()) {
118
            if (this.attrdesc.isComputed()) {
119
                return false;
120
            }
121
        }
122
        return true;
123
    }
124

    
125
    @Override
126
    public boolean hasMoreValues() {
127
        return this.hasMoreValues;
128
    }
129

    
130
    @Override
131
    public List<Element> getValues() {
132
        if (this.values == null) {
133
            hasMoreValues = false;
134
            ExpressionEvaluatorSwingManager manager = ExpressionEvaluatorSwingLocator.getManager();
135
            final List<Object> values = new ArrayList<>();
136
            final int limit = this.getConfig() == null ? 60 : this.getConfig().getPreferences().getSimpleElementsLimit();
137
            final long timeLimit = System.currentTimeMillis() + limit * 1000;
138
            try {
139
                FeatureSet set = this.store.getFeatureSet();
140
                set.accept(new Visitor() {
141
                    @Override
142
                    public void visit(Object o) throws VisitCanceledException, BaseException {
143
                        Object value = ((Feature) o).get(attrdesc.getName());
144
                        if (!values.contains(value)) {
145
                            values.add(value);
146
                        }
147
                        if (System.currentTimeMillis() > timeLimit) {
148
                            hasMoreValues = true;
149
                            throw new VisitCanceledException();
150
                        }
151
                    }
152
                });
153
            } catch (VisitCanceledException ex) {
154

    
155
            } catch (Exception ex) {
156
                // FIXME
157
            }
158
            this.values = new ArrayList<>();
159
            if (!values.isEmpty()) {
160
                LabeledValue[] availableValues = this.attrdesc.getAvailableValues();
161
                Map<String, String> availableValuesMap = new HashMap<>();
162
                if( availableValues!=null ) {
163
                    for (LabeledValue availableValue : availableValues) {
164
                        availableValuesMap.put(
165
                                Objects.toString(availableValue.getValue()),
166
                                availableValue.getLabel()
167
                        );
168
                    }
169
                }
170
                for (Object value : values) {
171
                    String key = Objects.toString(value);
172
                    String label = availableValuesMap.getOrDefault(key, null);
173
                    ConstantElement e = manager.createConstantElement(
174
                            value,
175
                            label
176
                    );
177
                    this.values.add(e);
178
                    e.setConfig(this.getConfig());
179
                }
180
            }
181
        }
182
        return this.values;
183
    }
184

    
185
    @Override
186
    public String getName() {
187
        return attrdesc.getName();
188
    }
189
    
190
    @Override
191
    public String getLabel() {
192
        if (getConfig().getPreferences().getShowLabelsOfElements()) {
193
            if (!StringUtils.equals(attrdesc.getName(), attrdesc.getLabel())) {
194
                return String.format(
195
                    "<html>%s<br><font size=\"2\"><i>(%s)</i></font></html>", 
196
                    attrdesc.getLocalizedLabel(), 
197
                    attrdesc.getName()
198
                );
199
            }
200
            return String.format(
201
                "<html>%s</html>", 
202
                attrdesc.getLocalizedLabel()
203
            );
204
        }
205
        return String.format(
206
            "<html>%s</html>", 
207
            attrdesc.getName()
208
        );
209
    }
210

    
211
    @Override
212
    public String getDescription() {
213
        I18nManager i18n = ToolsLocator.getI18nManager();
214

    
215
        StringBuilder html = new StringBuilder();
216
        html.append("<html>\n");
217

    
218
        html.append("<b>").append(this.attrdesc.getName()).append("</b><br>");
219
        if( !StringUtils.equalsIgnoreCase(attrdesc.getName(), attrdesc.getLabel()) ) {
220
            html.append(this.attrdesc.getLabel()).append("<br>\n");
221
        }
222
        html.append("<br>\n");
223
        html.append("<b>").append(i18n.getTranslation("_Source")).append("</b> ").append(this.store.getName()).append("<br>\n");
224
        html.append("<b>").append(i18n.getTranslation("_Name")).append("</b> ").append(this.getName()).append("<br>\n");
225
        html.append("<b>").append(i18n.getTranslation("_Label")).append("</b> ").append(this.attrdesc.getLocalizedLabel()).append("<br>\n");
226
        html.append("<b>").append(i18n.getTranslation("_Type")).append("</b> ").append(this.attrdesc.getDataTypeName()).append("<br>\n");
227
        if (!StringUtils.isBlank(this.attrdesc.getDescription())
228
                && StringUtils.equalsAnyIgnoreCase(this.getName(), this.attrdesc.getDescription())) {
229
            html.append("<b>").append(i18n.getTranslation("_Description")).append("</b><br>\n");
230
            html.append(this.attrdesc.getDescription().replace("\n", "<br>")).append("<br>\n");
231
        }
232
        html.append("</html>\n");
233
        return html.toString();
234
    }
235

    
236
    @Override
237
    @SuppressWarnings("empty-statement")
238
    public void used() {
239
        attrdesc.recentUsed();
240
    }
241
}