From 9e5a1e17be7ac080130a2bba5aff62bf6b18ffda Mon Sep 17 00:00:00 2001 From: Camille Letavernier Date: Wed, 4 Dec 2013 13:10:33 +0100 Subject: 370797: [Theme] Papyrus should provide a support for CSS files on its diagrams https://bugs.eclipse.org/bugs/show_bug.cgi?id=370797 Fix the reset style handler--- .../properties/ui/MultipleStylableElement.xwt | 2 +- .../model/properties/ui/SingleDiagram.xwt | 2 +- .../model/properties/ui/SingleStylableElement.xwt | 2 +- .../css/properties/widgets/ResetStyleWidget.java | 30 ++-- .../gmfdiag/css/command/ResetStyleCommand.java | 162 ++++++++++++++++++ .../gmfdiag/css/handler/ResetStyleHandler.java | 181 +-------------------- .../infra/gmfdiag/css/helper/ResetStyleHelper.java | 67 ++++++++ 7 files changed, 254 insertions(+), 192 deletions(-) create mode 100644 plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/command/ResetStyleCommand.java create mode 100644 plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/helper/ResetStyleHelper.java diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/MultipleStylableElement.xwt b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/MultipleStylableElement.xwt index f4d2cf428bf..208f3a1d236 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/MultipleStylableElement.xwt +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/MultipleStylableElement.xwt @@ -8,5 +8,5 @@ - + \ No newline at end of file diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleDiagram.xwt b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleDiagram.xwt index 36e0cdb35e2..64704634d72 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleDiagram.xwt +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleDiagram.xwt @@ -18,7 +18,7 @@ - + diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleStylableElement.xwt b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleStylableElement.xwt index d993f070dd5..b4b4f789285 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleStylableElement.xwt +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/model/properties/ui/SingleStylableElement.xwt @@ -11,7 +11,7 @@ - + diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/widgets/ResetStyleWidget.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/widgets/ResetStyleWidget.java index d514aa3b5d6..470316e7202 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/widgets/ResetStyleWidget.java +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.properties/src/org/eclipse/papyrus/infra/gmfdiag/css/properties/widgets/ResetStyleWidget.java @@ -11,12 +11,9 @@ *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.css.properties.widgets; -import java.util.HashMap; - -import org.eclipse.core.commands.ExecutionEvent; -import org.eclipse.core.commands.ExecutionException; -import org.eclipse.papyrus.infra.gmfdiag.css.handler.ResetStyleHandler; -import org.eclipse.papyrus.infra.gmfdiag.css.properties.Activator; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.papyrus.infra.gmfdiag.css.helper.ResetStyleHelper; +import org.eclipse.papyrus.views.properties.modelelement.DataSource; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; @@ -28,6 +25,8 @@ public class ResetStyleWidget implements SelectionListener { private Button button; + private DataSource input; + public ResetStyleWidget(Composite parent, int style) { button = new Button(parent, SWT.PUSH); button.addSelectionListener(this); @@ -36,17 +35,12 @@ public class ResetStyleWidget implements SelectionListener { } public void widgetSelected(SelectionEvent event) { - if(event.widget != button) { + if(event.widget != button || input == null) { return; } - ResetStyleHandler handler = new ResetStyleHandler(); - try { - //FIXME: The ExecutionEvent needs the EclipseContext - handler.execute(new ExecutionEvent(null, new HashMap(), event, null)); - } catch (ExecutionException ex) { - Activator.log.error(ex); - } + IStructuredSelection viewSelection = input.getSelection(); + ResetStyleHelper.resetStyle(viewSelection); } public void widgetDefaultSelected(SelectionEvent e) { @@ -68,4 +62,12 @@ public class ResetStyleWidget implements SelectionListener { public String getToolTipText() { return button.getToolTipText(); } + + public void setInput(DataSource input) { + this.input = input; + } + + public DataSource getInput() { + return input; + } } diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/command/ResetStyleCommand.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/command/ResetStyleCommand.java new file mode 100644 index 00000000000..26d718edd61 --- /dev/null +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/command/ResetStyleCommand.java @@ -0,0 +1,162 @@ +/***************************************************************************** + * Copyright (c) 2012 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: + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.infra.gmfdiag.css.command; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.NamedStyle; +import org.eclipse.gmf.runtime.notation.NotationPackage; +import org.eclipse.gmf.runtime.notation.Style; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.papyrus.infra.emf.appearance.helper.VisualInformationPapyrusConstants; +import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper; +import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSAnnotations; +import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles; + + +public class ResetStyleCommand extends RecordingCommand { + + private static Set papyrusStyleAnnotations = new HashSet(); + + static { + papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.DISPLAY_NAMELABELICON); + papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.SHADOWFIGURE); + papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.QUALIFIED_NAME); + } + + private Iterator iterator; + + public ResetStyleCommand(TransactionalEditingDomain domain, Iterator iterator) { + super(domain); + this.iterator = iterator; + } + + @Override + public void doExecute() { + while(iterator.hasNext()) { + Object selectedItem = iterator.next(); + View view = NotationHelper.findView(selectedItem); + if(view == null) { + continue; + } + + if(view instanceof Diagram) { + resetDiagram((Diagram)view); + break; + } else { + //Reset the style attribute to their default value + resetStyle(view, true); + } + } + } + + private void resetDiagram(Diagram diagram) { + for(Object viewObject : diagram.getChildren()) { + if(viewObject instanceof View) { + resetStyle((View)viewObject, true); + } + } + for(Object lineObject : diagram.getEdges()) { + if(lineObject instanceof View) { + resetStyle((View)lineObject, true); + } + } + } + + private void resetStyle(View view, boolean recursive) { + resetStyle(view); + if(recursive) { + for(Object childObject : view.getChildren()) { + if(childObject instanceof View) { + resetStyle((View)childObject, recursive); + } + } + } + } + + private void resetStyle(View view) { + + Iterator styleIterator = view.getStyles().iterator(); + while(styleIterator.hasNext()) { + Object styleObject = styleIterator.next(); + if(styleObject instanceof NamedStyle) { + NamedStyle customStyle = (NamedStyle)styleObject; + if(!CSSStyles.RESERVED_KEYWORDS.contains(customStyle.getName())) { + styleIterator.remove(); + } + } else if(styleObject instanceof Style) { + resetStyle((Style)styleObject); + } + } + + if(view instanceof Style) { + resetStyle((Style)view); + } + + //Remove the "forceValue" annotations + resetAnnotations(view); + //Remove the Papyrus Style EAnnotations + resetStyleAnnotations(view); + + if(view.eClass() != NotationPackage.eINSTANCE.getDecorationNode()) { + //Reset the visibility (Except for labels which are not yet supported) + view.eUnset(NotationPackage.eINSTANCE.getView_Visible()); + } + } + + private void resetStyle(Style style) { + if(style instanceof NamedStyle) { + //Skip custom styles. + //TODO: We should skip CSS Styles (CSSClass, CSSId, CSSStyle, DiagramStyleSheets), + //and reset custom GMF Styles (elementIcon, shadow, ...). + //What about external custom styles (ie. unkwnown styles)? + //They should be stylable, but they might contain something we don't want to reset... + return; + } + + for(EStructuralFeature feature : style.eClass().getEAllStructuralFeatures()) { + //Only edit Style features + if(NotationPackage.eINSTANCE.getStyle().isSuperTypeOf(feature.getEContainingClass())) { + //Reset the value to default + style.eUnset(feature); + } + } + } + + //Resets the "Force Value" annotations (Tags to indicate that the user + //has manually selected a value, which will override the CSS Style) + private void resetAnnotations(View view) { + Iterator iterator = view.getEAnnotations().iterator(); + while(iterator.hasNext()) { + if(CSSAnnotations.CSS_FORCE_VALUE.equals(iterator.next().getSource())) { + iterator.remove(); + } + } + } + + //Resets the "Custom style" Annotations (elementIcon, shadow, qualifiedName) + private void resetStyleAnnotations(View view) { + Iterator iterator = view.getEAnnotations().iterator(); + while(iterator.hasNext()) { + if(papyrusStyleAnnotations.contains(iterator.next().getSource())) { + iterator.remove(); + } + } + } +} diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/handler/ResetStyleHandler.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/handler/ResetStyleHandler.java index f2dcb79960e..2cbfb1c521e 100644 --- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/handler/ResetStyleHandler.java +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/handler/ResetStyleHandler.java @@ -11,34 +11,15 @@ *****************************************************************************/ package org.eclipse.papyrus.infra.gmfdiag.css.handler; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; -import org.eclipse.emf.common.command.Command; -import org.eclipse.emf.ecore.EAnnotation; -import org.eclipse.emf.ecore.EStructuralFeature; -import org.eclipse.emf.edit.domain.EditingDomain; -import org.eclipse.emf.transaction.RecordingCommand; -import org.eclipse.emf.transaction.TransactionalEditingDomain; -import org.eclipse.gmf.runtime.notation.Diagram; -import org.eclipse.gmf.runtime.notation.NamedStyle; -import org.eclipse.gmf.runtime.notation.NotationPackage; -import org.eclipse.gmf.runtime.notation.Style; -import org.eclipse.gmf.runtime.notation.View; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.papyrus.infra.core.editor.IMultiDiagramEditor; import org.eclipse.papyrus.infra.core.services.ServiceException; import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers; -import org.eclipse.papyrus.infra.emf.appearance.helper.VisualInformationPapyrusConstants; -import org.eclipse.papyrus.infra.emf.utils.EMFHelper; -import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper; -import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSAnnotations; -import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles; +import org.eclipse.papyrus.infra.gmfdiag.css.helper.ResetStyleHelper; /** @@ -46,22 +27,18 @@ import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSStyles; * for a set of GMF Views. * * @author Camille Letavernier + * + * @deprecated Not used. Use ResetStyleHelper instead */ +@Deprecated public class ResetStyleHandler extends AbstractHandler { - private static Set papyrusStyleAnnotations = new HashSet(); - static { - papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.DISPLAY_NAMELABELICON); - papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.SHADOWFIGURE); - papyrusStyleAnnotations.add(VisualInformationPapyrusConstants.QUALIFIED_NAME); - } - public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection; try { //FIXME: Use ServiceUtilsForHandlers - //FIXME: This method is sometimes called manually with a null ExecutionEvent. It won't work with ServiceUtilsForHandlers + //FIXME: This method is sometimes called manually with a null ExecutionEvent. It won't work with ServiceUtilsForHandlers IMultiDiagramEditor editor = ServiceUtilsForActionHandlers.getInstance().getServiceRegistry().getService(IMultiDiagramEditor.class); selection = editor.getEditorSite().getSelectionProvider().getSelection(); } catch (ServiceException ex) { @@ -78,156 +55,10 @@ public class ResetStyleHandler extends AbstractHandler { } IStructuredSelection sSelection = (IStructuredSelection)selection; - Iterator iterator = sSelection.iterator(); - - TransactionalEditingDomain domain = null; - while(iterator.hasNext()) { - Object selectedItem = iterator.next(); - View view = NotationHelper.findView(selectedItem); - if(view == null) { - continue; - } - - EditingDomain editingDomain = EMFHelper.resolveEditingDomain(view); - if(editingDomain instanceof TransactionalEditingDomain) { - domain = (TransactionalEditingDomain)editingDomain; - } - break; - } - - if(domain == null) { - return null; - } - Command command = new ResetStyleCommand(domain, sSelection); - domain.getCommandStack().execute(command); + ResetStyleHelper.resetStyle(sSelection); return null; } - private class ResetStyleCommand extends RecordingCommand { - - private IStructuredSelection selection; - - public ResetStyleCommand(TransactionalEditingDomain domain, IStructuredSelection selection) { - super(domain); - this.selection = selection; - } - - @Override - public void doExecute() { - Iterator iterator = selection.iterator(); - while(iterator.hasNext()) { - Object selectedItem = iterator.next(); - View view = NotationHelper.findView(selectedItem); - if(view == null) { - continue; - } - - if(view instanceof Diagram) { - resetDiagram((Diagram)view); - break; - } else { - //Reset the style attribute to their default value - resetStyle(view, true); - } - } - } - - private void resetDiagram(Diagram diagram) { - for(Object viewObject : diagram.getChildren()) { - if(viewObject instanceof View) { - resetStyle((View)viewObject, true); - } - } - for(Object lineObject : diagram.getEdges()) { - if(lineObject instanceof View) { - resetStyle((View)lineObject, true); - } - } - } - - private void resetStyle(View view, boolean recursive) { - resetStyle(view); - if(recursive) { - for(Object childObject : view.getChildren()) { - if(childObject instanceof View) { - resetStyle((View)childObject, recursive); - } - } - } - } - - private void resetStyle(View view) { - - Iterator styleIterator = view.getStyles().iterator(); - while(styleIterator.hasNext()) { - Object styleObject = styleIterator.next(); - if(styleObject instanceof NamedStyle) { - NamedStyle customStyle = (NamedStyle)styleObject; - if(!CSSStyles.RESERVED_KEYWORDS.contains(customStyle.getName())) { - styleIterator.remove(); - } - } else if(styleObject instanceof Style) { - resetStyle((Style)styleObject); - } - } - - if(view instanceof Style) { - resetStyle((Style)view); - } - - //Remove the "forceValue" annotations - resetAnnotations(view); - //Remove the Papyrus Style EAnnotations - resetStyleAnnotations(view); - - if(view.eClass() != NotationPackage.eINSTANCE.getDecorationNode()) { - //Reset the visibility (Except for labels which are not yet supported) - view.eUnset(NotationPackage.eINSTANCE.getView_Visible()); - } - } - - private void resetStyle(Style style) { - if(style instanceof NamedStyle) { - //Skip custom styles. - //TODO: We should skip CSS Styles (CSSClass, CSSId, CSSStyle, DiagramStyleSheets), - //and reset custom GMF Styles (elementIcon, shadow, ...). - //What about external custom styles (ie. unkwnown styles)? - //They should be stylable, but they might contain something we don't want to reset... - return; - } - - for(EStructuralFeature feature : style.eClass().getEAllStructuralFeatures()) { - //Only edit Style features - if(NotationPackage.eINSTANCE.getStyle().isSuperTypeOf(feature.getEContainingClass())) { - //Reset the value to default - style.eUnset(feature); - } - } - } - - //Resets the "Force Value" annotations (Tags to indicate that the user - //has manually selected a value, which will override the CSS Style) - private void resetAnnotations(View view) { - Iterator iterator = view.getEAnnotations().iterator(); - while(iterator.hasNext()) { - if(CSSAnnotations.CSS_FORCE_VALUE.equals(iterator.next().getSource())) { - iterator.remove(); - } - } - } - - //Resets the "Custom style" Annotations (elementIcon, shadow, qualifiedName) - private void resetStyleAnnotations(View view) { - Iterator iterator = view.getEAnnotations().iterator(); - while(iterator.hasNext()) { - if(papyrusStyleAnnotations.contains(iterator.next().getSource())) { - iterator.remove(); - } - } - } - } - - } diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/helper/ResetStyleHelper.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/helper/ResetStyleHelper.java new file mode 100644 index 00000000000..4f0634b12e9 --- /dev/null +++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css/src/org/eclipse/papyrus/infra/gmfdiag/css/helper/ResetStyleHelper.java @@ -0,0 +1,67 @@ +package org.eclipse.papyrus.infra.gmfdiag.css.helper; + +import java.util.Collection; +import java.util.Iterator; + +import org.eclipse.emf.common.command.Command; +import org.eclipse.emf.edit.domain.EditingDomain; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.papyrus.infra.emf.utils.EMFHelper; +import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper; +import org.eclipse.papyrus.infra.gmfdiag.css.command.ResetStyleCommand; + + +public class ResetStyleHelper { + + public static void resetStyle(IStructuredSelection selection) { + if(selection == null || selection.isEmpty()) { + return; + } + + Iterator selectionIterator = selection.iterator(); + + TransactionalEditingDomain editingDomain = getEditingDomain(selectionIterator); + + resetStyle(editingDomain, selection.iterator()); + } + + public static void resetStyle(Collection selection) { + if(selection == null) { + return; + } + + TransactionalEditingDomain editingDomain = getEditingDomain(selection.iterator()); + + resetStyle(editingDomain, selection.iterator()); + } + + public static void resetStyle(TransactionalEditingDomain domain, Iterator iterator) { + if(domain == null || iterator == null || !iterator.hasNext()) { + return; + } + + Command command = new ResetStyleCommand(domain, iterator); + domain.getCommandStack().execute(command); + } + + private static TransactionalEditingDomain getEditingDomain(Iterator iterator) { + while(iterator.hasNext()) { + Object selectedItem = iterator.next(); + View view = NotationHelper.findView(selectedItem); + if(view == null) { + continue; + } + + EditingDomain editingDomain = EMFHelper.resolveEditingDomain(view); + if(editingDomain instanceof TransactionalEditingDomain) { + return (TransactionalEditingDomain)editingDomain; + } + break; + } + + return null; + } + +} -- cgit v1.2.3