Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26e8b359e0b646fd2dce0ff3b0acfc07802da7d9 (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
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.papyrus.xwt.internal.utils;

import java.util.HashMap;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.papyrus.xwt.IEventGroup;
import org.eclipse.papyrus.xwt.IObservableValueListener;
import org.eclipse.papyrus.xwt.XWT;
import org.eclipse.papyrus.xwt.javabean.metadata.properties.EventProperty;
import org.eclipse.papyrus.xwt.metadata.IMetaclass;
import org.eclipse.papyrus.xwt.metadata.IProperty;
import org.eclipse.papyrus.xwt.metadata.ModelUtils;
import org.eclipse.swt.widgets.Event;

public class ObservableValueManager implements IObservableValueListener {

	protected HashMap<String, IObservableValue> map;

	protected Object host;

	public ObservableValueManager(Object host) {
		this.host = host;
	}

	public Object getHost() {
		return host;
	}

	public void changeValueHandle(Object object, Event event) {
		// TODO the cast is not clean. 
		EventProperty property = (EventProperty)object;
		IObservableValue value = map.get(property.getName());
		if(value != null) {
			Boolean oldValue = (Boolean)value.getValue();
			if(oldValue == null) {
				oldValue = false;
			}
			value.setValue(!oldValue);
		}

		IMetaclass metaclass = XWT.getMetaclass(host);

		// TODO this conversion should be simplied
		String eventName = ModelUtils.normalizePropertyName(property.getEvent().getName());
		IEventGroup eventGroup = metaclass.getEventGroup(eventName);
		if(eventGroup != null) {
			eventGroup.fireEvent(this, property);
		}
	}

	public void registerValue(IProperty property, IObservableValue observableValue) {
		if(map == null) {
			map = new HashMap<String, IObservableValue>();
		}
		map.put(property.getName(), observableValue);

		IMetaclass metaclass = XWT.getMetaclass(host);
		// TODO it is not clean. 
		EventProperty eventProperty = (EventProperty)property;

		// TODO this conversion should be simplied
		String eventName = ModelUtils.normalizePropertyName(eventProperty.getEvent().getName());
		IEventGroup eventGroup = metaclass.getEventGroup(eventName);
		if(eventGroup != null) {
			eventGroup.registerEvent(this, property);
		}
	}

	public IObservableValue getValue(IProperty property) {
		if(map == null) {
			return null;
		}
		return map.get(property.getName());
	}
}

Back to the top