1 11 package org.eclipse.jdt.internal.ui.javaeditor.selectionactions; 12 13 import java.util.List ; 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 NEXT= "SelectNextElement"; public static final String PREVIOUS= "SelectPreviousElement"; public static final String ENCLOSING= "SelectEnclosingElement"; public static final String HISTORY= "RestoreLastSelection"; 46 private JavaEditor fEditor; 47 private SelectionHistory fSelectionHistory; 48 49 protected StructureSelectionAction(String 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 60 protected StructureSelectionAction() { 61 super(""); } 63 64 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 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); return new SourceRange(oldSourceRange.getOffset(), oldSourceRange.getLength()); 110 } 111 } 112 113 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 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 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 149 static ISourceRange createSourceRange(int offset, int end){ 150 int length= end - offset + 1; 151 if (length == 0) 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 siblings= (List ) parent.getStructuralProperty(locationInParent); 161 return (ASTNode[]) siblings.toArray(new ASTNode[siblings.size()]); 162 } 163 return null; 164 } 165 166 static int findIndex(Object [] array, Object o){ 167 for (int i= 0; i < array.length; i++) { 168 Object object= array[i]; 169 if (object == o) 170 return i; 171 } 172 return -1; 173 } 174 175 } 176 | Popular Tags |