/******************************************************************************* * Copyright (c) 2006-2012 * Software Technology Group, Dresden University of Technology * DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026 * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Software Technology Group - TU Dresden, Germany; * DevBoost GmbH - Berlin, Germany * - initial API and implementation ******************************************************************************/ /* * Copyright (c) 2006, 2007 Borland Software Corporation * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Dmitry Stadnik (Borland) - initial API and implementation */ package org.reuseware.application.taipan.gmf.editor.edit.policies; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Iterator; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.emf.common.ui.URIEditorInput; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.transaction.util.TransactionUtil; import org.eclipse.gef.EditPart; import org.eclipse.gef.Request; import org.eclipse.gef.commands.Command; import org.eclipse.gmf.runtime.common.core.command.CommandResult; import org.eclipse.gmf.runtime.diagram.core.preferences.PreferencesHint; import org.eclipse.gmf.runtime.diagram.core.services.ViewService; import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; import org.eclipse.gmf.runtime.diagram.ui.editpolicies.OpenEditPolicy; import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand; import org.eclipse.gmf.runtime.notation.Diagram; import org.eclipse.gmf.runtime.notation.HintedDiagramLinkStyle; import org.eclipse.gmf.runtime.notation.NotationPackage; import org.eclipse.gmf.runtime.notation.Style; import org.eclipse.gmf.runtime.notation.View; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.actions.WorkspaceModifyOperation; import org.reuseware.application.taipan.gmf.editor.part.Messages; import org.reuseware.application.taipan.gmf.editor.part.TaiPanDiagramEditorPlugin; import org.reuseware.application.taipan.gmf.editor.part.TaiPanDiagramEditorUtil; /** * @generated */ public class OpenDiagramEditPolicy extends OpenEditPolicy { /** * @generated */ protected Command getOpenCommand(Request request) { EditPart targetEditPart = getTargetEditPart(request); if (false == targetEditPart.getModel() instanceof View) { return null; } View view = (View) targetEditPart.getModel(); Style link = view.getStyle(NotationPackage.eINSTANCE .getHintedDiagramLinkStyle()); if (false == link instanceof HintedDiagramLinkStyle) { return null; } return new ICommandProxy(new OpenDiagramCommand( (HintedDiagramLinkStyle) link)); } /** * @generated */ private static class OpenDiagramCommand extends AbstractTransactionalCommand { /** * @generated */ private final HintedDiagramLinkStyle diagramFacet; /** * @generated */ OpenDiagramCommand(HintedDiagramLinkStyle linkStyle) { // editing domain is taken for original diagram, // if we open diagram from another file, we should use another editing domain super(TransactionUtil.getEditingDomain(linkStyle), Messages.CommandName_OpenDiagram, null); diagramFacet = linkStyle; } /** * @generated */ protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException { try { Diagram diagram = getDiagramToOpen(); if (diagram == null) { diagram = intializeNewDiagram(); } URI uri = EcoreUtil.getURI(diagram); String editorName = uri.lastSegment() + "#" + diagram.eResource().getContents().indexOf(diagram); //$NON-NLS-1$ IEditorInput editorInput = new URIEditorInput(uri, editorName); IWorkbenchPage page = PlatformUI.getWorkbench() .getActiveWorkbenchWindow().getActivePage(); page.openEditor(editorInput, getEditorID()); return CommandResult.newOKCommandResult(); } catch (Exception ex) { throw new ExecutionException("Can't open diagram", ex); } } /** * @generated */ protected Diagram getDiagramToOpen() { return diagramFacet.getDiagramLink(); } /** * @generated */ protected Diagram intializeNewDiagram() throws ExecutionException { Diagram d = ViewService.createDiagram(getDiagramDomainElement(), getDiagramKind(), getPreferencesHint()); if (d == null) { throw new ExecutionException("Can't create diagram of '" + getDiagramKind() + "' kind"); } diagramFacet.setDiagramLink(d); assert diagramFacet.eResource() != null; diagramFacet.eResource().getContents().add(d); EObject container = diagramFacet.eContainer(); while (container instanceof View) { ((View) container).persist(); container = container.eContainer(); } try { new WorkspaceModifyOperation() { protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException { try { for (Iterator it = diagramFacet.eResource() .getResourceSet().getResources().iterator(); it .hasNext();) { Resource nextResource = (Resource) it.next(); if (nextResource.isLoaded() && !getEditingDomain().isReadOnly( nextResource)) { nextResource.save(TaiPanDiagramEditorUtil .getSaveOptions()); } } } catch (IOException ex) { throw new InvocationTargetException(ex, "Save operation failed"); } } }.run(null); } catch (InvocationTargetException e) { throw new ExecutionException("Can't create diagram of '" + getDiagramKind() + "' kind", e); } catch (InterruptedException e) { throw new ExecutionException("Can't create diagram of '" + getDiagramKind() + "' kind", e); } return d; } /** * @generated */ protected EObject getDiagramDomainElement() { // use same element as associated with EP return ((View) diagramFacet.eContainer()).getElement(); } /** * @generated */ protected PreferencesHint getPreferencesHint() { // XXX prefhint from target diagram's editor? return TaiPanDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT; } /** * @generated */ protected String getDiagramKind() { return "Port"; } /** * @generated */ protected String getEditorID() { return "org.reuseware.application.taipan.port.diagram.part.PortDiagramEditorID"; } } }