1 11 package org.eclipse.jdt.internal.corext.dom; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 28 public class SelectionAnalyzer extends GenericVisitor { 29 30 private Selection fSelection; 31 private boolean fTraverseSelectedNode; 32 private ASTNode fLastCoveringNode; 33 34 private List 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 91 protected boolean visitNode(ASTNode node) { 92 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 return true; 111 } 112 113 protected void reset() { 114 fSelectedNodes= null; 115 } 116 117 protected void handleFirstSelectedNode(ASTNode node) { 118 fSelectedNodes= new ArrayList (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 internalGetSelectedNodes() { 133 return fSelectedNodes; 134 } 135 136 private boolean isFirstNode() { 137 return fSelectedNodes == null; 138 } 139 } 140 | Popular Tags |