KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > selectionactions > StructureSelectNextAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.javaeditor.selectionactions;
12
13 import org.eclipse.jdt.core.ISourceRange;
14 import org.eclipse.jdt.core.ISourceReference;
15 import org.eclipse.jdt.core.JavaModelException;
16 import org.eclipse.jdt.core.dom.ASTNode;
17
18 import org.eclipse.ui.PlatformUI;
19
20 import org.eclipse.jdt.internal.corext.dom.GenericVisitor;
21 import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer;
22
23 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
24 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
25
26 public class StructureSelectNextAction extends StructureSelectionAction{
27
28     private static class NextNodeAnalyzer extends GenericVisitor {
29         private final int fOffset;
30         private ASTNode fNextNode;
31         private NextNodeAnalyzer(int offset) {
32             super(true);
33             fOffset= offset;
34         }
35         public static ASTNode perform(int offset, ASTNode lastCoveringNode) {
36             NextNodeAnalyzer analyzer= new NextNodeAnalyzer(offset);
37             lastCoveringNode.accept(analyzer);
38             return analyzer.fNextNode;
39         }
40         protected boolean visitNode(ASTNode node) {
41             int start= node.getStartPosition();
42             int end= start + node.getLength();
43             if (start == fOffset) {
44                 fNextNode= node;
45                 return true;
46             } else {
47                 return (start < fOffset && fOffset < end);
48             }
49         }
50     }
51
52     public StructureSelectNextAction(JavaEditor editor, SelectionHistory history) {
53         super(SelectionActionMessages.StructureSelectNext_label, editor, history);
54         setToolTipText(SelectionActionMessages.StructureSelectNext_tooltip);
55         setDescription(SelectionActionMessages.StructureSelectNext_description);
56         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.STRUCTURED_SELECT_NEXT_ACTION);
57     }
58
59     /*
60      * This constructor is for testing purpose only.
61      */

62     public StructureSelectNextAction() {
63     }
64
65     /* non java doc
66      * @see StructureSelectionAction#internalGetNewSelectionRange(ISourceRange, ICompilationUnit, SelectionAnalyzer)
67      */

68     ISourceRange internalGetNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException{
69         if (oldSourceRange.getLength() == 0 && selAnalyzer.getLastCoveringNode() != null) {
70             ASTNode previousNode= NextNodeAnalyzer.perform(oldSourceRange.getOffset(), selAnalyzer.getLastCoveringNode());
71             if (previousNode != null)
72                 return getSelectedNodeSourceRange(sr, previousNode);
73         }
74         ASTNode first= selAnalyzer.getFirstSelectedNode();
75         if (first == null)
76             return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer);
77
78         ASTNode parent= first.getParent();
79         if (parent == null)
80             return getLastCoveringNodeRange(oldSourceRange, sr, selAnalyzer);
81
82         ASTNode lastSelectedNode= selAnalyzer.getSelectedNodes()[selAnalyzer.getSelectedNodes().length - 1];
83         ASTNode nextNode= getNextNode(parent, lastSelectedNode);
84         if (nextNode == parent)
85             return getSelectedNodeSourceRange(sr, first.getParent());
86         int offset= oldSourceRange.getOffset();
87         int end= Math.min(sr.getSourceRange().getLength(), nextNode.getStartPosition() + nextNode.getLength() - 1);
88         return StructureSelectionAction.createSourceRange(offset, end);
89     }
90
91     private static ASTNode getNextNode(ASTNode parent, ASTNode node){
92         ASTNode[] siblingNodes= StructureSelectionAction.getSiblingNodes(node);
93         if (siblingNodes == null || siblingNodes.length == 0)
94             return parent;
95         if (node == siblingNodes[siblingNodes.length -1 ])
96             return parent;
97         else
98             return siblingNodes[StructureSelectionAction.findIndex(siblingNodes, node) + 1];
99     }
100 }
101
102
Popular Tags