Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / selectioncount / SelectionCount.java @ 42811

History | View | Annotate | Download (7.27 KB)

1
/*
2
 * Copyright 2015 DiSiD Technologies S.L.L. All rights reserved.
3
 *
4
 * Project  : DiSiD org.gvsig.app.mainplugin
5
 * SVN Id   : $Id$
6
 */
7
package org.gvsig.app.extension.selectioncount;
8

    
9
import javax.swing.JLabel;
10

    
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.feature.FeatureSelection;
13
import org.gvsig.fmap.dal.feature.FeatureStore;
14
import org.gvsig.fmap.dal.feature.FeatureStoreNotification;
15
import org.gvsig.fmap.mapcontext.events.AtomicEvent;
16
import org.gvsig.fmap.mapcontext.events.listeners.AtomicEventListener;
17
import org.gvsig.fmap.mapcontext.layers.FLayer;
18
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
19
import org.gvsig.fmap.mapcontext.layers.LayerEvent;
20
import org.gvsig.fmap.mapcontext.layers.LayerListener;
21
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
22
import org.gvsig.fmap.mapcontrol.MapControl;
23
import org.gvsig.gui.beans.controls.IControl;
24
import org.gvsig.tools.observer.Observable;
25
import org.gvsig.tools.observer.Observer;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28

    
29
/**
30
 * Counts total and selected features and shows them in the status bar
31
 * @author Daniel Martinez
32
 *
33
 */
34
public class SelectionCount implements AtomicEventListener, LayerListener,Observer{
35
    private static Logger logger =
36
        LoggerFactory.getLogger(SelectionCount.class);
37

    
38
    private MapControl mapControl;
39
    private IControl control;
40

    
41
    /**
42
     * Listens the map control to show the count through the control in the status bar
43
     * @param mapControl
44
     * @param control
45
     */
46
    public SelectionCount (MapControl mapControl, IControl control){
47
        this.mapControl=mapControl;
48
        this.control=control;
49
        mapControl.getMapContext().addAtomicEventListener(this);
50
    }
51

    
52
    /**
53
     * To use methods showFeatureCount and clean with another map control
54
     * @param control
55
     */
56
    public SelectionCount (IControl control){
57
        this.control=control;
58
    }
59

    
60
    @Override
61
    public void atomicEvent(AtomicEvent e) {
62
        LayerCollectionEvent[] events = e.getLayerCollectionEvents();
63
        for( int i=0; i<events.length ; i++ ) {
64
            if( events[i].getEventType() == LayerCollectionEvent.LAYER_ADDED ) {
65
                FLayer fLayer = events[i].getAffectedLayer();
66
                fLayer.addLayerListener(this);
67
            }
68
            if( events[i].getEventType() == LayerCollectionEvent.LAYER_REMOVED ) {
69
                FLayer fLayer = events[i].getAffectedLayer();
70
                fLayer.removeLayerListener(this);
71
                showFeatureCount(mapControl);
72
            }
73
        }
74
    }
75

    
76
    @Override
77
    public void visibilityChanged(LayerEvent e) {
78
        // Do nothing
79
    }
80

    
81
    @Override
82
    public void activationChanged(LayerEvent e) {
83
        if (e.getEventType()==LayerEvent.ACTIVATION_CHANGED){
84
            FLayer fLayer =e.getSource();
85
            if( fLayer.isAvailable() ) {
86
                showFeatureCount(mapControl);
87
                if (fLayer.isActive()){
88
                    if (fLayer instanceof FLyrVect) {
89
                        FLyrVect lyrVect = (FLyrVect) fLayer;
90
                        lyrVect.getFeatureStore().addObserver(this);
91
                    }
92
                }else{
93
                    if (fLayer instanceof FLyrVect) {
94
                        FLyrVect lyrVect = (FLyrVect) fLayer;
95
                        lyrVect.getFeatureStore().deleteObserver(this);
96
                    }
97
                }
98
            }
99
        }
100
    }
101

    
102
    @Override
103
    public void nameChanged(LayerEvent e) {
104
     // Do nothing
105
    }
106

    
107
    @Override
108
    public void editionChanged(LayerEvent e) {
109
     // Do nothing
110
    }
111

    
112
    @Override
113
    public void drawValueChanged(LayerEvent e) {
114
     // Do nothing
115
    }
116

    
117
    @Override
118
    public void update(final Observable observable, final Object notification) {
119
        if (notification instanceof FeatureStoreNotification) {
120
            FeatureStoreNotification event
121
                    = (FeatureStoreNotification) notification;
122
            if (event.getType() == FeatureStoreNotification.AFTER_DELETE ||
123
                event.getType() == FeatureStoreNotification.AFTER_INSERT ||
124
                event.getType() == FeatureStoreNotification.AFTER_FINISHEDITING ||
125
                event.getType() == FeatureStoreNotification.SELECTION_CHANGE ){
126

    
127
                if (event.getSource() instanceof FeatureStore){
128
                    showFeatureCount(mapControl);
129
                }
130
            }
131
        }
132
    }
133

    
134
    /**
135
     * Gets the features from the selected layers and shows them through the control
136
     * @param mapControl
137
     */
138
    public void showFeatureCount(MapControl mapControl){
139
        Long totalFeaturesCount=Long.valueOf(0);
140
        Long selectedFeaturesCount=Long.valueOf(0);;
141
        if (mapControl!=null){
142
            FLayer[] actives =mapControl.getMapContext().getLayers().getActives();
143
            if (actives!=null && actives.length>0){
144
                for(FLayer fLayer:actives){
145
                    if (fLayer instanceof FLyrVect) {
146
                        FLyrVect lyrVect = (FLyrVect) fLayer;
147
                        FeatureStore featureStore=lyrVect.getFeatureStore();
148
                        if (featureStore != null) {
149
                            try {
150
                                totalFeaturesCount +=
151
                                    featureStore.getFeatureCount();
152
                            } catch (DataException e) {
153
                                logger.warn(
154
                                    "Problem obtaining total features count");
155
                                totalFeaturesCount = 0L;
156
                            }
157
                            try {
158
                                FeatureSelection featureSelection =
159
                                    featureStore.getFeatureSelection();
160
                                if (featureSelection != null) {
161
                                    selectedFeaturesCount +=
162
                                        featureSelection.getSize();
163
                                }
164
                            } catch (DataException e) {
165
                                logger.warn(
166
                                    "Problem obtaining selected features count");
167
                                selectedFeaturesCount = 0L;
168
                            }
169
                        }
170
                    }
171
                }
172
                messageToStatusBar(totalFeaturesCount,selectedFeaturesCount);
173
            }else {
174
                clean();
175
            }
176
        }else {
177
            clean();
178
        }
179
    }
180

    
181
    /**
182
     * Cleans the text shown in the control
183
     */
184
    public void clean(){
185
        if (control!=null && control instanceof JLabel ){
186
            JLabel jlabel = (JLabel)control;
187
            jlabel.setText("");
188
        }
189
    }
190

    
191
    private void messageToStatusBar(Long totalFeaturesCount, Long selectedFeaturesCount){
192
        String strTotal;
193
        String strSelected;
194
        if (totalFeaturesCount!=null){
195
            strTotal=totalFeaturesCount.toString();
196
        }else{
197
            strTotal="N/A";
198
        }
199
        if (selectedFeaturesCount!=null){
200
            strSelected=selectedFeaturesCount.toString();
201
        }else{
202
            strSelected="N/A";
203
        }
204
        if (control!=null && control instanceof JLabel ){
205
            JLabel jlabel = (JLabel)control;
206
            jlabel.setText(strSelected+"/"+strTotal);
207
        }
208
    }
209

    
210
}