diff options
author | Benoit Maggi | 2014-11-20 14:06:44 +0000 |
---|---|---|
committer | Benoit Maggi | 2014-11-21 17:00:28 +0000 |
commit | 7003abdf9e35f6bacaa49885a9f422ae1d29a3a8 (patch) | |
tree | 373c484a3550954230c22f431d9c6a84244166d4 /extraplugins/papyrus4ecore | |
parent | 9d6adc6f91518c2ece23db160de480a63c076940 (diff) | |
download | org.eclipse.papyrus-7003abdf9e35f6bacaa49885a9f422ae1d29a3a8.tar.gz org.eclipse.papyrus-7003abdf9e35f6bacaa49885a9f422ae1d29a3a8.tar.xz org.eclipse.papyrus-7003abdf9e35f6bacaa49885a9f422ae1d29a3a8.zip |
[Papyrus4Ecore] Add a convert to uml facility
- add the convert uml to ecore menu in model explorer and
File>Export
- add an export image for all exports in Papyrus
Change-Id: I1b6f62dd3bc56809d54dd2b2cbf020934aaef2d4
Signed-off-by: Benoit Maggi <benoit.maggi@cea.fr>
Diffstat (limited to 'extraplugins/papyrus4ecore')
10 files changed, 1939 insertions, 1 deletions
diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/META-INF/MANIFEST.MF b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/META-INF/MANIFEST.MF index 3e2c9668f88..9b639dcd1cf 100644 --- a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/META-INF/MANIFEST.MF +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/META-INF/MANIFEST.MF @@ -9,6 +9,13 @@ Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, org.eclipse.papyrus.infra.viewpoints.policy;bundle-version="1.1.0", org.eclipse.papyrus.infra.newchild;bundle-version="1.1.0", - org.eclipse.papyrus.views.properties;bundle-version="1.1.0" + org.eclipse.papyrus.views.properties;bundle-version="1.1.0", + org.eclipse.uml2.uml, + org.eclipse.papyrus.infra.core, + org.eclipse.emf.ecore.editor, + org.eclipse.papyrus.infra.onefile, + com.google.guava, + org.eclipse.uml2.examples.uml.ui;bundle-version="5.0.0", + org.eclipse.papyrus.infra.emf Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/plugin.xml b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/plugin.xml index 114c024e922..31391bd879f 100644 --- a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/plugin.xml +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/plugin.xml @@ -21,4 +21,44 @@ isCustomizable="true"> </context> </extension> + <extension + point="org.eclipse.ui.exportWizards"> + <wizard + category="org.eclipse.papyrus.infra.core.exportcategory" + id="org.eclipse.papyrus.uml.diagram.ecore.converttoecorewizard" + class="org.eclipse.papyrus.uml.diagram.ecore.wizard.ConvertToEcoreWizard" + name="Convert To Ecore"> + <selection + class="org.eclipse.core.resources.IFile" + name="*.di"> + </selection> + <selection + class="org.eclipse.papyrus.infra.onefile.model.IPapyrusFile"> + </selection> + </wizard> + </extension> + <extension + point="org.eclipse.ui.menus"> + <menuContribution + allPopups="true" + locationURI="popup:org.eclipse.papyrus.uml.export.menu"> + <command + commandId="org.eclipse.papyrus.uml.diagram.ecore.convertToEcore" + id="org.eclipse.papyrus.uml.diagram.ecore.convertToEcore" + label="Convert To Ecore..." + style="push"> + </command> + </menuContribution> + </extension> + <extension + id="convert" + name="Convert Uml To Ecore" + point="org.eclipse.ui.commands"> + <command + id="org.eclipse.papyrus.uml.diagram.ecore.convertToEcore" + defaultHandler="org.eclipse.papyrus.uml.diagram.ecore.handler.ConvertToEcoreHandler" + name="Convert To Ecore"> + </command> + </extension> + </plugin> diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/dialog/PapyrusUML2EcoreConverterOptionsDialog.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/dialog/PapyrusUML2EcoreConverterOptionsDialog.java new file mode 100644 index 00000000000..38099b8b950 --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/dialog/PapyrusUML2EcoreConverterOptionsDialog.java @@ -0,0 +1,45 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.dialog; + +import java.util.Map; + +import org.eclipse.jface.dialogs.IDialogConstants; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.papyrus.uml.diagram.ecore.helper.UmlToEcoreOptionHelper; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Shell; + +/** + * This class is used instead of org.eclipse.uml2.examples.uml.ui.dialogs.UML2EcoreConverterOptionsDialog + * so that control code can be refactor in a helper and shared between dialog and wizardPage + * + * @author Benoit Maggi + * + */ +public class PapyrusUML2EcoreConverterOptionsDialog extends MessageDialog { + + protected Map<String, String> options; + + public PapyrusUML2EcoreConverterOptionsDialog(Shell parent, String title, String message, Map<String, String> options) { + super(parent, title, null, message, QUESTION, new String[]{ IDialogConstants.OK_LABEL, IDialogConstants.CANCEL_LABEL }, 0); + this.options = options; + } + + @Override + protected Control createCustomArea(Composite parent) { + UmlToEcoreOptionHelper umlToEcoreOptionHelper = new UmlToEcoreOptionHelper(options); + return umlToEcoreOptionHelper.createControl(parent); + } +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/handler/ConvertToEcoreHandler.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/handler/ConvertToEcoreHandler.java new file mode 100644 index 00000000000..80784565b58 --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/handler/ConvertToEcoreHandler.java @@ -0,0 +1,128 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.handler; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.window.Window; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; +import org.eclipse.papyrus.uml.diagram.ecore.dialog.PapyrusUML2EcoreConverterOptionsDialog; +import org.eclipse.papyrus.uml.diagram.ecore.util.ConvertToEcoreUtil; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.handlers.HandlerUtil; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Package; + +/** + * handler for the transformation from uml to ecore + * @author Benoit Maggi + */ +public class ConvertToEcoreHandler extends AbstractHandler { + + private List<?> selection = Collections.emptyList(); + + protected List<?> getSelection() { + return selection; + } + + /** + * Set the selection + * @param event + */ + protected void setSelection(ExecutionEvent event) { + ISelection selection = HandlerUtil.getActiveMenuSelection(event); + this.selection = (selection instanceof IStructuredSelection) ? ((IStructuredSelection)selection).toList() : Collections.emptyList(); + } + + /* (non-Javadoc) + * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent) + */ + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + // Get selection from handler event + setSelection(event); + Package package_ = getSelectedPackage(); + if (package_ != null){ + final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); + final Map<String, String> options = new HashMap<String, String>(); + final String label = "Convert Uml to Ecore"; + final String message = "Convert Uml to Ecore"; + MessageDialog optionsDialog = new PapyrusUML2EcoreConverterOptionsDialog(shell, label,message , options); + if(optionsDialog.open() == Window.OK) { + ConvertToEcoreUtil.convertToEcore(package_, options); + } + } + return null; + } + + + + /** + * Return the selected package for the uml to ecore transformation + * @return + */ + protected Package getSelectedPackage() { + List<EObject> selectedElements = getSelectedElements(); + if (selectedElements != null && selectedElements.size() > 0){ + EObject eObject = selectedElements.get(0); + if(eObject instanceof Element) { + return ((Element)eObject).getNearestPackage(); + } + } + return null; + } + + /** + * <pre> + * Parse current selection and extract the list of {@link EObject} from + * this selection. + * + * This also tries to adapt selected element into {@link EObject} + * (for example to get the {@link EObject} from a selection in the ModelExplorer). + * + * @return a list of currently selected {@link EObject} + * </pre> + * + */ + protected List<EObject> getSelectedElements() { + List<EObject> selectedEObjects = new ArrayList<EObject>(); + // Get current selection + Collection<?> selection = getSelection(); + // Treat non-null selected object (try to adapt and return EObject) + if(!selection.isEmpty()) { + // Parse current selection + for(Object current : selection) { + // Adapt current selection to EObject + EObject selectedEObject = EMFHelper.getEObject(current); + if(selectedEObject != null) { + // we avoid to add null element in the list! + selectedEObjects.add(selectedEObject); + } + } + } + return selectedEObjects; + } +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/helper/UmlToEcoreOptionHelper.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/helper/UmlToEcoreOptionHelper.java new file mode 100644 index 00000000000..772b7fc6759 --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/helper/UmlToEcoreOptionHelper.java @@ -0,0 +1,148 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.helper; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CCombo; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.uml2.examples.uml.ui.UMLExamplesUIPlugin; +import org.eclipse.uml2.uml.editor.UMLEditorPlugin; +import org.eclipse.uml2.uml.util.UMLUtil; + +/** + * This helper refactor the control part of org.eclipse.uml2.examples.uml.ui.dialogs.UML2EcoreConverterOptionsDialog + * so that it can be shared between dialog and wizardPage + * + * @author Benoit Maggi + */ +public class UmlToEcoreOptionHelper { + + protected final Map<String, String> options; + + protected final Map<String, String> choiceLabels = new HashMap<String, String>(); + + protected final String discardChoiceLabel; + + protected final String ignoreChoiceLabel; + + protected final String processChoiceLabel; + + protected final String reportChoiceLabel; + + public UmlToEcoreOptionHelper(Map<String, String> options) { + this.options=options; + choiceLabels.put(discardChoiceLabel = UMLEditorPlugin.INSTANCE.getString("_UI_Discard_label"), UMLUtil.OPTION__DISCARD); //$NON-NLS-1$ + choiceLabels.put(ignoreChoiceLabel = UMLEditorPlugin.INSTANCE.getString("_UI_Ignore_label"), UMLUtil.OPTION__IGNORE); //$NON-NLS-1$ + choiceLabels.put(processChoiceLabel = UMLEditorPlugin.INSTANCE.getString("_UI_Process_label"), UMLUtil.OPTION__PROCESS); //$NON-NLS-1$ + choiceLabels.put(reportChoiceLabel = UMLEditorPlugin.INSTANCE.getString("_UI_Report_label"), UMLUtil.OPTION__REPORT); //$NON-NLS-1$ + } + + /** + * Create contents of the wizard. + * + * @param parent + */ + public Composite createControl(Composite parent) { + Composite container = new Composite(parent, SWT.NONE); + { + GridLayout layout = new GridLayout(); + layout.numColumns = 2; + container.setLayout(layout); + GridData data = new GridData(); + data.verticalAlignment = GridData.FILL; + data.grabExcessVerticalSpace = true; + data.horizontalAlignment = GridData.FILL; + data.grabExcessHorizontalSpace = true; + container.setLayoutData(data); + } + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_EcoreTaggedValues_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__ECORE_TAGGED_VALUES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_DerivedFeatures_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__DERIVED_FEATURES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_DuplicateFeatureInheritance_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__DUPLICATE_FEATURE_INHERITANCE, new String[]{ ignoreChoiceLabel, reportChoiceLabel, discardChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_DuplicateFeatures_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__DUPLICATE_FEATURES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, discardChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_DuplicateOperationInheritance_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__DUPLICATE_OPERATION_INHERITANCE, new String[]{ ignoreChoiceLabel, reportChoiceLabel, discardChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_DuplicateOperations_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__DUPLICATE_OPERATIONS, new String[]{ ignoreChoiceLabel, reportChoiceLabel, discardChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_RedefiningOperations_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__REDEFINING_OPERATIONS, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_RedefiningProperties_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__REDEFINING_PROPERTIES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_SubsettingProperties_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__SUBSETTING_PROPERTIES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_UnionProperties_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__UNION_PROPERTIES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_SuperClassOrder_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__SUPER_CLASS_ORDER, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_AnnotationDetails_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__ANNOTATION_DETAILS, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_InvariantConstraints_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__INVARIANT_CONSTRAINTS, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_ValidationDelegates_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__VALIDATION_DELEGATES, new String[]{ ignoreChoiceLabel, processChoiceLabel }, ignoreChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_NonAPIInvariants_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__NON_API_INVARIANTS, new String[]{ ignoreChoiceLabel, processChoiceLabel }, ignoreChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_OperationBodies_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__OPERATION_BODIES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_InvocationDelegates_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__INVOCATION_DELEGATES, new String[]{ ignoreChoiceLabel, processChoiceLabel }, ignoreChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_PropertyDefaultExpressions_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__PROPERTY_DEFAULT_EXPRESSIONS, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_Comments_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__COMMENTS, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_CamelCaseNames_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__CAMEL_CASE_NAMES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, processChoiceLabel }, processChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_UntypedProperties_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__UNTYPED_PROPERTIES, new String[]{ ignoreChoiceLabel, reportChoiceLabel, discardChoiceLabel, processChoiceLabel }, reportChoiceLabel); + createOptionArea(container, UMLExamplesUIPlugin.INSTANCE.getString("_UI_OppositeRoleNames_label"), //$NON-NLS-1$ + UMLUtil.UML2EcoreConverter.OPTION__OPPOSITE_ROLE_NAMES, new String[]{ ignoreChoiceLabel, processChoiceLabel }, ignoreChoiceLabel); + + return container; + } + + protected void createOptionArea(Composite parent, String text, final String option, String[] choices, String initialChoice) { + Label label = new Label(parent, SWT.LEFT); + { + label.setText(text); + GridData data = new GridData(); + data.horizontalAlignment = GridData.FILL; + label.setLayoutData(data); + } + final CCombo combo = new CCombo(parent, SWT.BORDER | SWT.READ_ONLY); + { + GridData data = new GridData(); + data.horizontalAlignment = GridData.FILL; + data.grabExcessHorizontalSpace = true; + combo.setLayoutData(data); + combo.setItems(choices); + combo.addModifyListener(new ModifyListener() { + + public void modifyText(ModifyEvent me) { + options.put(option, choiceLabels.get(combo.getText())); + } + }); + combo.setText(initialChoice); + } + } +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/util/ConvertToEcoreUtil.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/util/ConvertToEcoreUtil.java new file mode 100644 index 00000000000..29f34577523 --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/util/ConvertToEcoreUtil.java @@ -0,0 +1,114 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.emf.common.util.BasicDiagnostic; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EPackage; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.plugin.EcorePlugin; +import org.eclipse.emf.ecore.presentation.EcoreEditor; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.utils.DiResourceSet; +import org.eclipse.papyrus.infra.onefile.providers.PapyrusLabelProvider; +import org.eclipse.uml2.common.util.UML2Util; +import org.eclipse.uml2.examples.uml.ui.UMLExamplesUIPlugin; +import org.eclipse.uml2.uml.Package; +import org.eclipse.uml2.uml.util.UMLUtil; +import org.eclipse.uml2.uml.util.UMLValidator; + +/** + * + * Utility class to convert uml to ecore + * @author Benoit Maggi + * + */ +public class ConvertToEcoreUtil { + + /** + * A utily class should'nt be instancied + */ + private ConvertToEcoreUtil() {} + + /** + * Convert the uml package and its contents to ecore + * @param package_ + * @param options + */ + public static boolean convertToEcore(Package package_, Map<String, String> options) { + final BasicDiagnostic diagnostics = new BasicDiagnostic(UMLValidator.DIAGNOSTIC_SOURCE, 0, EcorePlugin.INSTANCE.getString("_UI_DiagnosticRoot_diagnostic", //$NON-NLS-1$ + new Object[]{ package_.getName() }), new Object[]{ package_ }); + Map<Object, Object> context = new HashMap<Object, Object>(); + context.put(UML2Util.QualifiedTextProvider.class, qualifiedTextProvider); + return convertToEcore(package_, context, diagnostics, options); + } + + /** + * Convert the uml package and its contents to ecore + * @param package_ + * @param context + * @param diagnostics + * @param options + */ + public static boolean convertToEcore(Package package_, Map<Object, Object> context, BasicDiagnostic diagnostics, Map<String, String> options) { + Resource resource = package_.eResource(); + ResourceSet resourceSet = resource.getResourceSet(); + URI uri = resourceSet.getURIConverter().normalize(resource.getURI()).trimFileExtension().trimSegments(1); + List<Resource> resources = new ArrayList<Resource>(); + ModelSet modelSet = new DiResourceSet(); + for(EPackage ePackage : UMLUtil.convertToEcore(package_, options, diagnostics, context)) { + URI appendSegment = uri.appendSegment(ePackage.getName()); + URI appendFileExtension = appendSegment.appendFileExtension(EcoreEditor.ECORE_FILE_EXTENSION); + resource = modelSet.createResource(appendFileExtension); + resources.add(resource); + resource.getContents().add(ePackage); + } + + for(Resource r : resources) { + try { + r.save(null); + } catch (Exception e) { + UMLExamplesUIPlugin.INSTANCE.log(e); + } + } + return diagnostics.getSeverity() == BasicDiagnostic.OK; + } + + protected final static UMLUtil.QualifiedTextProvider qualifiedTextProvider = new UMLUtil.QualifiedTextProvider() { + + PapyrusLabelProvider papyrusLabelProvider= new PapyrusLabelProvider(); + + @Override + public String getFeatureText(EStructuralFeature eStructuralFeature) { + return getLabelProvider().getText(eStructuralFeature); + } + + public PapyrusLabelProvider getLabelProvider() { + return papyrusLabelProvider; + } + + @Override + public String getClassText(EObject eObject) { + return getLabelProvider().getText(eObject.eClass()); + } + }; + +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/CheckboxTreeAndListGroup.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/CheckboxTreeAndListGroup.java new file mode 100644 index 00000000000..0bdc007e5a8 --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/CheckboxTreeAndListGroup.java @@ -0,0 +1,924 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Obeo - Adaptations. + *******************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.wizard; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.eclipse.core.runtime.SafeRunner; +import org.eclipse.jface.util.ListenerList; +import org.eclipse.jface.util.SafeRunnable; +import org.eclipse.jface.viewers.CheckStateChangedEvent; +import org.eclipse.jface.viewers.CheckboxTableViewer; +import org.eclipse.jface.viewers.CheckboxTreeViewer; +import org.eclipse.jface.viewers.ICheckStateListener; +import org.eclipse.jface.viewers.ILabelProvider; +import org.eclipse.jface.viewers.ISelectionChangedListener; +import org.eclipse.jface.viewers.IStructuredContentProvider; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.ITreeViewerListener; +import org.eclipse.jface.viewers.SelectionChangedEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.TreeExpansionEvent; +import org.eclipse.jface.viewers.ViewerSorter; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.BusyIndicator; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.Tree; + +/** + * Workbench-level composite that combines a CheckboxTreeViewer and + * CheckboxListViewer. All viewer selection-driven interactions are handled + * within this object + */ +// This is copied from org.eclipse.ui.internal.ide.misc, so we'd rather keep it as close as the original than trying to fix the warnings. +@SuppressWarnings({ "rawtypes", "deprecation", "unchecked" }) +public class CheckboxTreeAndListGroup implements ICheckStateListener, ISelectionChangedListener, ITreeViewerListener { + private Object root; + + private Object currentTreeSelection; + + private List expandedTreeNodes = new ArrayList(); + + private Map checkedStateStore = new HashMap(9); + + private List whiteCheckedTreeItems = new ArrayList(); + + private ListenerList listeners = new ListenerList(); + + private ITreeContentProvider treeContentProvider; + + private IStructuredContentProvider listContentProvider; + + private ILabelProvider treeLabelProvider; + + private ILabelProvider listLabelProvider; + + // widgets + private CheckboxTreeViewer treeViewer; + + private CheckboxTableViewer listViewer; + + /** + * Create an instance of this class. Use this constructor if you wish to + * specify the width and/or height of the combined widget (to only hardcode + * one of the sizing dimensions, specify the other dimension's value as -1) + * + * @param parent + * the parent. + * @param rootObject + * the root. + * @param treeContentProvider + * the tree content provider. + * @param treeLabelProvider + * the label provider. + * @param listContentProvider + * the list content provider. + * @param listLabelProvider + * the list label provider. + * @param style + * the style. + * @param width + * the width. + * @param height + * the height. + */ + // CHECKSTYLE:OFF + public CheckboxTreeAndListGroup(final Composite parent, final Object rootObject, final ITreeContentProvider treeContentProvider, final ILabelProvider treeLabelProvider, + final IStructuredContentProvider listContentProvider, final ILabelProvider listLabelProvider, final int style, final int width, final int height) { + + root = rootObject; + this.treeContentProvider = treeContentProvider; + this.listContentProvider = listContentProvider; + this.treeLabelProvider = treeLabelProvider; + this.listLabelProvider = listLabelProvider; + createContents(parent, width, height, style); + } + + // CHECKSTYLE:ON + + /** + * Returns the tree viewer. + * + * @return the tree viewer + * @author www.obeo.fr + */ + public CheckboxTreeViewer getTreeViewer() { + return treeViewer; + } + + /** + * Returns the list viewer. + * + * @return the list viewer + * @author www.obeo.fr + */ + public CheckboxTableViewer getListViewer() { + return listViewer; + } + + /** + * This method must be called just before this window becomes visible. + */ + public void aboutToOpen() { + determineWhiteCheckedDescendents(root); + checkNewTreeElements(treeContentProvider.getElements(root)); + currentTreeSelection = null; + + // select the first element in the list + final Object[] elements = treeContentProvider.getElements(root); + final Object primary = elements.length > 0 ? elements[0] : null; + if (primary != null) { + treeViewer.setSelection(new StructuredSelection(primary)); + } + treeViewer.getControl().setFocus(); + } + + /** + * Add the passed listener to self's collection of clients that listen for + * changes to element checked states. + * + * @param listener + * ICheckStateListener + */ + public void addCheckStateListener(final ICheckStateListener listener) { + listeners.add(listener); + } + + /** + * Add the receiver and all of it's ancestors to the checkedStateStore if + * they are not already there. + */ + private void addToHierarchyToCheckedStore(final Object treeElement) { + + // if this tree element is already gray then its ancestors all are as + // well + if (!checkedStateStore.containsKey(treeElement)) { + checkedStateStore.put(treeElement, new ArrayList()); + } + + final Object parent = treeContentProvider.getParent(treeElement); + if (parent != null) { + addToHierarchyToCheckedStore(parent); + } + } + + /** + * Return a boolean indicating whether all children of the passed tree + * element are currently white-checked. + * + * @return boolean + * @param treeElement + * java.lang.Object + */ + protected boolean areAllChildrenWhiteChecked(final Object treeElement) { + final Object[] children = treeContentProvider.getChildren(treeElement); + for (int i = 0; i < children.length; ++i) { + if (!whiteCheckedTreeItems.contains(children[i])) { + return false; + } + } + + return true; + } + + /** + * Return a boolean indicating whether all list elements associated with the + * passed tree element are currently checked. + * + * @return boolean + * @param treeElement + * java.lang.Object + */ + protected boolean areAllElementsChecked(final Object treeElement) { + final List checkedElements = (List) checkedStateStore.get(treeElement); + if (checkedElements == null) { + return false; + } + + return getListItemsSize(treeElement) == checkedElements.size(); + } + + /** + * Iterate through the passed elements which are being realized for the + * first time and check each one in the tree viewer as appropriate. + * + * @param elements + * the elements + */ + protected void checkNewTreeElements(final Object[] elements) { + for (int i = 0; i < elements.length; ++i) { + final Object currentElement = elements[i]; + if (currentElement != null) { + final boolean checked = checkedStateStore.containsKey(currentElement); + treeViewer.setChecked(currentElement, checked); + treeViewer.setGrayed(currentElement, checked && !whiteCheckedTreeItems.contains(currentElement)); + } + } + } + + /** + * An item was checked in one of self's two views. Determine which view this + * occurred in and delegate appropriately + * + * @param event + * CheckStateChangedEvent + */ + public void checkStateChanged(final CheckStateChangedEvent event) { + + // Potentially long operation - show a busy cursor + BusyIndicator.showWhile(treeViewer.getControl().getDisplay(), new Runnable() { + public void run() { + if (event.getCheckable().equals(treeViewer)) { + treeItemChecked(event.getElement(), event.getChecked()); + } else { + listItemChecked(event.getElement(), event.getChecked(), true); + } + + notifyCheckStateChangeListeners(event); + } + }); + } + + /** + * Lay out and initialize self's visual components. + * + * @param parent + * org.eclipse.swt.widgets.Composite + * @param width + * int + * @param height + * int + * @param style + * the style. + */ + protected void createContents(final Composite parent, final int width, final int height, final int style) { + // group pane + final Composite composite = new Composite(parent, style); + final GridLayout layout = new GridLayout(); + layout.numColumns = 2; + layout.makeColumnsEqualWidth = true; + layout.marginHeight = 0; + layout.marginWidth = 0; + composite.setLayout(layout); + composite.setLayoutData(new GridData(GridData.FILL_BOTH)); + composite.setFont(parent.getFont()); + + createTreeViewer(composite, width / 2, height); + createListViewer(composite, width / 2, height); + + initialize(); + } + + /** + * Create this group's list viewer. + * + * @param parent + * the parent + * @param width + * int + * @param height + * int + */ + protected void createListViewer(final Composite parent, final int width, final int height) { + listViewer = CheckboxTableViewer.newCheckList(parent, SWT.BORDER); + final GridData data = new GridData(GridData.FILL_BOTH); + data.widthHint = width; + data.heightHint = height; + listViewer.getTable().setLayoutData(data); + listViewer.getTable().setFont(parent.getFont()); + listViewer.setContentProvider(listContentProvider); + listViewer.setLabelProvider(listLabelProvider); + listViewer.addCheckStateListener(this); + } + + /** + * Create this group's tree viewer. + * + * @param parent + * the parent + * @param width + * int + * @param height + * int + */ + protected void createTreeViewer(final Composite parent, final int width, final int height) { + final Tree tree = new Tree(parent, SWT.CHECK | SWT.BORDER); + final GridData data = new GridData(GridData.FILL_BOTH); + data.widthHint = width; + data.heightHint = height; + tree.setLayoutData(data); + tree.setFont(parent.getFont()); + + treeViewer = new CheckboxTreeViewer(tree); + treeViewer.setContentProvider(treeContentProvider); + treeViewer.setLabelProvider(treeLabelProvider); + treeViewer.addTreeListener(this); + treeViewer.addCheckStateListener(this); + treeViewer.addSelectionChangedListener(this); + } + + /** + * Returns a boolean indicating whether the passed tree element should be at + * LEAST gray-checked. Note that this method does not consider whether it + * should be white-checked, so a specified tree item which should be + * white-checked will result in a <code>true</code> answer from this method. + * To determine whether a tree item should be white-checked use method + * #determineShouldBeWhiteChecked(Object). + * + * @param treeElement + * java.lang.Object + * @return boolean + * @see #determineShouldBeWhiteChecked(java.lang.Object) + */ + protected boolean determineShouldBeAtLeastGrayChecked(final Object treeElement) { + // if any list items associated with treeElement are checked then it + // retains its gray-checked status regardless of its children + boolean result = false; + final List checked = (List) checkedStateStore.get(treeElement); + if (checked != null && (!checked.isEmpty())) { + result = true; + } + + // if any children of treeElement are still gray-checked then + // treeElement + // must remain gray-checked as well + final Object[] children = treeContentProvider.getChildren(treeElement); + for (int i = 0; i < children.length; ++i) { + if (checkedStateStore.containsKey(children[i])) { + result = true; + } + } + + return result; + } + + /** + * Returns a boolean indicating whether the passed tree item should be + * white-checked. + * + * @return boolean + * @param treeElement + * java.lang.Object + */ + protected boolean determineShouldBeWhiteChecked(final Object treeElement) { + return areAllChildrenWhiteChecked(treeElement) && areAllElementsChecked(treeElement); + } + + /** + * Recursively add appropriate tree elements to the collection of known + * white-checked tree elements. + * + * @param treeElement + * java.lang.Object + */ + protected void determineWhiteCheckedDescendents(final Object treeElement) { + // always go through all children first since their white-checked + // statuses will be needed to determine the white-checked status for + // this tree element + final Object[] children = treeContentProvider.getElements(treeElement); + for (int i = 0; i < children.length; ++i) { + determineWhiteCheckedDescendents(children[i]); + } + + // now determine the white-checked status for this tree element + if (determineShouldBeWhiteChecked(treeElement)) { + setWhiteChecked(treeElement, true); + } + } + + /** + * Cause the tree viewer to expand all its items. + */ + public void expandAll() { + treeViewer.expandAll(); + } + + /** + * Answer a flat collection of all of the checked elements in the list + * portion of self. + * + * @return java.util.Vector + */ + public Iterator getAllCheckedListItems() { + final List result = new ArrayList(); + final Iterator listCollectionsEnum = checkedStateStore.values().iterator(); + + while (listCollectionsEnum.hasNext()) { + final Iterator currentCollection = ((List) listCollectionsEnum.next()).iterator(); + while (currentCollection.hasNext()) { + result.add(currentCollection.next()); + } + } + + return result.iterator(); + } + + /** + * Answer a collection of all of the checked elements in the tree portion of + * self. + * + * @return java.util.Vector + */ + public Set getAllCheckedTreeItems() { + return checkedStateStore.keySet(); + } + + /** + * Answer the number of elements that have been checked by the user. + * + * @return int + */ + public int getCheckedElementCount() { + return checkedStateStore.size(); + } + + /** + * Return a count of the number of list items associated with a given tree + * item. + * + * @return int + * @param treeElement + * java.lang.Object + */ + protected int getListItemsSize(final Object treeElement) { + final Object[] elements = listContentProvider.getElements(treeElement); + return elements.length; + } + + /** + * Get the table the list viewer uses. + * + * @return org.eclipse.swt.widgets.Table + */ + public Table getListTable() { + return this.listViewer.getTable(); + } + + /** + * Logically gray-check all ancestors of treeItem by ensuring that they + * appear in the checked table. + * + * @param treeElement + * the tree element. + */ + protected void grayCheckHierarchy(final Object treeElement) { + + // if this tree element is already gray then its ancestors all are as + // well + if (checkedStateStore.containsKey(treeElement)) { + return; // no need to proceed upwards from here + } + + checkedStateStore.put(treeElement, new ArrayList()); + if (determineShouldBeWhiteChecked(treeElement)) { + setWhiteChecked(treeElement, true); + } + final Object parent = treeContentProvider.getParent(treeElement); + if (parent != null) { + grayCheckHierarchy(parent); + } + } + + /** + * Set the initial checked state of the passed list element to true. + * + * @param element + * the element. + */ + public void initialCheckListItem(final Object element) { + final Object parent = treeContentProvider.getParent(element); + currentTreeSelection = parent; + // As this is not done from the UI then set the box for updating from + // the selection to false + listItemChecked(element, true, false); + updateHierarchy(parent); + } + + /** + * Set the initial checked state of the passed element to true, as well as + * to all of its children and associated list elements. + * + * @param element + * the element. + */ + public void initialCheckTreeItem(final Object element) { + treeItemChecked(element, true); + } + + /** + * Initialize this group's viewers after they have been laid out. + */ + protected void initialize() { + treeViewer.setInput(root); + } + + /** + * Callback that's invoked when the checked status of an item in the list is + * changed by the user. Do not try and update the hierarchy if we are + * building the initial list. + * + * @param listElement + * the elements. + * @param state + * the state + * @param updatingFromSelection + * updating. + */ + protected void listItemChecked(final Object listElement, final boolean state, final boolean updatingFromSelection) { + List checkedListItems = (List) checkedStateStore.get(currentTreeSelection); + + if (state) { + if (checkedListItems == null) { + // since the associated tree item has gone from 0 -> 1 checked + // list items, tree checking may need to be updated + grayCheckHierarchy(currentTreeSelection); + checkedListItems = (List) checkedStateStore.get(currentTreeSelection); + } + checkedListItems.add(listElement); + } else { + checkedListItems.remove(listElement); + if (checkedListItems.isEmpty()) { + // since the associated tree item has gone from 1 -> 0 checked + // list items, tree checking may need to be updated + ungrayCheckHierarchy(currentTreeSelection); + } + } + + if (updatingFromSelection) { + updateHierarchy(currentTreeSelection); + } + } + + /** + * Notify all checked state listeners that the passed element has had its + * checked state changed to the passed state. + * + * @param event + * the event. + */ + protected void notifyCheckStateChangeListeners(final CheckStateChangedEvent event) { + final Object[] array = listeners.getListeners(); + for (Object element : array) { + final ICheckStateListener l = (ICheckStateListener) element; + SafeRunner.run(new SafeRunnable() { + public void run() { + l.checkStateChanged(event); + } + }); + } + } + + /** + * Set the contents of the list viewer based upon the specified selected + * tree element. This also includes checking the appropriate list items. + * + * @param treeElement + * java.lang.Object + */ + protected void populateListViewer(final Object treeElement) { + listViewer.setInput(treeElement); + final List listItemsToCheck = (List) checkedStateStore.get(treeElement); + + if (listItemsToCheck != null) { + final Iterator listItemsEnum = listItemsToCheck.iterator(); + while (listItemsEnum.hasNext()) { + listViewer.setChecked(listItemsEnum.next(), true); + } + } + } + + /** + * Remove the passed listener from self's collection of clients that listen + * for changes to element checked states. + * + * @param listener + * ICheckStateListener + */ + public void removeCheckStateListener(final ICheckStateListener listener) { + listeners.remove(listener); + } + + /** + * Handle the selection of an item in the tree viewer. + * + * @param event + * SelectionChangedEvent + */ + public void selectionChanged(final SelectionChangedEvent event) { + final IStructuredSelection selection = (IStructuredSelection) event.getSelection(); + final Object selectedElement = selection.getFirstElement(); + if (selectedElement == null) { + currentTreeSelection = null; + listViewer.setInput(currentTreeSelection); + return; + } + + // ie.- if not an item deselection + populateListViewer(selectedElement); + currentTreeSelection = selectedElement; + } + + /** + * Select or deselect all of the elements in the tree depending on the value + * of the selection boolean. Be sure to update the displayed files as well. + * + * @param selection + * the selection. + */ + public void setAllSelections(final boolean selection) { + + // Potentially long operation - show a busy cursor + BusyIndicator.showWhile(treeViewer.getControl().getDisplay(), new Runnable() { + public void run() { + setTreeChecked(root, selection); + listViewer.setAllChecked(selection); + } + }); + } + + /** + * Set the list viewer's providers to those passed. + * + * @param contentProvider + * ITreeContentProvider + * @param labelProvider + * ILabelProvider + */ + public void setListProviders(final IStructuredContentProvider contentProvider, final ILabelProvider labelProvider) { + listViewer.setContentProvider(contentProvider); + listViewer.setLabelProvider(labelProvider); + } + + /** + * Set the sorter that is to be applied to self's list viewer. + * + * @param sorter + * the sorter. + */ + public void setListSorter(final ViewerSorter sorter) { + listViewer.setSorter(sorter); + } + + /** + * Set the root of the widget to be new Root. Regenerate all of the tables + * and lists from this value. + * + * @param newRoot + * the new root. + */ + public void setRoot(final Object newRoot) { + this.root = newRoot; + initialize(); + } + + /** + * Set the checked state of the passed tree element appropriately, and do so + * recursively to all of its child tree elements as well. + * + * @param treeElement + * the tree element. + * @param state + * the state. + */ + protected void setTreeChecked(final Object treeElement, final boolean state) { + if (treeElement != null) { + if (treeElement.equals(currentTreeSelection)) { + listViewer.setAllChecked(state); + } + + if (state) { + final Object[] listItems = listContentProvider.getElements(treeElement); + final List listItemsChecked = new ArrayList(); + for (int i = 0; i < listItems.length; ++i) { + listItemsChecked.add(listItems[i]); + } + + checkedStateStore.put(treeElement, listItemsChecked); + } else { + checkedStateStore.remove(treeElement); + } + + setWhiteChecked(treeElement, state); + treeViewer.setChecked(treeElement, state); + treeViewer.setGrayed(treeElement, false); + + // now logically check/uncheck all children as well + final Object[] children = treeContentProvider.getChildren(treeElement); + for (int i = 0; i < children.length; ++i) { + setTreeChecked(children[i], state); + } + } + } + + /** + * Set the tree viewer's providers to those passed. + * + * @param contentProvider + * ITreeContentProvider + * @param labelProvider + * ILabelProvider + */ + public void setTreeProviders(final ITreeContentProvider contentProvider, final ILabelProvider labelProvider) { + treeViewer.setContentProvider(contentProvider); + treeViewer.setLabelProvider(labelProvider); + } + + /** + * Set the sorter that is to be applied to self's tree viewer. + * + * @param sorter + * the sorter. + */ + public void setTreeSorter(final ViewerSorter sorter) { + treeViewer.setSorter(sorter); + } + + /** + * Adjust the collection of references to white-checked tree elements + * appropriately. + * + * @param treeElement + * java.lang.Object + * @param isWhiteChecked + * boolean + */ + protected void setWhiteChecked(final Object treeElement, final boolean isWhiteChecked) { + if (isWhiteChecked) { + if (!whiteCheckedTreeItems.contains(treeElement)) { + whiteCheckedTreeItems.add(treeElement); + } + } else { + whiteCheckedTreeItems.remove(treeElement); + } + } + + /** + * Handle the collapsing of an element in a tree viewer. + * + * @param event + * the event. + */ + public void treeCollapsed(final TreeExpansionEvent event) { + // We don't need to do anything with this + } + + /** + * Handle the expansionsion of an element in a tree viewer. + * + * @param event + * the event. + */ + public void treeExpanded(final TreeExpansionEvent event) { + + final Object item = event.getElement(); + + // First see if the children need to be given their checked state at + // all. If they've + // already been realized then this won't be necessary + if (!expandedTreeNodes.contains(item)) { + expandedTreeNodes.add(item); + checkNewTreeElements(treeContentProvider.getChildren(item)); + } + } + + /** + * Callback that's invoked when the checked status of an item in the tree is + * changed by the user. + * + * @param treeElement + * the tree element. + * @param state + * the state. + */ + protected void treeItemChecked(final Object treeElement, final boolean state) { + + // recursively adjust all child tree elements appropriately + setTreeChecked(treeElement, state); + + final Object parent = treeContentProvider.getParent(treeElement); + if (parent == null) { + return; + } + + // now update upwards in the tree hierarchy + if (state) { + grayCheckHierarchy(parent); + } else { + ungrayCheckHierarchy(parent); + } + + updateHierarchy(treeElement); + } + + /** + * Logically un-gray-check all ancestors of treeItem iff appropriate. + * + * @param treeElement + * the tree element. + */ + protected void ungrayCheckHierarchy(final Object treeElement) { + if (!determineShouldBeAtLeastGrayChecked(treeElement)) { + checkedStateStore.remove(treeElement); + } + + final Object parent = treeContentProvider.getParent(treeElement); + if (parent != null) { + ungrayCheckHierarchy(parent); + } + } + + /** + * Set the checked state of self and all ancestors appropriately. + * + * @param treeElement + * the tree element. + */ + protected void updateHierarchy(final Object treeElement) { + + final boolean whiteChecked = determineShouldBeWhiteChecked(treeElement); + final boolean shouldBeAtLeastGray = determineShouldBeAtLeastGrayChecked(treeElement); + + treeViewer.setChecked(treeElement, shouldBeAtLeastGray); + setWhiteChecked(treeElement, whiteChecked); + if (!whiteChecked) { + treeViewer.setGrayed(treeElement, shouldBeAtLeastGray); + } + + // proceed up the tree element hierarchy + final Object parent = treeContentProvider.getParent(treeElement); + if (parent != null) { + updateHierarchy(parent); + } + } + + /** + * Update the selections of the tree elements in items to reflect the new + * selections provided. + * + * @param items + * Map with keys of Object (the tree element) and values of List + * (the selected list elements). + */ + public void updateSelections(final Map items) { + + // Potentially long operation - show a busy cursor + BusyIndicator.showWhile(treeViewer.getControl().getDisplay(), new Runnable() { + public void run() { + Iterator keyIterator = items.keySet().iterator(); + + // Update the store before the hierarchy to prevent + // updating + // parents before all of the children are done + while (keyIterator.hasNext()) { + final Object key = keyIterator.next(); + // Replace the items in the checked state store with + // those + // from the supplied items + final List selections = (List) items.get(key); + if (selections.size() == 0) { + // If it is empty remove it from the list + checkedStateStore.remove(key); + } else { + checkedStateStore.put(key, selections); + // proceed up the tree element hierarchy + final Object parent = treeContentProvider.getParent(key); + if (parent != null) { + addToHierarchyToCheckedStore(parent); + } + } + } + + // Now update hierarchies + keyIterator = items.keySet().iterator(); + + while (keyIterator.hasNext()) { + final Object key = keyIterator.next(); + updateHierarchy(key); + if (currentTreeSelection != null && currentTreeSelection.equals(key)) { + listViewer.setAllChecked(false); + listViewer.setCheckedElements(((List) items.get(key)).toArray()); + } + } + } + }); + + } +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/ConvertToEcoreWizard.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/ConvertToEcoreWizard.java new file mode 100644 index 00000000000..8a46d923922 --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/ConvertToEcoreWizard.java @@ -0,0 +1,93 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.wizard; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.core.utils.DiResourceSet; +import org.eclipse.papyrus.uml.diagram.ecore.util.ConvertToEcoreUtil; +import org.eclipse.ui.IExportWizard; +import org.eclipse.ui.IWorkbench; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Package; + +/** + * Convert uml to Ecore Wizard + */ +public class ConvertToEcoreWizard extends Wizard implements IExportWizard { + + protected Map<String, String> options = new HashMap<String, String>(); + + /** wizard page to export all diagram from a Papyrus model. */ + protected SelectFilesWizardPage selectModelPage; + + /** wizard page to select option for the uml to ecore transformation */ + private ConvertToEcoreWizardPage convertToEcoreWizardPage; + + /** Selected iPath. */ + private IPath iPath; + + /** + * Constructor. + * + */ + public ConvertToEcoreWizard() { + super(); + setWindowTitle("Convert Papyrus Model to Ecore"); + } + + /** + * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection) + * + * @param workbench + * @param selection + */ + @Override + public void init(IWorkbench workbench, IStructuredSelection selection) { + String[] extensionFilter = { "uml" }; //$NON-NLS-1$ + selectModelPage = new SelectFilesWizardPage("Convert uml to Ecore", 0, 1, extensionFilter, iPath); + addPage(selectModelPage); + convertToEcoreWizardPage = new ConvertToEcoreWizardPage(options); + addPage(convertToEcoreWizardPage); + } + + /** + * @see org.eclipse.jface.wizard.Wizard#performFinish() + * + * @return + */ + @Override + public boolean performFinish() { + IPath iPath = selectModelPage.getIPath(); + ModelSet modelSet = new DiResourceSet(); + URI uri = URI.createPlatformResourceURI(iPath.toString(), Boolean.TRUE); + Resource resource = modelSet.getResource(uri, Boolean.TRUE); + EObject eObject = resource.getContents().get(0); + if(eObject instanceof Element) { + Element element = (Element)eObject; + Package nearestPackage = element.getNearestPackage(); + if(nearestPackage != null) { + ConvertToEcoreUtil.convertToEcore(nearestPackage, options); + } + } + return Boolean.TRUE; + } +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/ConvertToEcoreWizardPage.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/ConvertToEcoreWizardPage.java new file mode 100644 index 00000000000..7396b326f4a --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/ConvertToEcoreWizardPage.java @@ -0,0 +1,51 @@ +/***************************************************************************** + * Copyright (c) 2014 CEA LIST. + * + * + * 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: + * Benoit Maggi benoit.maggi@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.uml.diagram.ecore.wizard; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.papyrus.uml.diagram.ecore.helper.UmlToEcoreOptionHelper; +import org.eclipse.swt.widgets.Composite; + +/** + * wizard page to export all diagram from a Papyrus model + */ +public class ConvertToEcoreWizardPage extends WizardPage { + + protected Map<String, String> options; + + protected final Map<String, String> choiceLabels = new HashMap<String, String>(); + + /** + * Create the wizard. + */ + public ConvertToEcoreWizardPage(Map<String, String> options) { + super("Convert Uml to Ecore"); + setTitle("Convert Uml to Ecore"); + setDescription("Convert Uml to Ecore"); + this.options = options; + } + + /** + * Create contents of the wizard. + * + * @param parent + */ + public void createControl(Composite parent) { + UmlToEcoreOptionHelper umlToEcoreOptionHelper = new UmlToEcoreOptionHelper(options); + Composite container = umlToEcoreOptionHelper.createControl(parent); + setControl(container); + } +} diff --git a/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/SelectFilesWizardPage.java b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/SelectFilesWizardPage.java new file mode 100644 index 00000000000..849ce6603fb --- /dev/null +++ b/extraplugins/papyrus4ecore/org.eclipse.papyrus.uml.diagram.ecore/src/org/eclipse/papyrus/uml/diagram/ecore/wizard/SelectFilesWizardPage.java @@ -0,0 +1,388 @@ +/* + * Copyright (c) 2005-2008 Obeo + * + * 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: + * Obeo - initial API and implementation + */ + +package org.eclipse.papyrus.uml.diagram.ecore.wizard; + +import java.net.URL; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.viewers.AbstractTreeViewer; +import org.eclipse.jface.viewers.CheckStateChangedEvent; +import org.eclipse.jface.viewers.ICheckStateListener; +import org.eclipse.jface.viewers.IOpenListener; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.ITreeContentProvider; +import org.eclipse.jface.viewers.OpenEvent; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ControlEvent; +import org.eclipse.swt.events.ControlListener; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.ui.model.WorkbenchContentProvider; +import org.eclipse.ui.model.WorkbenchLabelProvider; +import org.osgi.framework.Bundle; + +import com.google.common.collect.Lists; + +// Disable checkstyle. This class comes from Acceleo. +// CHECKSTYLE:OFF + +/** + * The role of this wizard page is to make a collection of all of the checked + * resources. + * + * @author www.obeo.fr + */ +public class SelectFilesWizardPage extends WizardPage { + + private static final String JOKER = "*"; + + // Constants + private static final int SELECTION_WIDGET_WIDTH = 400; + + private static final int SELECTION_WIDGET_HEIGHT = 300; + + /** + * The selection group. + */ + private CheckboxTreeAndListGroup checkboxGroup; + + /** + * The min length of the selection. + */ + private int lower; + + /** + * The max length of the selection. + */ + private int upper; + + /** + * The extensions. + */ + private String[] extensions; + + private IResource initialResourceSelection; + + private IPath iPath; + + /** + * Constructor. + * + * @param pageName + * is the name of the page + * @param lower + * is the min length of the selection + * @param upper + * is the max length of the selection + * @param extensions + * is a table of extensions + */ + public SelectFilesWizardPage(final String pageName, final int lower, final int upper, final String[] extensions, IPath iPath) { + super(pageName); + setTitle(pageName); + setDescription("This page is used to select file(s) in the workspace."); //$NON-NLS-1$ + this.lower = lower; + this.upper = upper; + this.extensions = extensions; + this.iPath = iPath; + } + + /** + * @return the iPath + */ + public IPath getIPath() { + return iPath; + } + + /** + * @param uri the iPath to set + */ + public void setUri(IPath iPath) { + this.iPath = iPath; + } + + public void setInitialSelection(final IStructuredSelection selection) { + final Object element = selection.getFirstElement(); + if (element instanceof IResource) { + initialResourceSelection = (IResource) element; + } + } + + /** + * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage() + * + * @return + */ + @Override + public boolean canFlipToNextPage() { + boolean canFlipToNextPage = super.canFlipToNextPage(); + // at least on selection + IPath[] selection = getSelection(); + if (selection != null && selection.length != 0) { + IPath iPath = selection[0]; + this.iPath = iPath; + } else { + iPath = null; + } + return canFlipToNextPage && iPath != null; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite) + */ + public void createControl(final Composite parent) { + final Composite container = new Composite(parent, SWT.NULL); + final GridLayout containerLayout = new GridLayout(); + containerLayout.numColumns = 1; + containerLayout.marginTop = 14; + containerLayout.verticalSpacing = 9; + containerLayout.marginLeft = 7; + containerLayout.marginRight = 7; + container.setLayout(containerLayout); + final WorkbenchLabelProvider treeLabelProvider = new WorkbenchLabelProvider(); + final WorkbenchLabelProvider listLabelProvider = new WorkbenchLabelProvider(); + checkboxGroup = new CheckboxTreeAndListGroup(container, ResourcesPlugin.getWorkspace().getRoot(), getContentProvider(IResource.FOLDER | IResource.PROJECT | IResource.ROOT), treeLabelProvider, + getContentProvider(IResource.FILE), listLabelProvider, SWT.NONE, SELECTION_WIDGET_WIDTH, SELECTION_WIDGET_HEIGHT); + checkboxGroup.addCheckStateListener(new ICheckStateListener() { + public void checkStateChanged(CheckStateChangedEvent event) { + dialogChanged(); + } + }); + checkboxGroup.getTreeViewer().addOpenListener(new IOpenListener() { + public void open(OpenEvent event) { + ISelection selection = event.getSelection(); + if (!selection.isEmpty() && selection instanceof StructuredSelection) { + checkboxGroup.getTreeViewer().expandToLevel(((StructuredSelection) selection).getFirstElement(), AbstractTreeViewer.ALL_LEVELS); + } + } + }); + + if (initialResourceSelection != null) { + checkboxGroup.initialCheckListItem(initialResourceSelection); + } + + container.addControlListener(new ControlListener() { + public void controlMoved(ControlEvent e) { + } + + public void controlResized(ControlEvent e) { + TableColumn[] columns = checkboxGroup.getListTable().getColumns(); + for (TableColumn column : columns) { + column.pack(); + } + } + }); + + dialogChanged(); + setControl(container); + } + + /** + * Returns the content provider. + * + * @param resourceType + * is the type of the resource + * @return the content provider + */ + private ITreeContentProvider getContentProvider(final int resourceType) { + return new WorkbenchContentProvider() { + @Override + public Object[] getChildren(Object o) { + if (resourceType != IResource.FILE && o instanceof IExtensionRegistry) { + Set<Bundle> result = new TreeSet<Bundle>(new Comparator<Bundle>() { + public int compare(Bundle bundle0, Bundle bundle1) { + return bundle0.getSymbolicName().compareTo(bundle1.getSymbolicName()); + } + }); + return result.toArray(); + } else if (resourceType == IResource.FILE && o instanceof Bundle) { + final Bundle bundle = (Bundle) o; + final List<IPath> result = new ArrayList<IPath>(); + for (String extension : extensions) { + if (JOKER.equals(extension)) { + result.clear(); + final Enumeration<?> enumeration = bundle.findEntries("/", JOKER, true); //$NON-NLS-1$ + while (enumeration != null && enumeration.hasMoreElements()) { + final Object child = enumeration.nextElement(); + if (child instanceof URL) { + result.add(new Path(bundle.getSymbolicName()).append(((URL) child).getPath())); + } + } + break; + } else { + final Enumeration<?> enumeration = bundle.findEntries("/", "*." + extension, true); //$NON-NLS-1$ //$NON-NLS-2$ + while (enumeration != null && enumeration.hasMoreElements()) { + final Object child = enumeration.nextElement(); + if (child instanceof URL) { + result.add(new Path(bundle.getSymbolicName()).append(((URL) child).getPath())); + } + } + } + } + return result.toArray(); + } else if (resourceType == IResource.FILE && o instanceof IPath) { + return new Object[0]; + } else if (o instanceof IContainer) { + IResource[] members = null; + try { + members = ((IContainer) o).members(); + } catch (final CoreException e) { + return new Object[0]; + } + final ArrayList<IResource> results = new ArrayList<IResource>(); + for (IResource member : members) { + if ((member.getType() & resourceType) > 0 && isSignificant(member)) { + if (member instanceof IFile) { + String extension = member.getFileExtension(); + if (extension == null) { + extension = ""; //$NON-NLS-1$ + } + for (String extension2 : extensions) { + if (JOKER.equals(extension2) || extension.equals(extension2)) { + results.add(member); + break; + } + } + } else if (member instanceof IContainer && countMembers((IContainer) member) > 0) { + results.add(member); + } + } + } + return results.toArray(); + } else if (o instanceof List) { + List<Object> result = Lists.newArrayList(); + Iterator<?> it = ((List<?>) o).iterator(); + while (it.hasNext()) { + Object element = it.next(); + if (element instanceof IContainer) { + if (((IContainer) element).isAccessible() && countMembers((IContainer) element) > 0) { + result.add(element); + } + } else { + result.add(element); + } + } + return result.toArray(); + } else { + return new Object[0]; + } + } + }; + } + + private int countMembers(final IContainer container) { + try { + final IResource[] members = container.members(); + int result = 0; + for (IResource member : members) { + if (isSignificant(member)) { + if (member instanceof IFile) { + String extension = member.getFileExtension(); + if (extension == null) { + extension = ""; //$NON-NLS-1$ + } + for (String extension2 : extensions) { + if (JOKER.equals(extension2) || extension.equals(extension2)) { + result++; + break; + } + } + } else if (member instanceof IContainer) { + result += countMembers((IContainer) member); + } + } + } + return result; + } catch (final CoreException e) { + return 0; + } + } + + private boolean isSignificant(final IResource resource) { + return !(resource instanceof IFolder && (resource.getName().startsWith("."))); //$NON-NLS-1$ + } + + /** + * Validates the changes on the page. + */ + private void dialogChanged() { + final List<IPath> items = getAllCheckedListItems(); + if (items.size() < lower) { + updateStatus("Not enough files were selected."); //$NON-NLS-1$ + return; + } else if (items.size() > upper && upper != -1) { + updateStatus("Too many files were selected."); //$NON-NLS-1$ + return; + } + updateStatus(null); + } + + /** + * Updates the status of the page. + * + * @param message + * is the error message. + */ + private void updateStatus(String message) { + setMessage(message); + setPageComplete(message == null); + } + + /** + * Returns the selected files. + * + * @return the selected files + */ + public IPath[] getSelection() { + List<IPath> items = getAllCheckedListItems(); + return items.toArray(new IPath[items.size()]); + } + + private List<IPath> getAllCheckedListItems() { + List<IPath> items = new ArrayList<IPath>(); + Iterator<?> resultEnum = checkboxGroup.getAllCheckedListItems(); + while (resultEnum.hasNext()) { + Object member = resultEnum.next(); + if (member instanceof IFile) { + items.add(((IFile) member).getFullPath()); + } else if (member instanceof IPath) { + items.add((IPath) member); + } + } + return items; + } + +} + +// CHECKSTYLE:ON |