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.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 20 import org.eclipse.ltk.core.refactoring.Change; 21 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 22 import org.eclipse.ltk.core.refactoring.TextEditBasedChange; 23 import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup; 24 25 import org.eclipse.jface.text.IRegion; 26 27 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer; 28 import org.eclipse.ltk.ui.refactoring.TextEditChangeNode; 29 import org.eclipse.ltk.ui.refactoring.TextEditChangeNode.ChildNode; 30 31 public abstract class InternalLanguageElementNode extends TextEditChangeNode.ChildNode { 32 33 private List fChildren; 34 private GroupCategorySet fGroupCategories; 35 36 protected InternalLanguageElementNode(PreviewNode parent) { 37 super(parent); 38 } 39 40 ChangePreviewViewerDescriptor getChangePreviewViewerDescriptor() throws CoreException { 41 InternalTextEditChangeNode element= getTextEditChangeNode(); 42 if (element == null) 43 return null; 44 return element.getChangePreviewViewerDescriptor(); 45 } 46 47 void feedInput(IChangePreviewViewer viewer, List categories) throws CoreException { 48 InternalTextEditChangeNode element= getTextEditChangeNode(); 49 if (element != null) { 50 Change change= element.getChange(); 51 if (change instanceof TextEditBasedChange) { 52 List groups= collectTextEditBasedChangeGroups(categories); 53 viewer.setInput(TextEditChangePreviewViewer.createInput(change, 54 (TextEditBasedChangeGroup[])groups.toArray(new TextEditBasedChangeGroup[groups.size()]), 55 getTextRange())); 56 } 57 } else { 58 viewer.setInput(null); 59 } 60 } 61 62 void setEnabled(boolean enabled) { 63 for (Iterator iter= fChildren.iterator(); iter.hasNext();) { 64 PreviewNode element= (PreviewNode)iter.next(); 65 element.setEnabled(enabled); 66 } 67 } 68 69 void setEnabledShallow(boolean enabled) { 70 } 72 73 int getActive() { 74 Assert.isTrue(fChildren.size() > 0); 75 int result= ((PreviewNode)fChildren.get(0)).getActive(); 76 for (int i= 1; i < fChildren.size(); i++) { 77 PreviewNode element= (PreviewNode)fChildren.get(i); 78 result= PreviewNode.ACTIVATION_TABLE[element.getActive()][result]; 79 if (result == PreviewNode.PARTLY_ACTIVE) 80 break; 81 } 82 return result; 83 } 84 85 PreviewNode[] getChildren() { 86 if (fChildren == null) 87 return PreviewNode.EMPTY_CHILDREN; 88 return (PreviewNode[]) fChildren.toArray(new PreviewNode[fChildren.size()]); 89 } 90 91 boolean hasOneGroupCategory(List categories) { 92 if (fChildren == null) 93 return false; 94 return getGroupCategorySet().containsOneCategory(categories); 95 } 96 97 boolean hasDerived() { 98 if (fChildren == null) 99 return false; 100 for (Iterator iter= fChildren.iterator(); iter.hasNext();) { 101 PreviewNode node= (PreviewNode)iter.next(); 102 if (node.hasDerived()) 103 return true; 104 } 105 return false; 106 } 107 108 private GroupCategorySet getGroupCategorySet() { 109 if (fGroupCategories == null) { 110 fGroupCategories= GroupCategorySet.NONE; 111 for (Iterator iter= fChildren.iterator(); iter.hasNext();) { 112 PreviewNode node= (PreviewNode)iter.next(); 113 GroupCategorySet other= null; 114 if (node instanceof TextEditGroupNode) { 115 other= ((TextEditGroupNode)node).getGroupCategorySet(); 116 } else if (node instanceof InternalLanguageElementNode) { 117 other= ((InternalLanguageElementNode)node).getGroupCategorySet(); 118 } else { 119 Assert.isTrue(false, "Shouldn't happen"); } 121 fGroupCategories= GroupCategorySet.union(fGroupCategories, other); 122 } 123 } 124 return fGroupCategories; 125 } 126 127 protected void internalAddChild(ChildNode child) { 128 if (fChildren == null) 129 fChildren= new ArrayList (2); 130 fChildren.add(child); 131 } 132 133 private List collectTextEditBasedChangeGroups(List categories) { 134 List result= new ArrayList (10); 135 PreviewNode[] children= getChildren(); 136 for (int i= 0; i < children.length; i++) { 137 PreviewNode child= children[i]; 138 if (child instanceof TextEditGroupNode) { 139 TextEditBasedChangeGroup changeGroup= ((TextEditGroupNode)child).getChangeGroup(); 140 if (categories == null || changeGroup.getGroupCategorySet().containsOneCategory(categories)) 141 result.add(changeGroup); 142 } else if (child instanceof InternalLanguageElementNode) { 143 result.addAll(((InternalLanguageElementNode)child).collectTextEditBasedChangeGroups(categories)); 144 } 145 } 146 return result; 147 } 148 149 155 public abstract IRegion getTextRange() throws CoreException; 156 } 157 | Popular Tags |