01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tool.select.internal;
16:
17: import java.io.IOException;
18:
19: import net.refractions.udig.project.ILayer;
20: import net.refractions.udig.project.ui.tool.AbstractActionTool;
21: import net.refractions.udig.tool.select.SelectPlugin;
22: import net.refractions.udig.ui.ProgressManager;
23:
24: import org.geotools.data.DefaultQuery;
25: import org.geotools.data.FeatureSource;
26: import org.geotools.data.Query;
27:
28: import com.vividsolutions.jts.geom.Envelope;
29:
30: /**
31: * Sets the ViewportModel bounds to equal the bounds of the selected features
32: * in the selected layer.
33: *
34: * @author Jesse
35: * @since 1.1.0
36: */
37: public class ZoomSelection extends AbstractActionTool {
38:
39: public static final String ID = "net.refractions.udig.tool.default.show.selection"; //$NON-NLS-1$
40:
41: public ZoomSelection() {
42: }
43:
44: public void run() {
45: ILayer layer = getContext().getSelectedLayer();
46: if (layer.hasResource(FeatureSource.class)) {
47: try {
48: FeatureSource resource = layer.getResource(
49: FeatureSource.class, ProgressManager.instance()
50: .get());
51: Query query = new DefaultQuery(resource.getSchema()
52: .getTypeName(), layer.getFilter(),
53: new String[] { resource.getSchema()
54: .getDefaultGeometry().getName() });
55: Envelope bounds = resource.getBounds(query);
56: if (bounds == null) {
57: bounds = resource.getFeatures(query).getBounds();
58: }
59: getContext().sendASyncCommand(
60: getContext().getNavigationFactory()
61: .createSetViewportBBoxCommand(bounds,
62: layer.getCRS()));
63: } catch (IOException e) {
64: SelectPlugin.log("failed to obtain resource", e); //$NON-NLS-1$
65: }
66:
67: }
68: }
69:
70: public void dispose() {
71: }
72:
73: }
|