Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4228f20e6d2513c946a5afc144d73ae472422c43 (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
/*******************************************************************************
 * 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 org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.papyrus.xwt.XWT;
import org.eclipse.papyrus.xwt.XWTException;
import org.eclipse.papyrus.xwt.metadata.IMetaclass;
import org.eclipse.papyrus.xwt.metadata.IProperty;


/**
 * Setter of the class Style, which is used to define the in-line XAML style
 * 
 * @see Style
 * @author yyang
 */
public class Setter extends SetterBase {

	protected String property;

	protected Object value;

	protected String targetName;

	public String getTargetName() {
		return targetName;
	}

	public void setTargetName(String targetName) {
		this.targetName = targetName;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public Object applyTo(Object element, boolean update) {
		String propName = getProperty();
		Object propValue = getValue();
		String targetName = getTargetName();
		Object setterTarget = element;
		Object oldValue = null;
		if(targetName != null) {
			setterTarget = TriggerBase.getElementByName(element, targetName);
		}
		IMetaclass metaclass = XWT.getMetaclass(setterTarget);
		IProperty prop = metaclass.findProperty(propName);
		if(prop != null && propValue != null) {
			Object toValue = propValue;
			Class<?> valueType = propValue.getClass();
			Class<?> targetType = prop.getType();
			if(targetType != null && !targetType.isAssignableFrom(valueType)) {
				IConverter converter = XWT.findConvertor(valueType, targetType);
				if(converter != null) {
					toValue = converter.convert(propValue);
				} else {
					throw new XWTException("Converter doesn't exist from \"" + valueType.getName() + "\" to \"" + targetType.getName());
				}
			}
			try {
				oldValue = prop.getValue(setterTarget);
				if(update) {
					prop.setValue(setterTarget, toValue);
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		return oldValue;
	}

	public void undo(Object element, Object value) {
		String propName = getProperty();
		String targetName = getTargetName();
		Object setterTarget = element;
		if(targetName != null) {
			setterTarget = TriggerBase.getElementByName(element, targetName);
		}
		IMetaclass metaclass = XWT.getMetaclass(setterTarget);
		IProperty prop = metaclass.findProperty(propName);
		if(prop != null) {
			try {
				prop.setValue(setterTarget, value);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}
}

Back to the top