01: /*
02: * uDig - User Friendly Desktop Internet GIS client
03: * http://udig.refractions.net
04: * (C) 2004, Refractions Research Inc.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package net.refractions.udig.project.ui.operations.example;
18:
19: import java.text.MessageFormat;
20:
21: import net.refractions.udig.project.ILayer;
22: import net.refractions.udig.project.IMap;
23: import net.refractions.udig.project.ui.internal.Messages;
24: import net.refractions.udig.ui.operations.IOp;
25:
26: import org.eclipse.core.runtime.IProgressMonitor;
27: import org.eclipse.jface.dialogs.MessageDialog;
28: import org.eclipse.swt.widgets.Display;
29: import org.geotools.data.DefaultQuery;
30: import org.geotools.data.FeatureSource;
31: import org.geotools.feature.FeatureCollection;
32: import org.geotools.filter.Filter;
33:
34: /**
35: * Counts all the features that are within the current view.
36: *
37: * @author jeichar
38: * @since 1.0
39: */
40: public class FeaturesInView implements IOp {
41:
42: /**
43: * @see net.refractions.udig.ui.operations.IOp#op(org.eclipse.swt.widgets.Display,
44: * java.lang.Object, org.eclipse.core.runtime.IProgressMonitor)
45: */
46: public void op(final Display display, Object target,
47: IProgressMonitor monitor) throws Exception {
48: IMap map = (IMap) target;
49:
50: Exception e = null;
51:
52: int featureCount = 0;
53: for (ILayer layer : map.getMapLayers()) {
54: if (layer.isType(FeatureSource.class) && layer.isVisible()) {
55: try {
56: FeatureSource source = layer.getResource(
57: FeatureSource.class, monitor);
58: Filter filter = layer.createBBoxFilter(map
59: .getViewportModel().getBounds(), monitor);
60: FeatureCollection results = source
61: .getFeatures(new DefaultQuery(layer
62: .getSchema().getTypeName(), filter));
63: int count = results.size();
64: // FeatureReader reader=results.reader()
65: if (count > 0) {
66: featureCount += count;
67: }
68: } catch (Exception e1) {
69: e = e1;
70: // continue
71: }
72: }
73: }
74:
75: final Exception exception = e;
76: final int finalCount = featureCount;
77:
78: display.asyncExec(new Runnable() {
79: public void run() {
80: if (exception == null)
81: MessageDialog.openInformation(display
82: .getActiveShell(),
83: Messages.FeaturesInView_0,
84: Messages.FeaturesInView_1 + finalCount);
85: else
86: MessageDialog.openInformation(display
87: .getActiveShell(),
88: Messages.FeaturesInView_0,
89: MessageFormat.format(
90: Messages.FeaturesInView_3,
91: new Object[] { finalCount }));
92: }
93: });
94: }
95:
96: }
|