KickJava   Java API By Example, From Geeks To Geeks.

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


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 StructureSelectPreviousAction extends StructureSelectionAction {
27
28     private static class PreviousNodeAnalyzer extends GenericVisitor {
29         private final int fOffset;
30         private ASTNode fPreviousNode;
31         private PreviousNodeAnalyzer(int offset) {
32             super(true);
33             fOffset= offset;
34         }
35         public static ASTNode perform(int offset, ASTNode lastCoveringNode) {
36             PreviousNodeAnalyzer analyzer= new PreviousNodeAnalyzer(offset);
37             lastCoveringNode.accept(analyzer);
38             return analyzer.fPreviousNode;
39         }
40         protected boolean visitNode(ASTNode node) {
41             int start= node.getStartPosition();
42             int end= start + node.getLength();
43             if (end == fOffset) {
44                 fPreviousNode= node;
45                 return true;
46             } else {
47                 return (start < fOffset && fOffset < end);
48             }
49         }
50     }
51
52     public StructureSelectPreviousAction(JavaEditor editor, SelectionHistory history) {
53         super(SelectionActionMessages.StructureSelectPrevious_label, editor, history);
54         setToolTipText(SelectionActionMessages.StructureSelectPrevious_tooltip);
55         setDescription(SelectionActionMessages.StructureSelectPrevious_description);
56         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.STRUCTURED_SELECT_PREVIOUS_ACTION);
57     }
58
59     /*
60      * This constructor is for testing purpose only.
61      */

62     public StructureSelectPreviousAction() {
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= PreviousNodeAnalyzer.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 previousNode= getPreviousNode(parent, selAnalyzer.getSelectedNodes()[0]);
83         if (previousNode == parent)
84             return getSelectedNodeSourceRange(sr, parent);
85
86         int offset= previousNode.getStartPosition();
87         int end= oldSourceRange.getOffset() + oldSourceRange.getLength() - 1;
88         return StructureSelectionAction.createSourceRange(offset, end);
89     }
90
91     private static ASTNode getPreviousNode(ASTNode parent, ASTNode node){
92         ASTNode[] siblingNodes= StructureSelectionAction.getSiblingNodes(node);
93         if (siblingNodes == null || siblingNodes.length == 0)
94             return parent;
95         if (node == siblingNodes[0]) {
96             return parent;
97         } else {
98             int index= StructureSelectionAction.findIndex(siblingNodes, node);
99             if (index < 1)
100                 return parent;
101             return siblingNodes[index - 1];
102         }
103     }
104 }
105
106
Popular Tags