Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 077c5e350b51568c700802f832075e588d4ad354 (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
/*******************************************************************************
 * 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.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Set;

import org.eclipse.core.databinding.observable.IObservableCollection;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.observable.set.WritableSet;
import org.eclipse.papyrus.xwt.XWT;

/**
 * The proxy of a CollectionView class.
 * 
 * @author yyang (yves.yang@soyatec.com)
 */
public class CollectionViewSource {

	/**
	 * Gets or sets the collection object from which to create this view. This is a dependency property.
	 */
	private Object source;

	/**
	 * Gets the view object that is currently associated with this instance of CollectionViewSource. This is a dependency property.
	 * 
	 */
	private IObservableCollection view;

	private GroupDescription[] groupDescription = GroupDescription.EMPTY_ARRAY;

	private SortDescription[] sortDescription = SortDescription.EMPTY_ARRAY;

	private Locale locale = Locale.getDefault();

	/**
	 * Gets or sets the desired view type.
	 */
	private Class<?> collectionViewType = Object.class;

	public Object getSource() {
		if(source == null) {
			source = new ArrayList<Object>();
		}
		return source;
	}

	public void setSource(Object source) {
		if(this.source == source) {
			return;
		}
		view = null;
		this.source = source;
	}

	public IObservableCollection getView() {
		if(view == null) {
			Object source = getSource();
			if(!(source instanceof IObservableCollection)) {
				Class<?> elementType = getCollectionViewType();
				if(source.getClass().isArray()) {
					Object[] array = (Object[])source;
					elementType = source.getClass().getComponentType();
					source = Arrays.asList(array);
				}
				if(source instanceof List<?>) {
					view = new WritableList(XWT.getRealm(), (List<?>)source, elementType);
				} else if(source instanceof Set<?>) {
					view = new WritableSet(XWT.getRealm(), (List<?>)source, elementType);
				}
			}
		}
		return view;
	}

	public GroupDescription[] getGroupDescription() {
		return groupDescription;
	}

	public void setGroupDescription(GroupDescription[] groupDescription) {
		this.groupDescription = groupDescription;
	}

	public SortDescription[] getSortDescription() {
		return sortDescription;
	}

	public void setSortDescription(SortDescription[] sortDescription) {
		this.sortDescription = sortDescription;
	}

	public Class<?> getCollectionViewType() {
		return collectionViewType;
	}

	public void setCollectionViewType(Class<?> collectionViewType) {
		this.collectionViewType = collectionViewType;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}
}

Back to the top