Skip to main content
summaryrefslogtreecommitdiffstats
blob: d14f6cd0a103e3c396ad017355940d90126dfed7 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
 * Copyright (c) 2012 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.fx.ui.databinding;

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

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.ListDiffVisitor;
import org.eclipse.jdt.annotation.NonNull;

import javafx.collections.ObservableList;
import javafx.scene.control.TreeItem;

/**
 * Utility to setup a tree
 */
public class TreeUtil {
	/**
	 * Create a tree model backed by the given {@link ObservableFactory}
	 *
	 * @param root
	 *            the root of the model
	 * @param factory
	 *            the factory to create children
	 * @param <T>
	 *            the the item value type
	 * @return the tree item backed by the given root model instance
	 */
	@NonNull
	public static <T> TreeItem<T> createModel(@NonNull T root, @NonNull ObservableFactory<T> factory) {
		return new TreeItemImpl<T>(root, factory);
	}

	/**
	 * Factory to create child observables
	 *
	 * @param <T>
	 *            the type
	 */
	public interface ObservableFactory<T> {
		/**
		 * Create an observable list for the parent element
		 *
		 * @param parent
		 *            the parent
		 * @return the list
		 */
		public IObservableList createObservable(@NonNull T parent);
	}

	static class TreeItemImpl<T> extends TreeItem<T> {
		private IObservableList list;
		@NonNull
		ObservableFactory<T> factory;
		private boolean hasLoadedChildren = false;

		public TreeItemImpl(@NonNull T element, @NonNull ObservableFactory<T> factory) {
			setValue(element);
			this.factory = factory;
			this.list = factory.createObservable(element);
			getChildren().add(new TreeItem<>());

			expandedProperty().addListener((o) -> {
				if( isExpanded() ) {
					if( ! this.hasLoadedChildren ) {
						loadChildren();
					}
				}
			});
		}

//		@Override
//		public ObservableList<TreeItem<T>> getChildren() {
//			if (this.hasLoadedChildren == false) {
//				loadChildren();
//			}
//			return super.getChildren();
//		}

//		@Override
//		public boolean isLeaf() {
//			if( this.hasLoadedChildren ) {
//				loadChildren();
//			}
//			return super.isLeaf();
//		}

		// public boolean isLeaf() {
		// if (this.hasLoadedChildren == false) {
		// loadChildren();
		// }
		// return super.getChildren().isEmpty();
		// }

		@SuppressWarnings("null")
		private void loadChildren() {
			this.hasLoadedChildren = true;
			if (this.list != null) {
				final ObservableList<@NonNull TreeItem<@NonNull T>> itemList = getChildren();
				this.list.addListChangeListener(new IListChangeListener() {

					@Override
					public void handleListChange(ListChangeEvent event) {
						event.diff.accept(new ListDiffVisitor() {

							@Override
							public void handleRemove(int index, Object element) {
								if (itemList.size() < index) {
									TreeItem<T> t = itemList.get(index);
									if (t.getValue() == element) {
										itemList.remove(index);
									} else {
										Iterator<TreeItem<T>> it = itemList.iterator();
										while (it.hasNext()) {
											if (it.next().getValue() == element) {
												it.remove();
												break;
											}
										}
									}
								}
							}

							@SuppressWarnings({ "unchecked" })
							@Override
							public void handleAdd(int index, Object element) {
								if (itemList.size() > index) {
									itemList.add(index, new TreeItemImpl<@NonNull T>((T) element, TreeItemImpl.this.factory));
								} else {
									itemList.add(new TreeItemImpl<@NonNull T>((T) element, TreeItemImpl.this.factory));
								}
							}

							@Override
							public void handleMove(int oldIndex, int newIndex, Object element) {
								TreeItem<T> item = getChildren().remove(oldIndex);
								getChildren().add(newIndex,item);
							}
						});
					}
				});
				List<TreeItemImpl<@NonNull T>> l = new ArrayList<>(this.list.size());

				for (Object o : this.list) {
					@SuppressWarnings("unchecked")
					T t = (T) o;
					l.add(new TreeItemImpl<@NonNull T>(t, this.factory));
				}

				itemList.setAll(l);
			} else {
				getChildren().clear();
			}
		}

	}
}

Back to the top