KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > compare > JavaTextBufferNode


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.jdt.internal.ui.compare;
12
13 import java.io.ByteArrayInputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IFileState;
20 import org.eclipse.core.resources.IResource;
21
22 import org.eclipse.swt.graphics.Image;
23
24 import org.eclipse.jface.text.IDocument;
25
26 import org.eclipse.compare.HistoryItem;
27 import org.eclipse.compare.IEncodedStreamContentAccessor;
28 import org.eclipse.compare.IResourceProvider;
29 import org.eclipse.compare.ITypedElement;
30 import org.eclipse.compare.ResourceNode;
31 import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
32
33 import org.eclipse.jdt.core.dom.ASTNode;
34
35 import org.eclipse.jdt.internal.ui.JavaPlugin;
36
37 /**
38  * Implements the IStreamContentAccessor and ITypedElement protocols
39  * for a TextBuffer.
40  */

41 class JavaTextBufferNode implements ITypedElement, IEncodedStreamContentAccessor, IResourceProvider {
42     
43     private IFile fFile;
44     private IDocument fDocument;
45     private boolean fInEditor;
46     
47     JavaTextBufferNode(IFile file, IDocument document, boolean inEditor) {
48         fFile= file;
49         fDocument= document;
50         fInEditor= inEditor;
51     }
52     
53     public String JavaDoc getName() {
54         if (fInEditor)
55             return CompareMessages.Editor_Buffer;
56         return CompareMessages.Workspace_File;
57     }
58     
59     public String JavaDoc getType() {
60         return "java"; //$NON-NLS-1$
61
}
62     
63     public Image getImage() {
64         return null;
65     }
66     
67     public InputStream JavaDoc getContents() {
68         return new ByteArrayInputStream JavaDoc(JavaCompareUtilities.getBytes(fDocument.get(), "UTF-16")); //$NON-NLS-1$
69
}
70     
71     public String JavaDoc getCharset() {
72         return "UTF-16"; //$NON-NLS-1$
73
}
74
75     public IResource getResource() {
76         return fFile;
77     }
78     
79     static final ITypedElement[] buildEditions(ITypedElement target, IFile file) {
80
81         // setup array of editions
82
IFileState[] states= null;
83         // add available editions
84
try {
85             states= file.getHistory(null);
86         } catch (CoreException ex) {
87             JavaPlugin.log(ex);
88         }
89         
90         int count= 1;
91         if (states != null)
92             count+= states.length;
93
94         ITypedElement[] editions= new ITypedElement[count];
95         editions[0]= new ResourceNode(file);
96         if (states != null)
97             for (int i= 0; i < states.length; i++)
98                 editions[i+1]= new HistoryItem(target, states[i]);
99         return editions;
100     }
101     
102     /**
103      * Returns the corresponding place holder type for the given element.
104      * @return a place holder type (see ASTRewrite) or -1 if there is no corresponding placeholder
105      */

106     static final int getPlaceHolderType(ITypedElement element) {
107         
108         if (element instanceof DocumentRangeNode) {
109             JavaNode jn= (JavaNode) element;
110             switch (jn.getTypeCode()) {
111                 
112             case JavaNode.PACKAGE:
113                 return ASTNode.PACKAGE_DECLARATION;
114
115             case JavaNode.CLASS:
116             case JavaNode.INTERFACE:
117                 return ASTNode.TYPE_DECLARATION;
118                 
119             case JavaNode.ENUM:
120                 return ASTNode.ENUM_DECLARATION;
121                 
122             case JavaNode.ANNOTATION:
123                 return ASTNode.ANNOTATION_TYPE_DECLARATION;
124                 
125             case JavaNode.CONSTRUCTOR:
126             case JavaNode.METHOD:
127                 return ASTNode.METHOD_DECLARATION;
128                 
129             case JavaNode.FIELD:
130                 return ASTNode.FIELD_DECLARATION;
131                 
132             case JavaNode.INIT:
133                 return ASTNode.INITIALIZER;
134
135             case JavaNode.IMPORT:
136             case JavaNode.IMPORT_CONTAINER:
137                 return ASTNode.IMPORT_DECLARATION;
138
139             case JavaNode.CU:
140                 return ASTNode.COMPILATION_UNIT;
141             }
142         }
143         return -1;
144     }
145 }
146
Popular Tags