| //------------------------------------------------------------------------------ |
| // Copyright (c) 2005, 2007 IBM Corporation and others. |
| // 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: |
| // IBM Corporation - initial implementation |
| //------------------------------------------------------------------------------ |
| /** |
| * |
| */ |
| package org.eclipse.epf.diagramming.base.parts; |
| |
| import org.eclipse.core.resources.IFile; |
| import org.eclipse.core.resources.IResource; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.NullProgressMonitor; |
| import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain; |
| import org.eclipse.epf.diagram.core.bridge.ActivityDiagramAdapter; |
| import org.eclipse.epf.diagram.core.part.DiagramEditorInput; |
| import org.eclipse.epf.diagram.core.part.DiagramFileEditorInputProxy; |
| import org.eclipse.epf.diagramming.base.persistence.DiagramFileCreatorEx; |
| import org.eclipse.epf.diagramming.base.persistence.DiagramPersister; |
| import org.eclipse.epf.diagramming.base.persistence.DiagramService; |
| import org.eclipse.epf.diagramming.base.persistence.IDiagramService; |
| import org.eclipse.epf.library.edit.process.BreakdownElementWrapperItemProvider; |
| import org.eclipse.epf.uma.Activity; |
| import org.eclipse.epf.uma.MethodElement; |
| import org.eclipse.gef.commands.Command; |
| import org.eclipse.gmf.runtime.common.core.command.ICommand; |
| import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy; |
| import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; |
| import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart; |
| import org.eclipse.gmf.runtime.notation.Diagram; |
| import org.eclipse.ui.IEditorDescriptor; |
| import org.eclipse.ui.IEditorInput; |
| import org.eclipse.ui.IEditorPart; |
| import org.eclipse.ui.IFileEditorInput; |
| import org.eclipse.ui.IWorkbenchPage; |
| import org.eclipse.ui.ide.IDE; |
| |
| /** |
| * Diagrm helper class (replacing the DiagramEditorUtil). |
| * @author Shashidhar Kannoori |
| * |
| */ |
| public final class DiagramHelper { |
| |
| /** |
| * To open a diagram and create a file. |
| * @written |
| */ |
| public static void openDiagramEditor(IWorkbenchPage page, IEditorInput input, String diagramKind){ |
| |
| IProgressMonitor monitor = new NullProgressMonitor(); |
| |
| MethodElement methodElement = (MethodElement)((DiagramEditorInput)input).getMethodElement(); |
| Diagram diagram = IDiagramService.eInstance.getDiagram((Activity)methodElement, diagramKind, false, new NullProgressMonitor()); |
| IFile diagramFile = DiagramPersister.getFile(input) ; |
| if(diagramFile == null){ |
| diagramFile = DiagramPersister.createNewDiagramFile(DiagramFileCreatorEx.getInstance(), |
| input, page.getWorkbenchWindow().getShell(), |
| monitor); |
| } |
| if(diagram == null){ |
| diagram = IDiagramService.eInstance.createDiagram((Activity)methodElement, diagramKind, false); |
| //TODO: remove this save option |
| try{ |
| DiagramPersister.saveDiagram(IDiagramService.eInstance.createEditingDomain(), |
| diagramFile, diagram, false); |
| }catch(Exception e){ |
| e.printStackTrace(); |
| } |
| } |
| |
| |
| DiagramFileEditorInputProxy diagramFileEditorInput = new DiagramFileEditorInputProxy( |
| diagramFile, (DiagramEditorInput)input, null); |
| |
| if (diagramFile != null) { |
| DiagramEditPart editPart = openEditor(diagramFileEditorInput, page, monitor, true, true); |
| initialize((Diagram) editPart.getModel(), diagramFileEditorInput); |
| } |
| |
| } |
| |
| private static final DiagramEditPart openEditor(IFileEditorInput input, |
| IWorkbenchPage page,IProgressMonitor progressMonitor, boolean activate, |
| boolean determineContentType){ |
| IEditorPart editorPart = null; |
| try { |
| //IWorkbenchPage page = dWindow.getActivePage(); |
| IEditorPart parent = page.getActiveEditor(); |
| if (page != null) { |
| //sanity checks |
| if (page == null) { |
| throw new IllegalArgumentException(); |
| } |
| |
| // open the editor on the file |
| IEditorDescriptor editorDesc = IDE.getEditorDescriptor(input.getFile(), determineContentType); |
| editorPart = page.openEditor(input, editorDesc.getId(), |
| activate); |
| if(editorPart != null && parent != null){ |
| // set the parent. |
| } |
| } |
| input.getFile().refreshLocal(IResource.DEPTH_ZERO, null); |
| return ((IDiagramWorkbenchPart) editorPart).getDiagramEditPart(); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| |
| return null; |
| } |
| |
| public static String getActivityDiagramKind(){ |
| return DiagramService.AD_kind; |
| } |
| /** |
| * |
| * @param model |
| * @param diagramFileEditorInput |
| */ |
| private static void initialize(Diagram model, DiagramFileEditorInputProxy diagramFileEditorInput) { |
| BreakdownElementWrapperItemProvider wrapper = diagramFileEditorInput.getDiagramEditorInput().getWrapper(); |
| ActivityDiagramAdapter adapter = wrapper != null ? |
| new ActivityDiagramAdapter(wrapper) : |
| new ActivityDiagramAdapter((Activity)diagramFileEditorInput.getDiagramEditorInput().getMethodElement()); |
| adapter.setEditingDomain((InternalTransactionalEditingDomain) diagramFileEditorInput.getEditingDomain()); |
| adapter.setSuppression(diagramFileEditorInput.getDiagramEditorInput().getSuppression()); |
| model.getElement().eAdapters().add(adapter); |
| adapter.populateDiagram(); |
| } |
| |
| /** |
| * @generated |
| */ |
| public static Command getCommandWrapper(ICommand cmd) { |
| return new ICommandProxy(cmd); |
| } |
| } |