From a2de1ce1a6bf19b8b5409d801ee80009d6e3b4ed Mon Sep 17 00:00:00 2001
From: Dongyue Mou <mou@fortiss.org>
Date: Wed, 28 Sep 2011 10:23:37 +0000
Subject: [PATCH] Undo the changes about databinding in the last commit refs
 153

---
 .../trunk/META-INF/MANIFEST.MF                |   1 +
 .../databinding/EObjectObservableValue.java   | 128 ++++++++++++++++++
 .../kernel/ui/databinding/ObservableUtil.java |  48 +++++++
 .../kernel/ui/databinding/package.html        |   8 ++
 .../properties/IdLabeledPropertySection.java  |   4 +-
 .../NamedCommentedPropertySection.java        |   6 +-
 6 files changed, 190 insertions(+), 5 deletions(-)
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/EObjectObservableValue.java
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/ObservableUtil.java
 create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/package.html

diff --git a/org.fortiss.tooling.kernel.ui/trunk/META-INF/MANIFEST.MF b/org.fortiss.tooling.kernel.ui/trunk/META-INF/MANIFEST.MF
index bdb5cee23..ae868f1be 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/META-INF/MANIFEST.MF
+++ b/org.fortiss.tooling.kernel.ui/trunk/META-INF/MANIFEST.MF
@@ -16,6 +16,7 @@ Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.6
 Bundle-Vendor: fortiss
 Export-Package: org.fortiss.tooling.kernel.ui,
+ org.fortiss.tooling.kernel.ui.databinding,
  org.fortiss.tooling.kernel.ui.extension,
  org.fortiss.tooling.kernel.ui.extension.base,
  org.fortiss.tooling.kernel.ui.extension.data,
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/EObjectObservableValue.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/EObjectObservableValue.java
new file mode 100644
index 000000000..4e9caba24
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/EObjectObservableValue.java
@@ -0,0 +1,128 @@
+/*--------------------------------------------------------------------------+
+$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.kernel.ui.databinding;
+
+import org.eclipse.core.databinding.observable.Diffs;
+import org.eclipse.core.databinding.observable.Realm;
+import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
+import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.emf.common.notify.Notification;
+import org.eclipse.emf.common.notify.impl.AdapterImpl;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.swt.widgets.Display;
+import org.fortiss.tooling.kernel.service.ICommandStackService;
+
+/**
+ * Implementation of {@link IObservableValue} for {@link EObject}s.
+ * 
+ * @author hummel
+ * @author $Author$
+ * @version $Rev$
+ * @ConQAT.Rating GREEN Hash: 94D2F02DEDBCCDFD7ABC432477684C9B
+ */
+public class EObjectObservableValue extends AbstractObservableValue {
+
+	/** The wrapped object. */
+	private final EObject eObject;
+
+	/** The wrapped structural feature. */
+	private final EStructuralFeature structuralFeature;
+
+	/** Flag used for detecting our own updates. */
+	private boolean updating = false;
+
+	/** The adapter for sending change events. */
+	private final Adapter modelChangeAdapter = new AdapterImpl() {
+		/** {@inheritDoc} */
+		@Override
+		public void notifyChanged(final Notification msg) {
+			if (!updating && msg.getFeature() == structuralFeature) {
+				Display.getDefault().asyncExec(new Runnable() {
+					@Override
+					public void run() {
+						fireValueChange(Diffs.createValueDiff(
+								msg.getOldValue(), msg.getNewValue()));
+					}
+				});
+			}
+		}
+	};
+
+	/** Constructor. */
+	public EObjectObservableValue(Realm realm, EObject eObject,
+			EStructuralFeature structuralFeature) {
+		super(realm);
+		this.eObject = eObject;
+		this.structuralFeature = structuralFeature;
+
+		eObject.eAdapters().add(modelChangeAdapter);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public synchronized void dispose() {
+		eObject.eAdapters().remove(modelChangeAdapter);
+		super.dispose();
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	protected Object doGetValue() {
+		return eObject.eGet(structuralFeature);
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	protected void doSetValue(final Object value) {
+		Object oldValue = doGetValue();
+		if (oldValue == null && value == null || oldValue != null
+				&& oldValue.equals(value)) {
+			return;
+		}
+
+		updating = true;
+		ICommandStackService.INSTANCE.runAsCommand(eObject, new Runnable() {
+			@Override
+			public void run() {
+				eObject.eSet(structuralFeature, value);
+			}
+		});
+		updating = false;
+	}
+
+	/** {@inheritDoc} */
+	@Override
+	public Object getValueType() {
+		if (structuralFeature.isMany()) {
+			return EList.class;
+		}
+
+		if (structuralFeature instanceof EAttribute) {
+			return ((EAttribute) structuralFeature).getEAttributeType()
+					.getInstanceClass();
+		}
+		return ((EReference) structuralFeature).getEReferenceType()
+				.getInstanceClass();
+	}
+
+}
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/ObservableUtil.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/ObservableUtil.java
new file mode 100644
index 000000000..21ad4547d
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/ObservableUtil.java
@@ -0,0 +1,48 @@
+/*--------------------------------------------------------------------------+
+$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.kernel.ui.databinding;
+
+import org.conqat.lib.commons.assertion.CCSMPre;
+import org.eclipse.core.databinding.observable.Realm;
+import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+
+/**
+ * Observerables for {@link EObject} models.
+ * 
+ * @author hummel
+ * @author $Author$
+ * @version $Rev$
+ * @ConQAT.Rating GREEN Hash: 714FEC022718BEA0D783DC7A39A08742
+ */
+public class ObservableUtil {
+
+	/** Returns an observable for the given object and structural feature. */
+	public static IObservableValue observeValue(EObject eObject,
+			EStructuralFeature structuralFeature) {
+		return observeValue(Realm.getDefault(), eObject, structuralFeature);
+	}
+
+	/** Returns an observable for the given object and structural feature. */
+	public static IObservableValue observeValue(Realm realm, EObject eObject,
+			EStructuralFeature structuralFeature) {
+		CCSMPre.isTrue(eObject != null, "Can not observe null value!");
+		return new EObjectObservableValue(realm, eObject, structuralFeature);
+	}
+}
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/package.html b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/package.html
new file mode 100644
index 000000000..e6cd7c225
--- /dev/null
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/databinding/package.html
@@ -0,0 +1,8 @@
+<!--  
+  $Id$
+  @version $Rev$
+  @ConQAT.Rating GREEN Hash: 996283773B2EC0D22D4A844A3C9E30A8
+-->
+<body>
+Support classes for using the data binding mechanism with EMF EObjects.
+</body>
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/IdLabeledPropertySection.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/IdLabeledPropertySection.java
index 663e0b699..b2bcbcdd1 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/IdLabeledPropertySection.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/IdLabeledPropertySection.java
@@ -18,7 +18,6 @@ $Id$
 package org.fortiss.tooling.kernel.ui.internal.properties;
 
 import org.eclipse.core.databinding.observable.value.IObservableValue;
-import org.eclipse.emf.databinding.EMFObservables;
 import org.eclipse.jface.databinding.swt.SWTObservables;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
@@ -26,6 +25,7 @@ import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
 import org.fortiss.tooling.kernel.model.FortissToolingKernelPackage;
 import org.fortiss.tooling.kernel.model.IIdLabeled;
+import org.fortiss.tooling.kernel.ui.databinding.ObservableUtil;
 import org.fortiss.tooling.kernel.ui.extension.base.PropertySectionBase;
 
 /**
@@ -65,7 +65,7 @@ public class IdLabeledPropertySection extends PropertySectionBase {
 	public void refresh() {
 		super.refresh();
 
-		IObservableValue modelObservable = EMFObservables
+		IObservableValue modelObservable = ObservableUtil
 				.observeValue(idLabeled,
 						FortissToolingKernelPackage.Literals.IID_LABELED__ID);
 		dbc.bindValue(SWTObservables.observeText(idText, SWT.None),
diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/NamedCommentedPropertySection.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/NamedCommentedPropertySection.java
index 3d19626eb..66802fd3b 100644
--- a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/NamedCommentedPropertySection.java
+++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/internal/properties/NamedCommentedPropertySection.java
@@ -18,7 +18,6 @@ $Id$
 package org.fortiss.tooling.kernel.ui.internal.properties;
 
 import org.eclipse.core.databinding.observable.value.IObservableValue;
-import org.eclipse.emf.databinding.EMFObservables;
 import org.eclipse.jface.databinding.swt.SWTObservables;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
@@ -26,6 +25,7 @@ import org.eclipse.swt.widgets.Text;
 import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
 import org.fortiss.tooling.kernel.model.FortissToolingKernelPackage;
 import org.fortiss.tooling.kernel.model.INamedCommentedElement;
+import org.fortiss.tooling.kernel.ui.databinding.ObservableUtil;
 import org.fortiss.tooling.kernel.ui.extension.base.PropertySectionBase;
 
 /**
@@ -73,12 +73,12 @@ public class NamedCommentedPropertySection extends PropertySectionBase {
 	public void refresh() {
 		super.refresh();
 
-		IObservableValue modelObservable = EMFObservables.observeValue(
+		IObservableValue modelObservable = ObservableUtil.observeValue(
 				namedCommented,
 				FortissToolingKernelPackage.Literals.INAMED_ELEMENT__NAME);
 		dbc.bindValue(SWTObservables.observeText(nameText, SWT.FocusOut),
 				modelObservable, null, null);
-		modelObservable = EMFObservables
+		modelObservable = ObservableUtil
 				.observeValue(
 						namedCommented,
 						FortissToolingKernelPackage.Literals.INAMED_COMMENTED_ELEMENT__COMMENT);
-- 
GitLab