getMap() {
+ return map;
+ }
+ }
+
+ /**
+ * No instances, just an utility class.
+ */
+ private ValidationHelper() {
+ super();
+ }
+
+
+ /**
+ * Indicates whether the severiti of the given diagnostic matches the given
+ * bitmask.
+ * Note that a diagnostic with severity OK
will never match;
+ * use isOK
instead to detect a diagnostic with a severity of OK
.
+ *
+ * @param diagnostic a diagnostic to test for severity match
+ * @param severityBitMask a mask formed by bitwise or'ing severity mask constants
+ * (ERROR
, WARNING
, INFO
, CANCEL
)
+ *
+ * @return true
if there is at least one match, false
otherwise
+ *
+ * @see Diagnostic#ERROR
+ * @see Diagnostic#WARNING
+ * @see Diagnostic#INFO
+ * @see Diagnostic#CANCEL
+ */
+ public static boolean matches(Diagnostic diagnostic, int severityBitMask) {
+ return (diagnostic.getSeverity() & severityBitMask) != 0;
+ }
+
+ /**
+ * Returns whether this diagnostic indicates everything is OK. (neither
+ * info, warning, nor error).
+ *
+ * @return true
if this diagnostic has severity
+ * OK
, and false
otherwise
+ */
+ public static boolean isOK(Diagnostic diagnostic) {
+ return diagnostic.getSeverity() == Diagnostic.OK;
+ }
+
+ /**
+ * Validates the given EObject
and its all contents.
+ *
+ * If the EMF basic validation results in Diagnostic.CANCEL
+ * severity, no problem markers are created at all.
+ *
+ * The problem markers created by this operation are added as extra-data to
+ * the returned diagnostic and are accessible via
+ * {@link #getDiagnosticMarkerMap(Diagnostic)}.
+ *
+ * @param createMarkers
+ * if true
this operation produces validation
+ * problems markers provided that eObject.eResource() represents
+ * a file existing in the workspace. If true
no
+ * attempt to create markers is performed.
+ *
+ * @param progressMonitor
+ * the progress monitor to track validation progress, or
+ * null
if no progress monitoring is required.
+ * The implementation creates {@link SubProgressMonitor} as
+ * a sub-task of the given parent progressMonitor
+ * allocating 1 tick from the parent. #beginTask and #done operation
+ * on the subprogress monitor is called.
+ *
+ * @return the validation result diagnostic
+ */
+ public static Diagnostic validate(EObject eObject, boolean createMarkers, IProgressMonitor progressMonitor) {
+ IProgressMonitor monitor = null;
+ try {
+ int count = IProgressMonitor.UNKNOWN;
+ if(progressMonitor != null) {
+ for (Iterator i = eObject.eAllContents(); i.hasNext(); i.next()) {
+ ++count;
+ }
+ }
+
+ monitor = (progressMonitor != null) ? new SubProgressMonitor(progressMonitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK) : new NullProgressMonitor();
+ monitor.beginTask("", count); //$NON-NLS-1$
+ monitor.subTask(Messages.ValidationHelper_task_validate);
+
+ Diagnostic validationStatus = new SmartDiagnostician(monitor).validate(eObject);
+ if(validationStatus.getSeverity() == Diagnostic.CANCEL) {
+ return validationStatus;
+ }
+
+ if(createMarkers) {
+ return createMarkers(validationStatus, EValidator.MARKER);
+ }
+
+ return validationStatus;
+
+ } finally {
+ monitor.done();
+ }
+ }
+
+ /**
+ * Validates the given EObject
and its all contents.
+ *
+ * This method is progress monitoring ignorant shorthand of {@link #validate(EObject, boolean, IProgressMonitor)}
+ *
+ * @param createMarkers
+ * if true
this operation produces validation
+ * problems markers provided that eObject.eResource() represents
+ * a file existing in the workspace. If true
no
+ * attempt to create markers is performed.
+ *
+ * @return the validation result diagnostic
+ *
+ * @see #validate(EObject, boolean, IProgressMonitor)
+ */
+ public static Diagnostic validate(EObject eObject, boolean createMarkers) {
+ return validate(eObject, createMarkers, null);
+ }
+
+ /**
+ * Gets the diagnostic marker map associated with the given validation
+ * diagnostic.
+ *
+ * @param diagnostic
+ * a non-null validation diagnostic which resulted from calling
+ * {@link #validate(EObject, boolean, IProgressMonitor)}}
+ *
+ * @see #validate(EObject, boolean, IProgressMonitor)
+ */
+ public static DiagnosticMarkerMap getDiagnosticMarkerMap(Diagnostic diagnostic) {
+ List data = (diagnostic.getData() != null) ? diagnostic.getData() : Collections.EMPTY_LIST;
+ for (Iterator it = data.iterator(); it.hasNext();) {
+ Object dataItem = it.next();
+ if (dataItem instanceof DiagnosticMarkerMap) {
+ return (DiagnosticMarkerMap) dataItem;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Extracts the file of the validated model instance referenced by the given
+ * diagnostic.
+ *
+ * @param diagnostic
+ * a non-null validation diagnostic
+ * @return the file object or null
in case the resource
+ * associated with the validated object does not represents a file
+ * existing in the workspace.
+ */
+ public static IFile getFileFromDiagnostic(Diagnostic diagnostic) {
+ return new GMFMarkerHelper(EValidator.MARKER).getFileFromDiagnostic(diagnostic);
+ }
+
+ /**
+ * Creates resource problem markers {@link Resource.Diagnostic} encapsualted
+ * in the given diagnostic object.
+ *
+ * @param diagnostic
+ * non-null diagnostic eventually containing a hiearchy of
+ * diagnostics for which markers are to be created.
+ *
+ * @see #getDiagnosticMarkerMap(Diagnostic)
+ * @see Resource.Diagnostic
+ */
+ public static Diagnostic createResourceProblemMarkers(Diagnostic diagnostic) {
+ return createMarkers(diagnostic, null /* resource problem markers*/);
+ }
+
+ private static Diagnostic createMarkers(Diagnostic diagnostic, String markerID) {
+ GMFMarkerHelper markerHelper = new GMFMarkerHelper(markerID);
+ try {
+ markerHelper.deleteMarkers(diagnostic, false, IResource.DEPTH_ZERO);
+ if(!diagnostic.getChildren().isEmpty() && !isOK(diagnostic)) {
+ markerHelper.createMarkers(diagnostic);
+ }
+ } catch (CoreException e) {
+ IStatus status = Plugin.createError(Messages.ValidationHelper_e_marker_creation, e);
+ Plugin.log(status);
+ }
+ DiagnosticMarkerMap markerMap = new DiagnosticMarkerMap(markerHelper.getDiagnostic2MarkerMap());
+
+ List data = diagnostic.getData() != null ? (List)diagnostic.getData() : Collections.emptyList();
+ data = new ArrayList(data);
+ data.add(markerMap);
+
+ BasicDiagnostic result = new BasicDiagnostic(
+ diagnostic.getSource(), diagnostic.getCode(),
+ diagnostic.getMessage(), data.toArray());
+
+ result.addAll(diagnostic);
+ return result;
+ }
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/GMFGenNewFileCreationPage.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/GMFGenNewFileCreationPage.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/GMFGenNewFileCreationPage.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/GMFGenNewFileCreationPage.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,41 @@
+package org.eclipse.gmf.internal.bridge.transform;
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
+
+public class GMFGenNewFileCreationPage extends WizardNewFileCreationPage {
+
+ static final String EXT_GMFGEN = "gmfgen"; //$NON-NLS-1$
+
+ public GMFGenNewFileCreationPage(String pageId, IStructuredSelection selection) {
+ super(pageId, selection);
+ }
+
+ protected boolean validatePage() {
+ if (super.validatePage()) {
+ // Make sure the file ends in ".gmfgen".
+ //
+ String enteredExt = new Path(getFileName()).getFileExtension();
+ if (enteredExt == null || !enteredExt.equals(EXT_GMFGEN)) {
+ String pattern = Messages.GMFGenNewFileCreationPage_e_filename;
+ setErrorMessage(MessageFormat.format(pattern, new Object [] { EXT_GMFGEN }));
+ return false;
+ }
+ else {
+ return true;
+ }
+ }
+ else {
+ return false;
+ }
+ }
+
+ public IFile getModelFile() {
+ return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName()));
+ }
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/GenModelConfigurationPage.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/GenModelConfigurationPage.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/GenModelConfigurationPage.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/GenModelConfigurationPage.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,231 @@
+/**
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Alexander Fedorov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import org.eclipse.core.resources.IContainer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.importer.ui.EMFModelWizard;
+import org.eclipse.emf.importer.ui.GenModelReloadActionDelegate;
+import org.eclipse.gmf.internal.common.URIUtil;
+import org.eclipse.gmf.internal.common.ui.ResourceLocationProvider;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.window.Window;
+import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.Widget;
+import org.eclipse.ui.PlatformUI;
+
+
+class GenModelConfigurationPage extends ModelConfigurationPage {
+
+ private static final String FILE_EXT_ECORE = "ecore"; //$NON-NLS-1$
+ private static final String FILE_EXT_GENMODEL = "genmodel"; //$NON-NLS-1$
+
+ private Button createWizardBtn;
+ private Button createDefaultBtn;
+ private Button refreshStaleBtn;
+
+ GenModelConfigurationPage(String pageId, ResourceLocationProvider rlp, ResourceSet resourceSet) {
+ super(pageId, rlp, resourceSet);
+ }
+
+ protected String getModelFileExtension() {
+ return FILE_EXT_GENMODEL;
+ }
+
+ @Override
+ protected void createAdditionalControls(Composite parent) {
+ Composite createComposite = new Composite(parent, SWT.NONE);
+ createComposite.setLayout(new GridLayout(2, true));
+ createComposite.setLayoutData(createGridData());
+
+ createWizardBtn = new Button(createComposite, SWT.PUSH);
+ createWizardBtn.setText(Messages.GenModelConfigurationPage_btn_new_wizard);
+ setButtonLayoutData(createWizardBtn);
+
+ createDefaultBtn = new Button(createComposite, SWT.PUSH);
+ createDefaultBtn.setText(Messages.GenModelConfigurationPage_btn_create_default);
+ setButtonLayoutData(createDefaultBtn);
+
+ refreshStaleBtn = new Button(createComposite, SWT.PUSH);
+ refreshStaleBtn.setText(Messages.GenModelConfigurationPage_btn_refresh_stale);
+ setButtonLayoutData(refreshStaleBtn);
+
+ SelectionAdapter selectionAdapter = new SelectionAdapter() {
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ handleSelection(e.widget);
+ }
+ };
+ createWizardBtn.addSelectionListener(selectionAdapter);
+ createDefaultBtn.addSelectionListener(selectionAdapter);
+ refreshStaleBtn.addSelectionListener(selectionAdapter);
+ }
+
+ private GridData createGridData() {
+ GridData gd = new GridData();
+ gd.horizontalAlignment = GridData.FILL;
+ gd.grabExcessHorizontalSpace = true;
+ return gd;
+ }
+
+ void handleSelection(Widget w) {
+ if (createDefaultBtn.equals(w)){
+ createDefault();
+ } else if (createWizardBtn.equals(w)) {
+ launchWizard();
+ } else if (refreshStaleBtn.equals(w)) {
+ refreshGenmodel();
+ }
+ }
+
+ @Override
+ protected void initControls() {
+ super.initControls();
+ if (getURI() == null) {
+ findGenmodel();
+ } else {
+ updateControls();
+ }
+ }
+
+ void findGenmodel() {
+ try {
+ GenModel genModel = getOperation().findGenmodel(getResourceSet());
+ Resource r = genModel.eResource();
+ URI genURI = r.getURI();
+ setURI(genURI);
+ updateURI();
+ } catch (CoreException e) {
+ setErrorMessage(e.getMessage());
+ updateControls();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.gmf.internal.common.ui.ModelSelectionPage#resourceChanged()
+ */
+ @Override
+ protected void resourceChanged() {
+ super.resourceChanged();
+ updateControls();
+ }
+
+ private void updateControls() {
+ GenModelDetector gmd = getOperation().getGenModelDetector();
+ if (gmd!= null) {
+ createDefaultBtn.setEnabled(gmd.canCreateDefault());
+ }
+ IStatus stale = getOperation().getStaleGenmodelStatus();
+ if (stale != null && !stale.isOK()) {
+ setStatusMessage(stale);
+ refreshStaleBtn.setEnabled((getURI() != null));
+ } else {
+ refreshStaleBtn.setEnabled(false);
+ }
+ }
+
+ @Override
+ protected Resource doLoadResource(IProgressMonitor monitor) throws CoreException {
+ GenModel genModel = getOperation().loadGenModel(getResourceSet(), getURI(), monitor);
+ return genModel.eResource();
+ }
+
+ private void createDefault() {
+ try {
+ TransformToGenModelOperation to = getOperation();
+ GenModelDetector gmd = to.getGenModelDetector();
+ URI mapURI = to.getMapURI();
+ IFile mapFile = URIUtil.getFile(mapURI);
+ String pluginID = mapFile.getProject().getName();
+ IFile genmodel = gmd.createDefault(pluginID, mapFile);
+ URI genURI = URI.createPlatformResourceURI(genmodel.getFullPath().toString(), true);
+ setURI(genURI);
+ updateURI();
+ } catch (CoreException e) {
+ setErrorMessage(e.getMessage());
+ }
+ }
+
+ private void launchWizard() {
+ URI mapURI = getOperation().getMapURI();
+ IFile mapFile = URIUtil.getFile(mapURI);
+ IFile genmodel = createWithWizard(getShell(), mapFile);
+ if (genmodel != null) {
+ setURI(URI.createPlatformResourceURI(genmodel.getFullPath().toString(), true));
+ updateURI();
+ }
+ }
+
+ private static IFile createWithWizard(Shell shell, IFile patternResource) {
+ final IFile[] result = new IFile[1];
+ EMFModelWizard wizard = new EMFModelWizard() {
+ @Override
+ public boolean performFinish() {
+ result[0] = ResourcesPlugin.getWorkspace().getRoot().getFile(genModelContainerPath.append(genModelFileName));
+ return super.performFinish();
+ }
+ };
+ wizard.init(PlatformUI.getWorkbench(), createSelectionForEMFWizard(patternResource));
+ if (Window.OK == new WizardDialog(shell, wizard).open()) {
+ assert result[0] != null;
+ return result[0];
+ }
+ return null;
+ }
+ private static StructuredSelection createSelectionForEMFWizard(IFile patternResource) {
+ final IContainer parent = patternResource.getParent();
+ IFile neighbour = parent.getFile(new Path(patternResource.getName()).removeFileExtension().addFileExtension(FILE_EXT_ECORE));
+ if (neighbour.exists()) {
+ return new StructuredSelection(neighbour);
+ } else {
+ return new StructuredSelection(patternResource);
+ }
+ }
+
+ private void refreshGenmodel() {
+ IPath p = new Path(getURI().path()).removeFirstSegments(1);
+ IFile genModelFile = ResourcesPlugin.getWorkspace().getRoot().getFile(p);
+ GenModelReloadActionDelegate action = new GenModelReloadActionDelegate();
+ IAction uiAction = new Action() {
+ //empty
+ };
+ action.selectionChanged(uiAction, new StructuredSelection(genModelFile));
+ action.run(uiAction);
+ updateURI();
+ }
+
+ private TransformToGenModelOperation getOperation() {
+ TransformToGenModelWizard wizard = (TransformToGenModelWizard) getWizard();
+ return wizard.getTransformOperation();
+ }
+
+}
Index: .settings/org.eclipse.core.resources.prefs
===================================================================
RCS file: .settings/org.eclipse.core.resources.prefs
diff -N .settings/org.eclipse.core.resources.prefs
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ .settings/org.eclipse.core.resources.prefs 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,3 @@
+#Wed Jan 17 18:24:51 MSK 2007
+eclipse.preferences.version=1
+encoding//src/org/eclipse/gmf/internal/bridge/transform/messages.properties=8859_1
Index: src/org/eclipse/gmf/internal/bridge/transform/GenModelDetector.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/GenModelDetector.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/GenModelDetector.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/GenModelDetector.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,154 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EPackage;
+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.emf.ecore.util.EcoreUtil;
+import org.eclipse.gmf.internal.bridge.genmodel.BasicGenModelAccess;
+import org.eclipse.gmf.internal.bridge.genmodel.DummyGenModel;
+import org.eclipse.gmf.internal.bridge.genmodel.FileGenModelAccess;
+import org.eclipse.gmf.internal.bridge.genmodel.GenModelAccess;
+import org.eclipse.gmf.internal.bridge.ui.Plugin;
+import org.eclipse.gmf.mappings.Mapping;
+
+/**
+ * Interact with user to retrieve genmodel for the domain model. XXX detect
+ * stale genmodels and reconcile them based on user's decision
+ *
+ * @author artem
+ */
+public class GenModelDetector {
+ private final Mapping myMapping;
+ private GenModelAccess myGMAccess;
+ private Collection myPackages;
+
+ public GenModelDetector(Mapping mapping) {
+ assert mapping != null;
+ myMapping = mapping;
+ }
+
+ public IStatus detect() {
+ myPackages = findEPackages(myMapping);
+ if (myPackages.isEmpty()) {
+ myGMAccess = new GenModelAccess.Adapter(null);
+ return Status.OK_STATUS;
+ } else if (myPackages.size() == 1) {
+ final EPackage solePack = myPackages.iterator().next();
+ BasicGenModelAccess gma = new BasicGenModelAccess(solePack);
+ gma.initDefault();
+ return apply(gma);
+ } else {
+ return Plugin.createError(Messages.GenModelDetector_e_not_found, null);
+ }
+ }
+
+ public IStatus advise(URI genModelURI) {
+ assert genModelURI != null;
+ GenModelAccess gma = new FileGenModelAccess(genModelURI);
+ return apply(gma);
+ }
+
+ public IStatus advise(IFile workspaceFile) {
+ assert workspaceFile != null;
+ GenModelAccess gma = new FileGenModelAccess(workspaceFile);
+ return apply(gma);
+ }
+
+ private Collection findEPackages(Mapping mapping) {
+ Collection packages = new HashSet();
+ for (Iterator it = EcoreUtil.ExternalCrossReferencer.find(mapping).keySet().iterator(); it.hasNext();) {
+ Object next = it.next();
+ if (next instanceof EClass) {
+ packages.add(((EClass) next).getEPackage());
+ }
+ }
+ for (Iterator it = packages.iterator(); it.hasNext();) {
+ EPackage next = (EPackage) it.next();
+ if (next.getESuperPackage() != null && EcoreUtil.isAncestor(packages, next.getESuperPackage())) {
+ it.remove();
+ }
+ }
+ return packages;
+ }
+
+ private IStatus apply(GenModelAccess gma) {
+ IStatus load = gma.load(new ResourceSetImpl());
+ if (load.isOK()) {
+ gma.unload();
+ myGMAccess = gma;
+ return Status.OK_STATUS;
+ } else {
+ return load;
+ }
+ }
+
+ private EPackage getPrimaryPackage() {
+ if (myPackages == null || myPackages.size() == 0) {
+ return null;
+ }
+ return myPackages.iterator().next();
+ }
+
+ public boolean canCreateDefault() {
+ return myPackages != null && myPackages.size() == 1;
+ }
+
+ public IFile createDefault(String pluginID, IFile patternResource) throws CoreException {
+ DummyGenModel gma = new DummyGenModel(getPrimaryPackage(), null);
+ gma.setPluginID(pluginID);
+ GenModel model = gma.create();
+ IPath path = patternResource.getFullPath().removeFileExtension().addFileExtension("genmodel"); //$NON-NLS-1$
+ Resource res = new ResourceSetImpl().createResource(URI.createPlatformResourceURI(path.toString(), true));
+ res.getContents().add(model);
+ try {
+ res.save(null);
+ IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+ return file;
+ } catch (IOException ex) {
+ IStatus error = Plugin.createError(Messages.GenModelDetector_e_save, ex);
+ throw new CoreException(error);
+ }
+ }
+
+ public GenModel get(ResourceSet resSet) {
+ if (myGMAccess == null) {
+ throw new IllegalStateException(Messages.GenModelDetector_e_no_genmodelaccess);
+ }
+ IStatus s = myGMAccess.load(resSet);
+ if (!s.isOK()) {
+ throw new IllegalStateException(s.getMessage());
+ }
+ return myGMAccess.model();
+ }
+
+ public boolean checkState() {
+ return myGMAccess != null;
+ }
+
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/MapModelConfigurationPage.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/MapModelConfigurationPage.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/MapModelConfigurationPage.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/MapModelConfigurationPage.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Alexander Fedorov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.gmf.internal.common.ui.ResourceLocationProvider;
+import org.eclipse.gmf.mappings.Mapping;
+
+
+class MapModelConfigurationPage extends ModelConfigurationPage {
+
+ private static final String FILE_EXT_GMFMAP = "gmfmap"; //$NON-NLS-1$
+
+ MapModelConfigurationPage(String pageId, ResourceLocationProvider rlp, ResourceSet resourceSet) {
+ super(pageId, rlp, resourceSet);
+ }
+
+ protected String getModelFileExtension() {
+ return FILE_EXT_GMFMAP;
+ }
+
+ @Override
+ protected void initControls() {
+ super.initControls();
+ TransformToGenModelWizard wizard = (TransformToGenModelWizard) getWizard();
+ IFile target = wizard.getTargetFile();
+ getOperation().setGenURI(URI.createPlatformResourceURI(target.getFullPath().toString(), true));
+ }
+
+ @Override
+ protected Resource doLoadResource(IProgressMonitor monitor) throws CoreException {
+ TransformToGenModelOperation o = getOperation();
+ o.reset(getURI());
+ Mapping mapping = o.loadMappingModel(getResourceSet(), getURI(), monitor);
+ return mapping.eResource();
+ }
+
+ TransformToGenModelOperation getOperation() {
+ TransformToGenModelWizard wizard = (TransformToGenModelWizard) getWizard();
+ return wizard.getTransformOperation();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.gmf.internal.common.ui.ModelSelectionPage#resourceChanged()
+ */
+ @Override
+ protected void resourceChanged() {
+ super.resourceChanged();
+ IStatus load = getOperation().getLoadMappingStatus();
+ if (load != null) {
+ if ((load.getSeverity() == IStatus.WARNING) || (load.getSeverity() == IStatus.INFO) ) {
+ setStatusMessage(load);
+ }
+ }
+ }
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/GMFGenConfig.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/GMFGenConfig.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/GMFGenConfig.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/GMFGenConfig.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Michael Golubev (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.gmf.codegen.gmfgen.GMFGenPackage;
+import org.eclipse.gmf.codegen.gmfgen.GenChildContainer;
+import org.eclipse.gmf.codegen.gmfgen.GenNode;
+import org.eclipse.gmf.internal.common.reconcile.Copier;
+import org.eclipse.gmf.internal.common.reconcile.DefaultDecisionMaker;
+import org.eclipse.gmf.internal.common.reconcile.Matcher;
+import org.eclipse.gmf.internal.common.reconcile.ReconcilerConfigBase;
+import org.eclipse.gmf.internal.common.reconcile.ReflectiveMatcher;
+import org.eclipse.gmf.internal.common.reconcile.StringPatternDecisionMaker;
+
+/**
+ * XXX Perhaps, org.eclipse.gmf.codegen/oeg.internal.util would be better place for this class.
+ */
+public class GMFGenConfig extends ReconcilerConfigBase {
+ private final GMFGenPackage GMFGEN = GMFGenPackage.eINSTANCE;
+
+ public GMFGenConfig(){
+ setMatcher(GMFGEN.getGenEditorGenerator(), ALWAYS_MATCH);
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_CopyrightText());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_PackageNamePrefix());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_DomainFileExtension());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_DiagramFileExtension());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_SameFileForDiagramAndModel());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_ModelID());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_DynamicTemplates());
+ preserveIfSet(GMFGEN.getGenEditorGenerator(), GMFGEN.getGenEditorGenerator_TemplateDirectory());
+
+ setMatcher(GMFGEN.getGenPlugin(), ALWAYS_MATCH); //exactly one feature for ALWAYS_MATCH GenEditorGenerator
+ preserveIfSet(GMFGEN.getGenPlugin(), GMFGEN.getGenPlugin_ID());
+ preserveIfSet(GMFGEN.getGenPlugin(), GMFGEN.getGenPlugin_Name());
+ preserveIfSet(GMFGEN.getGenPlugin(), GMFGEN.getGenPlugin_Provider());
+ preserveIfSet(GMFGEN.getGenPlugin(), GMFGEN.getGenPlugin_Version());
+ preserveIfSet(GMFGEN.getGenPlugin(), GMFGEN.getGenPlugin_ActivatorClassName());
+ preserveIfSet(GMFGEN.getGenPlugin(), GMFGEN.getGenPlugin_PrintingEnabled());
+
+ setMatcher(GMFGEN.getGenEditorView(), ALWAYS_MATCH); //exactly one
+ preserveIfSet(GMFGEN.getGenEditorView(), GMFGEN.getGenEditorView_IconPath());
+ preserveIfSet(GMFGEN.getGenEditorView(), GMFGEN.getGenEditorView_ClassName());
+ preserveIfSet(GMFGEN.getGenEditorView(), GMFGEN.getGenEditorView_ID());
+
+ setMatcher(GMFGEN.getGenDiagram(), ALWAYS_MATCH);
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getShortcuts_ContainsShortcutsTo());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getShortcuts_ShortcutsProvidedFor());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getEditorCandies_CreationWizardIconPath());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getEditorCandies_CreationWizardCategoryID());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getGenDiagram_Synchronized());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_ValidationEnabled());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_LiveValidationUIFeedback());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_ValidationDecorators());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_ValidationDecoratorProviderClassName());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_ValidationDecoratorProviderPriority());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_ValidationProviderClassName());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_ValidationProviderPriority());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_MetricProviderPriority());
+ preserveIfSet(GMFGEN.getGenDiagram(), GMFGEN.getBatchValidation_MetricProviderClassName());
+
+ setMatcher(GMFGEN.getGenTopLevelNode(), getGenNodeMatcher());
+ preserveIfNotByPattern(GMFGEN.getGenTopLevelNode(), GMFGEN.getGenContainerBase_CanonicalEditPolicyClassName(), ".*" + GenChildContainer.CANONICAL_EDIT_POLICY_SUFFIX); //$NON-NLS-1$
+ preserveIfNotByPattern(GMFGEN.getGenTopLevelNode(), GMFGEN.getGenNode_GraphicalNodeEditPolicyClassName(), ".*" + GenNode.GRAPHICAL_NODE_EDIT_POLICY_SUFFIX); //$NON-NLS-1$
+ //[155332]preserveIfNotByPattern(GMFGEN.getGenTopLevelNode(), GMFGEN.getGenCommonBase_EditPartClassName(), ".*" + GenCommonBase.EDIT_PART_SUFFIX);
+ preserveIfSet(GMFGEN.getGenTopLevelNode(), GMFGEN.getGenNode_PrimaryDragEditPolicyQualifiedClassName());
+
+ setMatcher(GMFGEN.getGenChildNode(), getGenNodeMatcher());
+ preserveIfNotByPattern(GMFGEN.getGenChildNode(), GMFGEN.getGenContainerBase_CanonicalEditPolicyClassName(), ".*" + GenChildContainer.CANONICAL_EDIT_POLICY_SUFFIX); //$NON-NLS-1$
+ preserveIfNotByPattern(GMFGEN.getGenChildNode(), GMFGEN.getGenNode_GraphicalNodeEditPolicyClassName(), ".*" + GenNode.GRAPHICAL_NODE_EDIT_POLICY_SUFFIX); //$NON-NLS-1$
+ //[155332]preserveIfNotByPattern(GMFGEN.getGenChildNode(), GMFGEN.getGenCommonBase_EditPartClassName(), ".*" + GenCommonBase.EDIT_PART_SUFFIX);
+ preserveIfSet(GMFGEN.getGenChildNode(), GMFGEN.getGenNode_PrimaryDragEditPolicyQualifiedClassName());
+
+ setMatcher(GMFGEN.getGenChildLabelNode(), getGenNodeMatcher());
+ preserveIfSet(GMFGEN.getGenChildLabelNode(), GMFGEN.getGenNode_PrimaryDragEditPolicyQualifiedClassName());
+
+ setMatcher(GMFGEN.getGenChildSideAffixedNode(), getGenNodeMatcher());
+ preserveIfSet(GMFGEN.getGenChildSideAffixedNode(), GMFGEN.getGenNode_PrimaryDragEditPolicyQualifiedClassName());
+
+ setMatcher(GMFGEN.getGenCompartment(), new ReflectiveMatcher(GMFGEN.getGenCompartment_Title()));
+ preserveIfSet(GMFGEN.getGenCompartment(), GMFGEN.getGenCompartment_ListLayout());
+ preserveIfSet(GMFGEN.getGenCompartment(), GMFGEN.getGenCompartment_CanCollapse());
+ preserveIfSet(GMFGEN.getGenCompartment(), GMFGEN.getGenCompartment_HideIfEmpty());
+ preserveIfSet(GMFGEN.getGenCompartment(), GMFGEN.getGenCompartment_NeedsTitle());
+ //[155332]preserveIfNotByPattern(GMFGEN.getGenCompartment(), GMFGEN.getGenCommonBase_EditPartClassName(), ".*" + GenCommonBase.EDIT_PART_SUFFIX);
+
+ //if parent node is matched, then viemap is matched automatically because it is [1] feature.
+ //there are nothing to reconcile for viewmaps, all their properties are derived
+ //we need this only to dig into viewmap attributes
+ setMatcherForAllSubclasses(GMFGEN.getViewmap(), ALWAYS_MATCH);
+
+ setMatcher(GMFGEN.getDefaultSizeAttributes(), ALWAYS_MATCH);
+ setCopier(GMFGEN.getDefaultSizeAttributes(), Copier.COMPLETE_COPY);
+ preserveIfSet(GMFGEN.getDefaultSizeAttributes(), GMFGEN.getDefaultSizeAttributes_Height());
+ preserveIfSet(GMFGEN.getDefaultSizeAttributes(), GMFGEN.getDefaultSizeAttributes_Width());
+
+ // provided GenCommonBase matched, custom behaviour should be kept as is
+ setMatcher(GMFGEN.getCustomBehaviour(), ALWAYS_MATCH);
+ setCopier(GMFGEN.getCustomBehaviour(), Copier.COMPLETE_COPY);
+
+ setMatcher(GMFGEN.getMetamodelType(), ALWAYS_MATCH);
+ preserveIfSet(GMFGEN.getMetamodelType(), GMFGEN.getElementType_DisplayName());
+ preserveIfSet(GMFGEN.getMetamodelType(), GMFGEN.getElementType_DefinedExternally());
+
+ setMatcher(GMFGEN.getSpecializationType(), ALWAYS_MATCH);
+ preserveIfSet(GMFGEN.getSpecializationType(), GMFGEN.getElementType_DisplayName());
+ preserveIfSet(GMFGEN.getSpecializationType(), GMFGEN.getElementType_DefinedExternally());
+
+ setMatcher(GMFGEN.getGenPropertySheet(), ALWAYS_MATCH);
+ preserveIfSet(GMFGEN.getGenPropertySheet(), GMFGEN.getGenPropertySheet_ReadOnly());
+ preserveIfSet(GMFGEN.getGenPropertySheet(), GMFGEN.getGenPropertySheet_NeedsCaption());
+ preserveIfSet(GMFGEN.getGenPropertySheet(), GMFGEN.getGenPropertySheet_PackageName());
+ preserveIfSet(GMFGEN.getGenPropertySheet(), GMFGEN.getGenPropertySheet_LabelProviderClassName());
+
+ setMatcherForAllSubclasses(GMFGEN.getGenPropertyTab(), new ReflectiveMatcher(GMFGEN.getGenPropertyTab_ID()));
+ preserveIfSet(GMFGEN.getGenStandardPropertyTab(),GMFGEN.getGenPropertyTab_Label());
+
+ preserveIfSet(GMFGEN.getGenCustomPropertyTab(),GMFGEN.getGenPropertyTab_Label());
+ preserveIfSet(GMFGEN.getGenCustomPropertyTab(),GMFGEN.getGenCustomPropertyTab_ClassName());
+ }
+
+ private Matcher getGenNodeMatcher(){
+ //FIXME: use new AttributeMatcher("domainMetaClass")
+ return new ReflectiveMatcher(GMFGenPackage.eINSTANCE.getGenNode(), new ReflectiveMatcher.Reflector(){
+ public Object reflect(EObject target) {
+ GenNode genNode = (GenNode)target;
+ return genNode.getDomainMetaClass();
+ }
+ });
+ }
+
+ private void preserveIfSet(EClass eClass, EAttribute feature){
+ //FIXME: only attributes for now, allow references
+ addDecisionMaker(eClass, new DefaultDecisionMaker(feature));
+ }
+
+ private void preserveIfNotByPattern(EClass eClass, EAttribute feature, String pattern){
+ addDecisionMaker(eClass, new StringPatternDecisionMaker(pattern, feature));
+ }
+
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelOperation.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelOperation.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelOperation.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelOperation.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,354 @@
+/**
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Alexander Fedorov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
+import org.eclipse.emf.common.util.BasicDiagnostic;
+import org.eclipse.emf.common.util.Diagnostic;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.xmi.XMLResource;
+import org.eclipse.gmf.codegen.gmfgen.GenEditorGenerator;
+import org.eclipse.gmf.gmfgraph.util.FigureQualifiedNameSwitch;
+import org.eclipse.gmf.gmfgraph.util.RuntimeFQNSwitch;
+import org.eclipse.gmf.gmfgraph.util.RuntimeLiteFQNSwitch;
+import org.eclipse.gmf.graphdef.codegen.MapModeCodeGenStrategy;
+import org.eclipse.gmf.internal.bridge.VisualIdentifierDispenser;
+import org.eclipse.gmf.internal.bridge.genmodel.BasicDiagramRunTimeModelHelper;
+import org.eclipse.gmf.internal.bridge.genmodel.DiagramGenModelTransformer;
+import org.eclipse.gmf.internal.bridge.genmodel.DiagramRunTimeModelHelper;
+import org.eclipse.gmf.internal.bridge.genmodel.GenModelProducer;
+import org.eclipse.gmf.internal.bridge.genmodel.InnerClassViewmapProducer;
+import org.eclipse.gmf.internal.bridge.genmodel.ViewmapProducer;
+import org.eclipse.gmf.internal.bridge.naming.gen.GenModelNamingMediatorImpl;
+import org.eclipse.gmf.internal.bridge.ui.Plugin;
+import org.eclipse.gmf.internal.common.migrate.ModelLoadHelper;
+import org.eclipse.gmf.internal.common.reconcile.Reconciler;
+import org.eclipse.gmf.mappings.Mapping;
+
+
+public class TransformToGenModelOperation {
+
+ private URI myMapModelURI;
+ private URI myGMFGenModelURI;
+ private TransformOptions myOptions;
+ private Mapping myMapping;
+ private GenModelDetector myGMDetector;
+ private GenModel myGenModel;
+
+ private IStatus myLoadMapmodelStatus;
+ private IStatus myStaleGenmodelStatus;
+
+ public TransformToGenModelOperation(URI mapURI) {
+ assert mapURI != null;
+ this.myMapModelURI = mapURI;
+ this.myOptions = new TransformOptions();
+ }
+
+ public TransformOptions getOptions() {
+ return myOptions;
+ }
+
+ public URI getMapURI() {
+ return this.myMapModelURI;
+ }
+
+ public URI getGenURI() {
+ return this.myGMFGenModelURI;
+ }
+
+ void setGenURI(URI gmfGen) {
+ this.myGMFGenModelURI = gmfGen;
+ }
+
+ private GenModel getGenModel() {
+ return this.myGenModel;
+ }
+
+ Mapping getMapping() {
+ return this.myMapping;
+ }
+
+ private void setMapping(Mapping m) {
+ this.myMapping = m;
+ myGMDetector = null;
+ myGenModel = null;
+ }
+
+ GenModelDetector getGenModelDetector() {
+ if (myGMDetector == null) {
+ myGMDetector = new GenModelDetector(getMapping());
+ }
+ return myGMDetector;
+ }
+
+ IStatus getLoadMappingStatus() {
+ return this.myLoadMapmodelStatus;
+ }
+
+ IStatus getStaleGenmodelStatus() {
+ return this.myStaleGenmodelStatus;
+ }
+
+ Mapping loadMappingModel(ResourceSet rs, URI uri, IProgressMonitor pm) throws CoreException {
+ IProgressMonitor monitor = null;
+ try {
+ monitor = (pm != null) ? new SubProgressMonitor(pm, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK) : new NullProgressMonitor();
+ String cancelMessage = Messages.TransformToGenModelOperation_e_map_load_cancelled;
+ monitor.beginTask("", 100); //$NON-NLS-1$
+ subTask(monitor, 0, Messages.TransformToGenModelOperation_task_load, cancelMessage);
+ ModelLoadHelper loadHelper = new ModelLoadHelper(rs, uri);
+ IStatus loadStatus = loadHelper.getStatus();
+ if (!loadStatus.isOK()) {
+ throw new CoreException(loadStatus);
+ }
+ subTask(monitor, 20, Messages.TransformToGenModelOperation_task_validate, cancelMessage);
+ final Mapping content = (Mapping) loadHelper.getContentsRoot();
+ Diagnostic mapIsValid = ValidationHelper.validate(content, true, monitor);
+ monitor.worked(60);
+ IStatus mapStatus = getFirst(mapIsValid);
+ if (Diagnostic.CANCEL == mapStatus.getSeverity()) {
+ throw new CoreException(Plugin.createCancel(cancelMessage));
+ } else if(Diagnostic.ERROR == mapStatus.getSeverity()) {
+ throw new CoreException(mapStatus);
+ } else {
+ setMapping(content);
+ this.myLoadMapmodelStatus = mapStatus;
+ return content;
+ }
+ } finally {
+ monitor.done();
+ }
+ }
+
+ GenModel findGenmodel(ResourceSet rs) throws CoreException {
+ try {
+ GenModelDetector gmd = getGenModelDetector();
+ IStatus detect = gmd.detect();
+ if (detect.isOK()) {
+ GenModel genModel = gmd.get(rs);
+ this.myGenModel = genModel;
+ return genModel;
+ }
+ throw new CoreException(detect);
+
+ } catch (CoreException e) {
+ throw e;
+ } catch (Exception e) {
+ IStatus error = Plugin.createError(Messages.TransformToGenModelOperation_e_mapping_invalid, e);
+ throw new CoreException(error);
+ }
+ }
+
+ GenModel loadGenModel(ResourceSet rs, URI uri, IProgressMonitor pm) throws CoreException {
+ IProgressMonitor monitor = null;
+ try {
+ monitor = (pm != null) ? new SubProgressMonitor(pm, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK) : new NullProgressMonitor();
+ String cancelMessage = Messages.TransformToGenModelOperation_e_genmodel_load_cancelled;
+ monitor.beginTask("", 100); //$NON-NLS-1$
+ monitor.subTask(Messages.TransformToGenModelOperation_task_detect);
+ GenModelDetector gmd = getGenModelDetector();
+ IStatus status = Status.OK_STATUS;
+ if (uri == null) {
+ status = gmd.detect();
+ } else {
+ status = gmd.advise(uri);
+ }
+ if (!status.isOK()) {
+ throw new CoreException(status);
+ }
+ subTask(monitor, 30, Messages.TransformToGenModelOperation_task_load, cancelMessage);
+ GenModel genModel = gmd.get(rs);
+ subTask(monitor, 40, Messages.TransformToGenModelOperation_task_validate, cancelMessage);
+ StaleGenModelDetector staleDetector = new StaleGenModelDetector(genModel);
+ IStatus stale = staleDetector.detect();
+ this.myGenModel = genModel;
+ this.myStaleGenmodelStatus = stale;
+ return genModel;
+
+ } catch (CoreException e) {
+ throw e;
+ } catch (Exception e) {
+ IStatus error = Plugin.createError(Messages.TransformToGenModelOperation_e_genmodel_load, e);
+ throw new CoreException(error);
+ } finally {
+ if (monitor != null) {
+ monitor.done();
+ }
+ }
+ }
+
+ IStatus executeTransformation(ResourceSet rs, IProgressMonitor pm) {
+ IProgressMonitor monitor = null;
+ try {
+ monitor = (pm != null) ? new SubProgressMonitor(pm, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK) : new NullProgressMonitor();
+ monitor.beginTask("", 100); //$NON-NLS-1$
+ if (monitor.isCanceled()) {
+ return Status.CANCEL_STATUS;
+ }
+ final DiagramRunTimeModelHelper drtModelHelper = detectRunTimeModel();
+ final ViewmapProducer viewmapProducer = detectTransformationOptions();
+ final VisualIdentifierDispenserProvider idDispenser = getVisualIdDispenser();
+ idDispenser.acquire();
+
+ GenModelProducer t = createGenModelProducer(getGenModel(), drtModelHelper, viewmapProducer, idDispenser.get());
+
+ monitor.subTask(Messages.TransformToGenModelOperation_task_generate);
+ GenEditorGenerator genEditor = t.process(getMapping(), new SubProgressMonitor(monitor, 20));
+ if (monitor.isCanceled()) {
+ return Status.CANCEL_STATUS;
+ }
+ monitor.subTask(Messages.TransformToGenModelOperation_task_reconcile);
+ if (Plugin.needsReconcile()) {
+ reconcile(rs, genEditor);
+ }
+ monitor.worked(20);
+ if (monitor.isCanceled()) {
+ return Status.CANCEL_STATUS;
+ }
+ monitor.subTask(Messages.TransformToGenModelOperation_task_save);
+ save(rs, genEditor);
+ monitor.worked(20);
+ if (monitor.isCanceled()) {
+ return Status.CANCEL_STATUS;
+ }
+ monitor.subTask(Messages.TransformToGenModelOperation_task_validate);
+ return validate(genEditor, monitor);
+
+ } catch (Exception ex) {
+ return Plugin.createError(ex.getMessage(), ex);
+ } finally {
+ if (monitor != null) {
+ monitor.done();
+ }
+ }
+ }
+
+ public void reset(URI uri) {
+ assert uri != null;
+ myMapModelURI = uri;
+ setMapping(null);
+ myLoadMapmodelStatus = null;
+ myStaleGenmodelStatus = null;
+ }
+
+ private IStatus getFirst(Diagnostic d) {
+ if (d == null) {
+ return Status.OK_STATUS;
+ }
+ List children = d.getChildren();
+ if (children.isEmpty()) {
+ return BasicDiagnostic.toIStatus(d);
+ } else {
+ return BasicDiagnostic.toIStatus(children.get(0));
+ }
+ }
+
+ private DiagramRunTimeModelHelper detectRunTimeModel() {
+ return new BasicDiagramRunTimeModelHelper();
+ }
+
+ private ViewmapProducer detectTransformationOptions() {
+ FigureQualifiedNameSwitch fSwitch = getOptions().getUseRuntimeFigures() ? new RuntimeFQNSwitch() : new RuntimeLiteFQNSwitch();
+ MapModeCodeGenStrategy mmStrategy = getOptions().getUseMapMode() ? MapModeCodeGenStrategy.DYNAMIC : MapModeCodeGenStrategy.STATIC;
+ return new InnerClassViewmapProducer(fSwitch, mmStrategy);
+ }
+
+ private VisualIdentifierDispenserProvider getVisualIdDispenser() {
+ return new VisualIdentifierDispenserProvider(getGenURI());
+ }
+
+ private GenModelProducer createGenModelProducer(GenModel domainGenModel, final DiagramRunTimeModelHelper drtModelHelper, final ViewmapProducer viewmapProducer, final VisualIdentifierDispenser idDespenser) {
+ final DiagramGenModelTransformer t = new DiagramGenModelTransformer(drtModelHelper, new GenModelNamingMediatorImpl(), viewmapProducer, idDespenser, getOptions().getGenerateRCP());
+ if (domainGenModel != null) {
+ t.setEMFGenModel(domainGenModel);
+ }
+ return new GenModelProducer() {
+
+ public GenEditorGenerator process(Mapping mapping, IProgressMonitor progress) {
+ progress.beginTask(null, 1);
+ try {
+ t.transform(mapping);
+ return t.getResult();
+ } finally {
+ progress.done();
+ }
+ }
+ };
+ }
+
+ private void reconcile(ResourceSet rs, GenEditorGenerator genBurdern) {
+ GenEditorGenerator old = null;
+ Resource resource = null;
+ try {
+ resource = rs.getResource(getGenURI(), true);
+ List contents = resource.getContents();
+ if (!contents.isEmpty() && contents.get(0) instanceof GenEditorGenerator) {
+ old = (GenEditorGenerator) contents.get(0);
+ }
+ if (old != null) {
+ new Reconciler(new GMFGenConfig()).reconcileTree(genBurdern, old);
+ }
+ } catch (RuntimeException e) {
+ old = null;
+ } finally {
+ if (resource != null) {
+ resource.unload();
+ }
+ }
+ }
+
+ private void save(ResourceSet rs, GenEditorGenerator genBurdern) throws IOException {
+ Resource dgmmRes = rs.createResource(getGenURI());
+ dgmmRes.getContents().add(genBurdern);
+ dgmmRes.save(getSaveOptions());
+ }
+
+ private Map getSaveOptions() {
+ HashMap saveOptions = new HashMap();
+ saveOptions.put(XMLResource.OPTION_ENCODING, "UTF-8"); //$NON-NLS-1$
+ return saveOptions;
+ }
+
+ private IStatus validate(GenEditorGenerator genBurdern, IProgressMonitor monitor) {
+ Diagnostic d = ValidationHelper.validate(genBurdern, true, monitor);
+ return getFirst(d);
+ }
+
+ private static void subTask(IProgressMonitor monitor, int ticks, String name, String cancelMessage) throws CoreException{
+ if (monitor == null) {
+ return;
+ }
+ if (monitor.isCanceled()) {
+ IStatus cancel = Plugin.createCancel(cancelMessage);
+ throw new CoreException(cancel);
+ }
+ if (ticks > 0) {
+ monitor.worked(ticks);
+ }
+ if (name != null) {
+ monitor.subTask(name);
+ }
+ }
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelWizard.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelWizard.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelWizard.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelWizard.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,164 @@
+/**
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Alexander Fedorov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.gmf.internal.bridge.wizards.WizardUtil;
+import org.eclipse.gmf.internal.common.ui.ResourceLocationProvider;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchWizard;
+
+
+public class TransformToGenModelWizard extends Wizard implements IWorkbenchWizard {
+
+ private static final String PAGE_ID_GMFGEN = "gmfgen"; //$NON-NLS-1$
+ private static final String PAGE_ID_GENMODEL = "genmodel"; //$NON-NLS-1$
+ private static final String PAGE_ID_GMFMAP = "gmfmap"; //$NON-NLS-1$
+ private static final String PAGE_ID_TRANSFORM = "transform"; //$NON-NLS-1$
+
+ private IStructuredSelection mySelection;
+
+ private GMFGenNewFileCreationPage newFileCreationPage;
+ private MapModelConfigurationPage mapModelPage;
+ private GenModelConfigurationPage genModelPage;
+ private ViewmapProducerWizardPage transformOptionPage;
+
+ private TransformToGenModelOperation myOperation;
+
+ ResourceSet resourceSet;
+
+ @Override
+ public void addPages() {
+ super.addPages();
+
+ final String defaultName = "My"; //$NON-NLS-1$
+ newFileCreationPage = new GMFGenNewFileCreationPage(PAGE_ID_GMFGEN, mySelection);
+ newFileCreationPage.setTitle(Messages.TransformToGenModelWizard_title_gmfgen);
+ newFileCreationPage.setDescription(Messages.TransformToGenModelWizard_descr_gmfgen);
+ newFileCreationPage.setFileName(WizardUtil.getDefaultFileName(mySelection, defaultName, GMFGenNewFileCreationPage.EXT_GMFGEN));
+ addPage(newFileCreationPage);
+
+ resourceSet = new ResourceSetImpl();
+ ResourceLocationProvider rlp = new ResourceLocationProvider(mySelection);
+ mapModelPage = new MapModelConfigurationPage(PAGE_ID_GMFMAP, rlp, resourceSet);
+ mapModelPage.setTitle(Messages.TransformToGenModelWizard_title_mapmodel);
+ mapModelPage.setDescription(Messages.TransformToGenModelWizard_descr_mapmodel);
+ mapModelPage.setPageComplete(false);
+ mapModelPage.setModelRequired(true);
+ addPage(mapModelPage);
+
+ genModelPage = new GenModelConfigurationPage(PAGE_ID_GENMODEL, rlp, resourceSet);
+ genModelPage.setTitle(Messages.TransformToGenModelWizard_title_genmodel);
+ genModelPage.setDescription(Messages.TransformToGenModelWizard_descr_genmodel);
+ genModelPage.setPageComplete(false);
+ genModelPage.setModelRequired(true);
+ addPage(genModelPage);
+
+ transformOptionPage = new ViewmapProducerWizardPage(PAGE_ID_TRANSFORM);
+ transformOptionPage.setTitle(Messages.TransformToGenModelWizard_title_options);
+ transformOptionPage.setDescription(Messages.TransformToGenModelWizard_descr_options);
+ transformOptionPage.setPageComplete(false);
+ addPage(transformOptionPage);
+
+ }
+
+ public void init(IWorkbench workbench, IStructuredSelection selection) {
+ this.mySelection = selection;
+ setWindowTitle(Messages.TransformToGenModelWizard_title_wizard);
+ setNeedsProgressMonitor(true);
+ IFile mapFile = (IFile) selection.getFirstElement();
+ URI mapURI = URI.createPlatformResourceURI(mapFile.getFullPath().toString(), true);
+ initOperation(mapURI);
+ }
+
+ void initOperation(URI mapURI) {
+ myOperation = new TransformToGenModelOperation(mapURI);
+ }
+
+ @Override
+ public boolean performFinish() {
+ try {
+ final IStatus[] s = new IStatus[1];
+ IRunnableWithProgress iwr = new IRunnableWithProgress() {
+ public void run(IProgressMonitor monitor) {
+ s[0] = getTransformOperation().executeTransformation(resourceSet, monitor);
+ }
+ };
+ getContainer().run(false, false, iwr);
+ if (s[0].isOK()) {
+ setErrorMessage(null);
+ saveTransformOptions();
+ return true;
+ }
+ setErrorMessage(s[0].getMessage());
+ return false;
+ } catch (InvocationTargetException ex) {
+ Throwable targetException = ex.getTargetException();
+ if (targetException != null && targetException.getMessage() != null) {
+ String message = targetException.getMessage();
+ if (message == null) {
+ message = Messages.TransformToGenModelWizard_e_generator_creation;
+ }
+ }
+ return false;
+ } catch (InterruptedException ex){
+ setErrorMessage(Messages.TransformToGenModelWizard_e_operation_cancelled);
+ return false;
+ }
+ }
+
+ private void saveTransformOptions() {
+ if (getTransformOperation() != null) {
+ getTransformOperation().getOptions().flush();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.wizard.Wizard#performCancel()
+ */
+ @Override
+ public boolean performCancel() {
+ if (getTransformOperation() != null) {
+ getTransformOperation().getOptions().reset();
+ }
+ return super.performCancel();
+ }
+
+ TransformToGenModelOperation getTransformOperation() {
+ return myOperation;
+ }
+
+ IFile getTargetFile() {
+ return newFileCreationPage.getModelFile();
+ }
+
+ private void setErrorMessage(String message) {
+ WizardDialog wd = (WizardDialog) getContainer();
+ WizardPage wp = (WizardPage) wd.getCurrentPage();
+ if (wp != null) {
+ wp.setErrorMessage(message);
+ }
+ }
+
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/TransformOptions.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/TransformOptions.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/TransformOptions.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/TransformOptions.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,99 @@
+/**
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Alexander Fedorov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.Preferences;
+import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
+import org.eclipse.core.runtime.preferences.DefaultScope;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.gmf.internal.bridge.ui.Plugin;
+
+
+public class TransformOptions extends AbstractPreferenceInitializer {
+
+ public static final String PREF_GENERATE_RCP = "generate_rcp"; //$NON-NLS-1$
+ public static final String PREF_USE_MAP_MODE = "use_map_mode"; //$NON-NLS-1$
+ public static final String PREF_USE_RUNTIME_FIGURES = "use_runtime_figures"; //$NON-NLS-1$
+
+ private Preferences myPreferences;
+
+ public TransformOptions() {
+ myPreferences = new Preferences();
+ reset();
+ }
+
+ private Preferences getPreferences() {
+ return myPreferences;
+ }
+
+ public void reset() {
+ Preferences pluginPrefs = Plugin.getDefault().getPluginPreferences();
+ copyPreferences(pluginPrefs, myPreferences);
+ }
+
+ public void flush() {
+ Preferences pluginPrefs = Plugin.getDefault().getPluginPreferences();
+ copyPreferences(myPreferences, pluginPrefs);
+ Plugin.getDefault().savePluginPreferences();
+ }
+
+ private void copyPreferences(Preferences source, Preferences target) {
+ String[] propNames = source.propertyNames();
+ for (int i = 0; i < propNames.length; i++) {
+ String name = propNames[i];
+ String value = source.getString(name);
+ target.setValue(name, value);
+ }
+ }
+
+ public boolean getGenerateRCP() {
+ return getPreferences().getBoolean(PREF_GENERATE_RCP);
+ }
+
+ public boolean getUseMapMode() {
+ return getPreferences().getBoolean(PREF_USE_MAP_MODE);
+ }
+
+ public boolean getUseRuntimeFigures() {
+ return getPreferences().getBoolean(PREF_USE_RUNTIME_FIGURES);
+ }
+
+ public void setGenerateRCP(boolean value) {
+ getPreferences().setValue(PREF_GENERATE_RCP, value);
+ }
+
+ public void setUseMapMode(boolean value) {
+ getPreferences().setValue(PREF_USE_MAP_MODE, value);
+ }
+
+ public void setUseRuntimeFigures(boolean value) {
+ getPreferences().setValue(PREF_USE_RUNTIME_FIGURES, value);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
+ */
+ @Override
+ public void initializeDefaultPreferences() {
+ DefaultScope scope = new DefaultScope();
+ IEclipsePreferences node = scope.getNode(Plugin.getPluginID());
+ node.putBoolean(PREF_GENERATE_RCP, false);
+ node.putBoolean(PREF_USE_MAP_MODE, true);
+ node.putBoolean(PREF_USE_RUNTIME_FIGURES, true);
+
+ }
+
+ static boolean checkLiteOptionPresent() {
+ return Platform.getBundle("org.eclipse.gmf.codegen.lite") != null; //$NON-NLS-1$
+ }
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelAction.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelAction.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelAction.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/TransformToGenModelAction.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2005,2006 Borland Software Corporation
+ *
+ * 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:
+ * Artem Tikhomirov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IObjectActionDelegate;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * .gmfmap to .gmfgen
+ *
+ * @author artem
+ */
+public class TransformToGenModelAction implements IObjectActionDelegate {
+
+ private static final int WIZARD_WIDTH = 500;
+ private static final int WIZARD_HEIGHT = 600;
+
+ private IWorkbenchPart myPart;
+
+ private IStructuredSelection sselection;
+
+ public void setActivePart(IAction action, IWorkbenchPart targetPart) {
+ myPart = targetPart;
+ }
+
+ public void selectionChanged(IAction action, ISelection selection) {
+ final IStructuredSelection structuredSelection = ((IStructuredSelection) selection);
+ sselection = structuredSelection;
+ }
+
+ public void run(IAction action) {
+ final TransformToGenModelWizard wiz = new TransformToGenModelWizard();
+ wiz.setWindowTitle(action.getText());
+ wiz.init(PlatformUI.getWorkbench(), sselection);
+ WizardDialog wd = new WizardDialog(getShell(), wiz);
+ wd.create();
+ wd.getShell().setSize(WIZARD_WIDTH, WIZARD_HEIGHT);
+ wd.open();
+ }
+
+ private Shell getShell() {
+ return myPart.getSite().getShell();
+ }
+}
Index: src/org/eclipse/gmf/internal/bridge/transform/ModelConfigurationPage.java
===================================================================
RCS file: src/org/eclipse/gmf/internal/bridge/transform/ModelConfigurationPage.java
diff -N src/org/eclipse/gmf/internal/bridge/transform/ModelConfigurationPage.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ src/org/eclipse/gmf/internal/bridge/transform/ModelConfigurationPage.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,114 @@
+/**
+ * Copyright (c) 2006 Borland Software Corporation
+ *
+ * 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:
+ * Alexander Fedorov (Borland) - initial API and implementation
+ */
+package org.eclipse.gmf.internal.bridge.transform;
+
+import java.lang.reflect.InvocationTargetException;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.gmf.internal.bridge.ui.Plugin;
+import org.eclipse.gmf.internal.common.ui.ModelSelectionPage;
+import org.eclipse.gmf.internal.common.ui.ResourceLocationProvider;
+import org.eclipse.jface.dialogs.IMessageProvider;
+import org.eclipse.ui.actions.WorkspaceModifyOperation;
+
+
+public class ModelConfigurationPage extends ModelSelectionPage {
+
+ public ModelConfigurationPage(String pageId, ResourceLocationProvider rlp, ResourceSet resourceSet) {
+ super(pageId, rlp, resourceSet);
+ }
+
+ @Override
+ protected Resource loadResource() {
+ Resource current = getResource();
+ if (current != null) {
+ current.getResourceSet().getResources().remove(current);
+ }
+
+ final Resource[] result = new Resource[1];
+ WorkspaceModifyOperation initializeOperation = new WorkspaceModifyOperation()
+ {
+
+ protected void execute(IProgressMonitor progressMonitor) throws CoreException {
+ IProgressMonitor monitor = null;
+ try {
+ monitor = (progressMonitor != null) ? new SubProgressMonitor(progressMonitor, 1, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK) : new NullProgressMonitor();
+ setStatusMessage(Status.OK_STATUS);
+ result[0] = doLoadResource(monitor);
+ } catch (CoreException exception) {
+ throw exception;
+ } catch (Exception exception) {
+ throw new CoreException(Plugin.createError(exception.getMessage(), exception));
+ } finally {
+ if (monitor != null) {
+ monitor.done();
+ }
+ }
+ }
+ };
+
+ try {
+ getContainer().run(false, false, initializeOperation);
+ setStatusMessage(Status.OK_STATUS);
+ } catch (InvocationTargetException e) {
+ Throwable target = e.getTargetException();
+ if (target instanceof CoreException) {
+ CoreException ce = (CoreException) target;
+ setStatusMessage(ce.getStatus());
+ }
+ setStatusMessage(Plugin.createError(target.getMessage(), target));
+ } catch (InterruptedException e) {
+ setStatusMessage(Status.CANCEL_STATUS);
+ }
+ return result[0];
+ }
+
+ protected Resource doLoadResource(IProgressMonitor monitor) throws CoreException {
+ try {
+ Resource r = super.loadResource();
+ return r;
+ } catch (Exception e) {
+ throw new CoreException(Plugin.createError(e.getMessage(), e));
+ } finally {
+ if (monitor != null) {
+ monitor.done();
+ }
+ }
+ }
+
+ void setStatusMessage(IStatus status) {
+ if (status == null || status.isOK()) {
+ setMessage(null);
+ setErrorMessage(null);
+ } else if (IStatus.INFO == status.getSeverity()) {
+ setMessage(status.getMessage(), IMessageProvider.INFORMATION);
+ setErrorMessage(null);
+ } else if (IStatus.WARNING == status.getSeverity()) {
+ setMessage(status.getMessage(), IMessageProvider.WARNING);
+ setErrorMessage(null);
+ } else if (IStatus.ERROR == status.getSeverity()) {
+ setMessage(null);
+ setErrorMessage(status.getMessage());
+ } else if (IStatus.CANCEL == status.getSeverity()) {
+ setMessage(null);
+ setErrorMessage(status.getMessage());
+ }
+ }
+
+}