001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui.internal.actions;
018:
019: import java.io.IOException;
020: import java.util.Iterator;
021:
022: import net.refractions.udig.project.command.factory.NavigationCommandFactory;
023: import net.refractions.udig.project.internal.Layer;
024: import net.refractions.udig.project.internal.Map;
025: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
026:
027: import org.eclipse.core.runtime.IProgressMonitor;
028: import org.eclipse.jface.action.IAction;
029: import org.eclipse.jface.operation.IRunnableWithProgress;
030: import org.eclipse.jface.viewers.ISelection;
031: import org.eclipse.jface.viewers.IStructuredSelection;
032: import org.eclipse.swt.widgets.Event;
033: import org.eclipse.ui.IViewActionDelegate;
034: import org.eclipse.ui.IViewPart;
035: import org.eclipse.ui.PlatformUI;
036: import org.eclipse.ui.actions.ActionDelegate;
037:
038: import com.vividsolutions.jts.geom.Envelope;
039:
040: /**
041: * An action that sets the zoom to include all the data in the layer.
042: *
043: * @author jeichar
044: * @since 0.6.0
045: */
046: public class ZoomToLayer extends ActionDelegate implements
047: IViewActionDelegate {
048: IStructuredSelection selection;
049:
050: /**
051: * @see org.eclipse.ui.actions.ActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
052: * org.eclipse.jface.viewers.ISelection)
053: */
054: public void selectionChanged(IAction action, ISelection selection) {
055: try {
056: this .selection = (IStructuredSelection) selection;
057: } catch (Exception e) { // do nothing
058: }
059: }
060:
061: /**
062: * @see org.eclipse.ui.actions.ActionDelegate#runWithEvent(org.eclipse.jface.action.IAction,
063: * org.eclipse.swt.widgets.Event)
064: */
065: public void runWithEvent(IAction action, Event event) {
066: try {
067: PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(
068: false, true, new IRunnableWithProgress() {
069:
070: public void run(IProgressMonitor monitor) {
071: Envelope bounds = new Envelope();
072: bounds.setToNull();
073: Map map = ((Layer) selection
074: .getFirstElement())
075: .getMapInternal();
076: for (Iterator iter = (selection).iterator(); iter
077: .hasNext();) {
078: Layer layer = (Layer) iter.next();
079:
080: if (layer.getMap() != map)
081: return;
082: Envelope bbox = null;
083: try {
084: bbox = layer.getBounds(monitor, map
085: .getViewportModel()
086: .getCRS());
087: } catch (IOException e) {
088: ProjectUIPlugin
089: .log(
090: "exception getting bounds", e); //$NON-NLS-1$
091: }
092: if (bbox == null)
093: continue;
094: if (bounds.isNull())
095: bounds.init(bbox);
096: else
097: bounds.expandToInclude(bbox);
098: if (!bounds.isNull()) {
099: map
100: .sendCommandASync(NavigationCommandFactory
101: .getInstance()
102: .createSetViewportBBoxCommand(
103: bounds));
104: }
105: }
106: }
107: });
108: } catch (Exception e) {
109: ProjectUIPlugin.log(null, e);
110: }
111: }
112:
113: /**
114: * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
115: */
116: public void init(IViewPart view) {
117: // do nothing
118: }
119: }
|