Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 74c459722525ccd9906e473f28ed3f6a8153fada (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.properties.databinding;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.IObserving;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.notation.CanonicalStyle;
import org.eclipse.papyrus.commands.wrappers.GMFtoEMFCommandWrapper;
import org.eclipse.papyrus.infra.gmfdiag.canonical.editpolicy.CanonicalStateListener;
import org.eclipse.papyrus.infra.gmfdiag.common.commands.SetCanonicalCommand;
import org.eclipse.papyrus.infra.gmfdiag.common.helper.NotationHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.utils.DiagramEditPartsUtil;
import org.eclipse.papyrus.infra.gmfdiag.properties.modelelement.SynchronizationModelElement;
import org.eclipse.papyrus.infra.tools.databinding.AggregatedObservable;
import org.eclipse.papyrus.infra.tools.databinding.ReferenceCountedObservable;
import org.eclipse.papyrus.uml.tools.databinding.AggregatedPapyrusObservableValue;
import org.eclipse.papyrus.uml.tools.databinding.CommandBasedObservableValue;

/**
 * The observable boolean "canonical synchronization" state of an {@link EditPart}, as represented by
 * a {@link SynchronizationModelElement}.
 */
public class CanonicalObservableValue extends ReferenceCountedObservable.Value implements CommandBasedObservableValue, AggregatedObservable, IObserving {
	private TransactionalEditingDomain domain;
	private EditPart editPart;
	private boolean lastComputed;

	private CanonicalStateListener canonicalStateListener;
	private CanonicalStateListener.Handler refreshHandler;

	public CanonicalObservableValue(TransactionalEditingDomain domain, EditPart editPart) {
		super();

		this.domain = domain;
		this.editPart = editPart;

		final Runnable update = new Runnable() {
			public void run() {
				boolean oldValue = lastComputed; // doGetValue updates this
				fireValueChange(Diffs.createValueDiff(oldValue, doGetValue()));
			}
		};

		if (editPart instanceof IGraphicalEditPart) {
			IGraphicalEditPart graphical = (IGraphicalEditPart) editPart;
			canonicalStateListener = CanonicalStateListener.getInstance(graphical);

			refreshHandler = new CanonicalStateListener.Handler() {

				public Runnable handleAdd(CanonicalStyle style) {
					return update;
				}

				public Runnable handleRemove(CanonicalStyle style) {
					return update;
				}
			};
			canonicalStateListener.addCanonicalRefreshHandler(refreshHandler);
		}
	}

	@Override
	public synchronized void dispose() {
		try {
			if (canonicalStateListener != null) {
				canonicalStateListener.removeCanonicalRefreshHandler(refreshHandler);
				canonicalStateListener.release();
				canonicalStateListener = null;
			}
		} finally {
			super.dispose();
		}
	}

	public Object getObserved() {
		return editPart;
	}

	public Object getValueType() {
		return Boolean.class;
	}

	@Override
	protected Object doGetValue() {
		boolean result = DiagramEditPartsUtil.isCanonical(editPart);
		lastComputed = result;
		return result;
	}

	@Override
	protected void doSetValue(Object value) {
		Command command = getCommand(value);
		domain.getCommandStack().execute(command);
	}

	public Command getCommand(Object value) {
		boolean canonical = (value instanceof Boolean) && ((Boolean) value).booleanValue();

		return GMFtoEMFCommandWrapper.wrap(new SetCanonicalCommand(domain, NotationHelper.findView(editPart), canonical));
	}

	public AggregatedObservable aggregate(IObservable observable) {
		return new AggregatedPapyrusObservableValue(domain, this, observable);
	}

	public boolean hasDifferentValues() {
		return false; // Primitive component has only one value
	}
}

Back to the top