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 / SelectionAddAction.java @ 47426

History | View | Annotate | Download (3.62 KB)

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

    
3
import java.awt.event.ActionEvent;
4
import javax.swing.AbstractAction;
5
import javax.swing.Action;
6
import org.gvsig.expressionevaluator.Expression;
7
import org.gvsig.fmap.dal.DataStore;
8
import org.gvsig.fmap.dal.feature.FeatureQuery;
9
import org.gvsig.fmap.dal.feature.FeatureSelection;
10
import org.gvsig.fmap.dal.feature.FeatureSet;
11
import org.gvsig.fmap.dal.feature.FeatureStore;
12
import org.gvsig.fmap.dal.swing.AbstractDALActionFactory;
13
import org.gvsig.tools.ToolsLocator;
14
import org.gvsig.tools.i18n.I18nManager;
15
import org.gvsig.tools.swing.api.ToolsSwingLocator;
16
import org.slf4j.Logger;
17
import org.slf4j.LoggerFactory;
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

    
22
/**
23
 *
24
 * @author jjdelcerro
25
 */
26
@SuppressWarnings("UseSpecificCatch")
27
public class SelectionAddAction 
28
        extends AbstractAction 
29
    {
30

    
31
    public static class SelectionAddActionFactory extends AbstractDALActionFactory {
32

    
33
        public static final String ACTION_NAME = "SelectionAdd";
34
    
35
        public SelectionAddActionFactory() {
36
            super(ACTION_NAME);
37
        }
38

    
39
        @Override
40
        public Action createAction(DALActionContext context) {
41
            return new SelectionAddAction(context);
42
        }
43

    
44
        public static void selfRegister() {
45
            DataSwingManager dalSwingManager = DALSwingLocator.getSwingManager();
46
            dalSwingManager.registerStoreAction(new SelectionAddActionFactory());
47
        }        
48
     
49
    }
50
    
51
    private static final Logger LOGGER = LoggerFactory.getLogger(SelectionAddAction.class);
52
    
53
    private final DALActionContext context;
54

    
55
    public SelectionAddAction(DALActionContext context) {
56
        this.context = context;
57
        I18nManager i18n = ToolsLocator.getI18nManager();
58
        this.putValue(
59
                Action.ACTION_COMMAND_KEY, 
60
                SelectionAddActionFactory.ACTION_NAME
61
        );
62
        this.putValue(
63
                Action.SHORT_DESCRIPTION, 
64
                i18n.getTranslation("_Add_to_selection")
65
        );
66
        this.putValue(
67
                Action.SMALL_ICON, 
68
                ToolsSwingLocator.getIconThemeManager().getCurrent().get("storeaction-select-add")
69
        );
70
    }
71
    
72
    @Override
73
    public void actionPerformed(ActionEvent e) {
74
        try {
75
            DataStore store = this.context.getStore();
76
            if( !(store instanceof FeatureStore) ) {
77
                return ;
78
            }
79
            FeatureStore featureStore = (FeatureStore) store;
80
            FeatureSelection currentSelection = this.context.getSelecteds();
81
            if (currentSelection != null) {
82
                featureStore.getFeatureSelection().select(currentSelection);
83
                return;
84
            }
85
            Expression filter = null;
86
            FeatureQuery query = null;
87
            FeatureQuery contextQuery = this.context.getQuery();
88
            if (contextQuery != null) {
89
                filter = contextQuery.getExpressionFilter();
90
                if (filter != null) {
91
                    query = featureStore.createFeatureQuery();
92
                    query.setFilter(filter);
93
                }
94
            } 
95
            currentSelection = featureStore.getFeatureSelection();
96
            if( query == null ) {
97
                currentSelection.selectAll();
98
                return;
99
            }
100
            FeatureSet selection = featureStore.getFeatureSet(query);
101
            currentSelection.select(selection);
102
        } catch (Exception ex) {
103
            LOGGER.warn("Can't build selecction.", ex);
104
        }
105
    }
106

    
107
}