Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f5d013b2d473efd86e9e96837ebced2ec2247d07 (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
150
/*******************************************************************************
 * 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.dataproviders;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.papyrus.xwt.IDataProvider;
import org.eclipse.papyrus.xwt.core.IBinding;
import org.eclipse.papyrus.xwt.internal.core.ScopeManager;

/**
 * @author jliu (jin.liu@soyatec.com)
 */
public abstract class AbstractDataProvider implements IDataProvider {

	private HashMap<String, Object> properties = new HashMap<String, Object>();

	public Object getProperty(String property) {
		return properties.get(property);
	}

	public void setProperty(String property, Object value) {
		properties.put(property, value);
	}

	public boolean hasProperty(String property) {
		return properties.containsKey(property);
	}

	public void removeProperty(String property) {
		properties.remove(property);
	}

	public boolean isPropertyReadOnly(String path) {
		return false;
	}

	public IObservable observe(Object data, String path, Object elementType, int observeKind) {
		Object type = null;
		if (elementType == null) {
			type = getModelService().toModelType(data);
		} else {
			type = elementType;
		}
		Object propertyType = getModelService().toModelPropertyType(type, path);
		Class<?> propertyTypeClass = null;

		if (propertyType instanceof Class<?>) {
			propertyTypeClass = (Class<?>) propertyType;
			if (IBinding.class.isAssignableFrom(propertyTypeClass)) {
				return null;
			}
		}

		switch (observeKind) {
		case ScopeManager.AUTO:
			if (propertyTypeClass != null && (propertyTypeClass.isArray() || List.class.isAssignableFrom(propertyTypeClass))) {
				if (data instanceof IObservableValue) {
					IObservableValue observable = (IObservableValue) data;
					return observeDetailList(observable, type, path, propertyType);
				}
				return observeList(data, path);
			} else if (propertyTypeClass != null && (Set.class.isAssignableFrom(propertyTypeClass))) {
				if (data instanceof IObservableValue) {
					IObservableValue observable = (IObservableValue) data;
					return observeDetailSet(observable, type, path, propertyType);
				}
				return observeSet(data, path);
			} else {
				if (data instanceof IObservableValue) {
					IObservableValue observable = (IObservableValue) data;
					return observeDetailValue(observable, type, path, propertyType);
				}
				return observeValue(data, path);
			}
		case ScopeManager.VALUE:
			if (data instanceof IObservableValue) {
				IObservableValue observable = (IObservableValue) data;
				return observeDetailValue(observable, type, path, propertyType);
			}
			return observeValue(data, path);
		case ScopeManager.COLLECTION:
			if (propertyTypeClass != null && Set.class.isAssignableFrom(propertyTypeClass)) {
				if (data instanceof IObservableValue) {
					IObservableValue observable = (IObservableValue) data;
					return observeDetailSet(observable, type, path, propertyType);
				}
				return observeSet(data, path);
			}
			if (data instanceof IObservableValue) {
				IObservableValue observable = (IObservableValue) data;
				return observeDetailList(observable, type, path, propertyType);
			}
			return observeList(data, path);
		case ScopeManager.SET:
			if (data instanceof IObservableValue) {
				IObservableValue observable = (IObservableValue) data;
				return observeDetailSet(observable, type, path, propertyType);
			}
			return observeSet(data, path);
		case ScopeManager.LIST:
			if (data instanceof IObservableValue) {
				IObservableValue observable = (IObservableValue) data;
				return observeDetailList(observable, type, path, propertyType);
			}
			return observeList(data, path);
		}
		return null;
	}

	protected abstract IObservableValue observeValue(Object bean, String propertyName);

	protected IObservableList observeList(Object bean, String propertyName) {
		return null;
	}

	protected IObservableSet observeSet(Object bean, String propertyName) {
		return null;
	}

	protected IObservableList observeDetailList(IObservableValue bean, Object elementType, String propertyName, Object propertyType) {
		return null;
	}

	protected IObservableSet observeDetailSet(IObservableValue bean, Object elementType, String propertyName, Object propertyType) {
		return null;
	}

	protected abstract IObservableValue observeDetailValue(IObservableValue bean, Object elementType, String propertyName, Object propertyType);

	// TODO to remove
	public IValueProperty createValueProperty(Object type, String fullPath) {
		throw new UnsupportedOperationException("to remove this method");
	}
}

Back to the top