Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b8d3095430b73e68cf7aa2f1590ddd27cc397c57 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 * 
 * 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
 *****************************************************************************/
package org.eclipse.papyrus.infra.gmfdiag.properties.databinding.custom;

import java.util.Collection;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.databinding.EMFProperties;
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.emf.databinding.EMFObservableList;
import org.eclipse.papyrus.infra.gmfdiag.common.listener.CustomStyleListener;
import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor;


public class CustomStyleObservableList extends EMFObservableList implements IChangeListener {

	protected EClass eClass;

	protected EStructuralFeature feature;

	protected String styleName;

	protected View view;

	protected EditingDomain domain;

	protected CustomStyleListener listener;

	protected boolean styleExists;

	protected boolean changing = false;

	//Equals to concreteList
	protected IObservableList observableConcreteList;

	//	protected final List<?> transientList;

	public CustomStyleObservableList(View view, String styleName, EditingDomain domain, EClass eClass, EStructuralFeature feature) {
		super(getWrappedList(view, styleName, eClass, feature), domain, null, null);
		this.eClass = eClass;
		this.feature = feature;
		this.styleName = styleName;
		this.view = view;
		this.domain = domain;
		observableConcreteList = (IObservableList)concreteList;
		styleExists = isStyleCreated();

		view.eAdapters().add(listener = new CustomStyleListener(view, feature, this, styleName));
	}

	@Override
	public void commit(AbstractEditor editor) {
		changing = true;
		super.commit(editor);
		changing = false;
	}

	private boolean isStyleCreated() {
		return view.getNamedStyle(eClass, styleName) != null;
	}

	private static IObservableList getWrappedList(View view, String styleName, EClass eClass, EStructuralFeature feature) {
		NamedStyle style = view.getNamedStyle(eClass, styleName);

		if(style != null) {
			return EMFProperties.list(feature).observe(style);
		}

		//The style doesn't exist yet, we need to simulate it 
		//(With an empty list)
		return new WritableList();
	}

	@Override
	public Command getAddCommand(int index, Object value) {
		return new AddCustomStyleListValueCommand(domain, view, styleName, eClass, feature, value, index);
	}

	@Override
	public Command getAddCommand(Object value) {
		return new AddCustomStyleListValueCommand(domain, view, styleName, eClass, feature, value);
	}

	@Override
	public Command getAddAllCommand(Collection<?> values) {
		return new AddAllCustomStyleListValueCommand(domain, view, styleName, eClass, feature, values);
	}

	@Override
	public Command getAddAllCommand(int index, Collection<?> values) {
		return new AddCustomStyleListValueCommand(domain, view, styleName, eClass, feature, values, index);
	}

	@Override
	public Command getRemoveCommand(Object value) {
		return new RemoveCustomStyleListValueCommand(domain, view, styleName, eClass, feature, value);
	}

	@Override
	public Command getRemoveAllCommand(Collection<?> values) {
		return new RemoveAllCustomStyleListValueCommand(domain, view, styleName, eClass, feature, values);
	}

	@Override
	public Command getSetCommand(int index, Object value) {
		return new SetCustomStyleListValueCommand(domain, view, styleName, eClass, feature, index, value);
	}

	public void handleChange(ChangeEvent event) {
		//If the ListValueStyle has been created or removed, we need to resync
		//the concrete list with it
		if(styleExists != (styleExists = isStyleCreated())) {
			observableConcreteList.dispose();
			concreteList = observableConcreteList = getWrappedList(view, styleName, eClass, feature);
		}

		//If this observable is not the source of the change, the wrapped
		//list should also be refreshed.
		if(!changing) {
			refreshCacheList();
		}
	}

	@Override
	public void dispose() {
		view.eAdapters().remove(listener);
		listener.dispose();
		listener = null;
		observableConcreteList.dispose();
		super.dispose();
	}
}

Back to the top