KickJava   Java API By Example, From Geeks To Geeks.

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


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.javaeditor.selectionactions;
12
13 import java.util.List JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.dialogs.MessageDialog;
19
20 import org.eclipse.jface.text.ITextSelection;
21
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.ISourceRange;
24 import org.eclipse.jdt.core.ISourceReference;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.dom.ASTNode;
27 import org.eclipse.jdt.core.dom.CompilationUnit;
28 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor;
29
30 import org.eclipse.jdt.internal.corext.SourceRange;
31 import org.eclipse.jdt.internal.corext.dom.Selection;
32 import org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer;
33
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
36 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
38
39 public abstract class StructureSelectionAction extends Action {
40
41     public static final String JavaDoc NEXT= "SelectNextElement"; //$NON-NLS-1$
42
public static final String JavaDoc PREVIOUS= "SelectPreviousElement"; //$NON-NLS-1$
43
public static final String JavaDoc ENCLOSING= "SelectEnclosingElement"; //$NON-NLS-1$
44
public static final String JavaDoc HISTORY= "RestoreLastSelection"; //$NON-NLS-1$
45

46     private JavaEditor fEditor;
47     private SelectionHistory fSelectionHistory;
48
49     protected StructureSelectionAction(String JavaDoc text, JavaEditor editor, SelectionHistory history) {
50         super(text);
51         Assert.isNotNull(editor);
52         Assert.isNotNull(history);
53         fEditor= editor;
54         fSelectionHistory= history;
55     }
56
57     /*
58      * This constructor is for testing purpose only.
59      */

60     protected StructureSelectionAction() {
61         super(""); //$NON-NLS-1$
62
}
63
64     /*
65      * Method declared in IAction.
66      */

67     public final void run() {
68         IJavaElement inputElement= EditorUtility.getEditorInputJavaElement(fEditor, false);
69         if (!(inputElement instanceof ISourceReference && inputElement.exists()))
70             return;
71
72         ISourceReference source= (ISourceReference)inputElement;
73         ISourceRange sourceRange;
74         try {
75             sourceRange= source.getSourceRange();
76             if (sourceRange == null || sourceRange.getLength() == 0) {
77                 MessageDialog.openInformation(fEditor.getEditorSite().getShell(),
78                     SelectionActionMessages.StructureSelect_error_title,
79                     SelectionActionMessages.StructureSelect_error_message);
80                 return;
81             }
82         } catch (JavaModelException e) {
83         }
84         ITextSelection selection= getTextSelection();
85         ISourceRange newRange= getNewSelectionRange(createSourceRange(selection), source);
86         // Check if new selection differs from current selection
87
if (selection.getOffset() == newRange.getOffset() && selection.getLength() == newRange.getLength())
88             return;
89         fSelectionHistory.remember(new SourceRange(selection.getOffset(), selection.getLength()));
90         try {
91             fSelectionHistory.ignoreSelectionChanges();
92             fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength());
93         } finally {
94             fSelectionHistory.listenToSelectionChanges();
95         }
96     }
97
98     public final ISourceRange getNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr) {
99         try{
100             CompilationUnit root= getAST(sr);
101             if (root == null)
102                 return oldSourceRange;
103             Selection selection= Selection.createFromStartLength(oldSourceRange.getOffset(), oldSourceRange.getLength());
104             SelectionAnalyzer selAnalyzer= new SelectionAnalyzer(selection, true);
105             root.accept(selAnalyzer);
106             return internalGetNewSelectionRange(oldSourceRange, sr, selAnalyzer);
107         } catch (JavaModelException e){
108             JavaPlugin.log(e); //dialog would be too heavy here
109
return new SourceRange(oldSourceRange.getOffset(), oldSourceRange.getLength());
110         }
111     }
112
113     /**
114      * Subclasses determine the actual new selection.
115      */

116     abstract ISourceRange internalGetNewSelectionRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException;
117
118     protected final ITextSelection getTextSelection() {
119         return (ITextSelection)fEditor.getSelectionProvider().getSelection();
120     }
121     
122     // -- helper methods for subclasses to fit a node range into the source range
123

124     protected static ISourceRange getLastCoveringNodeRange(ISourceRange oldSourceRange, ISourceReference sr, SelectionAnalyzer selAnalyzer) throws JavaModelException {
125         if (selAnalyzer.getLastCoveringNode() == null)
126             return oldSourceRange;
127         else
128             return getSelectedNodeSourceRange(sr, selAnalyzer.getLastCoveringNode());
129     }
130
131     protected static ISourceRange getSelectedNodeSourceRange(ISourceReference sr, ASTNode nodeToSelect) throws JavaModelException {
132         int offset= nodeToSelect.getStartPosition();
133         int end= Math.min(sr.getSourceRange().getLength(), nodeToSelect.getStartPosition() + nodeToSelect.getLength() - 1);
134         return createSourceRange(offset, end);
135     }
136
137     //-- private helper methods
138

139     private static ISourceRange createSourceRange(ITextSelection ts){
140         return new SourceRange(ts.getOffset(), ts.getLength());
141     }
142
143     private static CompilationUnit getAST(ISourceReference sr) {
144         return ASTProvider.getASTProvider().getAST((IJavaElement) sr, ASTProvider.WAIT_YES, null);
145     }
146
147     //-- helper methods for this class and subclasses
148

149     static ISourceRange createSourceRange(int offset, int end){
150         int length= end - offset + 1;
151         if (length == 0) //to allow 0-length selection
152
length= 1;
153         return new SourceRange(Math.max(0, offset), length);
154     }
155
156     static ASTNode[] getSiblingNodes(ASTNode node) {
157         ASTNode parent= node.getParent();
158         StructuralPropertyDescriptor locationInParent= node.getLocationInParent();
159         if (locationInParent.isChildListProperty()) {
160             List JavaDoc siblings= (List JavaDoc) parent.getStructuralProperty(locationInParent);
161             return (ASTNode[]) siblings.toArray(new ASTNode[siblings.size()]);
162         }
163         return null;
164     }
165
166     static int findIndex(Object JavaDoc[] array, Object JavaDoc o){
167         for (int i= 0; i < array.length; i++) {
168             Object JavaDoc object= array[i];
169             if (object == o)
170                 return i;
171         }
172         return -1;
173     }
174
175 }
176
Popular Tags