KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > AssistContext


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.text.correction;
12
13 import org.eclipse.jdt.core.ICompilationUnit;
14 import org.eclipse.jdt.core.dom.ASTNode;
15 import org.eclipse.jdt.core.dom.CompilationUnit;
16
17 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
18
19 import org.eclipse.jdt.ui.text.java.IInvocationContext;
20
21 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
22
23 /**
24   */

25 public class AssistContext implements IInvocationContext {
26
27     private ICompilationUnit fCompilationUnit;
28     private int fOffset;
29     private int fLength;
30
31     private CompilationUnit fASTRoot;
32
33     /*
34      * Constructor for CorrectionContext.
35      */

36     public AssistContext(ICompilationUnit cu, int offset, int length) {
37         fCompilationUnit= cu;
38         fOffset= offset;
39         fLength= length;
40
41         fASTRoot= null;
42     }
43
44     /**
45      * Returns the compilation unit.
46      * @return Returns a ICompilationUnit
47      */

48     public ICompilationUnit getCompilationUnit() {
49         return fCompilationUnit;
50     }
51
52     /**
53      * Returns the length.
54      * @return int
55      */

56     public int getSelectionLength() {
57         return fLength;
58     }
59
60     /**
61      * Returns the offset.
62      * @return int
63      */

64     public int getSelectionOffset() {
65         return fOffset;
66     }
67
68     public CompilationUnit getASTRoot() {
69         if (fASTRoot == null) {
70             fASTRoot= ASTProvider.getASTProvider().getAST(fCompilationUnit, ASTProvider.WAIT_YES, null);
71             if (fASTRoot == null) {
72                 // see bug 63554
73
fASTRoot= ASTResolving.createQuickFixAST(fCompilationUnit, null);
74             }
75         }
76         return fASTRoot;
77     }
78
79
80     /**
81      * @param root The ASTRoot to set.
82      */

83     public void setASTRoot(CompilationUnit root) {
84         fASTRoot= root;
85     }
86
87     /*(non-Javadoc)
88      * @see org.eclipse.jdt.ui.text.java.IInvocationContext#getCoveringNode()
89      */

90     public ASTNode getCoveringNode() {
91         NodeFinder finder= new NodeFinder(fOffset, fLength);
92         getASTRoot().accept(finder);
93         return finder.getCoveringNode();
94     }
95
96     /*(non-Javadoc)
97      * @see org.eclipse.jdt.ui.text.java.IInvocationContext#getCoveredNode()
98      */

99     public ASTNode getCoveredNode() {
100         NodeFinder finder= new NodeFinder(fOffset, fLength);
101         getASTRoot().accept(finder);
102         return finder.getCoveredNode();
103     }
104
105 }
106
Popular Tags