From f496fc6b64657e29c80f8409e28bcf7f613004a5 Mon Sep 17 00:00:00 2001
From: Andreas Bayha <bayha@fortiss.org>
Date: Tue, 13 Oct 2020 15:08:53 +0200
Subject: [PATCH] Annotations: Factored out AnnotationsFXUtils

Background color logic is now factored out of the UI provider an
reusable.

Issue-Ref: 4014
Issue-Url: https://af3-developer.fortiss.org/issues/4014
Signed-off-by: Andreas Bayha <bayha@fortiss.org>
---
 .../base/ui/annotation/view/fx/.ratings       |   1 +
 .../view/fx/AnnotationViewFXController.java   |  31 +----
 .../view/fx/AnnotationsFXUtils.java           | 106 ++++++++++++++++++
 3 files changed, 113 insertions(+), 25 deletions(-)
 create mode 100644 org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationsFXUtils.java

diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/.ratings b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/.ratings
index 565ccc5a9..08c70fbac 100644
--- a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/.ratings
+++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/.ratings
@@ -1,4 +1,5 @@
 AnnotationFxViewPart.java ca1548c49aa3842a9436262531464ba345b83688 YELLOW
 AnnotationViewFXController.java 3618e62b47054754708a5e6e6df6895757816d84 YELLOW
+AnnotationsFXUtils.java f34cfa95b26282d86c7425b7ab05f3c5d91feb80 RED
 ColumnHandle.java 761c2517b3f3e4725feb7ce7e8d5927ba191a4bb YELLOW
 FXAnnotationFilterContentProvider.java 90620e61e5c91669da26c0564527e0b4a31e4b9d YELLOW
diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationViewFXController.java b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationViewFXController.java
index 21041ec4f..3acca7e9f 100644
--- a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationViewFXController.java
+++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationViewFXController.java
@@ -18,9 +18,8 @@ package org.fortiss.tooling.base.ui.annotation.view.fx;
 import static java.util.Collections.emptyList;
 import static java.util.Collections.sort;
 import static java.util.stream.Collectors.toList;
-import static javafx.scene.paint.Color.BISQUE;
-import static javafx.scene.paint.Color.LIGHTGREY;
-import static javafx.scene.paint.Color.LIGHTSEAGREEN;
+import static org.fortiss.tooling.base.ui.annotation.view.fx.AnnotationsFXUtils.SELECTED_ANNOTATION_BACKGROUND_COLOR;
+import static org.fortiss.tooling.base.ui.annotation.view.fx.AnnotationsFXUtils.getBackgroundColorForEntry;
 import static org.fortiss.tooling.base.ui.annotation.view.fx.FXAnnotationFilterContentProvider.HIERARCHY_LEVELS_ALL;
 import static org.fortiss.tooling.base.ui.annotation.view.fx.FXAnnotationFilterContentProvider.HIERARCHY_LEVELS_CURRENT;
 import static org.fortiss.tooling.base.ui.annotation.view.fx.FXAnnotationFilterContentProvider.HIERARCHY_LEVELS_SELECTED_SUBMODEL;
@@ -87,15 +86,6 @@ public class AnnotationViewFXController extends CompositeFXControllerBase<SplitP
 	/** Option text for the annotation type filter combo box. */
 	private static final String SHOW_ALL_ANNOTATION_TYPES = "Show all annotation types";
 
-	/** Background color for table cells, which are not editable and empty. */
-	public static final Color READ_ONLY_EMPTY_ANNOTATION_BACKGROUND_COLOR = LIGHTGREY;
-
-	/** Background color for table cells, which are not editable. */
-	public static final Color READ_ONLY_ANNOTATION_BACKGROUND_COLOR = BISQUE;
-
-	/** Background color for table cells of the selected element. */
-	protected static final Color SELECTED_ANNOTATION_BACKGROUND_COLOR = LIGHTSEAGREEN;
-
 	/** {@link TextField} for entering the filter pattern. */
 	@FXML
 	private TextField txtFilterText;
@@ -517,22 +507,13 @@ public class AnnotationViewFXController extends CompositeFXControllerBase<SplitP
 		@Override
 		public Color getBackgroundColor(int column, AnnotationEntry element) {
 			if(element.getModelElement().equals(selected)) {
-				return READ_ONLY_EMPTY_ANNOTATION_BACKGROUND_COLOR;
+				return SELECTED_ANNOTATION_BACKGROUND_COLOR;
 			}
 
 			if(column > 1 && colIdxAnnotationMap.containsKey(column)) {
-				IAnnotatedSpecification spec =
-						colIdxAnnotationMap.get(column).getAnnotatedSpecification();
-				IAnnotationValueProvider<IAnnotatedSpecification> valueProvider =
-						element.getAnnotationValueProvider(spec.getClass());
-				if(valueProvider != null && valueProvider.getAnnotationValue(spec) != null &&
-						!isEditable(column, element)) {
-					return READ_ONLY_ANNOTATION_BACKGROUND_COLOR;
-				}
-
-				if(!isEditable(column, element)) {
-					return READ_ONLY_ANNOTATION_BACKGROUND_COLOR;
-				}
+				Class<IAnnotatedSpecification> specType =
+						colIdxAnnotationMap.get(column).getAnnotatedSpecificationType();
+				return getBackgroundColorForEntry(element, specType);
 			}
 
 			return super.getBackgroundColor(column, element);
diff --git a/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationsFXUtils.java b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationsFXUtils.java
new file mode 100644
index 000000000..81b8886fb
--- /dev/null
+++ b/org.fortiss.tooling.base.ui/src/org/fortiss/tooling/base/ui/annotation/view/fx/AnnotationsFXUtils.java
@@ -0,0 +1,106 @@
+/*-------------------------------------------------------------------------+
+| Copyright 2020 fortiss GmbH                                              |
+|                                                                          |
+| Licensed under the Apache License, Version 2.0 (the "License");          |
+| you may not use this file except in compliance with the License.         |
+| You may obtain a copy of the License at                                  |
+|                                                                          |
+|    http://www.apache.org/licenses/LICENSE-2.0                            |
+|                                                                          |
+| Unless required by applicable law or agreed to in writing, software      |
+| distributed under the License is distributed on an "AS IS" BASIS,        |
+| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+| See the License for the specific language governing permissions and      |
+| limitations under the License.                                           |
++--------------------------------------------------------------------------*/
+package org.fortiss.tooling.base.ui.annotation.view.fx;
+
+import static javafx.scene.paint.Color.BISQUE;
+import static javafx.scene.paint.Color.LIGHTGREY;
+import static javafx.scene.paint.Color.LIGHTSEAGREEN;
+import static javafx.scene.paint.Color.WHITE;
+import static org.eclipse.swt.widgets.Display.getCurrent;
+
+import org.eclipse.swt.graphics.Device;
+import org.fortiss.tooling.base.annotation.AnnotationEntry;
+import org.fortiss.tooling.base.annotation.valueprovider.IAnnotationValueProvider;
+import org.fortiss.tooling.base.model.element.IAnnotatedSpecification;
+
+import javafx.scene.paint.Color;
+
+/**
+ * Utils for the FX implementation of the annotations view.
+ * 
+ * @author bayha
+ */
+public class AnnotationsFXUtils {
+
+	/** Background color for table cells, which are not editable and empty. */
+	public static final Color READ_ONLY_EMPTY_ANNOTATION_BACKGROUND_COLOR = LIGHTGREY;
+
+	/** SWT background color for table cells, which are not editable and empty. */
+	@Deprecated
+	public static final org.eclipse.swt.graphics.Color READ_ONLY_EMPTY_ANNOTATION_BACKGROUND_COLOR_SWT =
+			convertFXColortoSWT(READ_ONLY_EMPTY_ANNOTATION_BACKGROUND_COLOR);
+
+	/** Background color for table cells, which are not editable. */
+	public static final Color READ_ONLY_ANNOTATION_BACKGROUND_COLOR = BISQUE;
+
+	/** Background color for table cells of the selected element. */
+	protected static final Color SELECTED_ANNOTATION_BACKGROUND_COLOR = LIGHTSEAGREEN;
+
+	/**
+	 * @param annotationEntry
+	 * @param annotationType
+	 * @return
+	 */
+	public static Color getBackgroundColorForEntry(AnnotationEntry annotationEntry,
+			Class<? extends IAnnotatedSpecification> annotationType) {
+		IAnnotationValueProvider<IAnnotatedSpecification> valueProvider =
+				annotationEntry.getAnnotationValueProvider(annotationType);
+		IAnnotatedSpecification specification = annotationEntry.getSpecification(annotationType);
+
+		if(valueProvider != null && valueProvider.getAnnotationValue(specification) != null &&
+				!valueProvider.canEdit(specification)) {
+			return READ_ONLY_ANNOTATION_BACKGROUND_COLOR;
+		}
+
+		if(valueProvider == null || !valueProvider.canEdit(specification)) {
+			return READ_ONLY_EMPTY_ANNOTATION_BACKGROUND_COLOR;
+		}
+
+		return WHITE;
+	}
+
+	/**
+	 * @param annotationEntry
+	 * @param annotationType
+	 * @param defaultColor
+	 * @return
+	 */
+	@Deprecated
+	public static org.eclipse.swt.graphics.Color getSWTBackgroundColorForEntry(
+			AnnotationEntry annotationEntry,
+			Class<? extends IAnnotatedSpecification> annotationType,
+			org.eclipse.swt.graphics.Color defaultColor) {
+		Color bgCol = getBackgroundColorForEntry(annotationEntry, annotationType);
+
+		if(bgCol.equals(WHITE)) {
+			return defaultColor;
+		}
+
+		return convertFXColortoSWT(bgCol);
+	}
+
+	/**
+	 * @param bgCol
+	 * @return
+	 */
+	private static org.eclipse.swt.graphics.Color convertFXColortoSWT(Color bgCol) {
+		Device device = getCurrent();
+		int red = (int)bgCol.getRed();
+		int green = (int)bgCol.getGreen();
+		int blue = (int)bgCol.getBlue();
+		return new org.eclipse.swt.graphics.Color(device, red, green, blue);
+	}
+}
-- 
GitLab