### Eclipse Workspace Patch 1.0 #P org.eclipse.gmf.graphdef.codegen.ui Index: src/org/eclipse/gmf/internal/graphdef/codegen/ui/ConverterSection.java =================================================================== RCS file: /cvsroot/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.graphdef.codegen.ui/src/org/eclipse/gmf/internal/graphdef/codegen/ui/ConverterSection.java,v retrieving revision 1.5 diff -u -r1.5 ConverterSection.java --- src/org/eclipse/gmf/internal/graphdef/codegen/ui/ConverterSection.java 10 Mar 2006 13:58:24 -0000 1.5 +++ src/org/eclipse/gmf/internal/graphdef/codegen/ui/ConverterSection.java 13 Mar 2006 15:04:55 -0000 @@ -33,8 +33,10 @@ 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.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.gmf.gmfgraph.Figure; +import org.eclipse.gmf.gmfgraph.Canvas; +import org.eclipse.gmf.gmfgraph.DiagramElement; import org.eclipse.gmf.gmfgraph.FigureGallery; import org.eclipse.gmf.gmfgraph.util.RuntimeFQNSwitch; import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator; @@ -59,22 +61,26 @@ public static final String OPTION_MAIN_PACKAGE_NAME = SECTION_ID + ".mainPackageName"; public static final String OPTION_NEEDS_MAP_MODE = SECTION_ID + ".needsMapMode"; public static final String OPTION_INPUT_RESOURCE_FULL_PATH = SECTION_ID + ".inputResource"; - public static final String OPTION_OUTPUT_RESOURCE_FULL_PATH = SECTION_ID + ".outputResource"; + public static final String OPTION_OUTPUT_GALLERY_FULL_PATH = SECTION_ID + ".outputGallery"; + public static final String OPTION_OUTPUT_DIAGRAM_ELEMENTS_FULL_PATH = SECTION_ID + ".outputDiagramElements"; private TemplateOption myPackageNameOption; private FileNameOption myInputPathOption; private FileNameOption myOutputGalleryPathOption; - private final CachedInputValidationState myCachedInputValidationState; + private FileNameOption myOutputDiagramElementsPathOption; + private final InputValidationState myCachedInputValidationState; private ManifestElement[] myRequiredBundles; public ConverterSection(){ setPageCount(THE_ONLY_PAGE_INDEX + 1); myPackageNameOption = addOption(OPTION_MAIN_PACKAGE_NAME, "Generate figures package", null, THE_ONLY_PAGE_INDEX); myInputPathOption = addFileNameOption(false, OPTION_INPUT_RESOURCE_FULL_PATH, "Input GMFGraph instance", "", THE_ONLY_PAGE_INDEX); - myOutputGalleryPathOption = addFileNameOption(true, OPTION_OUTPUT_RESOURCE_FULL_PATH, "Create Figure Gallery", "", THE_ONLY_PAGE_INDEX); + myOutputGalleryPathOption = addFileNameOption(true, OPTION_OUTPUT_GALLERY_FULL_PATH, "Create Figure Gallery", "", THE_ONLY_PAGE_INDEX); myOutputGalleryPathOption.setRequired(false); + myOutputDiagramElementsPathOption = addFileNameOption(true, OPTION_OUTPUT_DIAGRAM_ELEMENTS_FULL_PATH, "Mirror diagram elements", "", THE_ONLY_PAGE_INDEX); + myOutputDiagramElementsPathOption.setRequired(false); addOption(OPTION_NEEDS_MAP_MODE, "Use IMapMode", false, THE_ONLY_PAGE_INDEX); - myCachedInputValidationState = new CachedInputValidationState(); + myCachedInputValidationState = new InputValidationState(myOutputGalleryPathOption, myOutputDiagramElementsPathOption); } public void addPages(Wizard wizard) { @@ -85,8 +91,6 @@ wizard.addPage(page); markPagesAdded(); validateOptions(myPackageNameOption); - validateOptions(myInputPathOption); - validateOptions(myOutputGalleryPathOption); } public IPluginReference[] getDependencies(String schemaVersion) { @@ -95,8 +99,9 @@ } protected void generateFiles(IProgressMonitor monitor) throws CoreException { - Resource resource = loadResource(myInputPathOption.getText()); - FigureGallery[] figures = findFigures(resource); + Resource input = loadResource(myInputPathOption.getText()); + FigureGallery[] figures = findFigures(input); + assert(figures.length > 0); StandaloneGenerator.Config config = new StandaloneGeneratorConfigAdapter(this); StandaloneGenerator generator = new StandaloneGenerator(figures, config, new RuntimeFQNSwitch()); generator.setSkipPluginStructure(false); @@ -105,6 +110,10 @@ readRequiredBundles(); // XXX readBuildProperties() and use getNewFiles to propagate // XXX readPluginProperties(), use ??? + if (!generator.getRunStatus().isOK()){ + throw new CoreException(generator.getRunStatus()); + } + createSeparateResources(generator.getGenerationInfo(), input); } catch (InterruptedException e) { String message = e.getMessage(); if (message == null){ @@ -115,29 +124,43 @@ // perhaps, don't need to treat this as error? throw new CoreException(new Status(IStatus.ERROR, MY_PLUGIN_ID, 0, "Failed to read generated manifest.mf", ex)); } finally { - resource.unload(); - } - if (!generator.getRunStatus().isOK()){ - throw new CoreException(generator.getRunStatus()); + input.unload(); } - createFigureGallery(generator.getGenerationInfo()); } - private void createFigureGallery(StandaloneGenerator.GenerationInfo info) throws CoreException { - if (!myOutputGalleryPathOption.isEmpty()){ - String path = myOutputGalleryPathOption.getText(); - Resource galleryResource = new ResourceSetImpl().createResource(URI.createFileURI(path)); - galleryResource.getContents().add(new StandaloneGalleryConverter().convertFigureGallery(info)); + private void createSeparateResources(StandaloneGenerator.GenerationInfo info, Resource input) throws CoreException { + if (shouldGenerate(myOutputGalleryPathOption)){ + String figureGalleryPath = myOutputGalleryPathOption.getText(); + ResourceSet separateResourceSet = new ResourceSetImpl(); + StandaloneGalleryConverter converter = new StandaloneGalleryConverter(info); + + Resource galleryResource = separateResourceSet.createResource(URI.createFileURI(figureGalleryPath)); + galleryResource.getContents().add(converter.convertFigureGallery()); + + Resource diagramElementsResource = null; + if (shouldGenerate(myOutputDiagramElementsPathOption)){ + Canvas mirror = converter.mirrorDiagramElements(Collections.singleton(input)); + if (mirror != null){ + diagramElementsResource = separateResourceSet.createResource(URI.createFileURI(myOutputDiagramElementsPathOption.getText())); + diagramElementsResource.getContents().add(mirror); + } + } + try { galleryResource.save(null); + if (diagramElementsResource != null){ + diagramElementsResource.save(null); + } } catch (IOException e) { - throw new CoreException(new Status(// - IStatus.ERROR, MY_PLUGIN_ID, 0, e.getMessage(), e - )); + throw new CoreException(new Status(IStatus.ERROR, MY_PLUGIN_ID, 0, e.getMessage(), e)); } } } + private boolean shouldGenerate(FileNameOption option){ + return option.isEnabled() && !option.isEmpty(); + } + private void readRequiredBundles() throws CoreException, IOException { try { IFile f = findGeneratedManifest(); @@ -161,7 +184,7 @@ private FigureGallery[] findFigures(Resource resource) { return new FigureFinder().findFigures(resource); } - + public String getPluginActivatorClassFQN(){ return model instanceof IPluginModel ? ((IPluginModel)model).getPlugin().getClassName() : null; } @@ -191,16 +214,11 @@ //does not affect state return; } - if (!validatePackageName()){ - return; - } - if (!validateInputPath()){ - return; + if (validateInputPath() && validatePackageName() && + validateOutputOption(myOutputDiagramElementsPathOption) && + validateOutputOption(myOutputGalleryPathOption)){ + resetPageState(); } - if (!validateOutputGalleryPath()){ - return; - } - resetPageState(); } public boolean isDependentOnParentWizard() { @@ -281,39 +299,62 @@ private boolean validateInputPath() { if (myInputPathOption.isEmpty()){ flagMissingRequiredOption(myInputPathOption); + myOutputDiagramElementsPathOption.setEnabled(false); + myOutputGalleryPathOption.setEnabled(false); return false; } String path = myInputPathOption.getText(); myCachedInputValidationState.updateInput(path); if (!myCachedInputValidationState.isValid()){ - getTheOnlyPage().setPageComplete(false); - getTheOnlyPage().setErrorMessage(myCachedInputValidationState.getErrorMessage()); + flagError(myCachedInputValidationState.getErrorMessage()); return false; } return true; } - private boolean validateOutputGalleryPath() { - if (myOutputGalleryPathOption.isEmpty()){ + private boolean validateOutputOption(FileNameOption option) { + if (!option.isEnabled()){ + return false; + } + if (!validateMirrorDiagramWithoutFigureGallery()){ + return false; + } + if (option.isEmpty()){ //optional -- ok return true; } - String path = myOutputGalleryPathOption.getText(); + String path = option.getText(); + return validatePath(path); + } + + private boolean validateMirrorDiagramWithoutFigureGallery(){ + if (!myOutputDiagramElementsPathOption.isEmpty()){ + if (myOutputGalleryPathOption.isEmpty() || myOutputDiagramElementsPathOption.getText().equals(myOutputGalleryPathOption.getText())){ + flagError("In order to mirror diagram elements you have to generate separate figure gallery"); + return false; + } + } + return true; + } + + private boolean validatePath(String path){ try { - URI.createFileURI(path); + return URI.createFileURI(path) != null; } catch (IllegalArgumentException e){ - String message = MessageFormat.format("Path {0} is invalid", new Object[] {path}); - getTheOnlyPage().setPageComplete(false); - getTheOnlyPage().setErrorMessage(message); + flagError(MessageFormat.format("Path {0} is invalid", new Object[] {path})); return false; } - return true; } - + private WizardPage getTheOnlyPage() { return getPage(THE_ONLY_PAGE_INDEX); } + private void flagError(String message){ + getTheOnlyPage().setPageComplete(false); + getTheOnlyPage().setErrorMessage(message); + } + private static Resource loadResource(String path){ Resource resource = new ResourceSetImpl().createResource(URI.createFileURI(path)); try { @@ -324,53 +365,77 @@ } } - private static class CachedInputValidationState { + private static class InputValidationState { private String myCachedPath; - private boolean myCachedIsValid; private String myCachedErrorMessage; + private boolean myHasDiagramElement; + private boolean myHasFigure; + private final FileNameOption myDiagramElementsOption; + private final FileNameOption myGalleryOption; + + public InputValidationState(FileNameOption galleryOption, FileNameOption diagramElementsOption){ + myGalleryOption = galleryOption; + myDiagramElementsOption = diagramElementsOption; + } public void updateInput(String path){ if (myCachedPath == null || !myCachedPath.equals(path)){ - myCachedIsValid = validateInputPath(path); + myCachedPath = path; + validateInputPath(path); + myGalleryOption.setEnabled(myHasFigure); + myDiagramElementsOption.setEnabled(myHasDiagramElement); } } public boolean isValid(){ - return myCachedIsValid; + return myHasFigure; } public String getErrorMessage(){ return myCachedErrorMessage; } - private boolean hasAtLeastOneFigure(Resource resource){ - for (TreeIterator contents = resource.getAllContents(); contents.hasNext();){ - EObject next = (EObject) contents.next(); - if (next instanceof Figure){ - return true; - } - } - return false; - } - - private boolean validateInputPath(String path) { + private void validateInputPath(String path) { + myHasDiagramElement = false; + myHasFigure = false; myCachedErrorMessage = null; + if (path == null || !new File(path).exists()){ myCachedErrorMessage = MessageFormat.format("Can not find file {0}", new Object[] {path}); - return false; + return; } Resource resource = loadResource(path); - boolean isValid = resource != null && hasAtLeastOneFigure(resource); if (resource != null){ - resource.unload(); + classifyContents(resource); } - if (!isValid){ + + if (!myHasFigure){ myCachedErrorMessage = MessageFormat.format("File {0} does not contain any figure definitions", new Object[] {path}); - return false; } - return true; } + private void classifyContents(Resource resource){ + myHasDiagramElement = false; + myHasFigure = false; + for (TreeIterator contents = resource.getAllContents(); contents.hasNext();){ + EObject next = (EObject) contents.next(); + if (next instanceof FigureGallery){ + if (!myHasFigure){ + FigureGallery nextGallery = (FigureGallery) next; + myHasFigure = !nextGallery.getFigures().isEmpty(); + } + contents.prune(); + } + if (next instanceof DiagramElement){ + myHasDiagramElement = true; + contents.prune(); + } + if (myHasDiagramElement && myHasFigure){ + break; + } + } + } + } } Index: src/org/eclipse/gmf/internal/graphdef/codegen/ui/GeneratePluginAction.java =================================================================== RCS file: /cvsroot/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.graphdef.codegen.ui/src/org/eclipse/gmf/internal/graphdef/codegen/ui/GeneratePluginAction.java,v retrieving revision 1.2 diff -u -r1.2 GeneratePluginAction.java --- src/org/eclipse/gmf/internal/graphdef/codegen/ui/GeneratePluginAction.java 10 Mar 2006 15:50:44 -0000 1.2 +++ src/org/eclipse/gmf/internal/graphdef/codegen/ui/GeneratePluginAction.java 13 Mar 2006 15:04:55 -0000 @@ -21,6 +21,7 @@ import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.jobs.Job; 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.ecore.resource.impl.ResourceSetImpl; @@ -67,6 +68,7 @@ generator.setSkipPluginStructure(false); new Job(action.getText()) { + private IContainer myResourcesContainer; { // setUser(true); FIXME fixed after M5, uncoment when switching to M6 } @@ -77,9 +79,13 @@ if (!generator.getRunStatus().isOK()) { return generator.getRunStatus(); } - IFile galleryFile = decideOnDestinationFile(); - Resource r = rs.createResource(URI.createPlatformResourceURI(galleryFile.getFullPath().toString())); - return saveNewFigureGallery(r, generator.getGenerationInfo()); + StandaloneGalleryConverter converter = new StandaloneGalleryConverter(generator.getGenerationInfo()); + + IStatus result = saveToFile(decideOnDestinationFile("bundled"), converter.convertFigureGallery()); + if (result.isOK()){ + result = saveToFile(decideOnDestinationFile("mirrored"), converter.mirrorDiagramElements(rs.getResources())); + } + return result; } catch (InterruptedException e) { return Status.CANCEL_STATUS; } finally { @@ -89,35 +95,47 @@ } } - private IFile decideOnDestinationFile() { - StandaloneGenerator.Config config = generator.getGenerationInfo().getConfig(); - IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(config.getPluginID()); - assert p.exists(); // generator.runStatus.ok makes us believe - IContainer fileContainer = p; - IFolder modelsFolder = p.getFolder("models"); - if (!modelsFolder.exists()) { - try { - modelsFolder.create(true, true, new NullProgressMonitor()); - fileContainer = modelsFolder; - } catch (CoreException ex) { - // ignore or log? - } - } - final IPath baseName = new Path("bundled"); + private IFile decideOnDestinationFile(String baseName) { + final IPath basePath = new Path(baseName); final String ext = "gmfgraph"; - IFile galleryFile = fileContainer.getFile(baseName.addFileExtension(ext)); - for (int i = 1; galleryFile.exists(); i++) { - galleryFile = fileContainer.getFile(new Path(baseName.lastSegment() + String.valueOf(i)).addFileExtension(ext)); + IFile resultFile = getResourcesContainer().getFile(basePath.addFileExtension(ext)); + for (int i = 1; resultFile.exists(); i++) { + resultFile = getResourcesContainer().getFile(new Path(basePath.lastSegment() + String.valueOf(i)).addFileExtension(ext)); + } + return resultFile; + } + + private IContainer getResourcesContainer(){ + if (myResourcesContainer == null){ + IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(getConfig().getPluginID()); + assert p.exists(); // generator.runStatus.ok makes us believe + myResourcesContainer = p; + IFolder modelsFolder = p.getFolder("models"); + if (!modelsFolder.exists()) { + try { + modelsFolder.create(true, true, new NullProgressMonitor()); + myResourcesContainer = modelsFolder; + } catch (CoreException ex) { + // ignore or log? + } + } } - return galleryFile; + return myResourcesContainer; + } + + private StandaloneGenerator.Config getConfig(){ + return generator.getGenerationInfo().getConfig(); } - private IStatus saveNewFigureGallery(Resource galleryResource, StandaloneGenerator.GenerationInfo info) { - galleryResource.getContents().add(new StandaloneGalleryConverter().convertFigureGallery(info)); - try { - galleryResource.save(null); - } catch (IOException e) { - return new Status(IStatus.ERROR, "org.eclipse.gmf.graphdef.codegen.ui", 0, e.getMessage(), e); + private IStatus saveToFile(IFile outputFile, EObject root) { + if (root != null){ + Resource outputResource = rs.createResource(URI.createPlatformResourceURI(outputFile.getFullPath().toString())); + outputResource.getContents().add(root); + try { + outputResource.save(null); + } catch (IOException e) { + return new Status(IStatus.ERROR, "org.eclipse.gmf.graphdef.codegen.ui", 0, e.getMessage(), e); + } } return Status.OK_STATUS; } Index: src/org/eclipse/gmf/internal/graphdef/codegen/ui/StandaloneGalleryConverter.java =================================================================== RCS file: /cvsroot/technology/org.eclipse.gmf/plugins/org.eclipse.gmf.graphdef.codegen.ui/src/org/eclipse/gmf/internal/graphdef/codegen/ui/StandaloneGalleryConverter.java,v retrieving revision 1.2 diff -u -r1.2 StandaloneGalleryConverter.java --- src/org/eclipse/gmf/internal/graphdef/codegen/ui/StandaloneGalleryConverter.java 10 Mar 2006 15:50:15 -0000 1.2 +++ src/org/eclipse/gmf/internal/graphdef/codegen/ui/StandaloneGalleryConverter.java 13 Mar 2006 15:04:56 -0000 @@ -12,8 +12,16 @@ package org.eclipse.gmf.internal.graphdef.codegen.ui; +import java.util.Collection; import java.util.Enumeration; +import java.util.HashSet; +import java.util.Iterator; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.gmf.gmfgraph.Canvas; import org.eclipse.gmf.gmfgraph.ConnectionFigure; import org.eclipse.gmf.gmfgraph.CustomFigure; import org.eclipse.gmf.gmfgraph.DecorationFigure; @@ -23,20 +31,59 @@ import org.eclipse.gmf.graphdef.codegen.StandaloneGenerator; public class StandaloneGalleryConverter { - public FigureGallery convertFigureGallery(StandaloneGenerator.GenerationInfo generationInfo){ + private final StandaloneGenerator.GenerationInfo myGenerationInfo; + private final DiagramElementsCopier myDiagramElementsCopier; + + public StandaloneGalleryConverter(StandaloneGenerator.GenerationInfo generationInfo){ + myGenerationInfo = generationInfo; + myDiagramElementsCopier = new DiagramElementsCopier(); + } + + public FigureGallery convertFigureGallery(){ FigureGallery result = GMFGraphFactory.eINSTANCE.createFigureGallery(); - String generatedBundle = generationInfo.getConfig().getPluginID(); + String generatedBundle = myGenerationInfo.getConfig().getPluginID(); result.setImplementationBundle(generatedBundle); - for (Enumeration originalFigures = generationInfo.getProcessedFigures(); originalFigures.hasMoreElements();){ + for (Enumeration originalFigures = myGenerationInfo.getProcessedFigures(); originalFigures.hasMoreElements();){ Figure nextOriginal = (Figure) originalFigures.nextElement(); - String nextConvertedFqn = generationInfo.getGeneratedClassFQN(nextOriginal); + String nextConvertedFqn = myGenerationInfo.getGeneratedClassFQN(nextOriginal); CustomFigure custom = createCustomFigure(nextOriginal); custom.setName(nextOriginal.getName()); custom.setBundleName(generatedBundle); custom.setQualifiedClassName(nextConvertedFqn); result.getFigures().add(custom); + myDiagramElementsCopier.registerSubstitution(nextOriginal, custom); + } + return result; + } + + public Canvas mirrorDiagramElements(Collection resources){ + Canvas result = null; + for (Iterator it = resources.iterator(); it.hasNext();){ + Resource next = (Resource)it.next(); + if (!next.getContents().isEmpty()){ + EObject root = (EObject) next.getContents().get(0); + if (root instanceof Canvas){ + Canvas original = (Canvas)root; + if (result == null){ + result = GMFGraphFactory.eINSTANCE.createCanvas(); + result.setName(original.getName()); + } + Collection children = myDiagramElementsCopier.copyAll(original.getChildren()); + Collection compartments = myDiagramElementsCopier.copyAll(original.getCompartments()); + Collection labels = myDiagramElementsCopier.copyAll(original.getLabels()); + Collection nodes = myDiagramElementsCopier.copyAll(original.getNodes()); + + result.getChildren().addAll(children); + result.getCompartments().addAll(compartments); + result.getLabels().addAll(labels); + result.getNodes().addAll(nodes); + } + } + } + if (result != null && !result.eContents().isEmpty()){ + myDiagramElementsCopier.copyReferences(); } return result; } @@ -51,5 +98,23 @@ } return factory.createCustomFigure(); } + + private static class DiagramElementsCopier extends EcoreUtil.Copier { + private final HashSet myOriginalFigures = new HashSet(); + + public void registerSubstitution(Figure original, CustomFigure substituted){ + put(original, substituted); + myOriginalFigures.add(original); + } + + protected void copyReference(EReference eReference, EObject eObject, EObject copyEObject) { + if (EcoreUtil.isAncestor(myOriginalFigures, eObject)){ + //no such features in the CustomFigure's + return; + } + super.copyReference(eReference, eObject, copyEObject); + } + + } }