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 @ 42557
History | View | Annotate | Download (7.17 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 |
showFeatureCount(mapControl); |
85 |
FLayer fLayer =e.getSource(); |
86 |
if (fLayer.isActive()){
|
87 |
if (fLayer instanceof FLyrVect) { |
88 |
FLyrVect lyrVect = (FLyrVect) fLayer; |
89 |
lyrVect.getFeatureStore().addObserver(this);
|
90 |
} |
91 |
}else{
|
92 |
if (fLayer instanceof FLyrVect) { |
93 |
FLyrVect lyrVect = (FLyrVect) fLayer; |
94 |
lyrVect.getFeatureStore().deleteObserver(this);
|
95 |
} |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public void nameChanged(LayerEvent e) { |
102 |
// Do nothing
|
103 |
} |
104 |
|
105 |
@Override
|
106 |
public void editionChanged(LayerEvent e) { |
107 |
// Do nothing
|
108 |
} |
109 |
|
110 |
@Override
|
111 |
public void drawValueChanged(LayerEvent e) { |
112 |
// Do nothing
|
113 |
} |
114 |
|
115 |
@Override
|
116 |
public void update(final Observable observable, final Object notification) { |
117 |
if (notification instanceof FeatureStoreNotification) { |
118 |
FeatureStoreNotification event |
119 |
= (FeatureStoreNotification) notification; |
120 |
if (event.getType() == FeatureStoreNotification.AFTER_DELETE ||
|
121 |
event.getType() == FeatureStoreNotification.AFTER_INSERT || |
122 |
event.getType() == FeatureStoreNotification.AFTER_FINISHEDITING || |
123 |
event.getType() == FeatureStoreNotification.SELECTION_CHANGE ){ |
124 |
|
125 |
if (event.getSource() instanceof FeatureStore){ |
126 |
showFeatureCount(mapControl); |
127 |
} |
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Gets the features from the selected layers and shows them through the control
|
134 |
* @param mapControl
|
135 |
*/
|
136 |
public void showFeatureCount(MapControl mapControl){ |
137 |
Long totalFeaturesCount=Long.valueOf(0); |
138 |
Long selectedFeaturesCount=Long.valueOf(0);; |
139 |
if (mapControl!=null){ |
140 |
FLayer[] actives =mapControl.getMapContext().getLayers().getActives();
|
141 |
if (actives!=null && actives.length>0){ |
142 |
for(FLayer fLayer:actives){
|
143 |
if (fLayer instanceof FLyrVect) { |
144 |
FLyrVect lyrVect = (FLyrVect) fLayer; |
145 |
FeatureStore featureStore=lyrVect.getFeatureStore(); |
146 |
if (featureStore != null) { |
147 |
try {
|
148 |
totalFeaturesCount += |
149 |
featureStore.getFeatureCount(); |
150 |
} catch (DataException e) {
|
151 |
logger.warn( |
152 |
"Problem obtaining total features count");
|
153 |
totalFeaturesCount = 0L;
|
154 |
} |
155 |
try {
|
156 |
FeatureSelection featureSelection = |
157 |
featureStore.getFeatureSelection(); |
158 |
if (featureSelection != null) { |
159 |
selectedFeaturesCount += |
160 |
featureSelection.getSize(); |
161 |
} |
162 |
} catch (DataException e) {
|
163 |
logger.warn( |
164 |
"Problem obtaining selected features count");
|
165 |
selectedFeaturesCount = 0L;
|
166 |
} |
167 |
} |
168 |
} |
169 |
} |
170 |
messageToStatusBar(totalFeaturesCount,selectedFeaturesCount); |
171 |
}else {
|
172 |
clean(); |
173 |
} |
174 |
}else {
|
175 |
clean(); |
176 |
} |
177 |
} |
178 |
|
179 |
/**
|
180 |
* Cleans the text shown in the control
|
181 |
*/
|
182 |
public void clean(){ |
183 |
if (control!=null && control instanceof JLabel ){ |
184 |
JLabel jlabel = (JLabel)control; |
185 |
jlabel.setText("");
|
186 |
} |
187 |
} |
188 |
|
189 |
private void messageToStatusBar(Long totalFeaturesCount, Long selectedFeaturesCount){ |
190 |
String strTotal;
|
191 |
String strSelected;
|
192 |
if (totalFeaturesCount!=null){ |
193 |
strTotal=totalFeaturesCount.toString(); |
194 |
}else{
|
195 |
strTotal="N/A";
|
196 |
} |
197 |
if (selectedFeaturesCount!=null){ |
198 |
strSelected=selectedFeaturesCount.toString(); |
199 |
}else{
|
200 |
strSelected="N/A";
|
201 |
} |
202 |
if (control!=null && control instanceof JLabel ){ |
203 |
JLabel jlabel = (JLabel)control; |
204 |
jlabel.setText(strSelected+"/"+strTotal);
|
205 |
} |
206 |
} |
207 |
|
208 |
} |