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.ltk.core.refactoring.Change; 20 import org.eclipse.ltk.core.refactoring.TextChange; 21 import org.eclipse.ltk.core.refactoring.TextEditChangeGroup; 22 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer; 23 24 import org.eclipse.jface.text.IRegion; 25 import org.eclipse.jface.util.Assert; 26 27 public abstract class PseudoLanguageChangeElement extends ChangeElement { 28 29 private List fChildren; 30 31 public PseudoLanguageChangeElement(ChangeElement parent) { 32 super(parent); 33 } 34 35 public Change getChange() { 36 return null; 37 } 38 39 public ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException { 40 DefaultChangeElement element= getDefaultChangeElement(); 41 if (element == null) 42 return null; 43 return element.getChangePreviewViewerDescriptor(); 44 } 45 46 public void feedInput(IChangePreviewViewer viewer) throws CoreException { 47 DefaultChangeElement element= getDefaultChangeElement(); 48 if (element != null) { 49 Change change= element.getChange(); 50 if (change instanceof TextChange) { 51 List edits= collectTextEditChanges(); 52 viewer.setInput(TextChangePreviewViewer.createInput(change, 53 (TextEditChangeGroup[])edits.toArray(new TextEditChangeGroup[edits.size()]), 54 getTextRange())); 55 } 56 } else { 57 viewer.setInput(null); 58 } 59 } 60 61 public void setEnabled(boolean enabled) { 62 for (Iterator iter= fChildren.iterator(); iter.hasNext();) { 63 ChangeElement element= (ChangeElement)iter.next(); 64 element.setEnabled(enabled); 65 } 66 } 67 68 public void setEnabledShallow(boolean enabled) { 69 } 71 72 public int getActive() { 73 Assert.isTrue(fChildren.size() > 0); 74 int result= ((ChangeElement)fChildren.get(0)).getActive(); 75 for (int i= 1; i < fChildren.size(); i++) { 76 ChangeElement element= (ChangeElement)fChildren.get(i); 77 result= ACTIVATION_TABLE[element.getActive()][result]; 78 if (result == PARTLY_ACTIVE) 79 break; 80 } 81 return result; 82 } 83 84 87 public ChangeElement[] getChildren() { 88 if (fChildren == null) 89 return EMPTY_CHILDREN; 90 return (ChangeElement[]) fChildren.toArray(new ChangeElement[fChildren.size()]); 91 } 92 93 99 public void addChild(TextEditChangeElement child) { 100 doAddChild(child); 101 } 102 103 109 public void addChild(PseudoLanguageChangeElement child) { 110 doAddChild(child); 111 } 112 113 private void doAddChild(ChangeElement child) { 114 if (fChildren == null) 115 fChildren= new ArrayList (2); 116 fChildren.add(child); 117 } 118 119 private DefaultChangeElement getDefaultChangeElement() { 120 ChangeElement element= getParent(); 121 while(!(element instanceof DefaultChangeElement) && element != null) { 122 element= element.getParent(); 123 } 124 return (DefaultChangeElement)element; 125 } 126 127 private List collectTextEditChanges() { 128 List result= new ArrayList (10); 129 ChangeElement[] children= getChildren(); 130 for (int i= 0; i < children.length; i++) { 131 ChangeElement child= children[i]; 132 if (child instanceof TextEditChangeElement) { 133 result.add(((TextEditChangeElement)child).getTextEditChange()); 134 } else if (child instanceof PseudoLanguageChangeElement) { 135 result.addAll(((PseudoLanguageChangeElement)child).collectTextEditChanges()); 136 } 137 } 138 return result; 139 } 140 141 147 public abstract IRegion getTextRange() throws CoreException; 148 } 149 | Popular Tags |