From c65c550062a84306745891a8220c6f1af58cf24e Mon Sep 17 00:00:00 2001
From: Dongyue Mou <mou@fortiss.org>
Date: Mon, 20 Feb 2012 14:04:47 +0000
Subject: [PATCH] fixed random testing refs 283

---
 .../base/ui/editor/TreeViewerEditorBase.java  | 107 ++++++++++++++++++
 1 file changed, 107 insertions(+)
 create mode 100644 org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TreeViewerEditorBase.java

diff --git a/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TreeViewerEditorBase.java b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TreeViewerEditorBase.java
new file mode 100644
index 000000000..7378d09ea
--- /dev/null
+++ b/org.fortiss.tooling.base.ui/trunk/src/org/fortiss/tooling/base/ui/editor/TreeViewerEditorBase.java
@@ -0,0 +1,107 @@
+/*--------------------------------------------------------------------------+
+$Id$
+|                                                                          |
+| 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.base.ui.editor;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.emf.common.notify.Notification;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.util.EContentAdapter;
+import org.eclipse.jface.viewers.TreeViewer;
+import org.eclipse.swt.widgets.Composite;
+import org.fortiss.tooling.base.ui.dnd.jface.ViewerElementCompositionDropAdapter;
+import org.fortiss.tooling.kernel.ui.extension.base.EditorBase;
+import org.fortiss.tooling.kernel.ui.util.EObjectSelectionUtils;
+
+/**
+ * An editor which only consists of a tree viewer. It uses {@link TreeViewer},
+ * which also allows to define columns, i.e., use the tree and table at the same
+ * time.
+ * 
+ * @author hoelzl
+ * @author $Author$
+ * @version $Rev$
+ * @ConQAT.Rating YELLOW Hash: 532479EDC4922A50828399F6761787F3
+ */
+public abstract class TreeViewerEditorBase<T extends EObject> extends
+		EditorBase<T> {
+
+	/** Stores the {@link TreeViewer}. */
+	private TreeViewer treeViewer;
+
+	/** The adapter used for dealing with changes of the */
+	private final Adapter editedObjectChanged = new EContentAdapter() {
+		@Override
+		public void notifyChanged(Notification notification) {
+			super.notifyChanged(notification);
+			if (notification.getEventType() != Notification.REMOVING_ADAPTER
+					&& !treeViewer.getTree().isDisposed()) {
+				treeViewer.refresh(true);
+			}
+		}
+	};
+
+	/** {@inheritDoc} */
+	@Override
+	public void createPartControl(Composite parent) {
+		treeViewer = new TreeViewer(parent);
+
+		setupTreeViewer(treeViewer);
+
+		ViewerElementCompositionDropAdapter dndAdapter = new ViewerElementCompositionDropAdapter(
+				treeViewer);
+		treeViewer.addDropSupport(dndAdapter.getSupportedDNDOperations(),
+				dndAdapter.getPreferredTransfers(), dndAdapter);
+
+		treeViewer.setInput(editedObject);
+		getEditorSite().setSelectionProvider(treeViewer);
+
+		editedObject.eAdapters().add(editedObjectChanged);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public void dispose() {
+		editedObject.eAdapters().remove(editedObjectChanged);
+		super.dispose();
+	}
+
+	/** Returns the elements currently selected in the tree. */
+	protected Collection<EObject> getTreeSelection() {
+		List<EObject> result = EObjectSelectionUtils
+				.getEObjectElements(treeViewer.getSelection());
+		if (result.isEmpty()) {
+			result.add(editedObject);
+		}
+		return result;
+	}
+
+	/**
+	 * This method is used to configure the tree viewer, e.g. set the content
+	 * provider, and the label provider.
+	 */
+	protected abstract void setupTreeViewer(TreeViewer treeViewer);
+
+	/** {@inheritDoc} */
+	@Override
+	public void setFocus() {
+		treeViewer.getTree().setFocus();
+	}
+}
-- 
GitLab