diff options
Diffstat (limited to 'plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/UMLXtextReferenceValueEditor.java')
-rw-r--r-- | plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/UMLXtextReferenceValueEditor.java | 346 |
1 files changed, 346 insertions, 0 deletions
diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/UMLXtextReferenceValueEditor.java b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/UMLXtextReferenceValueEditor.java new file mode 100644 index 00000000000..a990905d246 --- /dev/null +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties.xtext/UMLXtextReferenceValueEditor.java @@ -0,0 +1,346 @@ +/***************************************************************************** + * Copyright (c) 2015 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: + * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.textual.editors.example.widget; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.emf.transaction.util.TransactionUtil; +import org.eclipse.gmf.runtime.common.core.command.ICommand; +import org.eclipse.gmf.runtime.common.ui.services.parser.IParser; +import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper; +import org.eclipse.papyrus.extensionpoints.editors.Activator; +import org.eclipse.papyrus.extensionpoints.editors.configuration.IDirectEditorConfiguration; +import org.eclipse.papyrus.extensionpoints.editors.utils.DirectEditorsUtil; +import org.eclipse.papyrus.extensionpoints.editors.utils.IDirectEditorsIds; +import org.eclipse.papyrus.infra.emf.dialog.NestedEditingDialogContext; +import org.eclipse.papyrus.infra.widgets.editors.StyledTextReferenceDialog; +import org.eclipse.papyrus.infra.widgets.editors.StyledTextStringEditor; +import org.eclipse.papyrus.uml.xtext.integration.DefaultXtextDirectEditorConfiguration; +import org.eclipse.papyrus.uml.xtext.integration.StyledTextXtextAdapter; +import org.eclipse.papyrus.uml.xtext.integration.core.ContextElementAdapter; +import org.eclipse.papyrus.uml.xtext.integration.core.ContextElementAdapter.IContextElementProvider; +import org.eclipse.papyrus.uml.xtext.integration.core.ContextElementAdapter.IContextElementProviderWithInit; +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.StyledText; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.events.FocusListener; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; + +/** + * This class provides a ReferenceValueEditor, with a text field with the xtext + * completion and syntax instead of the CLabel. + */ +public class UMLXtextReferenceValueEditor extends StyledTextReferenceDialog + implements IContextElementProvider, SelectionListener { + + /** + * The xtext adapter. + */ + private StyledTextXtextAdapter xtextAdapter; + + /** + * The xtext direct editor configuration used. + */ + private DefaultXtextDirectEditorConfiguration configuration; + + /** + * The context element adapter. + */ + final private ContextElementAdapter contextElementAdapter = new ContextElementAdapter( + this); + + /** + * Constructor. + * + * @param parent + * The composite in which the widget will be displayed. + * @param style + * The style for the widget. + */ + public UMLXtextReferenceValueEditor(final Composite parent, final int style) { + super(parent, style); + styledTextStringEditor.getText().addFocusListener(new FocusListener() { + + public void focusLost(FocusEvent e) { + IParser parser = getParser(); + if (null == xtextAdapter) { + return; + } + + if (null != xtextAdapter + && null != xtextAdapter.getCompletionProposalAdapter() + && xtextAdapter.getCompletionProposalAdapter() + .delayedIsPopupOpen()) { + // ignore focus lost + return; + } + manageParserCommand(parser); + } + + public void focusGained(FocusEvent e) { + // Nothing + } + }); + } + + /** + * This allow to manage the parser command. + * + * @param parser + * The parser used. + */ + protected void manageParserCommand(final IParser parser) { + if (null != parser) { + ICommand command = null; + if (null != modelProperty + && modelProperty.getValueType() instanceof EStructuralFeature) { + command = parser.getParseCommand(new EObjectAdapter( + (EStructuralFeature) modelProperty.getValueType()), + styledTextStringEditor.getText().getText(), 0); + } else { + command = parser.getParseCommand(new EObjectAdapter( + (EObject) getValue()), styledTextStringEditor.getText() + .getText(), 0); + } + + TransactionalEditingDomain domain = TransactionUtil + .getEditingDomain(getContextElement()); + if (null == domain) { + // can be null for opaque expression that have been + // created but have not been added to parent + // try to get resource set from nested dialog context + ResourceSet rs = NestedEditingDialogContext.getInstance() + .getResourceSet(); + domain = TransactionUtil.getEditingDomain(rs); + } + if (null != domain) { + domain.getCommandStack().execute( + new GMFtoEMFCommandWrapper(command)); + } + } + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.papyrus.infra.widgets.editors.StyledTextReferenceDialog#createStyledTextStringEditor(org.eclipse.swt.widgets.Composite, + * java.lang.String, int) + */ + @Override + protected StyledTextStringEditor createStyledTextStringEditor( + final Composite parent, final String initialValue, final int style) { + // Change the style to set the singleText to a single line + int createdStyle = style | SWT.SINGLE; + return new StyledTextStringEditor(parent, createdStyle) { + public StyledText createStyledText(Composite parent, String value, + int createdStyle) { + StyledText txt = new StyledText(parent, createdStyle); + if (null != labelProvider) { + txt.setText(labelProvider.getText(getValue())); + } + return txt; + } + }; + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.papyrus.infra.widgets.editors.StyledTextReferenceDialog#update() + */ + @Override + public void update() { + super.update(); + updateControls(); + } + + /** + * This allow to update the xtext adapter for the styled text. + * + * @param styledText + * The styled text which one to adapt the xtext adapter. + */ + protected void updateXtextAdapters(final Control styledText) { + final Object oldObjectToEdit = null != configuration ? configuration + .getObjectToEdit() : null; + + final DefaultXtextDirectEditorConfiguration newConfiguration = getConfigurationFromSelection(); + // Check if configuration has changed and update adapters + if (null != newConfiguration && newConfiguration != configuration) { + if (null != xtextAdapter) { + xtextAdapter.getFakeResourceContext().getFakeResource() + .eAdapters().remove(contextElementAdapter); + } + configuration = newConfiguration; + xtextAdapter = new StyledTextXtextAdapter( + configuration.getInjector()); + + EObject semanticElement = (EObject) getValue(); + if (null != semanticElement) { + newConfiguration.preEditAction(semanticElement); + } + + xtextAdapter.getFakeResourceContext().getFakeResource().eAdapters() + .add(contextElementAdapter); + xtextAdapter.adapt((StyledText) styledText); + } + + if (null != configuration + && configuration.getObjectToEdit() != oldObjectToEdit) { + IContextElementProvider provider = configuration + .getContextProvider(); + if (provider instanceof IContextElementProviderWithInit) { + // update resource, if required by text editor + if (null != xtextAdapter) { + ((IContextElementProviderWithInit) provider) + .initResource(xtextAdapter.getFakeResourceContext() + .getFakeResource()); + } + } + } + } + + /** + * This allow to get the xtext direct editor configuration depending on the + * value of the styled text. + * + * @return The {@link DefaultXtextDirectEditorConfiguration} corresponding. + */ + protected DefaultXtextDirectEditorConfiguration getConfigurationFromSelection() { + DefaultXtextDirectEditorConfiguration result = null; + final Object feature = modelProperty.getValueType(); + if (null != feature && feature instanceof EStructuralFeature) { + IPreferenceStore store = Activator.getDefault() + .getPreferenceStore(); + String semanticClassName = ((EStructuralFeature) feature) + .getEType().getInstanceClassName(); + + String key = IDirectEditorsIds.EDITOR_FOR_ELEMENT + + semanticClassName; + String languagePreferred = store.getString(key); + + if (languagePreferred != null && !languagePreferred.equals("")) { //$NON-NLS-1$ + IDirectEditorConfiguration configuration = DirectEditorsUtil + .findEditorConfiguration(languagePreferred, + semanticClassName); + if (configuration instanceof DefaultXtextDirectEditorConfiguration) { + + final Object contextElement = getContextElement(); + if (null != contextElement + && contextElement instanceof EObject) { + DefaultXtextDirectEditorConfiguration xtextConfiguration = (DefaultXtextDirectEditorConfiguration) configuration; + xtextConfiguration + .preEditAction(((EObject) contextElement) + .eGet((EStructuralFeature) feature)); + result = xtextConfiguration; + } + } + } + } + + if (null == result) { + EObject semanticElement = (EObject) getValue(); + + if (null != semanticElement) { + IPreferenceStore store = Activator.getDefault() + .getPreferenceStore(); + String semanticClassName = semanticElement.eClass() + .getInstanceClassName(); + + String key = IDirectEditorsIds.EDITOR_FOR_ELEMENT + + semanticClassName; + String languagePreferred = store.getString(key); + + if (languagePreferred != null && !languagePreferred.equals("")) { //$NON-NLS-1$ + IDirectEditorConfiguration configuration = DirectEditorsUtil + .findEditorConfiguration(languagePreferred, + semanticClassName); + if (configuration instanceof DefaultXtextDirectEditorConfiguration) { + + DefaultXtextDirectEditorConfiguration xtextConfiguration = (DefaultXtextDirectEditorConfiguration) configuration; + xtextConfiguration.preEditAction(semanticElement); + return xtextConfiguration; + } + } + } + } + return result; + } + + /** + * Get the parser corresponding to the value. + * + * @return The {@link IParser} + */ + protected IParser getParser() { + EObject parentSemanticElement = null; + final Object contextElement = getContextElement(); + if (null != contextElement && contextElement instanceof EObject) { + parentSemanticElement = (EObject) contextElement; + } + if (null != configuration && null != parentSemanticElement) { + return configuration.createParser(parentSemanticElement); + } + return null; + } + + /** + * {@inheritDoc} Update the xtext adapter. + * + * @see org.eclipse.papyrus.infra.widgets.editors.StyledTextReferenceDialog#doBinding() + */ + @Override + protected void doBinding() { + super.doBinding(); + updateXtextAdapters(styledTextStringEditor.getText()); + styledTextStringEditor.setValue(labelProvider.getText(getValue())); + updateLabel(); + } + + /** + * {@inheritDoc} Dispose the xtext adapter. + * + * @see org.eclipse.papyrus.infra.widgets.editors.StyledTextReferenceDialog#dispose() + * + */ + @Override + public void dispose() { + // dispose resources to avoid memory leaks + if (null != styledTextStringEditor) { + styledTextStringEditor.dispose(); + } + if (null != xtextAdapter) { + xtextAdapter.getFakeResourceContext().getFakeResource().eAdapters() + .remove(contextElementAdapter); + xtextAdapter.dispose(); + xtextAdapter = null; + } + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.papyrus.uml.xtext.integration.core.ContextElementAdapter.IContextElementProvider#getContextObject() + */ + @Override + public EObject getContextObject() { + final Object value = getValue(); + return value instanceof EObject ? (EObject) value : null; + } +} |