1 11 package org.eclipse.ltk.internal.ui.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.CoreException; 18 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.ISourceRange; 21 import org.eclipse.jdt.core.ISourceReference; 22 23 import org.eclipse.jface.text.IRegion; 24 import org.eclipse.jface.text.Region; 25 import org.eclipse.jface.util.Assert; 26 27 import org.eclipse.ltk.core.refactoring.Change; 28 import org.eclipse.ltk.core.refactoring.TextChange; 29 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup; 30 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer; 31 32 36 class PseudoJavaChangeElement extends ChangeElement { 37 38 private IJavaElement fJavaElement; 39 private List fChildren; 40 41 public PseudoJavaChangeElement(ChangeElement parent, IJavaElement element) { 42 super(parent); 43 fJavaElement= element; 44 Assert.isNotNull(fJavaElement); 45 } 46 47 52 public IJavaElement getJavaElement() { 53 return fJavaElement; 54 } 55 56 public Change getChange() { 57 return null; 58 } 59 60 public ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException { 61 DefaultChangeElement element= getDefaultChangeElement(); 62 if (element == null) 63 return null; 64 return element.getChangePreviewViewerDescriptor(); 65 } 66 67 public void feedInput(IChangePreviewViewer viewer) throws CoreException { 68 DefaultChangeElement element= getDefaultChangeElement(); 69 if (element != null) { 70 Change change= element.getChange(); 71 if (change instanceof TextChange) { 72 List edits= collectTextEditChanges(); 73 viewer.setInput(TextChangePreviewViewer.createInput(change, 74 (TextEditChangeGroup[])edits.toArray(new TextEditChangeGroup[edits.size()]), 75 getTextRange())); 76 } 77 } else { 78 viewer.setInput(null); 79 } 80 } 81 82 public void setActive(boolean active) { 83 for (Iterator iter= fChildren.iterator(); iter.hasNext();) { 84 ChangeElement element= (ChangeElement)iter.next(); 85 element.setActive(active); 86 } 87 } 88 89 public int getActive() { 90 Assert.isTrue(fChildren.size() > 0); 91 int result= ((ChangeElement)fChildren.get(0)).getActive(); 92 for (int i= 1; i < fChildren.size(); i++) { 93 ChangeElement element= (ChangeElement)fChildren.get(i); 94 result= ACTIVATION_TABLE[element.getActive()][result]; 95 if (result == PARTLY_ACTIVE) 96 break; 97 } 98 return result; 99 } 100 101 104 public ChangeElement[] getChildren() { 105 if (fChildren == null) 106 return EMPTY_CHILDREN; 107 return (ChangeElement[]) fChildren.toArray(new ChangeElement[fChildren.size()]); 108 } 109 110 116 public void addChild(TextEditChangeElement child) { 117 doAddChild(child); 118 } 119 120 126 public void addChild(PseudoJavaChangeElement child) { 127 doAddChild(child); 128 } 129 130 private void doAddChild(ChangeElement child) { 131 if (fChildren == null) 132 fChildren= new ArrayList (2); 133 fChildren.add(child); 134 } 135 136 private DefaultChangeElement getDefaultChangeElement() { 137 ChangeElement element= getParent(); 138 while(!(element instanceof DefaultChangeElement) && element != null) { 139 element= element.getParent(); 140 } 141 return (DefaultChangeElement)element; 142 } 143 144 private List collectTextEditChanges() { 145 List result= new ArrayList (10); 146 ChangeElement[] children= getChildren(); 147 for (int i= 0; i < children.length; i++) { 148 ChangeElement child= children[i]; 149 if (child instanceof TextEditChangeElement) { 150 result.add(((TextEditChangeElement)child).getTextEditChange()); 151 } else if (child instanceof PseudoJavaChangeElement) { 152 result.addAll(((PseudoJavaChangeElement)child).collectTextEditChanges()); 153 } 154 } 155 return result; 156 } 157 158 public IRegion getTextRange() throws CoreException { 159 ISourceRange range= ((ISourceReference)fJavaElement).getSourceRange(); 160 return new Region(range.getOffset(), range.getLength()); 161 } 162 } 163 164 | Popular Tags |