Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 34ba099dbb3b6ac899767d5f9c1be9f85a68433c (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.model;

import java.util.UUID;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.tcf.te.runtime.activator.CoreBundleActivator;
import org.eclipse.tcf.te.runtime.interfaces.tracing.ITraceIds;
import org.eclipse.tcf.te.runtime.model.interfaces.IContainerModelNode;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNode;
import org.eclipse.tcf.te.runtime.model.interfaces.IModelNodeProvider;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;

/**
 * A common model node implementation.
 * <p>
 * <b>Note:</b> The model node implementation is not thread-safe. Clients requiring
 *              a thread-safe implementation should subclass the properties container and
 *              overwrite {@link #checkThreadAccess()}.
 */
public class ModelNode extends PropertiesContainer implements IModelNode, IModelNodeProvider {
	// Reference to the parent model node
	private IContainerModelNode parent = null;

	// Flag to remember the dirty state of the model node.
	private boolean dirty;
	// Flag to remember the pending state of the model node.
	private boolean pending;

	// Flag to control if property change events are suppressed
	// until the model node is added to a parent container model node.
	protected boolean suppressEventsOnNullParent = true;
	// Flag to control if the node parent can change after set
	// to a non-null value. Default is that the node parent cannot
	// change after set to a non-null value.
	protected boolean allowSetParentOnNonNullParent = false;

	/**
	 * Constructor.
	 */
	public ModelNode() {
		super();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#getParent()
	 */
	@Override
	public final IContainerModelNode getParent() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		return parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.model.interfaces.IModelNode#getParent(java.lang.Class)
	 */
	@SuppressWarnings("unchecked")
    @Override
	public final <V extends IContainerModelNode> V getParent(Class<V> nodeType) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		if (this.parent != null) {
			if (nodeType.isInstance(this.parent)) {
				return (V)this.parent;
			}
			return this.parent.getParent(nodeType);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#setParent(org.eclipse.tcf.te.runtime.interfaces.nodes.IContainerModelNode)
	 */
	@Override
	public final void setParent(IContainerModelNode parent) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		if (this.parent != null && !allowSetParentOnNonNullParent) {
			throw new IllegalStateException("Model node already associated with a parent container model node!"); //$NON-NLS-1$
		}
		this.parent = parent;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#move(org.eclipse.tcf.te.runtime.interfaces.nodes.IContainerModelNode)
	 */
	@Override
	public final void move(IContainerModelNode newParent) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		Assert.isNotNull(newParent);

		// If the node is associated with a parent container, remove the node from
		// the container node non-recursive (keeping all children if being ourself
		// a container model node)
		if (this.parent != null) {
			// Remove the node from the old parent container
			if (!this.parent.contains(this) || this.parent.remove(this, false)) {
				// Unset the parent reference (will enable the add to the new container)
				this.parent = null;
			}
		}

		// Re-add to the new parent. This may cause an
		// IllegalStateException if the previous removal from
		// the old parent container failed.
		newParent.add(this);
		return;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#isVisible()
	 */
	@Override
	public boolean isVisible() {
		return getBooleanProperty(PROPERTY_VISIBLE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#getError()
	 */
	@Override
	public String getError() {
		return getStringProperty(PROPERTY_ERROR);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#getName()
	 */
	@Override
	public String getName() {
		String name = (String)super.getProperty(PROPERTY_NAME);
		return name != null ? name : ""; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#getDescription()
	 */
	@Override
	public String[] getDescription() {
		return new String[]{};
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#getImageId()
	 */
	@Override
	public String getImageId() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.nodes.PropertiesContainer#toString()
	 */
	@Override
	public String toString() {
		StringBuilder toString = new StringBuilder(getClass().getName());

		toString.append("{"); //$NON-NLS-1$
		toString.append(super.toString());
		toString.append("}"); //$NON-NLS-1$

		return toString.toString();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.nodes.PropertiesContainer#dropEvent(java.lang.Object, java.lang.String, java.lang.Object, java.lang.Object)
	 */
	@Override
	protected boolean dropEvent(Object source, String key, Object oldValue, Object newValue) {
		boolean drop = super.dropEvent(source, key, oldValue, newValue);
		if (drop || PROPERTY_IS_GHOST.equals(key)) {
			if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_EVENTS)) {
				CoreBundleActivator.getTraceHandler().trace("Drop change event (hidden property)\n\t\t" + //$NON-NLS-1$
															"for eventId = " + key, //$NON-NLS-1$
															0, ITraceIds.TRACE_EVENTS, IStatus.WARNING, this);
			}
			return true;
		}

		// If the parent is null, it must be allowed to fire change events explicitly
		if (parent == null && suppressEventsOnNullParent) {
			if (CoreBundleActivator.getTraceHandler().isSlotEnabled(0, ITraceIds.TRACE_EVENTS)) {
				CoreBundleActivator.getTraceHandler().trace("Drop change event (null parent)\n\t\t" + //$NON-NLS-1$
															"for eventId = " + key, //$NON-NLS-1$
															0, ITraceIds.TRACE_EVENTS, IStatus.WARNING, this);
			}
			return true;
		}

		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNodeProvider#getModelNode()
	 */
	@Override
	public final IModelNode getModelNode() {
		return this;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.ISchedulingRule#contains(org.eclipse.core.runtime.jobs.ISchedulingRule)
	 */
	@Override
	public boolean contains(ISchedulingRule rule) {
		// We deal only with scheduling rules we know about (as the interface
		// declaration of ISchedulingRule#contains requests).
		if (rule instanceof IModelNode) {
			// The IModelNode itself is an leaf node and cannot have children.
			// Therefore, the IModelNode can contains only itself.
			return rule == this;
		}

		// If we don't know about the scheduling rule, we must return false.
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.jobs.ISchedulingRule#isConflicting(org.eclipse.core.runtime.jobs.ISchedulingRule)
	 */
	@Override
	public boolean isConflicting(ISchedulingRule rule) {
		// We deal only with scheduling rules we know about (as the interface
		// declaration of ISchedulingRule#contains requests).
		if (rule instanceof IModelNode) {
			// The IModelNode itself is an leaf node and cannot have children.
			// Therefore, the IModelNode can conflict only with itself.
			return rule == this;
		}

		// If we don't know about the scheduling rule, we must return false.
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#setDirty(boolean)
	 */
	@Override
	public final void setDirty(boolean dirty) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		boolean oldDirtyState = this.dirty;
		this.dirty = dirty;
		fireChangeEvent("dirty", Boolean.valueOf(oldDirtyState), Boolean.valueOf(this.dirty)); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#isDirty()
	 */
	@Override
	public final boolean isDirty() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		return dirty;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#setPending(boolean)
	 */
	@Override
	public final void setPending(boolean pending) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		boolean oldPendingState = this.pending;
		this.pending = pending;
		fireChangeEvent("pending", Boolean.valueOf(oldPendingState), Boolean.valueOf(this.pending)); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#isPending()
	 */
	@Override
	public final boolean isPending() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		return pending;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.interfaces.nodes.IModelNode#find(java.util.UUID)
	 */
	@Override
	public IModelNode find(UUID uuid) {
		if (getUUID().equals(uuid)) {
			return this;
		}
		return null;
	}
}

Back to the top