Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: acce39e3c5cb22e55cd1e30242624326fd707d65 (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
/*******************************************************************************
 * Copyright (c) 2006, 2011 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.compare.structuremergeviewer;

import org.eclipse.compare.IEditableContentExtension;
import org.eclipse.compare.ISharedDocumentAdapter;
import org.eclipse.compare.ITypedElement;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.services.IDisposable;

/**
 * A node that acts as the root of the tree returned from a {@link StructureCreator}.
 * This node performs the following tasks tasks:
 * <ol>
 * <li>It adapts to an {@link ISharedDocumentAdapter} that provides the proper
 * document key (@see {@link #getAdapter(Class)}).</li>
 * <li>It invokes {@link IStructureCreator#save(IStructureComparator, Object)}
 * when {@link #nodeChanged(DocumentRangeNode)} is called.</li>
 * <li>It disposes of the {@link IDisposable} provided in the constructor when
 * {@link #dispose()} is called.</li>
 * </ol>
 * <p>
 * This class may be subclassed by clients.
 *
 * @since 3.3
 */
public class StructureRootNode extends DocumentRangeNode implements IDisposable, ITypedElement {
	/**
	 * The integer constant (value <code>0</code>) that is used as the type code of the root node.
	 * @see #getTypeCode()
	 */
	public static final int ROOT_TYPE = 0;

	/**
	 * The string constant (value <code>"root"</code>) that is used as the id of the root node.
	 * @see #getId()
	 */
	public static final String ROOT_ID = "root"; //$NON-NLS-1$

	private final Object fInput;
	private final StructureCreator fCreator;
	private ISharedDocumentAdapter fAdapter;

	/**
	 * Create the structure root node.
	 * @param document the document
	 * @param input the input associated with the document
	 * @param creator the structure creator that is creating the node
	 * @param adapter the shared document adapter from which the document was obtained or <code>null</code>
	 *    if the document was not obtained from an {@link ISharedDocumentAdapter}
	 */
	public StructureRootNode(IDocument document, Object input, StructureCreator creator, ISharedDocumentAdapter adapter) {
		super(null, ROOT_TYPE, ROOT_ID, document, 0, document.getLength());
		fInput = input;
		fCreator = creator;
		fAdapter = adapter;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.services.IDisposable#dispose()
	 */
	@Override
	public void dispose() {
		if (fAdapter != null) {
			fAdapter.disconnect(fInput);
		}
	}

	/**
	 * Override {@link IAdaptable#getAdapter(Class)} in order to provide
	 * an {@link ISharedDocumentAdapter} that provides the proper look up key based
	 * on the input from which this structure node was created.
	 * @param adapter the adapter class to look up
	 * @return the object adapted to the given class or <code>null</code>
	 * @see IAdaptable#getAdapter(Class)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public <T> T getAdapter(Class<T> adapter) {
		if (adapter == ISharedDocumentAdapter.class) {
			return (T) fAdapter;
		}
		return super.getAdapter(adapter);
	}

	/**
	 * Override in order to invoke {@link IStructureCreator#save(IStructureComparator, Object)} when the
	 * contents of a node have changed.
	 * @param node the changed node
	 */
	@Override
	protected void nodeChanged(DocumentRangeNode node) {
		fCreator.save(this, fInput);
	}

	@Override
	public ITypedElement replace(ITypedElement child, ITypedElement other) {
		// TODO: I believe the parent implementation is flawed but didn't to remove
		// it in case I was missing something so I overrode it instead
		nodeChanged(this);
		return child;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.ITypedElement#getImage()
	 */
	@Override
	public Image getImage() {
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.ITypedElement#getName()
	 */
	@Override
	public String getName() {
		return getId();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.ITypedElement#getType()
	 */
	@Override
	public String getType() {
		return FOLDER_TYPE;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.structuremergeviewer.DocumentRangeNode#isReadOnly()
	 */
	@Override
	public boolean isReadOnly() {
		if (fInput instanceof IEditableContentExtension) {
			IEditableContentExtension ext = (IEditableContentExtension) fInput;
			return ext.isReadOnly();
		}
		return super.isReadOnly();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.compare.structuremergeviewer.DocumentRangeNode#validateEdit(org.eclipse.swt.widgets.Shell)
	 */
	@Override
	public IStatus validateEdit(Shell shell) {
		if (fInput instanceof IEditableContentExtension) {
			IEditableContentExtension ext = (IEditableContentExtension) fInput;
			return ext.validateEdit(shell);
		}
		return super.validateEdit(shell);
	}

}

Back to the top