blob: 41997bcaefb16a4acd5c52e0267501de94ab2b84 [file] [log] [blame]
//------------------------------------------------------------------------------
// 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.util;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.OperationHistoryFactory;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.emf.common.command.CompoundCommand;
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.resource.ResourceSet;
import org.eclipse.emf.edit.command.CopyCommand.Helper;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.impl.InternalTransactionalEditingDomain;
import org.eclipse.epf.common.CommonPlugin;
import org.eclipse.epf.diagram.core.bridge.ActivityDiagramAdapter;
import org.eclipse.epf.diagram.core.part.DiagramEditorInput;
import org.eclipse.epf.diagram.core.part.DiagramEditorInputProxy;
import org.eclipse.epf.diagram.core.part.DiagramFileEditorInputProxy;
import org.eclipse.epf.diagramming.base.commands.CreateDiagramCommand;
import org.eclipse.epf.diagramming.base.persistence.DiagramFileCreatorEx;
import org.eclipse.epf.diagramming.base.persistence.DiagramService;
import org.eclipse.epf.diagramming.base.persistence.IDiagramService;
import org.eclipse.epf.diagramming.part.EPFDiagramEditorPlugin;
import org.eclipse.epf.diagramming.part.UMLDiagramFileCreator;
import org.eclipse.epf.library.LibraryService;
import org.eclipse.epf.library.edit.process.BreakdownElementWrapperItemProvider;
import org.eclipse.epf.library.edit.process.command.ActivityDropCommand;
import org.eclipse.epf.library.edit.util.IDiagramManager;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.epf.persistence.MethodLibraryPersister;
import org.eclipse.epf.persistence.MultiFileSaveUtil;
import org.eclipse.epf.uma.Activity;
import org.eclipse.epf.uma.BreakdownElement;
import org.eclipse.epf.uma.CapabilityPattern;
import org.eclipse.epf.uma.DeliveryProcess;
import org.eclipse.epf.uma.MethodElement;
import org.eclipse.epf.uma.MethodPlugin;
import org.eclipse.epf.uma.Process;
import org.eclipse.epf.uma.util.UmaUtil;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramWorkbenchPart;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide.util.IDEEditorUtil;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.util.DiagramFileCreator;
import org.eclipse.gmf.runtime.diagram.ui.resources.editor.util.EditorUtil;
import org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
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.PartInitException;
import org.eclipse.ui.ide.IDE;
/**
*
* @author Shashidhar Kannoori
* @author Shilpa Toraskar
* @author Phong Nguyen Le
*/
public class DiagramEditorUtil extends IDEEditorUtil {
public static final String kind = "AD";
public static final String default_file_name = "diagram";
public static final String create_diagram_label = "Create diagram and model";
private static final String AD_EDITOR_ID = "org.eclipse.epf.diagramming.part.DiagramEditorID"; //$NON-NLS-1$
private static String getEditorId(int diagramType) {
switch(diagramType) {
case IDiagramManager.ACTIVITY_DIAGRAM:
return AD_EDITOR_ID;
}
return null;
}
public static IEditorPart openDiagramEditor(IWorkbenchPage page, DiagramEditorInput input, IProgressMonitor progressMonitor) {
DiagramEditorInputProxy diagramFileEditorInput = new DiagramEditorInputProxy(input, EPFDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
IEditorPart editorPart = null;
try {
editorPart = page.openEditor(diagramFileEditorInput, getEditorId(input.getDiagramType()), true);
} catch (PartInitException e) {
CommonPlugin.getDefault().getLogger().logError(e);
}
// initialize the diagram by synchronizing it with library data
//
if(editorPart instanceof IDiagramWorkbenchPart) {
DiagramEditPart editPart = ((IDiagramWorkbenchPart)editorPart).getDiagramEditPart();
((DiagramDocumentEditor)editorPart).getDocumentProvider().setCanSaveDocument(diagramFileEditorInput);
initialize((Diagram) editPart.getModel(), diagramFileEditorInput);
}
return editorPart;
}
/**
* To open a diagram and create a file.
* @written
*/
public static void openDiagramEditor(IWorkbenchPage page, IEditorInput input){
MethodElement process = (MethodElement)((DiagramEditorInput)input).getMethodElement();
IPath containerPath = getContainerPath(process);
createAndOpenDiagram(
UMLDiagramFileCreator.getInstance(), containerPath, default_file_name,
EditorUtil.getInitialContents(), kind, page, new NullProgressMonitor(),
true, false, input);
}
/**
* @generated
*/
public static final IFile createAndOpenDiagram(
DiagramFileCreator diagramFileCreator, IPath containerPath,
String fileName, InputStream initialContents, String kind,
IWorkbenchPage page, IProgressMonitor progressMonitor,
boolean openEditor, boolean saveDiagram, IEditorInput input) {
IFile diagramFile = getFile(fileName, input, diagramFileCreator) ;
if(diagramFile == null){
diagramFile = createNewDiagramFile(
diagramFileCreator, containerPath, fileName, initialContents,
kind, page.getWorkbenchWindow().getShell(), progressMonitor, input);
}else{
}
// DiagramFileEditorInput diagramFileEditorInput = new DiagramFileEditorInput(
// diagramFile, (DiagramEditorInput)input);
DiagramFileEditorInputProxy diagramFileEditorInput = new DiagramFileEditorInputProxy(
diagramFile, (DiagramEditorInput)input, null);
if (diagramFile != null && openEditor) {
IEditorPart editorPart = doOpenDiagramEditor(diagramFileEditorInput, page, saveDiagram, progressMonitor);
// initialize the diagram by synchronizing it with library data
//
if(editorPart instanceof IDiagramWorkbenchPart) {
DiagramEditPart editPart = ((IDiagramWorkbenchPart)editorPart).getDiagramEditPart();
initialize((Diagram) editPart.getModel(), diagramFileEditorInput);
}
// remove the dirty flag
//
// if(editorPart instanceof IDocumentEditor) {
// IDocumentProvider dp = ((IDocumentEditor)editorPart).getDocumentProvider();
// if(dp instanceof DiagramEditorDocumentProvider) {
// ((DiagramEditorDocumentProvider)dp).unsetCanSaveDocument(editorPart.getEditorInput());
// }
// }
}
return diagramFile;
}
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());
adapter.setView(model);
model.getElement().eAdapters().add(adapter);
adapter.populateDiagram();
}
/**
* <p>
* This method should be called within a workspace modify operation since it creates resources.
* </p>
* @modified
* @return the created file resource, or <code>null</code> if the file was not created
*/
public static final IFile createNewDiagramFile(
DiagramFileCreator diagramFileCreator, IPath containerFullPath,
String fileName, InputStream initialContents, String kind,
Shell shell, IProgressMonitor progressMonitor, IEditorInput input) {
progressMonitor.beginTask("Creating notation diagram and model file", 4); //$NON-NLS-1$
final IProgressMonitor subProgressMonitor = new SubProgressMonitor(
progressMonitor, 1);
final IFile diagramFile = diagramFileCreator.createNewFile(
containerFullPath, fileName, initialContents, shell,
new IRunnableContext() {
public void run(boolean fork, boolean cancelable,
IRunnableWithProgress runnable)
throws InvocationTargetException,
InterruptedException {
runnable.run(subProgressMonitor);
}
});
createDiagramContent(diagramFile, progressMonitor, input);
return diagramFile;
}
/**
* Creates a diagram file content - GMF notation Diagram and UML Activity,
* UMA bridge.
*
*/
public static boolean createDiagramContent(IFile diagramFile, IProgressMonitor progressMonitor, IEditorInput input) {
TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE
.createEditingDomain();
ResourceSet resourceSet = editingDomain.getResourceSet();
final Resource diagramResource = resourceSet
.createResource(URI.createPlatformResourceURI(diagramFile
.getFullPath().toString()));
List affectedFiles = new ArrayList();
affectedFiles.add(diagramFile);
CreateDiagramCommand command = new CreateDiagramCommand(editingDomain,
"Creating diagram and model", affectedFiles, input,
diagramResource, kind);
try {
OperationHistoryFactory.getOperationHistory().execute(command,
new SubProgressMonitor(progressMonitor, 1), null);
} catch (ExecutionException e) {
EPFDiagramEditorPlugin.getInstance().logError(
"Unable to create model and diagram", e); //$NON-NLS-1$
return false;
}
return true;
}
/**
* Creates a diagram file content - GMF notation Diagram and UML Activity,
* UMA bridge.
*
*/
public static boolean createDiagramContent(TransactionalEditingDomain editingDomain,
IFile diagramFile, IProgressMonitor progressMonitor, IEditorInput input,
Resource resource) {
// TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE
// .createEditingDomain();
// ResourceSet resourceSet = editingDomain.getResourceSet();
// final Resource diagramResource = resourceSet
// .createResource(URI.createPlatformResourceURI(diagramFile
// .getFullPath().toString()));
List affectedFiles = new ArrayList();
affectedFiles.add(diagramFile);
CreateDiagramCommand command = new CreateDiagramCommand(editingDomain,
"Creating diagram and model", affectedFiles, input,
resource, kind);
try {
OperationHistoryFactory.getOperationHistory().execute(command,
new SubProgressMonitor(progressMonitor, 1), null);
} catch (ExecutionException e) {
EPFDiagramEditorPlugin.getInstance().logError(
"Unable to create model and diagram", e); //$NON-NLS-1$
return false;
}
return true;
}
/**
* @written
*/
public static IPath getContainerPath(){
IPath path = EditorUtil.getDefaultDiagramPath(null, null);
if (path == null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject[] projects = root.getProjects();
path = root.getFullPath();
for (int i = 0; i < projects.length; ++i) {
IProject project = projects[i];
if (project.isOpen()) {
path = project.getFullPath();
break;
}
}
}
return path;
}
/**
* Opens the diagram in an IEditorPart
*
* @param file IFile that contains the diagram
* @param dWindow the workbench window
* @param saveDiagram true to save the diagram after opening, false to not
* save it.
* @param progressMonitor used when opening the diagram.
* For now, it is only used when the diagram is being saved after opening.
*
* @return DiagramEditPart for the diagram opened in an IEditorPart.
*/
public static final DiagramEditPart openDiagramEditor(IFileEditorInput input,
IWorkbenchPage page, boolean saveDiagram,
IProgressMonitor progressMonitor) {
IEditorPart editorPart = doOpenDiagramEditor(input, page, saveDiagram, progressMonitor);
if(editorPart instanceof IDiagramWorkbenchPart) {
return ((IDiagramWorkbenchPart)editorPart).getDiagramEditPart();
}
return null;
}
public static final IEditorPart doOpenDiagramEditor(IFileEditorInput input,
IWorkbenchPage page, boolean saveDiagram,
IProgressMonitor progressMonitor) {
IEditorPart editorPart = null;
try {
//IWorkbenchPage page = dWindow.getActivePage();
IEditorPart parent = page.getActiveEditor();
if (page != null) {
editorPart = openEditor(page, input, true,true);
if(editorPart != null && parent != null){
// set the parent.
}
// set Active page editor as parent.
if (saveDiagram)
editorPart.doSave(progressMonitor);
}
input.getFile().refreshLocal(IResource.DEPTH_ZERO, null);
return editorPart;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Opens an editor on the given file resource. This method will attempt to
* resolve the editor based on content-type bindings as well as traditional
* name/extension bindings if <code>determineContentType</code> is
* <code>true</code>.
* <p>
* If the page already has an editor open on the target object then that
* editor is brought to front; otherwise, a new editor is opened. If
* <code>activate == true</code> the editor will be activated.
* <p>
* @param page
* the page in which the editor will be opened
* @param input
* the editor input
* @param activate
* if <code>true</code> the editor will be activated
* @param determineContentType
* attempt to resolve the content type for this file
* @return an open editor or <code>null</code> if an external editor was
* opened
* @exception PartInitException
* if the editor could not be initialized
* @see org.eclipse.ui.IWorkbenchPage#openEditor(org.eclipse.ui.IEditorInput,
* String, boolean)
* @since 3.1
*/
public static IEditorPart openEditor(IWorkbenchPage page, IFileEditorInput input,
boolean activate, boolean determineContentType) throws PartInitException {
//sanity checks
if (page == null) {
throw new IllegalArgumentException();
}
// open the editor on the file
IEditorDescriptor editorDesc = IDE.getEditorDescriptor(input.getFile(), determineContentType);
return page.openEditor(input, editorDesc.getId(),
activate);
}
/**
* Check whether file exists or not.
* @param szFileName
* @param input
* @param diagramFileCreator
* @return
*/
public static IFile getFile(String szFileName, IEditorInput input, DiagramFileCreator diagramFileCreator){
MethodElement e = ((DiagramEditorInput)input).getMethodElement();
Process srcProc = TngUtil.getOwningProcess((BreakdownElement) e);
IPath containerPath = getContainerPath(e);
String szNewFileName = szFileName;
IPath filePath = containerPath.append(diagramFileCreator.appendExtensionToFileName(szNewFileName));
// IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
// if(workspaceRoot.exists(filePath)){
// IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
// if(file.exists()){
// return null;
// }
// }
File libDirs = new File(LibraryService.getInstance().getCurrentMethodLibraryPath());
String diagramPath = libDirs.getAbsolutePath() + File.separator
+ MethodLibraryPersister.getElementPath(srcProc)
+ File.separator + srcProc.getName() + File.separator
+ "diagram.xmi";
File filex = new File(diagramPath);
if(filex.exists()){
return ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
}
return null;
}
/**
*
*
* @param proc
* @return
* @custom
*/
public static IPath getContainerPath(MethodElement e) {
Process proc = TngUtil.getOwningProcess(e);
// TODO handle the Wrapper Elements.
if (proc instanceof Process) {
MethodPlugin plugin = UmaUtil.getMethodPlugin(proc);
String relativeDir;
if (proc instanceof CapabilityPattern) {
relativeDir = MultiFileSaveUtil.CAPABILITY_PATTERN_PATH;
} else if (proc instanceof DeliveryProcess) {
relativeDir = MultiFileSaveUtil.DELIVERY_PROCESS_PATH;
} else {
relativeDir = ""; //$NON-NLS-1$
}
IPath workspacePath = EditorUtil.getDefaultDiagramPath(null, null);
if (workspacePath == null) {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject[] projects = root.getProjects();
workspacePath = root.getFullPath();
for (int i = 0; i < projects.length; ++i) {
IProject project = projects[i];
if (project.isOpen()) {
workspacePath = project.getFullPath();
break;
}
}
}
String libDir = workspacePath.toString();
String pluginDir = libDir + File.separator + plugin.getName();
String diagramDir = pluginDir + File.separator + relativeDir
+ File.separator + proc.getName() + File.separator;
//System.out.println("Print: " + diagramDir);
Path path = new Path(diagramDir);
return path;
// return workspacePath;
} else
return null;
}
/**
* Check whether file exists or not.
* @param szFileName
* @param input
* @param diagramFileCreator
* @return
*/
public static IFile getFile(String szFileName, BreakdownElement e, DiagramFileCreator diagramFileCreator){
Process srcProc = TngUtil.getOwningProcess((BreakdownElement) e);
IPath containerPath = getContainerPath(e);
String szNewFileName = szFileName;
IPath filePath = containerPath.append(diagramFileCreator.appendExtensionToFileName(szNewFileName));
// IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
// if(workspaceRoot.exists(filePath)){
// IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
// if(file.exists()){
// return null;
// }
// }
File libDirs = new File(LibraryService.getInstance().getCurrentMethodLibraryPath());
String diagramPath = libDirs.getAbsolutePath() + File.separator
+ MethodLibraryPersister.getElementPath(srcProc)
+ File.separator + srcProc.getName() + File.separator
+ "diagram.xmi";
File filex = new File(diagramPath);
if(filex.exists()){
return ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
}
return null;
}
/**
* This method should moved to LibraryEdit or Use NestedCommands using extension.
* @param command2
*/
protected void copyDiagrams(ActivityDropCommand command2) {
// TODO Auto-generated method stub
TransactionalEditingDomain domain = IDiagramService.eInstance.createEditingDomain();
Helper copyHelper = new Helper();
Helper copiedHelper = command2.getActivityHandler().getCopyHelper();
Set keys = copiedHelper.keySet();
CompoundCommand command = new CompoundCommand(CompoundCommand.MERGE_COMMAND_ALL);
for (Iterator iter = keys.iterator(); iter.hasNext();) {
EObject key = (EObject) iter.next();
EObject entry = (EObject)copiedHelper.get(key);
if(entry instanceof Activity && key instanceof Activity){
Activity act = (Activity)entry;
//TODO: Handle different diagrams.
Diagram diagram = IDiagramService.eInstance.getDiagram((Activity)key, DiagramService.AD_kind,
false, new NullProgressMonitor());
if(diagram != null){
// CopyDiagramElementCommand cmd = new CopyDiagramElementCommand(
// domain,diagram.getElement(),copyHelper,act );
// command.append(cmd);
}
}
}
try{
command.execute();
Process p = command2.getActivityHandler().getTargetProcess();
// Need to move this code to different place.
// p.getBr
IFile file = DiagramEditorUtil.getFile(default_file_name, (BreakdownElement)p, DiagramFileCreatorEx.getInstance());
if(file == null){
file = createNewDiagramFile(DiagramFileCreatorEx.getInstance(), p, Display.getCurrent().getActiveShell(),
new NullProgressMonitor());
}
Set xKeys = copyHelper.keySet();
for (Iterator iter = xKeys.iterator(); iter.hasNext();) {
EObject element = (EObject) iter.next();
if(element instanceof Diagram){
}
}
List affectedFiles = new ArrayList();
affectedFiles.add(file);
AbstractTransactionalCommand cmd = new AbstractTransactionalCommand(
domain, "copy diagrams", affectedFiles) {
protected CommandResult doExecuteWithResult(IProgressMonitor monitor,
IAdaptable info) throws ExecutionException {
return null;
}
};
}catch(Exception e){
}
finally{
command.dispose();
}
}
/**
* <p>
* This method should be called within a workspace modify operation since it creates resources.
* </p>
* @modified
* @return the created file resource, or <code>null</code> if the file was not created
*/
public static final IFile createNewDiagramFile(
DiagramFileCreatorEx diagramFileCreator, BreakdownElement element,
Shell shell, IProgressMonitor progressMonitor ) {
final IPath containerPath = getContainerPath(element);
final String diagramFileName = DiagramFileCreatorEx.default_diagram_file;
progressMonitor.beginTask("Creating notation diagram and model file", 4); //$NON-NLS-1$
final IProgressMonitor subProgressMonitor = new SubProgressMonitor(
progressMonitor, 1);
final IFile diagramFile = diagramFileCreator.createNewFile(
containerPath, diagramFileName,
getInitialContents(), shell,
new IRunnableContext() {
public void run(boolean fork, boolean cancelable,
IRunnableWithProgress runnable)
throws InvocationTargetException,
InterruptedException {
runnable.run(subProgressMonitor);
}
});
//createDiagramContent(diagramFile, progressMonitor, input);
progressMonitor.done();
return diagramFile;
}
}