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 / actions / SelectionSetAction.java @ 47693

History | View | Annotate | Download (6.2 KB)

1
package org.gvsig.fmap.dal.swing.impl.actions;
2

    
3
import java.awt.event.ActionEvent;
4
import java.util.Objects;
5
import javax.swing.AbstractAction;
6
import javax.swing.Action;
7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.lang3.mutable.MutableBoolean;
9
import org.gvsig.expressionevaluator.Code;
10
import org.gvsig.expressionevaluator.Expression;
11
import org.gvsig.fmap.dal.DataStore;
12
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
13
import org.gvsig.fmap.dal.feature.FeatureQuery;
14
import org.gvsig.fmap.dal.feature.FeatureSelection;
15
import org.gvsig.fmap.dal.feature.FeatureSet;
16
import org.gvsig.fmap.dal.feature.FeatureStore;
17
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory;
18
import org.gvsig.fmap.dal.swing.DALActionFactory.DALActionContext;
19
import org.gvsig.fmap.dal.swing.DALSwingLocator;
20
import org.gvsig.fmap.dal.swing.DataSwingManager;
21
import org.gvsig.tools.ToolsLocator;
22
import org.gvsig.tools.dispose.DisposeUtils;
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.visitor.VisitCanceledException;
27
import org.gvsig.tools.visitor.Visitor;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30

    
31
/**
32
 *
33
 * @author jjdelcerro
34
 */
35
@SuppressWarnings("UseSpecificCatch")
36
public class SelectionSetAction 
37
        extends AbstractAction 
38
    {
39

    
40
    public static class SelectionSetActionFactory extends AbstractDALActionFactory {
41

    
42
        public static final String ACTION_NAME = "SelectionSet";
43
    
44
        public SelectionSetActionFactory() {
45
            super(ACTION_NAME);
46
        }
47
        
48
        @Override
49
        public Action createAction(DALActionContext context) {
50
            return new SelectionSetAction(context);
51
        }
52

    
53
        public static void selfRegister() {
54
            DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager();
55
            dalSwingManager.registerStoreAction(new SelectionSetActionFactory());
56
        }        
57
    }
58

    
59
    private static final Logger LOGGER = LoggerFactory.getLogger(SelectionSetAction.class);
60
    
61
    private final DALActionContext context;
62

    
63
    @SuppressWarnings("OverridableMethodCallInConstructor")
64
    public SelectionSetAction(DALActionContext context) {
65
        this.context = context;
66
        I18nManager i18n = ToolsLocator.getI18nManager();
67
        this.putValue(
68
                Action.ACTION_COMMAND_KEY, 
69
                SelectionSetActionFactory.ACTION_NAME
70
        );
71
        this.putValue(
72
                Action.SHORT_DESCRIPTION, 
73
                i18n.getTranslation("_New_selection")
74
        );
75
        this.putValue(
76
                Action.SMALL_ICON, 
77
                ToolsSwingLocator.getIconThemeManager().getCurrent().get("storeaction-select")
78
        );
79
    }
80
    
81
    @Override
82
    public void actionPerformed(ActionEvent e) {
83
        FeatureSet selection = null;
84
        try {
85
            DataStore store = this.context.getStore();
86
            if (!(store instanceof FeatureStore)) {
87
                return;
88
            }
89
            FeatureStore featureStore = (FeatureStore) store;
90
            FeatureSelection currentSelection = this.context.getSelecteds();
91
            if (currentSelection != null) {
92
                featureStore.setSelection(currentSelection);
93
                return;
94
            }
95
            FeatureQuery query = null;
96
            FeatureQuery contextQuery = this.context.getQuery();
97
            if (contextQuery != null) {
98
                Expression filter = contextQuery.getExpressionFilter();
99
                if (filter != null) {
100
                    query = featureStore.createFeatureQuery();
101
                    query.setFilter(filter);
102
                    for (EditableFeatureAttributeDescriptor extraColumn : contextQuery.getExtraColumns()) {
103
                        if(expressionUseIdentifier(filter, extraColumn.getName())){
104
                            query.getExtraColumns().getColumns().add(extraColumn);
105
                        }
106
                    }
107
//                    query.retrievesAllAttributes();
108
                }
109
            } 
110
            currentSelection = featureStore.getFeatureSelection();
111
            if( query == null ) {
112
                currentSelection.selectAll();
113
                return;
114
            }
115
            selection = featureStore.getFeatureSet(query);
116
            currentSelection.deselectAll();
117
            currentSelection.select(selection);
118
        } catch (Exception ex) {
119
            LOGGER.warn("Can't build selection.", ex);
120
        } finally {
121
            DisposeUtils.disposeQuietly(selection);
122
        }
123
    }
124

    
125
    @SuppressWarnings("Convert2Lambda")
126
    private boolean expressionUseIdentifier(Expression filter, String name) {
127
        MutableBoolean res = new MutableBoolean(false);
128
        try {
129
            filter.getCode().accept(new Visitor() {
130
                @Override
131
                public void visit(Object obj) throws VisitCanceledException, BaseException {
132
                    if (((Code) obj).code() == Code.IDENTIFIER) {
133
                        if (StringUtils.equalsIgnoreCase(name, ((Code.Identifier) obj).name())) {
134
                            res.setTrue();
135
                            throw new VisitCanceledException();
136
                        }
137
                    } else if (((Code) obj).code() == Code.CALLABLE) {
138
                        Code.Callable callable = (Code.Callable) obj;
139
                        if (StringUtils.equalsIgnoreCase("GETATTR", callable.name()) &&
140
                            callable.parameters().size() == 2 &&
141
                            callable.parameters().get(1).code() == Code.CONSTANT) {
142
                            Code.Constant p1 = (Code.Constant) callable.parameters().get(1);
143
                            if (StringUtils.equalsIgnoreCase(name, Objects.toString(p1.value(), null))) {
144
                                res.setTrue();
145
                                throw new VisitCanceledException();
146
                            }
147
                        }
148
                    }
149
                }
150
            });
151
        } catch (VisitCanceledException e) {
152
        } catch (Exception e) {
153
            throw new RuntimeException("Can't check if '"+name+"' is in '"+filter.getPhrase()+"'", e);
154
        }
155
        return res.booleanValue();
156
    }
157

    
158

    
159
}