001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004, Refractions Research Inc. This
003: * library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
004: * License as published by the Free Software Foundation; version 2.1 of the License. This library is distributed in the
005: * hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
006: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
007: */
008: package net.refractions.udig.project.ui.internal.actions;
009:
010: import java.io.IOException;
011: import java.util.ArrayList;
012: import java.util.Collection;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.List;
016: import java.util.Map;
017: import java.util.Set;
018:
019: import net.refractions.udig.catalog.IGeoResource;
020: import net.refractions.udig.catalog.IService;
021: import net.refractions.udig.catalog.internal.ui.ResourceSelectionPage;
022: import net.refractions.udig.catalog.ui.workflow.ResourceSelectionState;
023: import net.refractions.udig.catalog.ui.workflow.Workflow;
024: import net.refractions.udig.catalog.ui.workflow.WorkflowWizard;
025: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardDialog;
026: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardPage;
027: import net.refractions.udig.catalog.ui.workflow.Workflow.State;
028: import net.refractions.udig.project.ui.ApplicationGIS;
029: import net.refractions.udig.project.ui.internal.Messages;
030: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
031: import net.refractions.udig.ui.PlatformGIS;
032: import net.refractions.udig.ui.ProgressManager;
033:
034: import org.eclipse.core.runtime.ISafeRunnable;
035: import org.eclipse.jface.action.IAction;
036: import org.eclipse.jface.dialogs.IDialogConstants;
037: import org.eclipse.jface.viewers.ISelection;
038: import org.eclipse.jface.viewers.IStructuredSelection;
039: import org.eclipse.swt.widgets.Control;
040: import org.eclipse.swt.widgets.Display;
041: import org.eclipse.swt.widgets.Menu;
042: import org.eclipse.ui.IObjectActionDelegate;
043: import org.eclipse.ui.IWorkbenchPart;
044: import org.eclipse.ui.IWorkbenchWindow;
045: import org.eclipse.ui.IWorkbenchWindowActionDelegate;
046: import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;
047:
048: /**
049: *
050: */
051: public class AddToNewMap implements IObjectActionDelegate,
052: IWorkbenchWindowActionDelegate,
053: IWorkbenchWindowPulldownDelegate {
054:
055: IStructuredSelection selection = null;
056:
057: /**
058: * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction,
059: * org.eclipse.ui.IWorkbenchPart)
060: */
061: public void setActivePart(IAction action, IWorkbenchPart targetPart) {
062: }
063:
064: /**
065: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
066: */
067: @SuppressWarnings("unchecked")
068: public void run(IAction action) {
069: PlatformGIS.run(new ISafeRunnable() {
070:
071: public void handleException(Throwable e) {
072: ProjectUIPlugin.log("", e); //$NON-NLS-1$
073: }
074:
075: public void run() throws Exception {
076: List toList = selection.toList();
077: Collection<IGeoResource> resources = getResources(toList);
078: if (resources != null)
079: ApplicationGIS.addLayersToMap(ApplicationGIS
080: .getActiveProject(),
081: new ArrayList<IGeoResource>(resources));
082: }
083:
084: });
085: }
086:
087: /**
088: * Collection is processed so that georesources are added to the returned collection and if a
089: * service has more than 1 child the user is queried to determine which services should be added
090: * to the returned collection. Services with only a single child are added to result
091: * automatically.
092: *
093: * @param collection the collection of {@link IService}s and {@link IGeoResource}s to process.
094: * @return the chosen resources.
095: */
096: static Collection<IGeoResource> getResources(
097: Collection<Object> collection) {
098: Set<IGeoResource> resources = new HashSet<IGeoResource>();
099: Set<IService> services = new HashSet<IService>();
100: boolean needsUserInput = false;
101: for (Object object : collection) {
102: try {
103: if (object instanceof IGeoResource) {
104: IGeoResource geoResource = (IGeoResource) object;
105: resources.add(geoResource);
106: services.add(geoResource.service(ProgressManager
107: .instance().get()));
108: }
109: if (object instanceof IService) {
110: IService service = (IService) object;
111: List<? extends IGeoResource> members;
112: members = service.members(ProgressManager
113: .instance().get());
114: if (members.isEmpty())
115: continue;
116: services.add(service);
117: if (members.size() == 1)
118: resources.add(members.get(0));
119: else {
120: needsUserInput = true;
121: }
122: }
123: } catch (IOException e) {
124: ProjectUIPlugin.log("", e); //$NON-NLS-1$
125: }
126: }
127:
128: if (needsUserInput) {
129: Map<Class<? extends State>, WorkflowWizardPage> pageMapping = new HashMap<Class<? extends State>, WorkflowWizardPage>();
130: ResourceSelectionPage resourceSelectionPage = new ResourceSelectionPage(
131: Messages.AddToNewMap_resource_selection_page_title,
132: null);
133: resourceSelectionPage.setCollapseCheckedInput(true);
134: pageMapping.put(ResourceSelectionState.class,
135: resourceSelectionPage);
136: ResourceSelectionState resourceSelectionState = new ResourceSelectionState();
137: resourceSelectionState.setServices(services);
138: Workflow workflow = new Workflow(
139: new State[] { resourceSelectionState });
140: workflow.setContext(resources);
141: WorkflowWizard wizard = new WorkflowWizard(workflow,
142: pageMapping);
143: WorkflowWizardDialog dialog = new WorkflowWizardDialog(
144: Display.getCurrent().getActiveShell(), wizard);
145: dialog.setBlockOnOpen(true);
146: dialog.open();
147: if (dialog.getReturnCode() == IDialogConstants.CANCEL_ID)
148: return null;
149: resources = resourceSelectionState.getResources().keySet();
150: }
151: return resources;
152: }
153:
154: /**
155: * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
156: * org.eclipse.jface.viewers.ISelection)
157: */
158: public void selectionChanged(IAction action, ISelection selection) {
159:
160: if (selection instanceof IStructuredSelection)
161: this .selection = (IStructuredSelection) selection;
162: }
163:
164: /*
165: * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
166: */
167: public void dispose() {
168: }
169:
170: /*
171: * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
172: */
173: public void init(IWorkbenchWindow window) {
174: // do nothing
175: }
176:
177: /*
178: * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
179: */
180: public Menu getMenu(Control parent) {
181: return null;
182: }
183:
184: }
|