Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ccd12e8fe31575ee930e89b64ea0550f584127c4 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*******************************************************************************
 * Copyright (c) 2006, 2012 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.viewers;

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

import org.eclipse.jface.viewers.TreePath;

/**
 * A node in an asynchronous model.
 *
 * @since 3.2
 */
public class ModelNode {

	private Object fElement; // model element
	private boolean fIsContainer; // whether this element may have children
	private ModelNode fParent; // parent node or null for root
	private ModelNode[] fChildren; // child nodes, possibly null
	private boolean fDisposed; // whether this node has been disposed

	public ModelNode(ModelNode parent, Object element) {
		fParent = parent;
		fElement = element;
	}

	public synchronized Object getElement() {
		return fElement;
	}

    public synchronized void remap(Object element) {
        fElement = element;
    }

	public ModelNode getParentNode() {
		return fParent;
	}

	public synchronized boolean isContainer() {
		return fIsContainer;
	}

	public synchronized ModelNode[] getChildrenNodes() {
		return fChildren;
	}

	public synchronized boolean isDisposed() {
		return fDisposed;
	}

	public synchronized void dispose() {
		fDisposed = true;
        ModelNode[] childrenNodes = getChildrenNodes();
        if (childrenNodes != null) {
            for (int i = 0; i < childrenNodes.length; i++) {
                childrenNodes[i].dispose();
            }
        }
	}

	/**
	 * Returns whether this node corresponds to the given path
	 *
	 * @param path tree path
	 */
	public synchronized boolean correspondsTo(TreePath path) {
		int index = path.getSegmentCount() - 1;
		ModelNode node = this;
		while (index >= 0 && node != null) {
			Object pathElement = path.getSegment(index);
			if (pathElement.equals(node.getElement())) {
				index--;
				node = node.getParentNode();
			} else {
				return false;
			}
		}
		return index == -1;
	}

	/**
	 * Returns a tree path corresponding to this node.
	 *
	 * @return
	 */
	public synchronized TreePath getTreePath() {
		List<Object> path = new ArrayList<>();
		ModelNode node = this;
		while (node != null) {
			path.add(0, node.getElement());
			node = node.getParentNode();
		}
		return new TreePath(path.toArray());
	}

	/**
	 * Adds the given child to this node.
	 *
	 * @param child
	 */
	public synchronized void addChild(ModelNode child) {
		if (fChildren == null) {
			fChildren = new ModelNode[] {child};
		} else {
			ModelNode[] kids = new ModelNode[fChildren.length + 1];
			System.arraycopy(fChildren, 0, kids, 0, fChildren.length);
			kids[fChildren.length] = child;
			fChildren = kids;
		}
	}

    /**
     * Removes the given child from this node.
     *
     * @param child
     */
    public synchronized void removeChild(ModelNode child) {
        if (fChildren != null) {
            for (int i = 0; i < fChildren.length; i++) {
                ModelNode kid = fChildren[i];
                if (child == kid) {
                    ModelNode[] newNodes= new ModelNode[fChildren.length - 1];
                    System.arraycopy(fChildren, 0, newNodes, 0, i);
                    if (i < newNodes.length) {
                        System.arraycopy(fChildren, i + 1, newNodes, i, newNodes.length - i);
                    }
                    fChildren = newNodes;
                    return;
                }
            }
        }
    }

	/**
	 * Sets the children for this node
	 *
	 * @param children
	 */
	public synchronized void setChildren(ModelNode[] children) {
		if (children != null && children.length == 0) {
			fChildren = null;
			setIsContainer(false);
		} else {
			fChildren = children;
		}
	}

	/**
	 * Returns the number of children for this node.
	 *
	 * @return
	 */
	public synchronized int getChildCount() {
		if (fChildren == null) {
            if (isContainer()) {
                return 1;
            }
			return 0;
		}
		return fChildren.length;
	}

    /**
     * Returns the index of the given child in this parent, or -1
     *
     * @param child
     */
    public synchronized int getChildIndex(ModelNode child) {
       if (fChildren != null) {
           for (int i = 0; i < fChildren.length; i++) {
                if (child == fChildren[i]) {
                    return i;
                }
           }
       }
       return -1;
    }

    /**
     * Sets whether this node has children.
     *
     * @param container
     */
    public synchronized void setIsContainer(boolean container) {
        fIsContainer = container;
    }

    @Override
	public String toString() {
    	StringBuffer buf = new StringBuffer();
    	if (isDisposed()) {
    		buf.append("[DISPOSED] "); //$NON-NLS-1$
    	}
    	if (isContainer()) {
    		buf.append("[+] "); //$NON-NLS-1$
    	}
    	buf.append(getElement());
    	return buf.toString();
    }

}

Back to the top