Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 334f47e31c455b353773ac7fa0ef932e9ba238c1 (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
/*******************************************************************************
 * Copyright (c) 2006, 2010 Soyatec (http://www.soyatec.com) 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:
 *     Soyatec - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.xwt.core;

import java.util.HashMap;

import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.papyrus.xwt.XWTException;
import org.eclipse.papyrus.xwt.internal.core.ScopeManager;
import org.eclipse.papyrus.xwt.internal.core.UpdateSourceTrigger;

public class MultiTrigger extends TriggerBase {

	private Condition[] conditions = Condition.EMPTY_ARRAY;

	private SetterBase[] setters;

	public Condition[] getConditions() {
		return conditions;
	}

	public void setConditions(Condition[] conditions) {
		this.conditions = conditions;
	}

	public SetterBase[] getSetters() {
		return setters;
	}

	public void setSetters(SetterBase[] setters) {
		this.setters = setters;
	}

	class ValueChangeListener extends AbstractChangeListener {

		public ValueChangeListener(Object element) {
			super(element);
		}

		public void handleChange(ChangeEvent event) {
			for(Condition condition : getConditions()) {
				if(!condition.evaluate(element)) {
					restoreValues();
					return;
				}
			}

			if(oldvalues != null && !oldvalues.isEmpty()) {
				return;
			}

			for(SetterBase setter : getSetters()) {
				try {
					Object oldValue = setter.applyTo(element, true);
					if(oldvalues == null) {
						oldvalues = new HashMap<SetterBase, Object>();
					}
					oldvalues.put(setter, oldValue);
				} catch (RuntimeException e) {
					continue;
				}
			}
		}
	}

	@Override
	public void on(Object target) {
	}

	@Override
	public void prepare(Object target) {
		if(getConditions().length == 0) {
			return;
		}
		ValueChangeListener changeListener = new ValueChangeListener(target);
		for(Condition condition : getConditions()) {
			String propertyName = condition.getProperty();
			String sourceName = condition.getSourceName();

			Object source = getElementByName(target, sourceName);
			if(source == null) {
				throw new XWTException("No element is found with the name = " + sourceName);
			}
			IObservable observableValue = ScopeManager.observeValue(source, source, propertyName, UpdateSourceTrigger.PropertyChanged);
			observableValue.addChangeListener(changeListener);
		}
	}
}

Back to the top