Bug 382928 - Introduce factory method(s) for easier gradient creation
Change-Id: Ie70f26dbcc43fbb0f3762072170b228d70846135
diff --git a/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemFeatureProvider.java b/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemFeatureProvider.java
index fa71605..8141a4b 100644
--- a/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemFeatureProvider.java
+++ b/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemFeatureProvider.java
@@ -9,22 +9,35 @@
  * 

  * Contributors:

  *    SAP AG - initial API, implementation and documentation

+ *    cbrand - Bug 382928 - Introduce factory method(s) for easier gradient creation

  * 

  * </copyright>

  */

 package org.eclipse.graphiti.examples.filesystem.diagram;

 

+import java.util.ArrayList;

+import java.util.Arrays;

+import java.util.List;

+

 import org.eclipse.graphiti.dt.IDiagramTypeProvider;

 import org.eclipse.graphiti.examples.filesystem.features.AddContainmentConnectionFeature;

 import org.eclipse.graphiti.examples.filesystem.features.CreateContainmentConnectionFeature;

+import org.eclipse.graphiti.examples.filesystem.features.GradientColorFeature;

 import org.eclipse.graphiti.examples.filesystem.patterns.FolderPattern;

+import org.eclipse.graphiti.examples.filesystem.ui.FilesystemPredefinedColoredAreas;

 import org.eclipse.graphiti.features.IAddFeature;

 import org.eclipse.graphiti.features.ICreateConnectionFeature;

 import org.eclipse.graphiti.features.context.IAddConnectionContext;

 import org.eclipse.graphiti.features.context.IAddContext;

+import org.eclipse.graphiti.features.context.ICustomContext;

+import org.eclipse.graphiti.features.custom.ICustomFeature;

 import org.eclipse.graphiti.pattern.DefaultFeatureProviderWithPatterns;

+import org.eclipse.graphiti.util.IPredefinedRenderingStyle;

 

 public class FilesystemFeatureProvider extends DefaultFeatureProviderWithPatterns {

+	static List<String> ALL_GRADIENT_IDS = Arrays.asList(FilesystemPredefinedColoredAreas.GREEN_WHITE_ID,

+			FilesystemPredefinedColoredAreas.RED_WHITE_ID, IPredefinedRenderingStyle.BLUE_WHITE_ID,

+			IPredefinedRenderingStyle.BLUE_WHITE_GLOSS_ID);

 

 	public FilesystemFeatureProvider(IDiagramTypeProvider dtp) {

 		super(dtp);

@@ -43,4 +56,15 @@
 		}

 		return super.getAddFeature(context);

 	}

-}

+

+	@Override

+	public ICustomFeature[] getCustomFeatures(ICustomContext context) {

+		ICustomFeature[] ret = super.getCustomFeatures(context);

+		List<ICustomFeature> retList = new ArrayList<ICustomFeature>();

+		for (String gid : ALL_GRADIENT_IDS) {

+			retList.add(new GradientColorFeature(this, gid));

+		}

+		ret = retList.toArray(ret);

+		return ret;

+	}

+}
\ No newline at end of file
diff --git a/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemToolBehaviorProvider.java b/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemToolBehaviorProvider.java
index 1323511..f62d3b2 100644
--- a/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemToolBehaviorProvider.java
+++ b/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/diagram/FilesystemToolBehaviorProvider.java
@@ -9,16 +9,25 @@
  * 

  * Contributors:

  *    SAP AG - initial API, implementation and documentation

+ *    cbrand - Bug 382928 - Introduce factory method(s) for easier gradient creation

  * 

  * </copyright>

  */

 package org.eclipse.graphiti.examples.filesystem.diagram;

 

+import java.util.ArrayList;

+import java.util.List;

+

 import org.eclipse.graphiti.dt.IDiagramTypeProvider;

+import org.eclipse.graphiti.examples.filesystem.features.GradientColorFeature;

 import org.eclipse.graphiti.examples.mm.filesystem.Folder;

+import org.eclipse.graphiti.features.context.ICustomContext;

+import org.eclipse.graphiti.features.custom.ICustomFeature;

 import org.eclipse.graphiti.mm.pictograms.PictogramElement;

 import org.eclipse.graphiti.mm.pictograms.Shape;

+import org.eclipse.graphiti.tb.ContextMenuEntry;

 import org.eclipse.graphiti.tb.DefaultToolBehaviorProvider;

+import org.eclipse.graphiti.tb.IContextMenuEntry;

 import org.eclipse.graphiti.tb.IToolBehaviorProvider;

 import org.eclipse.graphiti.util.ILocationInfo;

 import org.eclipse.graphiti.util.LocationInfo;

@@ -37,4 +46,32 @@
 		}

 		return super.getLocationInfo(pe, locationInfo);

 	}

+

+	@Override

+	public IContextMenuEntry[] getContextMenu(ICustomContext context) {

+		IContextMenuEntry[] ret = NO_CONTEXT_MENU_ENTRIES;

+		List<IContextMenuEntry> retList = new ArrayList<IContextMenuEntry>();

+		// custom features

+		ICustomContext customContext = context;

+		ICustomFeature[] customFeatures = getFeatureProvider().getCustomFeatures(customContext);

+

+		// menu groups

+		ContextMenuEntry changeGradientColorEntry = null;

+

+		for (int i = 0; i < customFeatures.length; i++) {

+			ICustomFeature customFeature = customFeatures[i];

+			ContextMenuEntry contextMenuEntry = new ContextMenuEntry(customFeature, context);

+			if (customFeature instanceof GradientColorFeature) {

+				if (changeGradientColorEntry == null) {

+					changeGradientColorEntry = new ContextMenuEntry(null, null);

+					changeGradientColorEntry.setSubmenu(true);

+					changeGradientColorEntry.setText("Gradient Color");

+					changeGradientColorEntry.setDescription("Change Gradient Color");

+					retList.add(changeGradientColorEntry);

+				}

+				changeGradientColorEntry.add(contextMenuEntry);

+			}

+		}

+		return retList.toArray(ret);

+	}

 }

diff --git a/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/features/GradientColorFeature.java b/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/features/GradientColorFeature.java
new file mode 100644
index 0000000..6d1d845
--- /dev/null
+++ b/examples/org.eclipse.graphiti.examples.filesystem/src/org/eclipse/graphiti/examples/filesystem/features/GradientColorFeature.java
@@ -0,0 +1,107 @@
+/*******************************************************************************
+ * <copyright>
+ *
+ * Copyright (c) 2012 SAP AG.
+ * 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:
+ *    SAP AG - initial API, implementation and documentation
+ *    cbrand - Bug 382928 - Introduce factory method(s) for easier gradient creation
+ *
+ * </copyright>
+ *
+ *******************************************************************************/
+/*
+ * Created on 8/31/2012
+ */
+package org.eclipse.graphiti.examples.filesystem.features;
+
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.graphiti.examples.filesystem.ui.FilesystemPredefinedColoredAreas;
+import org.eclipse.graphiti.features.IFeatureProvider;
+import org.eclipse.graphiti.features.context.ICustomContext;
+import org.eclipse.graphiti.features.custom.AbstractCustomFeature;
+import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm;
+import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
+import org.eclipse.graphiti.mm.algorithms.styles.AdaptedGradientColoredAreas;
+import org.eclipse.graphiti.mm.pictograms.PictogramElement;
+import org.eclipse.graphiti.services.Graphiti;
+
+/**
+ * The Class GradientColorFeature.
+ */
+public class GradientColorFeature extends AbstractCustomFeature {
+
+	private static final String DESCRIPTION = "Select Gradient";
+	private final String gradientId;
+
+	/**
+	 * The Constructor.
+	 * 
+	 * @param fp
+	 *            the fp
+	 */
+	public GradientColorFeature(IFeatureProvider fp, String gradientId) {
+		super(fp);
+		this.gradientId = gradientId;
+	}
+
+	@Override
+	public String getDescription() {
+		return DESCRIPTION + ": " + gradientId;
+	}
+
+	@Override
+	public String getName() {
+		return gradientId;
+	}
+
+	@Override
+	public boolean canExecute(ICustomContext context) {
+		AdaptedGradientColoredAreas coloredAreas = FilesystemPredefinedColoredAreas
+				.getAdaptedGradientColoredAreas(gradientId);
+		if (coloredAreas == null) {
+			return false;
+		}
+
+		PictogramElement[] pes = context.getPictogramElements();
+		if (pes != null && pes.length >= 1) {
+			GraphicsAlgorithm ga = pes[0].getGraphicsAlgorithm();
+			if (ga != null) {
+				for (GraphicsAlgorithm innerGa : ga.getGraphicsAlgorithmChildren()) {
+					if (innerGa == null) {
+						return false;
+					}
+				}
+				return true;
+			}
+		}
+		return false;
+	}
+
+	public void execute(ICustomContext context) {
+		PictogramElement[] pes = context.getPictogramElements();
+		if (pes != null && pes.length >= 1) {
+			for (int i = 0; i < pes.length; i++) {
+				PictogramElement pe = pes[i];
+				GraphicsAlgorithm currentGa = pe.getGraphicsAlgorithm();
+
+				// change color of inner GAs instead of the outer invsible one
+				EList<GraphicsAlgorithm> gaChildren = currentGa.getGraphicsAlgorithmChildren();
+				for (GraphicsAlgorithm innerGa : gaChildren) {
+					// only the rectangles, not the text GA
+					if (innerGa instanceof RoundedRectangle) {
+						// each GA has to have colored areas of his own; reason:
+						// aggregation in the metamodel
+						AdaptedGradientColoredAreas ca = FilesystemPredefinedColoredAreas
+								.getAdaptedGradientColoredAreas(gradientId);
+						Graphiti.getGaService().setRenderingStyle(innerGa, ca);
+					}
+				}
+			}
+		}
+	}
+}
\ No newline at end of file