Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 19928b072d9ecf9fde2a7ec8bc68b776acd049ac (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
126
127
128
129
130
/*****************************************************************************
 * Copyright (c) 2012, 2014 CEA LIST 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - 402525
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.common.databinding.custom;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.IObserving;
import org.eclipse.core.databinding.observable.value.AbstractObservableValue;
import org.eclipse.core.databinding.observable.value.ValueDiff;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.gmf.runtime.notation.NamedStyle;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.gmfdiag.common.listener.CustomStyleListener;
import org.eclipse.papyrus.infra.tools.databinding.CommandBasedObservableValue;

/**
 * Abstract implementation for an ObservableValue associated to a GMF custom
 * style (NamedStyle).
 *
 * @author Camille Letavernier
 *
 */
public abstract class AbstractCustomStyleObservableValue extends AbstractObservableValue implements CommandBasedObservableValue, IChangeListener, IObserving {

	protected View source;

	protected EditingDomain domain;

	protected String styleName;

	protected Object lastValue;

	protected CustomStyleListener listener;

	protected EClass styleClass;

	protected EStructuralFeature styleFeature;

	public AbstractCustomStyleObservableValue(View source, EditingDomain domain, String styleName, EClass styleClass, EStructuralFeature styleFeature) {
		this.source = source;
		this.domain = domain;
		this.styleName = styleName;
		this.styleClass = styleClass;
		this.styleFeature = styleFeature;
		source.eAdapters().add(listener = new CustomStyleListener(source, styleFeature, this, styleName));
	}

	@Override
	public void handleChange(ChangeEvent event) {

		final Object oldValue = lastValue;
		final Object newValue = doGetValue();

		fireValueChange(new ValueDiff() {

			@Override
			public Object getOldValue() {
				return oldValue;
			}

			@Override
			public Object getNewValue() {
				return newValue;
			}

		});
	}

	@Override
	protected Object doGetValue() {
		NamedStyle valueStyle = source.getNamedStyle(styleClass, styleName);
		if (valueStyle != null) {
			lastValue = valueStyle.eGet(styleFeature);
		} else {
			lastValue = getDefaultValue();
		}
		return lastValue;
	}

	/**
	 * Gets the default value when this style is not set.
	 *
	 * @return The default value for this NamedStyle
	 */
	protected abstract Object getDefaultValue();

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

	@Override
	public Command getCommand(Object value) {
		return new CustomStyleValueCommand(source, value, styleClass, styleFeature, styleName);
	}

	@Override
	public Object getObserved() {
		return source;
	}

	@Override
	public void dispose() {
		if (isDisposed()) {
			return;
		}
		if (listener != null) {
			source.eAdapters().remove(listener);
			listener.dispose();
			listener = null;
		}
		super.dispose();
	}
}

Back to the top