package pyUML.backend; import java.io.IOException; import java.util.List; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.core.commands.operations.OperationHistoryFactory; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.emf.common.util.URI; import org.eclipse.emf.common.util.WrappedException; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.gmf.runtime.common.core.command.CommandResult; import org.eclipse.gmf.runtime.diagram.core.services.ViewService; 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.dialogs.MessageDialog; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; import org.eclipse.uml2.diagram.clazz.edit.parts.PackageEditPart; import org.eclipse.uml2.diagram.clazz.part.Messages; import org.eclipse.uml2.diagram.clazz.part.UMLDiagramEditor; import org.eclipse.uml2.diagram.clazz.part.UMLDiagramEditorPlugin; import org.eclipse.uml2.diagram.clazz.part.UMLDiagramEditorUtil; import org.eclipse.uml2.diagram.clazz.part.UMLVisualIDRegistry; import pyUML.listeners.LiveValidationListener; import pyUML.listeners.ViewChangeListener; import pyUML.views.EditView; import pyUML.views.EditViewWizard; /** * This class contains methods for reading and writing eclipse * UML models and for model handling */ public class UMLToolsHelperMethods { /** * Loads an eclipse UML2 model * * @param umlFileName a String containing the filename of the eclipse uml2 diagram * @return the model root as EObject, null if an exception occured */ public static EObject loadUMLDiagram(String umlFileName) { try { Resource resource = new ResourceSetImpl().getResource(URI.createFileURI(umlFileName), true); return resource.getContents().get(0); /*for (EObject content : resource.getContents()) { if (content instanceof Model) return content; }*/ //return null; } catch (WrappedException ex) { MessageDialog.openError(null,ex.getMessage(),"Unable to load model: " + umlFileName+"\n"+ex.getMessage()); return null; } } /** * opens a eclipse uml2tools UML diagram in the editor * Will edit the diagram file, if a wrong path to a UML model is set * (Unfortunately, UML diagrams save absolute paths of the corresponding * UML file) * @param project the current project * @param diagramFile the fileName in the pyUml directory * @param putToFront if the editor is already opened, with putToFront==true * the editor is given the focus, with putToFront==false nothing is done * @param umlModelName The name of the corresponding UML model to this model * This is needed to ensure the diagram has a correct UML file path set * @return the IEitorPat of the newly opened Diagram, null on Error * @throws PartInitException */ public static IEditorPart openDiagram(IProject project, IFile diagramFile, String umlModelFileName, boolean putToFront) { // see, if UML model path is set correctly String absoluteUmlPath = project.getLocation().append(GlobalConstants.getPyUmlDir()) .append(umlModelFileName).toOSString(); String[] diagramFileContent = EclipseHelperMethods.iFileToString(diagramFile).split("\n"); for (int i =0; i< diagramFileContent.length; i++) { String line = diagramFileContent[i]; Pattern pattern = Pattern.compile("(\\s* affectedFiles = new Vector(); final org.eclipse.uml2.uml.Package model = (org.eclipse.uml2.uml.Package) modelObject; URI diagramModelURI = URI.createPlatformResourceURI(newDiagramFile.getFullPath().toString(), true); ResourceSet resourceSet = editingDomain.getResourceSet(); final Resource diagramResource = resourceSet.createResource(diagramModelURI); affectedFiles.add(newDiagramFile); // create diagram from model AbstractTransactionalCommand command = new AbstractTransactionalCommand(editingDomain, Messages.UMLNewDiagramFileWizard_InitDiagramCommand, affectedFiles) { public CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) { int diagramVID = UMLVisualIDRegistry.getDiagramVisualID(model); if (diagramVID != PackageEditPart.VISUAL_ID) { return CommandResult.newErrorCommandResult(Messages.UMLNewDiagramFileWizard_IncorrectRootError); } Diagram diagram = ViewService.createDiagram(model, PackageEditPart.MODEL_ID, UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT); diagramResource.getContents().add(diagram); return CommandResult.newOKCommandResult(); } }; // save the created diagram file try { OperationHistoryFactory.getOperationHistory().execute(command, new NullProgressMonitor(), null); diagramResource.save(UMLDiagramEditorUtil.getSaveOptions()); } catch (org.eclipse.core.commands.ExecutionException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // change full path file name to relative path String diagramFileContent = EclipseHelperMethods.iFileToString(newDiagramFile);//.split("\n"); Pattern pattern = Pattern.compile( "(.*