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 @ 44338

History | View | Annotate | Download (8.92 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.ExpressionUtils;
12
import org.gvsig.expressionevaluator.swing.Element;
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.DataManager;
18
import org.gvsig.fmap.dal.feature.Feature;
19
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
20
import org.gvsig.fmap.dal.feature.FeatureSet;
21
import org.gvsig.fmap.dal.feature.FeatureStore;
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.swing.api.ToolsSwingLocator;
26
import org.gvsig.tools.swing.icontheme.IconTheme;
27
import org.gvsig.tools.util.LabeledValue;
28
import org.gvsig.tools.visitor.VisitCanceledException;
29
import org.gvsig.tools.visitor.Visitor;
30

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

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

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

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

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

    
77
    @Override
78
    public String getRenderedValue() {
79
        if( this.parent instanceof DefaultFeatureStoreElement2 ) {
80
            return super.getRenderedValue();
81
        }
82
        List<String> fieldNames = new ArrayList<>();
83
        fieldNames.add(attrdesc.getName());
84
        Element theParent = this.parent;
85
        while( theParent!=null ) {
86
            if( theParent instanceof DefaultFeatureStoreElement2 ) {
87
                break;
88
            }
89
            if( !(theParent instanceof FeatureAttributeElement) ) {
90
                break; // Esto no deberia pasar
91
            }
92
            fieldNames.add(0, theParent.getName());
93
            theParent = ((FeatureAttributeElement)theParent).getParent();
94
        }
95
        ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
96
        ExpressionBuilder.Function list = builder.list();
97
        for (String fieldName : fieldNames) {
98
            list.parameter(builder.constant(fieldName));
99
        }
100
        String formula = builder.function(DataManager.FUNCTION_FOREING_VALUE, list).toString();
101
        return formula;
102
    }
103
    
104
    
105
    @Override
106
    public void reload() {
107
        this.values = null;
108
        hasMoreValues = false;
109
    }
110

    
111
    @Override
112
    public boolean isEnabled() {
113
        if (this.getConfig().getPreferences().isSQLCompatible()) {
114
            if (this.attrdesc.isComputed()) {
115
                return false;
116
            }
117
        }
118
        return true;
119
    }
120

    
121
    @Override
122
    public boolean hasMoreValues() {
123
        return this.hasMoreValues;
124
    }
125

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

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

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

    
207
    @Override
208
    public String getDescription() {
209
        I18nManager i18n = ToolsLocator.getI18nManager();
210

    
211
        StringBuilder html = new StringBuilder();
212
        html.append("<html>\n");
213

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

    
232
    @Override
233
    @SuppressWarnings("empty-statement")
234
    public void used() {
235
        attrdesc.recentUsed();
236
    }
237
}