### Eclipse Workspace Patch 1.0 #P org.eclipse.gmf.codegen.ui Index: plugin.properties =================================================================== RCS file: /cvsroot/modeling/org.eclipse.gmf/plugins/org.eclipse.gmf.codegen.ui/plugin.properties,v retrieving revision 1.16 diff -u -r1.16 plugin.properties --- plugin.properties 5 Apr 2007 20:57:00 -0000 1.16 +++ plugin.properties 16 Apr 2007 10:46:04 -0000 @@ -23,3 +23,5 @@ migrate.model.action=Migrate to GMF 2.0 dashboard.gende=Generate diagram editor + +addextensionmodel.action=Add extension model... Index: plugin.xml =================================================================== RCS file: /cvsroot/modeling/org.eclipse.gmf/plugins/org.eclipse.gmf.codegen.ui/plugin.xml,v retrieving revision 1.18 diff -u -r1.18 plugin.xml --- plugin.xml 5 Apr 2007 20:57:00 -0000 1.18 +++ plugin.xml 16 Apr 2007 10:46:04 -0000 @@ -29,6 +29,19 @@ id="gmf.codegen.ui.executeTemplatesAction"> + + + + 0) { + List nonAbstractClasses = readAllNonAbstractClasses(userEcoreUri); + int selectedClass = myInitialObjectCreationPage.getInitialObjectNumber(nonAbstractClasses); + if (selectedClass >= 0 && selectedClass < nonAbstractClasses.size()) { + EClass selected = nonAbstractClasses.get(selectedClass); + if (selected != null) { + EObject instantiated = selected.getEPackage().getEFactoryInstance().create(selected); + String gmfgenUri = URI.createPlatformResourceURI(getGmfgenFile().getFullPath().toString(), true).toString(); + Resource[] resources = getLoadedResources(new String[] { gmfgenUri, userEcoreUri }); + + resources[0].getContents().add(instantiated); + + // Save the contents of the resource to the file system. + // + Map options = new HashMap(); + options.put(XMLResource.OPTION_ENCODING, myInitialObjectCreationPage.getEncoding()); + resources[0].save(options); + resources[0].unload(); + resources[1].unload(); + } + } + } + } catch (Exception e) { + EcorePlugin.INSTANCE.getPluginLogger().log(e); + } finally { + progressMonitor.done(); + } + } + }; + + getContainer().run(false, false, operation); + + // Select the new file resource in the current view. + // + IWorkbenchWindow workbenchWindow = getWorkbench().getActiveWorkbenchWindow(); + IWorkbenchPage page = workbenchWindow.getActivePage(); + final IWorkbenchPart activePart = page.getActivePart(); + if (activePart instanceof ISetSelectionTarget) { + final ISelection targetSelection = new StructuredSelection(myFile); + getShell().getDisplay().asyncExec(new Runnable() { + + public void run() { + ((ISetSelectionTarget) activePart).selectReveal(targetSelection); + } + }); + } + + // Open an editor on the new file. + // + try { + IEditorDescriptor defaultEditor = getWorkbench().getEditorRegistry().getDefaultEditor(myFile.getFullPath().toString()); + page.openEditor(new FileEditorInput(myFile), defaultEditor == null ? "org.eclipse.gmf.codegen.gmfgen.presentation.GMFGenEditorID" : defaultEditor.getId()); //$NON-NLS-1$ + } catch (PartInitException exception) { + MessageDialog.openError(workbenchWindow.getShell(), Messages.AddExtensionModelWizard_8, exception.getMessage()); + return false; + } + return true; + } catch (Exception e) { + EcorePlugin.INSTANCE.getPluginLogger().log(e); + return false; + } + } + + /** + * This is the page which allows to browse workspace in order to locate custom metamodel. + */ + public class ModelImporterDetailPage extends ModelConverterURIPage { + + private String myUriString; + + private Resource[] myResources; + + public ModelImporterDetailPage(ModelConverter modelConverter, String pageName) { + super(modelConverter, pageName); + } + + protected boolean browseFileSystem() { + FileDialog fileDialog = new FileDialog(getShell(), SWT.OPEN | SWT.SINGLE); + fileDialog.setFilterExtensions(getFilterExtensions()); + URI modelURI = getModelLocationURI(getUserEcoreUriString()); + if (modelURI != null) { + fileDialog.setFileName(modelURI.toFileString()); + } + if (fileDialog.open() != null && fileDialog.getFileNames().length > 0) { + String filePath = fileDialog.getFilterPath() + File.separator + fileDialog.getFileNames()[0]; + String uriText = URI.createFileURI(filePath).toString(); + setURIText(uriText); + uriTextModified(uriText); + return true; + } + return false; + } + + public URI getModelLocationURI(String uriString) { + if (isPageComplete()) { + return URI.createURI(uriString); + } + return null; + } + + protected boolean browseWorkspace() { + IFile[] selected = WorkspaceResourceDialog.openFileSelection(getShell(), Messages.AddExtensionModelWizard_9, Messages.AddExtensionModelWizard_10, false, getInitialWorkspaceSelection(), + getViewerFiltersList()); + if (selected.length == 1 && selected[0] != null) { + String uriText = URI.createPlatformResourceURI(selected[0].getFullPath().toString(), true).toString(); + setURIText(uriText); + uriTextModified(uriText); + return true; + } + return false; + } + + protected void uriTextModified(String text) { + super.uriTextModified(text); + refreshModel(); + } + + protected void refreshModel() { + WorkspaceModifyOperation initializeOperation = new WorkspaceModifyOperation() { + + protected void execute(IProgressMonitor progressMonitor) throws CoreException { + Diagnostic errorDiagnostic = null; + setErrorMessage(null); + setMessage(null); + + Monitor monitor = BasicMonitor.toMonitor(progressMonitor); + try { + String uriString = uriText.getText(); + Resource[] resources = getLoadedResources(new String[] { uriString }); + setResources(resources); + if (validatePage()) { + List nonAbstractClassNames = readAllNonAbstractClasses(uriString); + myInitialObjectCreationPage.refresh(nonAbstractClassNames); + } + } catch (IOException e) { + EcorePlugin.INSTANCE.getPluginLogger().log(e); + errorDiagnostic = ConverterUtil.createErrorDiagnostic(e, true); + } catch (RuntimeException e) { + EcorePlugin.INSTANCE.getPluginLogger().log(e); + errorDiagnostic = ConverterUtil.createErrorDiagnostic(e, true); + } finally { + monitor.done(); + setResources(null); + } + + if (errorDiagnostic != null) { + handleDiagnostic(errorDiagnostic, errorDiagnostic.getMessage(), Messages.AddExtensionModelWizard_11, Messages.AddExtensionModelWizard_12); + } + } + + }; + + myUriString = uriText.getText(); + + try { + getContainer().run(false, false, initializeOperation); + } catch (Exception e) { + EcorePlugin.INSTANCE.getPluginLogger().log(e); + } + + if (validatePage()) { + setPageComplete(true); + } else { + setPageComplete(false); + uriText.selectAll(); + uriText.setFocus(); + } + } + + private void setResources(Resource[] resources) { + myResources = resources; + + } + + public String getUserEcoreUriString() { + return myUriString; + } + + public boolean validatePage() { + return myResources != null && myResources.length > 0; + } + + private Object[] convertResourcesToWorkspaceResources(Resource[] resources) { + IResource[] result = new IResource[resources.length]; + for (int i=0; i getViewerFiltersList() { + ViewerFilter filter = new ViewerFilter() { + + public boolean select(Viewer viewer, Object parentElement, Object element) { + if (element instanceof IContainer) { + return true; + } + if (element instanceof IFile) { + IFile iFile = (IFile) element; + String[] exts = getFilterExtensions(); + boolean goodExt = false; + for (int i = 0; i < exts.length; i++) { + if (iFile.getFileExtension().equals(exts[i])) { + goodExt = true; + } + } + return goodExt; + } + return false; + } + }; + return Collections.singletonList(filter); + } + + private String[] getFilterExtensions() { + return new String[] { "ecore" }; //$NON-NLS-1$ + } + } + + /** + * This is the page where the type of object to create is selected. + */ + public class EcoreModelWizardInitialObjectCreationPage extends WizardPage { + + protected Combo initialObjectField; + + protected List encodings; + + protected Combo encodingField; + + /** + * Pass in the selection. + */ + public EcoreModelWizardInitialObjectCreationPage(String pageId) { + super(pageId); + } + + public void refresh(List initialObjectNames) { + initialObjectField.setItems(new String[0]); + for (EClass objectName : initialObjectNames) { + initialObjectField.add(getLabel(objectName)); + } + if (initialObjectField.getItemCount() > 0) { + initialObjectField.select(0); + } + } + + public void createControl(Composite parent) { + Composite composite = new Composite(parent, SWT.NONE); + { + GridLayout layout = new GridLayout(); + layout.numColumns = 1; + layout.verticalSpacing = 12; + composite.setLayout(layout); + + GridData data = new GridData(); + data.verticalAlignment = GridData.FILL; + data.grabExcessVerticalSpace = true; + data.horizontalAlignment = GridData.FILL; + composite.setLayoutData(data); + } + + Label containerLabel = new Label(composite, SWT.LEFT); + { + containerLabel.setText(Messages.AddExtensionModelWizard_14); + + GridData data = new GridData(); + data.horizontalAlignment = GridData.FILL; + containerLabel.setLayoutData(data); + } + + initialObjectField = new Combo(composite, SWT.BORDER); + { + GridData data = new GridData(); + data.horizontalAlignment = GridData.FILL; + data.grabExcessHorizontalSpace = true; + initialObjectField.setLayoutData(data); + } + + initialObjectField.addModifyListener(validator); + + Label encodingLabel = new Label(composite, SWT.LEFT); + { + encodingLabel.setText(Messages.AddExtensionModelWizard_15); + + GridData data = new GridData(); + data.horizontalAlignment = GridData.FILL; + encodingLabel.setLayoutData(data); + } + encodingField = new Combo(composite, SWT.BORDER); + { + GridData data = new GridData(); + data.horizontalAlignment = GridData.FILL; + data.grabExcessHorizontalSpace = true; + encodingField.setLayoutData(data); + } + + for (String encoding : getEncodings()) { + encodingField.add(encoding); + } + + encodingField.select(0); + encodingField.addModifyListener(validator); + + setPageComplete(validatePage()); + setControl(composite); + } + + protected ModifyListener validator = new ModifyListener() { + + public void modifyText(ModifyEvent e) { + setPageComplete(validatePage()); + } + }; + + protected boolean validatePage() { + return initialObjectField.getText() != null && initialObjectField.getText().trim().length() > 0 && getEncodings().contains(encodingField.getText()); + } + + public void setVisible(boolean visible) { + super.setVisible(visible); + if (visible) { + if (initialObjectField.getItemCount() == 1) { + initialObjectField.clearSelection(); + encodingField.setFocus(); + } else { + encodingField.clearSelection(); + initialObjectField.setFocus(); + } + } + } + + public String getInitialObjectName(List allClasses) { + String label = initialObjectField.getText(); + + for (EClass next : allClasses) { + if (getLabel(next).equals(label)) { + return label; + } + } + return null; + } + + public int getInitialObjectNumber(List allClasses) { + String label = initialObjectField.getText(); + for (int i = 0; i < allClasses.size(); i++) { + EClass next = allClasses.get(i); + if (getLabel(next).equals(label)) { + return i; + } + } + return 0; + } + + public String getEncoding() { + return encodingField.getText(); + } + + /** + * Returns the label for the specified type name. + */ + protected String getLabel(EClass type) { + return type.getName(); + } + + protected Collection getEncodings() { + if (encodings == null) { + encodings = new ArrayList(); + for (StringTokenizer stringTokenizer = new StringTokenizer(Messages.AddExtensionModelWizard_16); stringTokenizer.hasMoreTokens();) { + encodings.add(stringTokenizer.nextToken()); + } + } + return encodings; + } + } + + public static Resource[] getLoadedResources(String[] textURIs) throws IOException { + Resource[] result = new Resource[textURIs.length]; + ResourceSet resourceSet = new ResourceSetImpl(); + for (int i = 0; i < result.length; i++) { + URI nextURI = URI.createURI(textURIs[i]); + Resource resource = resourceSet.createResource(nextURI); + resource.load(resourceSet.getLoadOptions()); + result[i] = resource; + } + return result; + } + + public static List readAllNonAbstractClasses(String textURI) throws IOException { + List nonAbstractClasses = new ArrayList(); + Resource resource = getLoadedResources(new String[] { textURI })[0]; + if (resource != null && resource.isLoaded()) { + Iterator contents = resource.getAllContents(); + while (contents.hasNext()) { + EObject next = contents.next(); + if (next instanceof EClass) { + EClass clazz = (EClass) next; + if (!clazz.isAbstract()) { + nonAbstractClasses.add(clazz); + } + } + } + resource.unload(); + } + return nonAbstractClasses; + } + +} Index: src/org/eclipse/gmf/internal/codegen/popup/actions/AddExtensionModelAction.java =================================================================== RCS file: src/org/eclipse/gmf/internal/codegen/popup/actions/AddExtensionModelAction.java diff -N src/org/eclipse/gmf/internal/codegen/popup/actions/AddExtensionModelAction.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/gmf/internal/codegen/popup/actions/AddExtensionModelAction.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2007 Borland Software 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: + * Borland Software Corporation - initial API and implementation + */ +package org.eclipse.gmf.internal.codegen.popup.actions; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.wizard.WizardDialog; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IObjectActionDelegate; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPart; + +public class AddExtensionModelAction implements IObjectActionDelegate { + + private IFile myFile; + + private IWorkbenchPart myPart; + + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + myPart = targetPart; + } + + public void selectionChanged(IAction action, ISelection selection) { + myFile = (IFile) ((IStructuredSelection) selection).getFirstElement(); + } + + public void run(IAction action) { + // Create the wizard + AddExtensionModelWizard wizard = new AddExtensionModelWizard(); + wizard.init(getWorkbench(), new StructuredSelection(myFile)); + + // Create the wizard dialog + WizardDialog dialog = new WizardDialog(getShell(), wizard); + // Open the wizard dialog + dialog.open(); + } + + private Shell getShell() { + return myPart.getSite().getShell(); + } + + private IWorkbench getWorkbench() { + return myPart.getSite().getWorkbenchWindow().getWorkbench(); + } + +}