KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > structuremergeviewer > StructureRootNode


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  *******************************************************************************/

11 package org.eclipse.compare.structuremergeviewer;
12
13 import org.eclipse.compare.*;
14 import org.eclipse.core.runtime.IAdaptable;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.services.IDisposable;
20
21 /**
22  * A node that acts as the root of the tree returned from a {@link StructureCreator}.
23  * This node performs the following tasks tasks:
24  * <ol>
25  * <li>It adapts to an {@link ISharedDocumentAdapter} that provides the proper
26  * document key (@see {@link #getAdapter(Class)}).</li>
27  * <li>It invokes {@link IStructureCreator#save(IStructureComparator, Object)}
28  * when {@link #nodeChanged(DocumentRangeNode)} is called.</li>
29  * <li>It disposes of the {@link IDisposable} provided in the constructor when
30  * {@link #dispose()} is called.</li>
31  * </ol>
32  * <p>
33  * This class may be subclassed by clients.
34  *
35  * @since 3.3
36  */

37 public class StructureRootNode extends DocumentRangeNode implements IDisposable, ITypedElement {
38
39     /**
40      * The integer constant (value <code>0</code>) that is used as the type code of the root node.
41      * @see #getTypeCode()
42      */

43     public static final int ROOT_TYPE = 0;
44     
45     /**
46      * The string constant (value <code>"root"</code>) that is used as the id of the root node.
47      * @see #getId()
48      */

49     public static final String JavaDoc ROOT_ID = "root"; //$NON-NLS-1$
50

51     private final Object JavaDoc fInput;
52     private final StructureCreator fCreator;
53     private ISharedDocumentAdapter fAdapter;
54     
55     /**
56      * Create the structure root node.
57      * @param document the document
58      * @param input the input associated with the document
59      * @param creator the structure creator that is creating the node
60      * @param adapter the shared document adapter from which the document was obtained or <code>null</code>
61      * if the document was not obtained from an {@link ISharedDocumentAdapter}
62      */

63     public StructureRootNode(IDocument document, Object JavaDoc input, StructureCreator creator, ISharedDocumentAdapter adapter) {
64         super(null, ROOT_TYPE, ROOT_ID, document, 0, document.getLength());
65         fInput = input;
66         fCreator = creator;
67         fAdapter = adapter;
68     }
69     
70     /* (non-Javadoc)
71      * @see org.eclipse.ui.services.IDisposable#dispose()
72      */

73     public void dispose() {
74         if (fAdapter != null) {
75             fAdapter.disconnect(fInput);
76         }
77     }
78
79     /**
80      * Override {@link IAdaptable#getAdapter(Class)} in order to provide
81      * an {@link ISharedDocumentAdapter} that provides the proper look up key based
82      * on the input from which this structure node was created.
83      * @param adapter the adapter class to look up
84      * @return the object adapted to the given class or <code>null</code>
85      * @see IAdaptable#getAdapter(Class)
86      */

87     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
88         if (adapter == ISharedDocumentAdapter.class) {
89             return fAdapter;
90         }
91         return super.getAdapter(adapter);
92     }
93     
94     /**
95      * Override in order to invoke {@link IStructureCreator#save(IStructureComparator, Object)} when the
96      * contents of a node have changed.
97      * @param node the changed node
98      */

99     protected void nodeChanged(DocumentRangeNode node) {
100         fCreator.save(this, fInput);
101     }
102     
103     /* (non-Javadoc)
104      * @see org.eclipse.compare.structuremergeviewer.DocumentRangeNode#replace(org.eclipse.compare.ITypedElement, org.eclipse.compare.ITypedElement)
105      */

106     public ITypedElement replace(ITypedElement child, ITypedElement other) {
107         // TODO: I believe the parent implementation is flawed but didn't to remove
108
// it in case I was missing something so I overrode it instead
109
nodeChanged(this);
110         return child;
111     }
112
113     /* (non-Javadoc)
114      * @see org.eclipse.compare.ITypedElement#getImage()
115      */

116     public Image getImage() {
117         return null;
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.compare.ITypedElement#getName()
122      */

123     public String JavaDoc getName() {
124         return getId();
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.compare.ITypedElement#getType()
129      */

130     public String JavaDoc getType() {
131         return FOLDER_TYPE;
132     }
133     
134     /* (non-Javadoc)
135      * @see org.eclipse.compare.structuremergeviewer.DocumentRangeNode#isReadOnly()
136      */

137     public boolean isReadOnly() {
138         if (fInput instanceof IEditableContentExtension) {
139             IEditableContentExtension ext = (IEditableContentExtension) fInput;
140             return ext.isReadOnly();
141         }
142         return super.isReadOnly();
143     }
144     
145     /* (non-Javadoc)
146      * @see org.eclipse.compare.structuremergeviewer.DocumentRangeNode#validateEdit(org.eclipse.swt.widgets.Shell)
147      */

148     public IStatus validateEdit(Shell shell) {
149         if (fInput instanceof IEditableContentExtension) {
150             IEditableContentExtension ext = (IEditableContentExtension) fInput;
151             return ext.validateEdit(shell);
152         }
153         return super.validateEdit(shell);
154     }
155
156 }
157
Popular Tags