001: package net.refractions.udig.project.ui.commands;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.List;
006:
007: import net.refractions.udig.project.IProjectElement;
008: import net.refractions.udig.project.command.Command;
009: import net.refractions.udig.project.command.UndoableCommand;
010: import net.refractions.udig.project.internal.Map;
011: import net.refractions.udig.project.ui.ApplicationGIS;
012: import net.refractions.udig.project.ui.UDIGEditorInput;
013: import net.refractions.udig.project.ui.internal.MapEditor;
014: import net.refractions.udig.project.ui.internal.Messages;
015: import net.refractions.udig.project.ui.internal.ProjectExplorer;
016: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
017: import net.refractions.udig.ui.PlatformGIS;
018:
019: import org.eclipse.core.runtime.IProgressMonitor;
020: import org.eclipse.swt.widgets.Display;
021: import org.eclipse.ui.IEditorPart;
022: import org.eclipse.ui.IEditorReference;
023: import org.eclipse.ui.IWorkbenchPage;
024: import org.eclipse.ui.PartInitException;
025: import org.eclipse.ui.PlatformUI;
026:
027: import com.vividsolutions.jts.geom.Envelope;
028:
029: public class OpenProjectElementCommand implements UndoableCommand {
030:
031: IProjectElement element;
032: private IEditorPart previous;
033:
034: public OpenProjectElementCommand(IProjectElement element) {
035: this .element = element;
036: }
037:
038: public void run(IProgressMonitor monitor) throws Exception {
039: try {
040: if (PlatformUI.getWorkbench().isClosing())
041: return;
042:
043: monitor.beginTask(Messages.OpenMapCommand_taskName,
044: IProgressMonitor.UNKNOWN);
045: final UDIGEditorInput input = ApplicationGIS
046: .getInput(element);
047: if (element instanceof Map) {
048: Map map = (Map) element;
049: if (map.getViewportModel().getBounds().isNull()) {
050: Envelope bounds = map.getBounds(monitor);
051: map.getViewportModelInternal().setBounds(bounds);
052: }
053: }
054: input.setProjectElement(element);
055:
056: PlatformGIS.syncInDisplayThread(new Runnable() {
057: public void run() {
058: IWorkbenchPage activePage = PlatformUI
059: .getWorkbench().getActiveWorkbenchWindow()
060: .getActivePage();
061: IEditorReference[] editors = activePage
062: .getEditorReferences();
063: for (IEditorReference reference : editors) {
064: try {
065: if (reference.getEditorInput()
066: .equals(input)) {
067: previous = activePage.getActiveEditor();
068: activePage.activate(reference
069: .getPart(true));
070: return;
071: }
072: } catch (PartInitException e) {
073: // ignore
074: }
075: }
076: openMap(input);
077: }
078:
079: });
080: } finally {
081: monitor.done();
082: }
083: }
084:
085: private void openMap(final UDIGEditorInput input) {
086: try {
087: IEditorPart part = PlatformUI.getWorkbench()
088: .getActiveWorkbenchWindow().getActivePage()
089: .openEditor(input, input.getEditorId());
090:
091: ProjectExplorer explorer = ProjectExplorer
092: .getProjectExplorer();
093: explorer.setSelection(Collections.singleton(input
094: .getProjectElement()), true);
095:
096: if (part instanceof MapEditor) {
097: MapEditor mapEditor = (MapEditor) part;
098: while (!mapEditor.getComposite().isVisible()
099: || !mapEditor.getComposite().isEnabled()) {
100: if (!Display.getCurrent().readAndDispatch()) {
101: Thread.sleep(300);
102: }
103: }
104: }
105: } catch (PartInitException e) {
106: ProjectUIPlugin.log(e.getLocalizedMessage(), e);
107: } catch (InterruptedException e) {
108: throw (RuntimeException) new RuntimeException()
109: .initCause(e);
110: }
111: }
112:
113: public String getName() {
114: return Messages.OpenMapCommand_commandName;
115: }
116:
117: public void rollback(IProgressMonitor monitor) throws Exception {
118: if (previous != null) {
119: previous.getEditorSite().getPage().activate(previous);
120: return;
121: }
122: PlatformGIS.syncInDisplayThread(new Runnable() {
123: public void run() {
124: UDIGEditorInput input = ApplicationGIS
125: .getInput(element);
126: IWorkbenchPage activePage = PlatformUI.getWorkbench()
127: .getActiveWorkbenchWindow().getActivePage();
128: IEditorReference[] editors = activePage
129: .getEditorReferences();
130: List<IEditorReference> matches = new ArrayList<IEditorReference>();
131: for (IEditorReference reference : editors) {
132: try {
133: if (reference.getEditorInput().equals(input)) {
134: matches.add(reference);
135: }
136: } catch (PartInitException e) {
137: // do nothing
138: }
139: }
140:
141: activePage.closeEditors(matches
142: .toArray(new IEditorReference[matches.size()]),
143: true);
144: }
145: });
146: }
147:
148: public Command copy() {
149: return null;
150: }
151:
152: }
|