Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcletavernie2012-04-04 08:54:37 +0000
committercletavernie2012-04-04 08:54:37 +0000
commit7f175e20d861032ed72533360206b144a5ce30de (patch)
tree798064fd6f38fc2ce1a2600816e04857fe9b6d9b
parent6e29a8ec6845975323836890449214bb4aa3d696 (diff)
downloadorg.eclipse.papyrus-7f175e20d861032ed72533360206b144a5ce30de.tar.gz
org.eclipse.papyrus-7f175e20d861032ed72533360206b144a5ce30de.tar.xz
org.eclipse.papyrus-7f175e20d861032ed72533360206b144a5ce30de.zip
370797: [Theme] Papyrus should provide a support for CSS files on its diagrams
https://bugs.eclipse.org/bugs/show_bug.cgi?id=370797
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/about.html28
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/build.properties4
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleDialog.java412
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleHandler.java299
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/CreateStyleHandler.java298
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/EditStyleHandler.java36
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleCreationDialog.java329
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleEditionDialog.java265
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/helper/XtextStylesheetHelper.java111
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/ExistingStyleContentProvider.java129
-rw-r--r--plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/StylesheetLabelProvider.java96
11 files changed, 1397 insertions, 610 deletions
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/about.html b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/about.html
new file mode 100644
index 00000000000..209103075a7
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/about.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
+<title>About</title>
+</head>
+<body lang="EN-US">
+<h2>About This Content</h2>
+
+<p>November 14, 2008</p>
+<h3>License</h3>
+
+<p>The Eclipse Foundation makes available all content in this plug-in (&quot;Content&quot;). Unless otherwise
+indicated below, the Content is provided to you under the terms and conditions of the
+Eclipse Public License Version 1.0 (&quot;EPL&quot;). A copy of the EPL is available
+at <a href="http://www.eclipse.org/legal/epl-v10.html">http://www.eclipse.org/legal/epl-v10.html</a>.
+For purposes of the EPL, &quot;Program&quot; will mean the Content.</p>
+
+<p>If you did not receive this Content directly from the Eclipse Foundation, the Content is
+being redistributed by another party (&quot;Redistributor&quot;) and different terms and conditions may
+apply to your use of any object code in the Content. Check the Redistributor's license that was
+provided with the Content. If no such license exists, contact the Redistributor. Unless otherwise
+indicated below, the terms and conditions of the EPL still apply to any source code in the Content
+and such source code may be obtained at <a href="http://www.eclipse.org">http://www.eclipse.org</a>.</p>
+
+</body>
+</html> \ No newline at end of file
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/build.properties b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/build.properties
index 285b8bf47b6..44a8b669f12 100644
--- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/build.properties
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/build.properties
@@ -3,4 +3,6 @@ output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml,\
- icons/
+ icons/,\
+ about.html
+src.includes = about.html
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleDialog.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleDialog.java
new file mode 100644
index 00000000000..6cc17db2b90
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleDialog.java
@@ -0,0 +1,412 @@
+/*****************************************************************************
+ * 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.configuration.handler;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.jface.dialogs.IDialogConstants;
+import org.eclipse.jface.dialogs.TrayDialog;
+import org.eclipse.jface.viewers.ILabelProvider;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
+import org.eclipse.papyrus.infra.gmfdiag.css.AttributeValue;
+import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
+import org.eclipse.papyrus.infra.gmfdiag.css.Expression;
+import org.eclipse.papyrus.infra.gmfdiag.css.HexColor;
+import org.eclipse.papyrus.infra.gmfdiag.css.Name;
+import org.eclipse.papyrus.infra.gmfdiag.css.Number;
+import org.eclipse.papyrus.infra.gmfdiag.css.SelectorCondition;
+import org.eclipse.papyrus.infra.gmfdiag.css.StringValue;
+import org.eclipse.papyrus.infra.gmfdiag.css.Subterm;
+import org.eclipse.papyrus.infra.gmfdiag.css.Term;
+import org.eclipse.papyrus.infra.gmfdiag.css.util.CssSwitch;
+import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor;
+import org.eclipse.papyrus.infra.widgets.editors.BooleanCheckbox;
+import org.eclipse.papyrus.infra.widgets.editors.EnumRadio;
+import org.eclipse.papyrus.infra.widgets.editors.ICommitListener;
+import org.eclipse.papyrus.infra.widgets.editors.StringEditor;
+import org.eclipse.papyrus.infra.widgets.providers.StaticContentProvider;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CLabel;
+import org.eclipse.swt.custom.CTabFolder;
+import org.eclipse.swt.custom.CTabItem;
+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.swt.widgets.Shell;
+
+
+public abstract class AbstractStyleDialog extends TrayDialog {
+
+ protected final Map<Attribute, Boolean> conditions;
+
+ protected final Map<Declaration, Boolean> declarations;
+
+ protected final String selectorName;
+
+ protected boolean useSelectorName = true;
+
+ protected boolean diagramRestriction = false;
+
+ protected String cssClass;
+
+ protected View contextView;
+
+ protected CLabel errorLabel;
+
+ protected StringEditor selectorPreview;
+
+ protected CTabFolder tabFolder;
+
+ protected Composite conditionsContainer;
+
+ protected Composite declarationsContainer;
+
+ public AbstractStyleDialog(Shell shell, Map<Attribute, Boolean> conditions, Map<Declaration, Boolean> declarations, String selectorName, View context) {
+ super(shell);
+ this.conditions = conditions;
+ this.declarations = declarations;
+ this.selectorName = selectorName;
+ this.contextView = context;
+ }
+
+ @Override
+ public void create() {
+ super.create();
+ Composite parent = getDialogArea();
+
+ selectorPreview = new StringEditor(parent, SWT.NONE);
+ selectorPreview.setReadOnly(true);
+ selectorPreview.setLabel("Selector preview:");
+
+ selectorPreview.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
+ GridLayout layout = (GridLayout)selectorPreview.getLayout();
+ layout.marginWidth = 0;
+
+ tabFolder = new CTabFolder(parent, SWT.BORDER);
+ tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+
+ CTabItem conditionsTab = new CTabItem(tabFolder, SWT.NONE);
+ CTabItem declarationsTab = new CTabItem(tabFolder, SWT.NONE);
+
+ conditionsTab.setText("Conditions");
+ declarationsTab.setText("Properties");
+
+ conditionsContainer = new Composite(tabFolder, SWT.NONE);
+ conditionsContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
+ conditionsContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
+ conditionsContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
+
+ conditionsTab.setControl(conditionsContainer);
+
+ declarationsContainer = new Composite(tabFolder, SWT.NONE);
+ declarationsContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
+ declarationsContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
+ declarationsContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
+
+ declarationsTab.setControl(declarationsContainer);
+
+ createConditions(conditionsContainer);
+ createDeclarations(declarationsContainer);
+
+ updateSelectorLabel();
+ updateButtons();
+
+ tabFolder.setSelection(conditionsTab);
+
+ getShell().pack();
+ }
+
+ protected void updateButtons() {
+ //Resets the error message
+ setError(null);
+ getButton(IDialogConstants.OK_ID).setEnabled(isValid());
+ getDialogArea().layout();
+ }
+
+ protected boolean isValid() {
+ boolean result = true;
+
+ //There must be at least one property declaration
+ boolean atLeastOneDeclaration = false;
+ for(Boolean value : declarations.values()) {
+ if(value) {
+ atLeastOneDeclaration = true;
+ break;
+ }
+ }
+
+ result = result && atLeastOneDeclaration;
+
+ if(!atLeastOneDeclaration) {
+ setError("There must be at least one property declaration");
+ }
+
+ return result;
+ }
+
+ protected void setError(String errorMessage) {
+ if(errorMessage == null && errorLabel != null) {
+ errorLabel.dispose();
+ errorLabel = null;
+ return;
+ }
+
+ if(errorMessage != null) {
+ if(errorLabel == null) {
+ errorLabel = new CLabel(getDialogArea(), SWT.WRAP);
+ errorLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
+ errorLabel.setImage(org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImage("icons/error.gif"));
+ }
+
+ if(errorLabel.getText() != null && !errorLabel.getText().trim().equals("")) {
+ errorLabel.setText(errorLabel.getText() + "\n" + errorMessage);
+ } else {
+ errorLabel.setText(errorMessage);
+ }
+ }
+ }
+
+ protected void createConditions(Composite parent) {
+ parent.setLayout(new GridLayout(3, false));
+
+ Label conditionsLabel = new Label(parent, SWT.WRAP);
+ conditionsLabel.setText("Select the condition(s) under which the style will be applied.");
+ conditionsLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+
+ createElementNameSection(parent);
+ createAttributeSelectorsSection(parent);
+ createStyleNameSection(parent);
+ }
+
+ protected void createElementNameSection(Composite parent) {
+ EnumRadio diagramRestrictionWidget = new EnumRadio(parent, SWT.NONE, "Diagram:");
+ diagramRestrictionWidget.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+
+ ILabelProvider labelProvider = new LabelProvider() {
+
+ @Override
+ public String getText(Object element) {
+ Boolean value = (Boolean)element;
+ return value ? contextView.getDiagram().getType() + " only" : "Any diagram";
+ }
+ };
+
+ diagramRestrictionWidget.setProviders(new StaticContentProvider(new Boolean[]{ true, false }), labelProvider);
+ diagramRestrictionWidget.setValue(this.diagramRestriction);
+ diagramRestrictionWidget.addCommitListener(new ICommitListener() {
+
+ public void commit(AbstractEditor editor) {
+ diagramRestriction = (Boolean)((EnumRadio)editor).getValue();
+ updateSelectorLabel();
+ }
+ });
+
+ EnumRadio selectorNameWidget = new EnumRadio(parent, SWT.NONE, "Applies to:");
+ selectorNameWidget.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+
+ labelProvider = new LabelProvider() {
+
+ @Override
+ public String getText(Object element) {
+ Boolean value = (Boolean)element;
+ return value ? selectorName + " only" : "Any kind of element";
+ }
+ };
+
+ selectorNameWidget.setProviders(new StaticContentProvider(new Boolean[]{ true, false }), labelProvider);
+ selectorNameWidget.setValue(this.useSelectorName);
+ selectorNameWidget.addCommitListener(new ICommitListener() {
+
+ public void commit(AbstractEditor editor) {
+ useSelectorName = (Boolean)((EnumRadio)editor).getValue();
+ updateSelectorLabel();
+ }
+ });
+ }
+
+ protected void createAttributeSelectorsSection(Composite parent) {
+ if(conditions.isEmpty()) {
+ return;
+ }
+
+ Label detailLabel = new Label(parent, SWT.NONE);
+ detailLabel.setText("If the following properties are matched:");
+ detailLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+
+ for(SelectorCondition condition : conditions.keySet()) {
+ final Attribute currentCondition = (Attribute)condition;
+
+ String attributeLabel = currentCondition.getName();
+ if(currentCondition.getValue() != null) {
+ attributeLabel += " " + currentCondition.getValue().getOperator() + " " + currentCondition.getValue().getValue();
+ }
+
+ BooleanCheckbox checkbox = new BooleanCheckbox(parent, SWT.NONE, attributeLabel);
+ checkbox.setValue(conditions.get(currentCondition));
+
+ checkbox.addCommitListener(new ICommitListener() {
+
+ public void commit(AbstractEditor editor) {
+ conditions.put(currentCondition, ((BooleanCheckbox)editor).getValue());
+ updateSelectorLabel();
+ }
+ });
+ }
+ }
+
+ protected void createStyleNameSection(Composite parent) {
+ Label styleNameLabel = new Label(parent, SWT.WRAP);
+
+ String label = "If a name is used for this style, it will have to be applied manually:";
+
+ styleNameLabel.setText(label);
+ styleNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+
+ StringEditor styleNameEditor = new StringEditor(parent, SWT.NONE, "Style name:");
+ styleNameEditor.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+ styleNameEditor.setValue(cssClass);
+ styleNameEditor.addCommitListener(new ICommitListener() {
+
+ public void commit(AbstractEditor editor) {
+ cssClass = (String)((StringEditor)editor).getValue();
+ updateSelectorLabel();
+ }
+
+ });
+ }
+
+ protected void createDeclarations(Composite parent) {
+ parent.setLayout(new GridLayout(3, false));
+
+ Label declarationsLabel = new Label(parent, SWT.WRAP);
+ declarationsLabel.setText("Select the properties you want to set. Unchecked properties will keep their default value\n (Which might be inherited from another style).");
+ declarationsLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
+
+ for(Declaration declaration : declarations.keySet()) {
+ String label = declaration.getProperty() + ": " + getLabel(declaration.getExpression());
+ BooleanCheckbox checkbox = new BooleanCheckbox(parent, SWT.NONE, label);
+
+ final Declaration currentDeclaration = declaration;
+
+ checkbox.setValue(declarations.get(currentDeclaration));
+
+ checkbox.addCommitListener(new ICommitListener() {
+
+ public void commit(AbstractEditor editor) {
+ boolean value = ((BooleanCheckbox)editor).getValue();
+ declarations.put(currentDeclaration, value);
+ updateButtons();
+ }
+ });
+
+ checkbox.setValue(declarations.get(declaration));
+ }
+ }
+
+ public String getCSSClass() {
+ if(cssClass != null) {
+ return cssClass.trim().equals("") ? null : cssClass.trim();
+ }
+ return null;
+ }
+
+ protected String getLabel(Expression expression) {
+ String label = getLabel(expression.getTerms());
+ for(Subterm subTerm : expression.getSubterms()) {
+ if(subTerm.getOperator() != null) {
+ label += subTerm.getOperator();
+ }
+ label += " " + getLabel(subTerm.getTerm());
+ }
+ return label;
+ }
+
+ protected String getLabel(Term term) {
+ return (new CssSwitch<String>() {
+
+ @Override
+ public String caseHexColor(HexColor term) {
+ return '#' + term.getValue();
+ }
+
+ @Override
+ public String caseName(Name term) {
+ return term.getValue();
+ }
+
+ @Override
+ public String caseStringValue(StringValue term) {
+ return "\"" + term.getValue() + "\"";
+ }
+
+ @Override
+ public String caseNumber(Number term) {
+ String label = "";
+ if(term.getOp() != null) {
+ label += term.getOp().getOperator();
+ }
+ label += term.getValue();
+ return label;
+ }
+ }).doSwitch(term);
+ }
+
+ protected void updateSelectorLabel() {
+ String selectorText = "";
+
+ if(diagramRestriction) {
+ selectorText += contextView.getDiagram().getType() + " ";
+ }
+
+ selectorText += useSelectorName ? selectorName : "*";
+
+ for(Entry<Attribute, Boolean> selectorEntry : conditions.entrySet()) {
+ if(selectorEntry.getValue()) {
+ Attribute condition = selectorEntry.getKey();
+ Attribute attribute = condition;
+ AttributeValue value = attribute.getValue();
+ selectorText += "[" + attribute.getName() + value.getOperator() + value.getValue() + "]";
+ }
+ }
+
+ if(cssClass != null && !"".equals(cssClass)) {
+ selectorText += "." + cssClass;
+ }
+
+ selectorPreview.setValue(selectorText);
+ getDialogArea().layout();
+ }
+
+ @Override
+ protected boolean isResizable() {
+ return true;
+ }
+
+ @Override
+ public Composite getDialogArea() {
+ return (Composite)super.getDialogArea();
+ }
+
+ public boolean useSelectorName() {
+ return useSelectorName;
+ }
+
+ public boolean getDiagramRestriction() {
+ return diagramRestriction;
+ }
+
+
+}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleHandler.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleHandler.java
new file mode 100644
index 00000000000..932df06e4a4
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/AbstractStyleHandler.java
@@ -0,0 +1,299 @@
+/*****************************************************************************
+ * 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.configuration.handler;
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.emf.common.util.Enumerator;
+import org.eclipse.emf.ecore.EDataType;
+import org.eclipse.emf.ecore.EEnum;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.EcorePackage;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
+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.gmf.runtime.notation.datatype.GradientData;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
+import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
+import org.eclipse.papyrus.infra.gmfdiag.css.ATTRIBUTE_OP;
+import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
+import org.eclipse.papyrus.infra.gmfdiag.css.AttributeValue;
+import org.eclipse.papyrus.infra.gmfdiag.css.CompositeSelector;
+import org.eclipse.papyrus.infra.gmfdiag.css.CssFactory;
+import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
+import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
+import org.eclipse.papyrus.infra.gmfdiag.css.SelectorCondition;
+import org.eclipse.papyrus.infra.gmfdiag.css.SimpleSelector;
+import org.eclipse.papyrus.infra.gmfdiag.css.Stylesheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.configuration.Activator;
+import org.eclipse.papyrus.infra.gmfdiag.css.handler.CSSRefreshHandler;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Shell;
+
+
+public abstract class AbstractStyleHandler extends AbstractHandler {
+
+ public Object execute(ExecutionEvent event) {
+ ISelection selection;
+ try {
+ selection = ServiceUtilsForActionHandlers.getInstance().getNestedActiveIEditorPart().getSite().getSelectionProvider().getSelection();
+ if(selection.isEmpty()) {
+ return null;
+ }
+ } catch (ServiceException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ return null;
+ }
+
+ if(!(selection instanceof IStructuredSelection)) {
+ return null;
+ }
+
+ IStructuredSelection sSelection = (IStructuredSelection)selection;
+ Object element = sSelection.getFirstElement();
+
+ View view = NotationHelper.findView(element);
+ if(view == null) {
+ Activator.log.warn("Cannot create a Style from the selected element ; the element is not a View");
+ return null;
+ }
+
+ Shell parentShell = ((Event)event.getTrigger()).widget.getDisplay().getActiveShell();
+
+ if(view.getElement() == null || view instanceof Diagram) {
+ MessageDialog.open(MessageDialog.WARNING, parentShell, "Style error", "The selected element's style cannot be exported", SWT.NONE);
+ return null;
+ }
+
+ Map<Declaration, Boolean> declarations = handleStyles(view);
+ Map<Attribute, Boolean> conditions = handleSemantic(view);
+
+ String selectorName = view.getElement().eClass().getName();
+
+ AbstractStyleDialog dialog = createStyleDialog(parentShell, declarations, conditions, selectorName, view);
+
+ if(dialog.open() != Dialog.OK) {
+ return null;
+ }
+
+ Ruleset ruleset = getRuleset(dialog);
+ SimpleSelector selector = CssFactory.eINSTANCE.createSimpleSelector();
+
+ if(dialog.useSelectorName()) {
+ selector.setElementName(selectorName);
+ } else {
+ selector.setElementName("*"); //$NON-NLS-1$
+ }
+
+ if(dialog.getDiagramRestriction()) {
+ String diagramType = view.getDiagram().getType();
+ CompositeSelector compositeSelector = CssFactory.eINSTANCE.createCompositeSelector();
+ compositeSelector.setRight(selector);
+
+ SimpleSelector diagramSelector = CssFactory.eINSTANCE.createSimpleSelector();
+ diagramSelector.setElementName(diagramType);
+ compositeSelector.setLeft(diagramSelector);
+
+ ruleset.getSelectors().add(compositeSelector);
+ } else {
+ ruleset.getSelectors().add(selector);
+ }
+
+ if(dialog.getCSSClass() != null) {
+ String cssClass = dialog.getCSSClass();
+ org.eclipse.papyrus.infra.gmfdiag.css.Class classCondition = CssFactory.eINSTANCE.createClass();
+ classCondition.setClass(cssClass);
+ selector.getCondition().add(classCondition);
+ }
+
+ for(SelectorCondition condition : conditions.keySet()) {
+ if(conditions.get(condition)) {
+ selector.getCondition().add(condition);
+ }
+ }
+
+ for(Declaration declaration : declarations.keySet()) {
+ if(declarations.get(declaration)) {
+ ruleset.getProperties().add(declaration);
+ }
+ }
+
+ Stylesheet xtextStylesheet = getStyleSheet(dialog, view);
+
+ if(xtextStylesheet == null) {
+ return null;
+ }
+
+ Resource resource = xtextStylesheet.eResource();
+
+ if(!xtextStylesheet.getContents().contains(ruleset)) {
+ xtextStylesheet.getContents().add(ruleset);
+ }
+
+ try {
+ resource.save(new HashMap<Object, Object>());
+ (new CSSRefreshHandler()).execute(null);
+ } catch (IOException ex) {
+ Activator.log.error(ex);
+ MessageDialog.open(MessageDialog.ERROR, parentShell, "Style error", "An unexpected error occured while trying to save the Stylesheet", SWT.NONE);
+ } catch (Exception ex) {
+ Activator.log.error(ex);
+ MessageDialog.open(MessageDialog.ERROR, parentShell, "Style error", "An unexpected error occured while trying to save the Stylesheet", SWT.NONE);
+ }
+
+ return null;
+ }
+
+ protected abstract AbstractStyleDialog createStyleDialog(Shell shell, Map<Declaration, Boolean> declarations, Map<Attribute, Boolean> conditions, String selectorName, View context);
+
+ protected abstract Ruleset getRuleset(AbstractStyleDialog dialog);
+
+ protected abstract Stylesheet getStyleSheet(AbstractStyleDialog dialog, View contextView);
+
+ protected Map<Declaration, Boolean> handleStyles(View view) {
+ Map<Declaration, Boolean> declarations = new LinkedHashMap<Declaration, Boolean>();
+
+ for(Object styleObject : view.getStyles()) {
+ Style style = (Style)styleObject;
+ declarations.putAll(handleStyle(style));
+ }
+
+ if(view instanceof Style) {
+ declarations.putAll(handleStyle((Style)view));
+ }
+
+ return declarations;
+ }
+
+ protected Map<Attribute, Boolean> handleSemantic(View view) {
+ Map<Attribute, Boolean> result = new LinkedHashMap<Attribute, Boolean>();
+
+ EObject semanticElement = view.getElement();
+
+ for(EStructuralFeature feature : semanticElement.eClass().getEAllStructuralFeatures()) {
+ if(isBoolean(feature) || isInteger(feature) || feature.getEType() instanceof EEnum) {
+ Attribute attributeCondition = CssFactory.eINSTANCE.createAttribute();
+ attributeCondition.setName(feature.getName());
+
+ AttributeValue attributeValue = CssFactory.eINSTANCE.createAttributeValue();
+ attributeValue.setOperator(ATTRIBUTE_OP.EQUALS);
+ attributeValue.setValue(semanticElement.eGet(feature).toString());
+ attributeCondition.setValue(attributeValue);
+
+ boolean check = semanticElement.eGet(feature) != feature.getDefaultValue();
+
+ result.put(attributeCondition, check);
+ }
+ }
+
+ return result;
+ }
+
+ protected boolean isBoolean(EStructuralFeature feature) {
+ if(feature.getEType() == EcorePackage.eINSTANCE.getEBoolean() || feature.getEType() == EcorePackage.eINSTANCE.getEBooleanObject()) {
+ return true;
+ }
+
+ if(feature.getEType() instanceof EDataType) {
+ EDataType datatype = (EDataType)feature.getEType();
+ return datatype.getName().equals("Boolean");
+ }
+
+ return false;
+ }
+
+ protected boolean isString(EStructuralFeature feature) {
+ return feature.getEType() == EcorePackage.eINSTANCE.getEString();
+ }
+
+ protected boolean isInteger(EStructuralFeature feature) {
+ if(feature.getEType() == EcorePackage.eINSTANCE.getEInt() || feature.getEType() == EcorePackage.eINSTANCE.getEIntegerObject()) {
+ return true;
+ }
+
+ if(feature.getEType() instanceof EDataType) {
+ EDataType datatype = (EDataType)feature.getEType();
+ return datatype.getName().equals("Integer");
+ }
+
+ return false;
+ }
+
+ protected Map<Declaration, Boolean> handleStyle(Style style) {
+ if(style instanceof NamedStyle) {
+ return Collections.emptyMap();
+ }
+
+ Map<Declaration, Boolean> declarations = new LinkedHashMap<Declaration, Boolean>();
+
+ for(EStructuralFeature feature : style.eClass().getEAllStructuralFeatures()) {
+ if(NotationPackage.eINSTANCE.getStyle().isSuperTypeOf(feature.getEContainingClass())) {
+ boolean check = style.eGet(feature) != feature.getDefaultValue();
+ declarations.put(handleStyleFeature(style, feature), check);
+ }
+ }
+
+ return declarations;
+ }
+
+ protected Declaration handleStyleFeature(Style style, EStructuralFeature feature) {
+ Declaration declaration = CssFactory.eINSTANCE.createDeclaration();
+ declaration.setProperty(feature.getName());
+
+ GMFToCSSConverter converter = GMFToCSSConverter.instance;
+
+ if(isString(feature)) {
+ declaration.setExpression(converter.convert((String)style.eGet(feature)));
+ }
+
+ if(isInteger(feature)) {
+ if(feature.getName().endsWith("Color")) {
+ declaration.setExpression(converter.convert(FigureUtilities.integerToColor((Integer)style.eGet(feature))));
+ } else {
+ declaration.setExpression(converter.convert((Integer)style.eGet(feature)));
+ }
+ }
+
+ if(feature.getEType() == NotationPackage.eINSTANCE.getGradientData()) {
+ declaration.setExpression(converter.convert((GradientData)style.eGet(feature)));
+ }
+
+ if(feature.getEType() instanceof EEnum) {
+ declaration.setExpression(converter.convert((Enumerator)style.eGet(feature)));
+ }
+
+ if(isBoolean(feature)) {
+ declaration.setExpression(converter.convert((Boolean)style.eGet(feature)));
+ }
+
+ return declaration;
+ }
+
+}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/CreateStyleHandler.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/CreateStyleHandler.java
index 81ede998d5f..dcdff93531c 100644
--- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/CreateStyleHandler.java
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/CreateStyleHandler.java
@@ -11,183 +11,53 @@
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.configuration.handler;
-import java.io.IOException;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
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.common.util.Enumerator;
-import org.eclipse.emf.common.util.URI;
-import org.eclipse.emf.ecore.EDataType;
-import org.eclipse.emf.ecore.EEnum;
-import org.eclipse.emf.ecore.EObject;
-import org.eclipse.emf.ecore.EStructuralFeature;
-import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.ecore.resource.ResourceSet;
-import org.eclipse.gmf.runtime.draw2d.ui.figures.FigureUtilities;
-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.gmf.runtime.notation.datatype.GradientData;
-import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.jface.viewers.ISelection;
-import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.papyrus.infra.core.services.ServiceException;
-import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
-import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
-import org.eclipse.papyrus.infra.gmfdiag.css.ATTRIBUTE_OP;
import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
-import org.eclipse.papyrus.infra.gmfdiag.css.AttributeValue;
-import org.eclipse.papyrus.infra.gmfdiag.css.CompositeSelector;
import org.eclipse.papyrus.infra.gmfdiag.css.CssFactory;
import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
-import org.eclipse.papyrus.infra.gmfdiag.css.SelectorCondition;
-import org.eclipse.papyrus.infra.gmfdiag.css.SimpleSelector;
import org.eclipse.papyrus.infra.gmfdiag.css.Stylesheet;
-import org.eclipse.papyrus.infra.gmfdiag.css.configuration.Activator;
-import org.eclipse.papyrus.infra.gmfdiag.css.handler.CSSRefreshHandler;
+import org.eclipse.papyrus.infra.gmfdiag.css.configuration.helper.XtextStylesheetHelper;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
-import org.eclipse.xtext.resource.XtextResourceSet;
-public class CreateStyleHandler extends AbstractHandler {
+public class CreateStyleHandler extends AbstractStyleHandler {
- public Object execute(ExecutionEvent event) throws ExecutionException {
- ISelection selection;
- try {
- selection = ServiceUtilsForActionHandlers.getInstance().getNestedActiveIEditorPart().getSite().getSelectionProvider().getSelection();
- if(selection.isEmpty()) {
- return null;
- }
- } catch (ServiceException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return null;
- }
-
- if(!(selection instanceof IStructuredSelection)) {
- return null;
- }
-
- IStructuredSelection sSelection = (IStructuredSelection)selection;
- Object element = sSelection.getFirstElement();
-
- View view = NotationHelper.findView(element);
- if(view == null) {
- Activator.log.warn("Cannot create a Style from the selected element ; the element is not a View");
- return null;
- }
-
- Shell parentShell = ((Event)event.getTrigger()).widget.getDisplay().getActiveShell();
-
- if(view.getElement() == null || view instanceof Diagram) {
- MessageDialog.open(MessageDialog.WARNING, parentShell, "Style error", "The selected element's style cannot be exported", SWT.NONE);
- return null;
- }
-
- Map<Declaration, Boolean> declarations = handleStyles(view);
- Map<SelectorCondition, Boolean> conditions = handleSemantic(view);
-
- String selectorName = view.getElement().eClass().getName();
-
- StyleCreationDialog dialog = new StyleCreationDialog(parentShell, conditions, declarations, selectorName, view);
- if(dialog.open() != Dialog.OK) {
- return null;
- }
-
- Ruleset ruleset = CssFactory.eINSTANCE.createRuleset();
- SimpleSelector selector = CssFactory.eINSTANCE.createSimpleSelector();
-
- if(dialog.useSelectorName()) {
- selector.setElementName(selectorName);
- } else {
- selector.setElementName("*"); //$NON-NLS-1$
- }
-
- if(dialog.getDiagramRestriction()) {
- String diagramType = view.getDiagram().getType();
- CompositeSelector compositeSelector = CssFactory.eINSTANCE.createCompositeSelector();
- compositeSelector.setRight(selector);
-
- SimpleSelector diagramSelector = CssFactory.eINSTANCE.createSimpleSelector();
- diagramSelector.setElementName(diagramType);
- compositeSelector.setLeft(diagramSelector);
-
- ruleset.getSelectors().add(compositeSelector);
- } else {
- ruleset.getSelectors().add(selector);
- }
-
- if(dialog.getCSSClass() != null) {
- String cssClass = dialog.getCSSClass();
- org.eclipse.papyrus.infra.gmfdiag.css.Class classCondition = CssFactory.eINSTANCE.createClass();
- classCondition.setClass(cssClass);
- selector.getCondition().add(classCondition);
- }
+ @Override
+ protected AbstractStyleDialog createStyleDialog(Shell shell, Map<Declaration, Boolean> declarations, Map<Attribute, Boolean> conditions, String selectorName, View context) {
+ return new StyleCreationDialog(shell, conditions, declarations, selectorName, context);
+ }
- for(SelectorCondition condition : conditions.keySet()) {
- if(conditions.get(condition)) {
- selector.getCondition().add(condition);
- }
- }
+ @Override
+ protected Ruleset getRuleset(AbstractStyleDialog dialog) {
+ return CssFactory.eINSTANCE.createRuleset();
+ }
- for(Declaration declaration : declarations.keySet()) {
- if(declarations.get(declaration)) {
- ruleset.getProperties().add(declaration);
- }
- }
+ @Override
+ protected Stylesheet getStyleSheet(AbstractStyleDialog dialog, View contextView) {
+ StyleSheet styleSheet = ((StyleCreationDialog)dialog).getStyleSheet();
- StyleSheet styleSheet = dialog.getStylesheet();
if(styleSheet == null) {
- MessageDialog.open(MessageDialog.ERROR, parentShell, "Stylesheet error", "Invalid stylesheet", SWT.NONE);
+ MessageDialog.open(MessageDialog.ERROR, dialog.getShell(), "Stylesheet error", "Invalid stylesheet", SWT.NONE);
return null;
}
- ResourceSet resourceSet = new XtextResourceSet();
Resource resource;
if(styleSheet instanceof StyleSheetReference) {
- //Supported pathes:
- // /<plugin>/path/file.css (Workspace, platform:/resource/)
- // relative/path/file.css (Relative)
- String path = ((StyleSheetReference)styleSheet).getPath();
- if(!path.endsWith(".css")) {
- //FIXME: The X-Text editor supports CSS Content Types, but it is not yet configured
- //to automatically accept other extensions than *.css
- //The parser should rely on the file's content type instead of the file extension
- //Attemps to serialize to a custom extension (even an extension compatible with CSS Content Type)
- //would result in xmi serialization. We'd better stop here.
- MessageDialog.open(MessageDialog.ERROR, parentShell, "Stylesheet error", "The stylesheet must have the *.css extension", SWT.NONE);
+ resource = XtextStylesheetHelper.loadStylesheet((StyleSheetReference)styleSheet, null, contextView, dialog.getShell());
+ if(resource == null) {
return null;
}
- URI uri;
- if(path.startsWith("/")) {
- uri = URI.createPlatformResourceURI(path, true);
- } else {
- uri = URI.createURI(path);
- uri = uri.resolve(view.eResource().getURI());
- }
-
- try {
- resource = resourceSet.getResource(uri, true);
- } catch (Exception ex) {
- resource = resourceSet.createResource(uri, "org.eclipse.wst.css.core.csssource");
- }
} else {
- MessageDialog.open(MessageDialog.ERROR, parentShell, "Stylesheet error", "Embedded stylesheets are not yet supported", SWT.NONE);
+ MessageDialog.open(MessageDialog.ERROR, dialog.getShell(), "Stylesheet error", "Embedded stylesheets are not yet supported", SWT.NONE);
return null;
}
@@ -201,139 +71,7 @@ public class CreateStyleHandler extends AbstractHandler {
xtextStylesheet = (Stylesheet)resource.getContents().get(0);
}
- xtextStylesheet.getContents().add(ruleset);
-
- try {
- resource.save(new HashMap<Object, Object>());
- (new CSSRefreshHandler()).execute(null);
- } catch (IOException ex) {
- Activator.log.error(ex);
- MessageDialog.open(MessageDialog.ERROR, parentShell, "Style error", "An unexpected error occured while trying to save the Stylesheet", SWT.NONE);
- } catch (Exception ex) {
- Activator.log.error(ex);
- MessageDialog.open(MessageDialog.ERROR, parentShell, "Style error", "An unexpected error occured while trying to save the Stylesheet", SWT.NONE);
- }
-
- return null;
- }
-
- private Map<Declaration, Boolean> handleStyles(View view) {
- Map<Declaration, Boolean> declarations = new LinkedHashMap<Declaration, Boolean>();
-
- for(Object styleObject : view.getStyles()) {
- Style style = (Style)styleObject;
- declarations.putAll(handleStyle(style));
- }
-
- if(view instanceof Style) {
- declarations.putAll(handleStyle((Style)view));
- }
-
- return declarations;
- }
-
- private Map<SelectorCondition, Boolean> handleSemantic(View view) {
- Map<SelectorCondition, Boolean> result = new LinkedHashMap<SelectorCondition, Boolean>();
-
- EObject semanticElement = view.getElement();
-
- for(EStructuralFeature feature : semanticElement.eClass().getEAllStructuralFeatures()) {
- if(isBoolean(feature) || isInteger(feature) || feature.getEType() instanceof EEnum) {
- Attribute attributeCondition = CssFactory.eINSTANCE.createAttribute();
- attributeCondition.setName(feature.getName());
-
- AttributeValue attributeValue = CssFactory.eINSTANCE.createAttributeValue();
- attributeValue.setOperator(ATTRIBUTE_OP.EQUALS);
- attributeValue.setValue(semanticElement.eGet(feature).toString());
- attributeCondition.setValue(attributeValue);
-
- boolean check = semanticElement.eGet(feature) != feature.getDefaultValue();
-
- result.put(attributeCondition, check);
- }
- }
-
- return result;
- }
-
- private boolean isBoolean(EStructuralFeature feature) {
- if(feature.getEType() == EcorePackage.eINSTANCE.getEBoolean() || feature.getEType() == EcorePackage.eINSTANCE.getEBooleanObject()) {
- return true;
- }
-
- if(feature.getEType() instanceof EDataType) {
- EDataType datatype = (EDataType)feature.getEType();
- return datatype.getName().equals("Boolean");
- }
-
- return false;
- }
-
- private boolean isString(EStructuralFeature feature) {
- return feature.getEType() == EcorePackage.eINSTANCE.getEString();
- }
-
- private boolean isInteger(EStructuralFeature feature) {
- if(feature.getEType() == EcorePackage.eINSTANCE.getEInt() || feature.getEType() == EcorePackage.eINSTANCE.getEIntegerObject()) {
- return true;
- }
-
- if(feature.getEType() instanceof EDataType) {
- EDataType datatype = (EDataType)feature.getEType();
- return datatype.getName().equals("Integer");
- }
-
- return false;
- }
-
- private Map<Declaration, Boolean> handleStyle(Style style) {
- if(style instanceof NamedStyle) {
- return Collections.emptyMap();
- }
-
- Map<Declaration, Boolean> declarations = new LinkedHashMap<Declaration, Boolean>();
-
- for(EStructuralFeature feature : style.eClass().getEAllStructuralFeatures()) {
- if(NotationPackage.eINSTANCE.getStyle().isSuperTypeOf(feature.getEContainingClass())) {
- boolean check = style.eGet(feature) != feature.getDefaultValue();
- declarations.put(handleStyleFeature(style, feature), check);
- }
- }
-
- return declarations;
- }
-
- private Declaration handleStyleFeature(Style style, EStructuralFeature feature) {
- Declaration declaration = CssFactory.eINSTANCE.createDeclaration();
- declaration.setProperty(feature.getName());
-
- GMFToCSSConverter converter = GMFToCSSConverter.instance;
-
- if(isString(feature)) {
- declaration.setExpression(converter.convert((String)style.eGet(feature)));
- }
-
- if(isInteger(feature)) {
- if(feature.getName().endsWith("Color")) {
- declaration.setExpression(converter.convert(FigureUtilities.integerToColor((Integer)style.eGet(feature))));
- } else {
- declaration.setExpression(converter.convert((Integer)style.eGet(feature)));
- }
- }
-
- if(feature.getEType() == NotationPackage.eINSTANCE.getGradientData()) {
- declaration.setExpression(converter.convert((GradientData)style.eGet(feature)));
- }
-
- if(feature.getEType() instanceof EEnum) {
- declaration.setExpression(converter.convert((Enumerator)style.eGet(feature)));
- }
-
- if(isBoolean(feature)) {
- declaration.setExpression(converter.convert((Boolean)style.eGet(feature)));
- }
-
- return declaration;
+ return xtextStylesheet;
}
}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/EditStyleHandler.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/EditStyleHandler.java
index 7932fbe9665..1fd90be5bea 100644
--- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/EditStyleHandler.java
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/EditStyleHandler.java
@@ -11,22 +11,34 @@
*****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.css.configuration.handler;
-import org.eclipse.core.commands.AbstractHandler;
-import org.eclipse.core.commands.ExecutionEvent;
-import org.eclipse.core.commands.ExecutionException;
-import org.eclipse.jface.dialogs.MessageDialog;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Event;
+import java.util.Map;
+
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
+import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
+import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
+import org.eclipse.papyrus.infra.gmfdiag.css.Stylesheet;
import org.eclipse.swt.widgets.Shell;
-public class EditStyleHandler extends AbstractHandler {
+public class EditStyleHandler extends AbstractStyleHandler {
+
+ @Override
+ protected AbstractStyleDialog createStyleDialog(Shell shell, Map<Declaration, Boolean> declarations, Map<Attribute, Boolean> conditions, String selectorName, View context) {
+ return new StyleEditionDialog(shell, conditions, declarations, selectorName, context);
+ }
+
+ @Override
+ protected Ruleset getRuleset(AbstractStyleDialog dialog) {
+ Ruleset ruleset = ((StyleEditionDialog)dialog).getSelectedRuleset();
+ ruleset.getSelectors().clear();
+ ruleset.getProperties().clear();
+ return ruleset;
+ }
- public Object execute(ExecutionEvent event) throws ExecutionException {
- //TODO
- Shell parentShell = ((Event)event.getTrigger()).widget.getDisplay().getActiveShell();
- MessageDialog.open(MessageDialog.WARNING, parentShell, "Edit style error", "This action is not yet supported", SWT.NONE);
- return null;
+ @Override
+ protected Stylesheet getStyleSheet(AbstractStyleDialog dialog, View contextView) {
+ return ((StyleEditionDialog)dialog).getStylesheet();
}
}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleCreationDialog.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleCreationDialog.java
index e7d12fa11d6..003932ef801 100644
--- a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleCreationDialog.java
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleCreationDialog.java
@@ -14,38 +14,20 @@ package org.eclipse.papyrus.infra.gmfdiag.css.configuration.handler;
import java.util.Map;
import org.eclipse.gmf.runtime.notation.View;
-import org.eclipse.jface.dialogs.IDialogConstants;
-import org.eclipse.jface.dialogs.TrayDialog;
-import org.eclipse.jface.viewers.ILabelProvider;
-import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
-import org.eclipse.papyrus.infra.gmfdiag.css.Expression;
-import org.eclipse.papyrus.infra.gmfdiag.css.HexColor;
-import org.eclipse.papyrus.infra.gmfdiag.css.Name;
-import org.eclipse.papyrus.infra.gmfdiag.css.Number;
-import org.eclipse.papyrus.infra.gmfdiag.css.SelectorCondition;
-import org.eclipse.papyrus.infra.gmfdiag.css.StringValue;
-import org.eclipse.papyrus.infra.gmfdiag.css.Subterm;
-import org.eclipse.papyrus.infra.gmfdiag.css.Term;
import org.eclipse.papyrus.infra.gmfdiag.css.properties.provider.CSSStyleSheetContentProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.properties.provider.CSSStyleSheetLabelProvider;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StylesheetsFactory;
-import org.eclipse.papyrus.infra.gmfdiag.css.util.CssSwitch;
import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor;
-import org.eclipse.papyrus.infra.widgets.editors.BooleanCheckbox;
-import org.eclipse.papyrus.infra.widgets.editors.EnumRadio;
import org.eclipse.papyrus.infra.widgets.editors.ICommitListener;
import org.eclipse.papyrus.infra.widgets.editors.ReferenceDialog;
import org.eclipse.papyrus.infra.widgets.editors.StringEditor;
import org.eclipse.papyrus.infra.widgets.editors.StringFileSelector;
-import org.eclipse.papyrus.infra.widgets.providers.StaticContentProvider;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.custom.CLabel;
-import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
@@ -54,25 +36,7 @@ import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
-public class StyleCreationDialog extends TrayDialog {
-
- private final Map<SelectorCondition, Boolean> conditions;
-
- private final Map<Declaration, Boolean> declarations;
-
- private final String selectorName;
-
- private boolean useSelectorName = true;
-
- private boolean diagramRestriction = false;
-
- private String cssClass;
-
- private View contextView;
-
- private StyleSheet stylesheet;
-
- private CLabel errorLabel;
+public class StyleCreationDialog extends AbstractStyleDialog {
private StringFileSelector externalStylesheetEditor;
@@ -84,6 +48,8 @@ public class StyleCreationDialog extends TrayDialog {
private AbstractEditor stylesheetSource;
+ private StyleSheet stylesheet;
+
/**
*
* @param shell
@@ -93,252 +59,47 @@ public class StyleCreationDialog extends TrayDialog {
* inout
* @param selectorName
*/
- public StyleCreationDialog(Shell shell, Map<SelectorCondition, Boolean> conditions, Map<Declaration, Boolean> declarations, String selectorName, View context) {
- super(shell);
- this.conditions = conditions;
- this.declarations = declarations;
- this.selectorName = selectorName;
- this.contextView = context;
+ public StyleCreationDialog(Shell shell, Map<Attribute, Boolean> conditions, Map<Declaration, Boolean> declarations, String selectorName, View context) {
+ super(shell, conditions, declarations, selectorName, context);
}
@Override
public void create() {
super.create();
- Composite parent = getDialogArea();
- CTabFolder tabFolder = new CTabFolder(parent, SWT.BORDER);
- tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ Composite parent = getDialogArea();
- CTabItem conditionsTab = new CTabItem(tabFolder, SWT.NONE);
- CTabItem declarationsTab = new CTabItem(tabFolder, SWT.NONE);
CTabItem stylesheetTab = new CTabItem(tabFolder, SWT.NONE);
-
- conditionsTab.setText("Conditions");
- declarationsTab.setText("Properties");
stylesheetTab.setText("Stylesheet");
- Composite conditionsContainer = new Composite(tabFolder, SWT.NONE);
- conditionsContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
- conditionsContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
- conditionsContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
-
- conditionsTab.setControl(conditionsContainer);
-
- Composite declarationsContainer = new Composite(tabFolder, SWT.NONE);
- declarationsContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
- declarationsContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
- declarationsContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
-
- declarationsTab.setControl(declarationsContainer);
-
Composite stylesheetContainer = new Composite(tabFolder, SWT.NONE);
stylesheetContainer.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, true, true));
stylesheetContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
stylesheetContainer.setBackgroundMode(SWT.INHERIT_DEFAULT);
stylesheetTab.setControl(stylesheetContainer);
-
- createConditions(conditionsContainer);
- createDeclarations(declarationsContainer);
createStylesheet(stylesheetContainer);
- updateButtons();
-
- getShell().setText("New Style");
- getShell().pack();
- }
-
- protected void updateButtons() {
- //Resets the error message
- setError(null);
- getButton(IDialogConstants.OK_ID).setEnabled(isValid());
- getDialogArea().layout();
+ getShell().setText("Create a new style");
}
+ @Override
protected boolean isValid() {
boolean result = true;
//There must be a stylesheet
- if(getStylesheet() == null) {
+ if(getStyleSheet() == null) {
setError("You must select a Stylesheet");
result = false;
}
//EmbeddedStyleSheets are not yet supported
- if(getStylesheet() instanceof EmbeddedStyleSheet) {
+ if(getStyleSheet() instanceof EmbeddedStyleSheet) {
setError("Edition of embedded stylesheets is not yet supported. Please select an external stylesheet");
result = false;
}
- //There must be at least one property declaration
- boolean atLeastOneDeclaration = false;
- for(Boolean value : declarations.values()) {
- if(value) {
- atLeastOneDeclaration = true;
- break;
- }
- }
-
- result = result && atLeastOneDeclaration;
-
- if(!atLeastOneDeclaration) {
- setError("There must be at least one property declaration");
- }
-
- return result;
- }
-
- protected void setError(String errorMessage) {
- if(errorMessage == null && errorLabel != null) {
- errorLabel.dispose();
- errorLabel = null;
- return;
- }
-
- if(errorMessage != null) {
- if(errorLabel == null) {
- errorLabel = new CLabel(getDialogArea(), SWT.WRAP);
- errorLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
- errorLabel.setImage(org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImage("icons/error.gif"));
- }
-
- if(errorLabel.getText() != null && !errorLabel.getText().trim().equals("")) {
- errorLabel.setText(errorLabel.getText() + "\n" + errorMessage);
- } else {
- errorLabel.setText(errorMessage);
- }
- }
- }
-
- protected void createConditions(Composite parent) {
- parent.setLayout(new GridLayout(3, false));
-
- Label conditionsLabel = new Label(parent, SWT.WRAP);
- conditionsLabel.setText("Select the condition(s) under which the style will be applied.");
- conditionsLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
-
- createElementNameSection(parent);
- createAttributeSelectorsSection(parent);
- createStyleNameSection(parent);
- }
-
- protected void createElementNameSection(Composite parent) {
- EnumRadio diagramRestrictionWidget = new EnumRadio(parent, SWT.NONE, "Diagram:");
- diagramRestrictionWidget.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
-
- ILabelProvider labelProvider = new LabelProvider() {
-
- @Override
- public String getText(Object element) {
- Boolean value = (Boolean)element;
- return value ? contextView.getDiagram().getType() + " only" : "Any diagram";
- }
- };
-
- diagramRestrictionWidget.setProviders(new StaticContentProvider(new Boolean[]{ true, false }), labelProvider);
- diagramRestrictionWidget.setValue(this.diagramRestriction);
- diagramRestrictionWidget.addCommitListener(new ICommitListener() {
-
- public void commit(AbstractEditor editor) {
- diagramRestriction = (Boolean)((EnumRadio)editor).getValue();
- }
- });
-
- EnumRadio selectorNameWidget = new EnumRadio(parent, SWT.NONE, "Applies to:");
- selectorNameWidget.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
-
- labelProvider = new LabelProvider() {
-
- @Override
- public String getText(Object element) {
- Boolean value = (Boolean)element;
- return value ? selectorName + " only" : "Any kind of element";
- }
- };
-
- selectorNameWidget.setProviders(new StaticContentProvider(new Boolean[]{ true, false }), labelProvider);
- selectorNameWidget.setValue(this.useSelectorName);
- selectorNameWidget.addCommitListener(new ICommitListener() {
-
- public void commit(AbstractEditor editor) {
- useSelectorName = (Boolean)((EnumRadio)editor).getValue();
- }
- });
- }
-
- protected void createAttributeSelectorsSection(Composite parent) {
- if(conditions.isEmpty()) {
- return;
- }
-
- Label detailLabel = new Label(parent, SWT.NONE);
- detailLabel.setText("If the following properties are matched:");
- detailLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
-
- for(SelectorCondition condition : conditions.keySet()) {
- final Attribute currentCondition = (Attribute)condition;
-
- String attributeLabel = currentCondition.getName();
- if(currentCondition.getValue() != null) {
- attributeLabel += " " + currentCondition.getValue().getOperator() + " " + currentCondition.getValue().getValue();
- }
-
- BooleanCheckbox checkbox = new BooleanCheckbox(parent, SWT.NONE, attributeLabel);
-
- checkbox.addCommitListener(new ICommitListener() {
-
- public void commit(AbstractEditor editor) {
- conditions.put(currentCondition, ((BooleanCheckbox)editor).getValue());
- }
- });
- }
- }
-
- protected void createStyleNameSection(Composite parent) {
- Label styleNameLabel = new Label(parent, SWT.WRAP);
-
- String label = "If a name is used for this style, it will have to be applied manually:";
-
- styleNameLabel.setText(label);
- styleNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
-
- StringEditor styleNameEditor = new StringEditor(parent, SWT.NONE, "Style name:");
- styleNameEditor.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
- styleNameEditor.addCommitListener(new ICommitListener() {
-
- public void commit(AbstractEditor editor) {
- cssClass = (String)((StringEditor)editor).getValue();
- }
-
- });
- }
-
- protected void createDeclarations(Composite parent) {
- parent.setLayout(new GridLayout(3, false));
-
- Label declarationsLabel = new Label(parent, SWT.WRAP);
- declarationsLabel.setText("Select the properties you want to set. Unchecked properties will keep their default value (Which might be inherited from another style).");
- declarationsLabel.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 3, 1));
-
- for(Declaration declaration : declarations.keySet()) {
- String label = declaration.getProperty() + ": " + getLabel(declaration.getExpression());
- BooleanCheckbox checkbox = new BooleanCheckbox(parent, SWT.NONE, label);
-
- final Declaration currentDeclaration = declaration;
-
- checkbox.setValue(declarations.get(currentDeclaration));
-
- checkbox.addCommitListener(new ICommitListener() {
-
- public void commit(AbstractEditor editor) {
- boolean value = ((BooleanCheckbox)editor).getValue();
- declarations.put(currentDeclaration, value);
- updateButtons();
- }
- });
-
- checkbox.setValue(declarations.get(declaration));
- }
+ return super.isValid() && result;
}
protected void createStylesheet(Composite parent) {
@@ -445,74 +206,8 @@ public class StyleCreationDialog extends TrayDialog {
ignoreEvents = false;
}
- public String getCSSClass() {
- if(cssClass != null) {
- return cssClass.trim().equals("") ? null : cssClass.trim();
- }
- return null;
- }
-
- protected String getLabel(Expression expression) {
- String label = getLabel(expression.getTerms());
- for(Subterm subTerm : expression.getSubterms()) {
- if(subTerm.getOperator() != null) {
- label += subTerm.getOperator();
- }
- label += " " + getLabel(subTerm.getTerm());
- }
- return label;
- }
-
- protected String getLabel(Term term) {
- return (new CssSwitch<String>() {
-
- @Override
- public String caseHexColor(HexColor term) {
- return '#' + term.getValue();
- }
-
- @Override
- public String caseName(Name term) {
- return term.getValue();
- }
-
- @Override
- public String caseStringValue(StringValue term) {
- return "\"" + term.getValue() + "\"";
- }
-
- @Override
- public String caseNumber(Number term) {
- String label = "";
- if(term.getOp() != null) {
- label += term.getOp().getOperator();
- }
- label += term.getValue();
- return label;
- }
- }).doSwitch(term);
- }
-
- public StyleSheet getStylesheet() {
+ public StyleSheet getStyleSheet() {
return stylesheet;
}
- @Override
- protected boolean isResizable() {
- return true;
- }
-
- @Override
- public Composite getDialogArea() {
- return (Composite)super.getDialogArea();
- }
-
- public boolean useSelectorName() {
- return useSelectorName;
- }
-
- public boolean getDiagramRestriction() {
- return diagramRestriction;
- }
-
}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleEditionDialog.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleEditionDialog.java
new file mode 100644
index 00000000000..3c8207caa18
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/handler/StyleEditionDialog.java
@@ -0,0 +1,265 @@
+/*****************************************************************************
+ * 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.configuration.handler;
+
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.papyrus.infra.gmfdiag.css.Attribute;
+import org.eclipse.papyrus.infra.gmfdiag.css.CompositeSelector;
+import org.eclipse.papyrus.infra.gmfdiag.css.CssPackage;
+import org.eclipse.papyrus.infra.gmfdiag.css.Declaration;
+import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
+import org.eclipse.papyrus.infra.gmfdiag.css.Selector;
+import org.eclipse.papyrus.infra.gmfdiag.css.SelectorCondition;
+import org.eclipse.papyrus.infra.gmfdiag.css.SimpleSelector;
+import org.eclipse.papyrus.infra.gmfdiag.css.Stylesheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.configuration.providers.ExistingStyleContentProvider;
+import org.eclipse.papyrus.infra.gmfdiag.css.configuration.providers.StylesheetLabelProvider;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.SashForm;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Shell;
+
+
+/**
+ * A dialog for editing an existing CSS Style, based on a selected element
+ *
+ * @author Camille Letavernier
+ */
+//TODO: When a condition or property doesn't exist on the selected element,
+//but exists in the selected style, add it to #declarations/#conditions
+//TODO: Delete an existing style?
+//TODO: Use a combo-box + ListViewer instead of a TreeViewer
+//TODO: Remove read-only stylesheets (Or add a warning/Error when a ruleset from a readonly stylesheet is selected)
+//TODO: Load an external stylesheet (Which is not yet applied on the diagram)
+//TODO: Support embedded stylesheets
+public class StyleEditionDialog extends AbstractStyleDialog implements ISelectionChangedListener {
+
+ protected Composite styleSelectionPanel;
+
+ protected Composite styleEditionPanel;
+
+ protected Ruleset ruleset;
+
+ /**
+ *
+ * @param shell
+ * @param conditions
+ * inout
+ * @param declarations
+ * inout
+ * @param selectorName
+ */
+ public StyleEditionDialog(Shell shell, Map<Attribute, Boolean> conditions, Map<Declaration, Boolean> declarations, String selectorName, View context) {
+ super(shell, conditions, declarations, selectorName, context);
+ }
+
+ @Override
+ public void create() {
+ super.create();
+ createStyleSelectionPanel();
+ getShell().setText("Edit an existing style");
+ // getShell().pack();
+ }
+
+ @Override
+ public Composite createDialogArea(Composite parent) {
+ Composite dialogArea = (Composite)super.createDialogArea(parent);
+
+ SashForm sash = new SashForm(dialogArea, SWT.HORIZONTAL);
+ sash.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+
+ // GridLayout parentLayout = (GridLayout)dialogArea.getLayout();
+ // parentLayout.numColumns = 2;
+ // parentLayout.makeColumnsEqualWidth = false;
+
+ styleSelectionPanel = new Composite(sash, SWT.NONE);
+
+ GridData data = new GridData(SWT.BEGINNING, SWT.FILL, false, true);
+ data.widthHint = 250;
+ styleSelectionPanel.setLayoutData(data);
+ styleSelectionPanel.setLayout(new GridLayout(1, true));
+
+ styleEditionPanel = new Composite(sash, SWT.NONE);
+ styleEditionPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ GridLayout layout = new GridLayout(1, true);
+ // layout.marginWidth = 0;
+ // layout.marginHeight = 0;
+ styleEditionPanel.setLayout(layout);
+
+ sash.setWeights(new int[]{ 2, 5 });
+
+ return styleEditionPanel;
+ }
+
+ protected void createStyleSelectionPanel() {
+ // styleSelectionPanel.setBackground(styleSelectionPanel.getDisplay().getSystemColor(SWT.COLOR_WHITE));
+ // styleSelectionPanel.setBackgroundMode(SWT.INHERIT_DEFAULT);
+ TreeViewer viewer = new TreeViewer(styleSelectionPanel, SWT.BORDER);
+ viewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
+ viewer.setContentProvider(new ExistingStyleContentProvider(contextView));
+ viewer.setLabelProvider(new StylesheetLabelProvider());
+ viewer.addSelectionChangedListener(this);
+ viewer.setInput(new Object());
+ styleSelectionPanel.layout();
+ styleSelectionPanel.getParent().layout();
+ }
+
+ protected Stylesheet getStylesheet() {
+ if(ruleset != null) {
+ return (Stylesheet)ruleset.eContainer();
+ }
+ return null;
+ }
+
+ public Ruleset getSelectedRuleset() {
+ return ruleset;
+ }
+
+ public void selectionChanged(SelectionChangedEvent event) {
+ ISelection selection = event.getSelection();
+ if(selection.isEmpty()) {
+ //TODO: Conditions + Declaration -> Readonly
+ }
+
+ if(selection instanceof IStructuredSelection) {
+ IStructuredSelection sSelection = (IStructuredSelection)selection;
+ Object selectedElement = sSelection.getFirstElement();
+ if(selectedElement instanceof Ruleset) {
+ handleSelectionChanged((Ruleset)selectedElement);
+ } else {
+ ruleset = null;
+ }
+ }
+
+ updateButtons();
+ }
+
+ protected void handleSelectionChanged(Ruleset selectedRuleset) {
+ this.ruleset = selectedRuleset;
+
+ //Sets all conditions to false. They will be set to true if the selected style contains the same condition
+ for(Attribute attribute : conditions.keySet()) {
+ conditions.put(attribute, false);
+ }
+
+ //Sets all properties to false. They will be set to true if the selected style contains the same property
+ for(Declaration declaration : declarations.keySet()) {
+ declarations.put(declaration, false);
+ }
+
+ diagramRestriction = false;
+
+ useSelectorName = false;
+
+ cssClass = null;
+
+ for(Selector selector : selectedRuleset.getSelectors()) {
+ handleSelector(selector);
+ }
+
+ handleDeclarations(selectedRuleset.getProperties());
+
+ updateContents();
+ updateSelectorLabel();
+ }
+
+ protected void handleDeclarations(List<Declaration> declarations) {
+ for(Declaration declaration : declarations) {
+ for(Declaration currentDeclaration : this.declarations.keySet()) {
+ if(currentDeclaration.getProperty().equals(declaration.getProperty())) {
+ this.declarations.put(currentDeclaration, true);
+ break;
+ }
+ }
+ }
+ }
+
+ protected void handleSelector(Selector selector) {
+ if(selector instanceof SimpleSelector) {
+ handleSelector((SimpleSelector)selector);
+ } else if(selector instanceof CompositeSelector) {
+ handleSelector((CompositeSelector)selector);
+ }
+ }
+
+ protected void handleSelector(SimpleSelector selector) {
+ if(contextView.getDiagram().getType().equals(selector.getElementName())) {
+ diagramRestriction = true;
+ }
+
+ if(contextView.getElement().eClass().getName().equals(selector.getElementName())) {
+ useSelectorName = true;
+ }
+
+ for(SelectorCondition condition : selector.getCondition()) {
+ if(condition instanceof Attribute) {
+ Attribute existingAttribute = (Attribute)condition;
+ for(Attribute selectedAttribute : conditions.keySet()) {
+ if(existingAttribute.getName().equals(selectedAttribute.getName())) {
+ conditions.put(selectedAttribute, true);
+ break;
+ }
+ }
+ } else if(condition.eClass() == CssPackage.eINSTANCE.getClass_()) {
+ cssClass = ((org.eclipse.papyrus.infra.gmfdiag.css.Class)condition).getClass_();
+ }
+ }
+ }
+
+ protected void updateContents() {
+ disposeContents(conditionsContainer);
+ createConditions(conditionsContainer);
+
+ disposeContents(declarationsContainer);
+ createDeclarations(declarationsContainer);
+
+ conditionsContainer.layout();
+ declarationsContainer.layout();
+ tabFolder.layout();
+ getDialogArea().layout();
+ }
+
+ protected void disposeContents(Composite composite) {
+ for(Control control : composite.getChildren()) {
+ control.dispose();
+ }
+ }
+
+ protected void handleSelector(CompositeSelector selector) {
+ handleSelector(selector.getLeft());
+ handleSelector(selector.getRight());
+ }
+
+ @Override
+ protected boolean isValid() {
+ boolean result = true;
+
+ if(ruleset == null) {
+ setError("You must select an existing Style");
+ result = false;
+ }
+
+ return super.isValid() && result;
+ }
+
+}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/helper/XtextStylesheetHelper.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/helper/XtextStylesheetHelper.java
new file mode 100644
index 00000000000..2580fdae5da
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/helper/XtextStylesheetHelper.java
@@ -0,0 +1,111 @@
+/*****************************************************************************
+ * 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.configuration.helper;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.xtext.resource.XtextResourceSet;
+
+/**
+ * A helper for XText stylesheets
+ *
+ * @author Camille Letavernier
+ *
+ */
+public class XtextStylesheetHelper {
+
+ /**
+ * Loads and returns a Resource containing an XText Stylesheet
+ *
+ * @param stylesheet
+ * The object representing the Stylesheet's path or contents
+ * @param resourceSet
+ * The resourceSet in which the resource will be loaded. May be null.
+ * @param contextView
+ * The context view. If the stylesheet's path is relative, it will be resolved against this view's URI.
+ * @param parentShell
+ * The shell used to open error dialogs. May be null
+ * @return
+ * A resource containing the loaded stylesheet, or null if the stylesheet could not be resolved or is invalid
+ */
+ public static Resource loadStylesheet(StyleSheet stylesheet, ResourceSet resourceSet, View contextView, Shell parentShell) {
+ if(stylesheet instanceof StyleSheetReference) {
+ return loadStylesheet(stylesheet, resourceSet, contextView, parentShell);
+ } else if(stylesheet instanceof EmbeddedStyleSheet) {
+ //TODO: Edit EmbeddedStyleSheets with XText
+ throw new UnsupportedOperationException();
+ }
+ return null;
+ }
+
+ /**
+ * Loads and returns a Resource containing an XText Stylesheet
+ *
+ * @param stylesheet
+ * The object representing the Stylesheet's path
+ * @param resourceSet
+ * The resourceSet in which the resource will be loaded. May be null.
+ * @param contextView
+ * The context view. If the stylesheet's path is relative, it will be resolved against this view's URI.
+ * @param parentShell
+ * The shell used to open error dialogs. May be null
+ * @return
+ * A resource containing the loaded stylesheet, or null if the stylesheet could not be resolved or is invalid
+ */
+ public static Resource loadStylesheet(StyleSheetReference stylesheet, ResourceSet resourceSet, View contextView, Shell parentShell) {
+ if(resourceSet == null) {
+ resourceSet = new XtextResourceSet();
+ }
+
+ //Supported pathes:
+ // /<plugin>/path/file.css (Workspace, platform:/resource/)
+ // relative/path/file.css (Relative)
+ String path = stylesheet.getPath();
+ if(!path.endsWith(".css")) {
+ //FIXME: The X-Text editor supports CSS Content Types, but it is not yet configured
+ //to automatically accept other extensions than *.css
+ //The parser should rely on the file's content type instead of the file extension
+ //Attemps to serialize to a custom extension (even an extension compatible with CSS Content Type)
+ //would result in xmi serialization. We'd better stop here.
+ if(parentShell != null) {
+ MessageDialog.open(MessageDialog.ERROR, parentShell, "Stylesheet error", "The stylesheet must have the *.css extension", SWT.NONE);
+ }
+ return null;
+ }
+
+ URI uri;
+ if(path.startsWith("/")) {
+ uri = URI.createPlatformResourceURI(path, true);
+ } else {
+ uri = URI.createURI(path);
+ uri = uri.resolve(contextView.eResource().getURI());
+ }
+
+ Resource resource;
+
+ try {
+ resource = resourceSet.getResource(uri, true);
+ } catch (Exception ex) {
+ resource = resourceSet.createResource(uri, "org.eclipse.wst.css.core.csssource"); //$NON-NLS-1$
+ }
+
+ return resource;
+ }
+}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/ExistingStyleContentProvider.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/ExistingStyleContentProvider.java
new file mode 100644
index 00000000000..291c328a865
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/ExistingStyleContentProvider.java
@@ -0,0 +1,129 @@
+/*****************************************************************************
+ * 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.configuration.providers;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.papyrus.infra.gmfdiag.css.Content;
+import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
+import org.eclipse.papyrus.infra.gmfdiag.css.Stylesheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.configuration.helper.XtextStylesheetHelper;
+import org.eclipse.papyrus.infra.gmfdiag.css.notation.CSSDiagram;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
+import org.eclipse.papyrus.infra.widgets.providers.IHierarchicContentProvider;
+
+
+public class ExistingStyleContentProvider implements IHierarchicContentProvider {
+
+ protected Map<StyleSheet, Stylesheet> stylesheets;
+
+ protected final View context;
+
+ public ExistingStyleContentProvider(View context) {
+ this.context = context;
+ }
+
+ public Object[] getElements(Object inputElement) {
+ return getStyleSheets().toArray();
+ }
+
+ public Object[] getChildren(Object parentElement) {
+ if(parentElement instanceof StyleSheet) {
+ Stylesheet xtextStylesheet = stylesheets.get(parentElement);
+
+ List<Ruleset> result = new LinkedList<Ruleset>();
+
+ for(Content stylesheetElement : xtextStylesheet.getContents()) {
+ if(stylesheetElement instanceof Ruleset) {
+ result.add((Ruleset)stylesheetElement);
+ }
+ }
+
+ return result.toArray();
+ } else {
+ return new Object[0];
+ }
+ }
+
+ public Object getParent(Object element) {
+ if(element instanceof Stylesheet) {
+ return null;
+ }
+
+ if(element instanceof Ruleset) {
+ return ((Ruleset)element).eContainer();
+ }
+
+ return null;
+ }
+
+ public boolean hasChildren(Object element) {
+ return getChildren(element).length > 0;
+ }
+
+ public void dispose() {
+
+ }
+
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+
+ }
+
+ public boolean isValidValue(Object element) {
+ return element instanceof Ruleset;
+ }
+
+ protected Collection<StyleSheet> getStyleSheets() {
+ if(stylesheets == null) {
+ stylesheets = new LinkedHashMap<StyleSheet, Stylesheet>();
+ if(context.getDiagram() instanceof CSSDiagram) {
+ CSSDiagram diagram = (CSSDiagram)context.getDiagram();
+ parseStyleSheets(diagram.getStyleSheets());
+ }
+ }
+ return stylesheets.keySet();
+ }
+
+ protected void parseStyleSheets(List<StyleSheet> appliedStylesheets) {
+ for(StyleSheet stylesheet : appliedStylesheets) {
+ if(stylesheet instanceof StyleSheetReference) {
+ parseStyleSheet((StyleSheetReference)stylesheet);
+ } else if(stylesheet instanceof EmbeddedStyleSheet) {
+ //Unsupported yet
+ }
+ }
+ }
+
+ protected void parseStyleSheet(StyleSheetReference stylesheet) {
+ Resource resource = XtextStylesheetHelper.loadStylesheet(stylesheet, null, context, null);
+ if(resource.getContents().isEmpty()) {
+ return;
+ }
+
+ for(EObject rootElement : resource.getContents()) {
+ if(rootElement instanceof Stylesheet) {
+ stylesheets.put(stylesheet, (Stylesheet)rootElement);
+ }
+ }
+ }
+
+}
diff --git a/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/StylesheetLabelProvider.java b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/StylesheetLabelProvider.java
new file mode 100644
index 00000000000..19ab6c6b34a
--- /dev/null
+++ b/plugins/infra/gmfdiag/css/org.eclipse.papyrus.infra.gmfdiag.css.configuration/src/org/eclipse/papyrus/infra/gmfdiag/css/configuration/providers/StylesheetLabelProvider.java
@@ -0,0 +1,96 @@
+/*****************************************************************************
+ * 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.configuration.providers;
+
+import org.eclipse.papyrus.infra.emf.providers.EMFLabelProvider;
+import org.eclipse.papyrus.infra.gmfdiag.CSSRuntimeModule;
+import org.eclipse.papyrus.infra.gmfdiag.css.Ruleset;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.EmbeddedStyleSheet;
+import org.eclipse.papyrus.infra.gmfdiag.css.stylesheets.StyleSheetReference;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.xtext.serializer.ISerializer;
+
+import com.google.inject.Guice;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+
+//TODO: Add a context to this LabelProvider (Selected View)
+//TODO: When a Ruleset is applied/applicable on the selected View, change its color
+public class StylesheetLabelProvider extends EMFLabelProvider {
+
+ @Inject
+ private ISerializer serializer;
+
+ public StylesheetLabelProvider() {
+ Injector injector = Guice.createInjector(new CSSRuntimeModule());
+ injector.injectMembers(this);
+ }
+
+ @Override
+ public String getText(Object element) {
+ if(element instanceof Ruleset) {
+ return getText((Ruleset)element);
+ }
+
+ if(element instanceof StyleSheetReference) {
+ return getText((StyleSheetReference)element);
+ }
+
+ if(element instanceof EmbeddedStyleSheet) {
+ return getText((EmbeddedStyleSheet)element);
+ }
+
+ return super.getText(element);
+ }
+
+ public String getText(Ruleset ruleset) {
+ //Delegate the label to XText serialization
+ String label = "";
+
+ if(!ruleset.getSelectors().isEmpty()) {
+ label += serializer.serialize(ruleset.getSelectors().get(0));
+ for(int i = 1; i < ruleset.getSelectors().size(); i++) {
+ label += ", " + serializer.serialize(ruleset.getSelectors().get(i));
+ }
+ label = label.trim().replaceAll("\\s+", " ");
+ }
+
+ return label;
+ }
+
+ public String getText(StyleSheetReference stylesheet) {
+ return stylesheet.getPath();
+ }
+
+ public String getText(EmbeddedStyleSheet stylesheet) {
+ return stylesheet.getLabel();
+ }
+
+ @Override
+ public Color getForeground(Object element) {
+ if(!(element instanceof Ruleset)) {
+ return Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
+ }
+ return super.getForeground(element);
+ }
+
+ @Override
+ public Image getImage(Object element) {
+ // if(element instanceof StyleSheetReference) {
+ // return org.eclipse.papyrus.infra.widgets.Activator.getDefault().getImage(Activator.PLUGIN_ID, "/icons/sourceEditor.gif");
+ // }
+ return null;
+ }
+}

Back to the top