Skip to main content
summaryrefslogtreecommitdiffstats
blob: a045a7657bafe73cc1c76e59657d774ba5b4dd76 (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
/*******************************************************************************
 * Copyright (c) 2010 BestSolution.at 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:
 *     Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
 ******************************************************************************/
package org.eclipse.e4.tools.emf.ui.internal.common;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.databinding.observable.Diffs;
import org.eclipse.core.databinding.observable.list.IListChangeListener;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
import org.eclipse.core.databinding.observable.list.ListDiff;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.property.list.IListProperty;

public abstract class VirtualEntry<M> {
	private int id;
	private Object originalParent;
	private String label;
	private IObservableList list;

	public VirtualEntry(int id, IListProperty property, Object originalParent, String label) {
		this.id = id;
		this.originalParent = originalParent;
		this.label = label;
		this.list = new WritableList();
		IObservableList origList = property.observe(originalParent);
		list.addAll(cleanedList(origList));

		origList.addListChangeListener(new IListChangeListener() {

			public void handleListChange(ListChangeEvent event) {
				List<Object> clean = cleanedList(event.getObservableList());
				ListDiff diff = Diffs.computeListDiff(VirtualEntry.this.list, clean);
				diff.applyTo(VirtualEntry.this.list);
			}
		});
	}

	private List<Object> cleanedList(IObservableList list) {
		List<Object> l = new ArrayList<Object>(list.size());

		for( Object o : list ) {
			if( accepted((M) o) ) {
				l.add(o);
			}
		}

		return l;
	}

	protected abstract boolean accepted(M o);

	public IObservableList getList() {
		return list;
	}

	public Object getOriginalParent() {
		return originalParent;
	}

	public int getId() {
		return id;
	}

	@Override
	public String toString() {
		return label;
	}
}

Back to the top