From 127aab5e39a094981de197bd2a8c09b608fb4bdb Mon Sep 17 00:00:00 2001
From: Daniel Ratiu <ratiu@fortiss.org>
Date: Thu, 20 Oct 2011 14:37:03 +0000
Subject: [PATCH] small code cleanings refs 236

---
 .../ui/util/ModelElementLabelProvider.java    |  3 +-
 .../kernel/ui/util/SelectionUtils.java        | 74 +++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java

diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java
index 14bf57e7b..96c0225d1 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/ModelElementLabelProvider.java
@@ -24,11 +24,12 @@ import org.fortiss.tooling.kernel.ui.extension.IModelElementHandler;
 import org.fortiss.tooling.kernel.ui.service.IModelElementHandlerService;
 
 /**
+ * Label provider for model elements that have corresponding handlers.
  * 
  * @author mou
  * @author $Author$
  * @version $Rev$
- * @ConQAT.Rating RED Hash:
+ * @ConQAT.Rating GREEN Hash: 7C9C1135B33D4F3D58BEB826E63AB70A
  */
 public class ModelElementLabelProvider extends LabelProvider {
 	/** {@inheritDoc} */
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java
new file mode 100644
index 000000000..76b950055
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/util/SelectionUtils.java
@@ -0,0 +1,74 @@
+/*--------------------------------------------------------------------------+
+$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
+|                                                                          |
+| Copyright 2011 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.kernel.ui.util;
+
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.ISelectionProvider;
+import org.eclipse.jface.viewers.IStructuredSelection;
+
+/**
+ * Utility methods for dealing with selections.
+ * 
+ * This class contains slight improvements with respect to
+ * {@link org.conqat.ide.commons.ui.selection.SelectionUtils}.
+ * 
+ * @author ratiu
+ * @author $Author: hoelzl $
+ * @version $Rev: 18709 $
+ * @ConQAT.Rating YELLOW Hash: 9641C2CB35796514DC1145D3DA0570B5
+ */
+public class SelectionUtils {
+
+	/**
+	 * For a selection provider object get the selected object, for simple
+	 * selections a cast is performed to the desired type; for a structured
+	 * selection returns the first element or <code>null</code> if the selection
+	 * is empty. This return <code>null</code> if the provided selection is null
+	 * or the element is not of the required type.
+	 */
+	public static <T> T checkAndPickFirst(ISelectionProvider selectionProvider,
+			Class<T> type) {
+		return checkAndPickFirst(selectionProvider.getSelection(), type);
+	}
+
+	/**
+	 * For simple selections a cast is performed to the desired type. For a
+	 * structured selection returns the first element or <code>null</code> if
+	 * the selection is empty. This return <code>null</code> if the provided
+	 * selection is null or the element is not of the required type.
+	 */
+	@SuppressWarnings("unchecked")
+	public static <T> T checkAndPickFirst(ISelection selection, Class<T> type) {
+		Object selectedElement;
+
+		if (selection == null || selection.isEmpty())
+			return null;
+
+		if (!(selection instanceof IStructuredSelection)) {
+			selectedElement = selection;
+		} else {
+			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
+			selectedElement = structuredSelection.getFirstElement();
+		}
+
+		if (!type.isInstance(selectedElement)) {
+			return null;
+		}
+		return (T) selectedElement;
+	}
+}
-- 
GitLab