comparison org.eclipse.core.databinding.observable/src/org/eclipse/core/internal/databinding/observable/tree/AbstractObservableTree.d @ 95:6208d4f6a277

Added trees for databinding.beans and observable
author Frank Benoit <benoit@tionex.de>
date Tue, 21 Apr 2009 10:55:51 +0200
parents
children b74ac5dfcc06
comparison
equal deleted inserted replaced
94:1d37a7813832 95:6208d4f6a277
1 /*******************************************************************************
2 * Copyright (c) 2006, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Brad Reynolds - bug 164134
11 *******************************************************************************/
12
13 package org.eclipse.core.internal.databinding.observable.tree;
14
15 import org.eclipse.core.databinding.observable.AbstractObservable;
16 import org.eclipse.core.databinding.observable.Realm;
17 import org.eclipse.core.databinding.util.Policy;
18 import org.eclipse.core.internal.databinding.BindingMessages;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.ListenerList;
21 import org.eclipse.core.runtime.Status;
22
23 /**
24 * @since 3.3
25 *
26 */
27 public abstract class AbstractObservableTree : AbstractObservable
28 , IObservableTree {
29
30 private bool stale;
31
32 private ListenerList treeListeners = new ListenerListcast(ListenerList.IDENTITY);
33
34 /**
35 * @param realm
36 */
37 public this(Realm realm) {
38 super(realm);
39 }
40
41 public void addChild(TreePath parentPath, Object childElement) {
42 throw new UnsupportedOperationException();
43 }
44
45 public void addTreeChangeListener(ITreeChangeListener listener) {
46 treeListeners.add(listener);
47 }
48
49 public int getChildCount(TreePath parentPath) {
50 return getChildren(parentPath).length;
51 }
52
53 public bool hasChildren(TreePath parentPath) {
54 return getChildCount(parentPath) > 0;
55 }
56
57 public void insertChild(TreePath parentPath, int index, Object childElement) {
58 throw new UnsupportedOperationException();
59 }
60
61 public bool isLazy() {
62 return false;
63 }
64
65 public bool isOrdered() {
66 return false;
67 }
68
69 public void removeChild(TreePath parentPath, Object childElement) {
70 throw new UnsupportedOperationException();
71 }
72
73 public void removeChild(TreePath parentPath, int index) {
74 throw new UnsupportedOperationException();
75 }
76
77 public void removeTreeChangeListener(ITreeChangeListener listener) {
78 treeListeners.remove(listener);
79 }
80
81 public void setChildCount(TreePath parentPath, int count) {
82 throw new UnsupportedOperationException();
83 }
84
85 public void setChildren(TreePath parentPath, Object[] children) {
86 throw new UnsupportedOperationException();
87 }
88
89 public void updateChildren(IChildrenUpdate update) {
90 TreePath parent = update.getParent();
91 Object[] children = getChildren(parent);
92 for (int i = 0; i < update.getLength(); i++) {
93 int targetIndex = update.getOffset() + i;
94 if (targetIndex < children.length) {
95 update.setChild(children[targetIndex], targetIndex);
96 } else {
97 update
98 .setStatus(new Status(
99 IStatus.WARNING,
100 Policy.JFACE_DATABINDING,
101 IStatus.OK,
102 BindingMessages
103 .getStringcast(BindingMessages.INDEX_OUT_OF_RANGE),
104 null));
105 }
106 }
107 update.done();
108 }
109
110 public void updateChildrenCount(IChildrenCountUpdate update) {
111 TreePath[] parents = update.getParents();
112 for (int i = 0; i < parents.length; i++) {
113 update.setChildCount(parents[i], getChildCount(parents[i]));
114 }
115 update.done();
116 }
117
118 public void updateHasChildren(IHasChildrenUpdate update) {
119 TreePath[] parents = update.getElements();
120 for (int i = 0; i < parents.length; i++) {
121 update.setHasChilren(parents[i], hasChildren(parents[i]));
122 }
123 update.done();
124 }
125
126 public bool isStale() {
127 return stale;
128 }
129
130 /**
131 * @param stale
132 */
133 public void setStale(bool stale) {
134 this.stale = stale;
135 if (stale) {
136 fireStale();
137 }
138 }
139
140 protected void fireTreeChange(TreeDiff diff) {
141 // fire general change event first
142 fireChange();
143
144 Object[] listeners = treeListeners.getListeners();
145 TreeChangeEvent event = new TreeChangeEvent(this, diff);
146 for (int i = 0; i < listeners.length; i++) {
147 (cast(ITreeChangeListener) listeners[i]).handleTreeChange(event);
148 }
149 }
150
151 }