KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > dom > SelectionAnalyzer


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.corext.dom;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.jdt.core.dom.ASTNode;
19 import org.eclipse.jdt.core.dom.Expression;
20
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.text.Region;
23
24
25 /**
26  * Maps a selection to a set of AST nodes.
27  */

28 public class SelectionAnalyzer extends GenericVisitor {
29     
30     private Selection fSelection;
31     private boolean fTraverseSelectedNode;
32     private ASTNode fLastCoveringNode;
33     
34     // Selected nodes
35
private List JavaDoc fSelectedNodes;
36     
37     public SelectionAnalyzer(Selection selection, boolean traverseSelectedNode) {
38         super(true);
39         Assert.isNotNull(selection);
40         fSelection= selection;
41         fTraverseSelectedNode= traverseSelectedNode;
42     }
43     
44     public boolean hasSelectedNodes() {
45         return fSelectedNodes != null && !fSelectedNodes.isEmpty();
46     }
47     
48     public ASTNode[] getSelectedNodes() {
49         if (fSelectedNodes == null || fSelectedNodes.isEmpty())
50             return new ASTNode[0];
51         return (ASTNode[]) fSelectedNodes.toArray(new ASTNode[fSelectedNodes.size()]);
52     }
53     
54     public ASTNode getFirstSelectedNode() {
55         if (fSelectedNodes == null || fSelectedNodes.isEmpty())
56             return null;
57         return (ASTNode)fSelectedNodes.get(0);
58     }
59     
60     public ASTNode getLastSelectedNode() {
61         if (fSelectedNodes == null || fSelectedNodes.isEmpty())
62             return null;
63         return (ASTNode)fSelectedNodes.get(fSelectedNodes.size() - 1);
64     }
65     
66     public boolean isExpressionSelected() {
67         if (!hasSelectedNodes())
68             return false;
69         return fSelectedNodes.get(0) instanceof Expression;
70     }
71     
72     public IRegion getSelectedNodeRange() {
73         if (fSelectedNodes == null || fSelectedNodes.isEmpty())
74             return null;
75         ASTNode firstNode= (ASTNode)fSelectedNodes.get(0);
76         ASTNode lastNode= (ASTNode)fSelectedNodes.get(fSelectedNodes.size() - 1);
77         int start= firstNode.getStartPosition();
78         return new Region(start, lastNode.getStartPosition() + lastNode.getLength() - start);
79     }
80     
81     public ASTNode getLastCoveringNode() {
82         return fLastCoveringNode;
83     }
84     
85     protected Selection getSelection() {
86         return fSelection;
87     }
88     
89     //--- node management ---------------------------------------------------------
90

91     protected boolean visitNode(ASTNode node) {
92         // The selection lies behind the node.
93
if (fSelection.liesOutside(node)) {
94             return false;
95         } else if (fSelection.covers(node)) {
96             if (isFirstNode()) {
97                 handleFirstSelectedNode(node);
98             } else {
99                 handleNextSelectedNode(node);
100             }
101             return fTraverseSelectedNode;
102         } else if (fSelection.coveredBy(node)) {
103             fLastCoveringNode= node;
104             return true;
105         } else if (fSelection.endsIn(node)) {
106             return handleSelectionEndsIn(node);
107         }
108         // There is a possibility that the user has selected trailing semicolons that don't belong
109
// to the statement. So dive into it to check if sub nodes are fully covered.
110
return true;
111     }
112     
113     protected void reset() {
114         fSelectedNodes= null;
115     }
116     
117     protected void handleFirstSelectedNode(ASTNode node) {
118         fSelectedNodes= new ArrayList JavaDoc(5);
119         fSelectedNodes.add(node);
120     }
121     
122     protected void handleNextSelectedNode(ASTNode node) {
123         if (getFirstSelectedNode().getParent() == node.getParent()) {
124             fSelectedNodes.add(node);
125         }
126     }
127
128     protected boolean handleSelectionEndsIn(ASTNode node) {
129         return false;
130     }
131     
132     protected List JavaDoc internalGetSelectedNodes() {
133         return fSelectedNodes;
134     }
135     
136     private boolean isFirstNode() {
137         return fSelectedNodes == null;
138     }
139 }
140
Popular Tags