1 11 package org.eclipse.ltk.ui.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.List ; 17 18 import org.eclipse.core.resources.IFile; 19 20 import org.eclipse.ltk.core.refactoring.Change; 21 import org.eclipse.ltk.core.refactoring.TextEditBasedChange; 22 import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup; 23 import org.eclipse.ltk.core.refactoring.TextFileChange; 24 25 import org.eclipse.ltk.internal.ui.refactoring.InternalTextEditChangeNode; 26 import org.eclipse.ltk.internal.ui.refactoring.Messages; 27 import org.eclipse.ltk.internal.ui.refactoring.PreviewNode; 28 import org.eclipse.ltk.internal.ui.refactoring.RefactoringPluginImages; 29 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages; 30 import org.eclipse.ltk.internal.ui.refactoring.TextEditGroupNode; 31 32 import org.eclipse.jface.resource.ImageDescriptor; 33 34 47 public class TextEditChangeNode extends InternalTextEditChangeNode { 48 49 public static abstract class ChildNode extends PreviewNode { 50 protected ChildNode(PreviewNode parent) { 51 super(parent); 52 } 53 protected TextEditChangeNode getTextEditChangeNode() { 54 return internalGetTextEditChangeNode(this); 55 } 56 } 57 58 67 public static ChildNode createTextEditGroupNode(ChildNode parent, TextEditBasedChangeGroup changeGroup) { 68 return new TextEditGroupNode(parent, changeGroup); 69 } 70 71 80 public static ChildNode createTextEditGroupNode(TextEditChangeNode parent, TextEditBasedChangeGroup changeGroup) { 81 return new TextEditGroupNode(parent, changeGroup); 82 } 83 84 90 public TextEditChangeNode(TextEditBasedChange change) { 91 super(null, change); 93 } 94 95 101 public final TextEditBasedChange getTextEditBasedChange() { 102 return super.getTextEditBasedChange(); 103 } 104 105 111 public String getText() { 112 Change change= getTextEditBasedChange(); 113 if (change instanceof TextFileChange) { 114 IFile file= ((TextFileChange)change).getFile(); 115 return Messages.format( 116 RefactoringUIMessages.PreviewWizardPage_changeElementLabelProvider_textFormat, 117 new String [] {file.getName(), getPath(file)}); 118 } 119 return super.getText(); 120 } 121 122 128 public ImageDescriptor getImageDescriptor() { 129 return RefactoringPluginImages.DESC_OBJS_FILE_CHANGE; 130 } 131 132 protected ChildNode[] createChildNodes() { 133 TextEditBasedChangeGroup[] groups= getSortedChangeGroups(getTextEditBasedChange()); 134 ChildNode[] result= new ChildNode[groups.length]; 135 for (int i= 0; i < groups.length; i++) { 136 result[i]= new TextEditGroupNode(this, groups[i]); 137 } 138 return result; 139 } 140 141 private static class OffsetComparator implements Comparator { 142 public int compare(Object o1, Object o2) { 143 TextEditBasedChangeGroup c1= (TextEditBasedChangeGroup)o1; 144 TextEditBasedChangeGroup c2= (TextEditBasedChangeGroup)o2; 145 int p1= getOffset(c1); 146 int p2= getOffset(c2); 147 if (p1 < p2) 148 return -1; 149 if (p1 > p2) 150 return 1; 151 return 0; 153 } 154 private int getOffset(TextEditBasedChangeGroup edit) { 155 return edit.getRegion().getOffset(); 156 } 157 } 158 159 private TextEditBasedChangeGroup[] getSortedChangeGroups(TextEditBasedChange change) { 160 TextEditBasedChangeGroup[] groups= change.getChangeGroups(); 161 List result= new ArrayList (groups.length); 162 for (int i= 0; i < groups.length; i++) { 163 if (!groups[i].getTextEditGroup().isEmpty()) 164 result.add(groups[i]); 165 } 166 Comparator comparator= new OffsetComparator(); 167 Collections.sort(result, comparator); 168 return (TextEditBasedChangeGroup[])result.toArray(new TextEditBasedChangeGroup[result.size()]); 169 } 170 171 private String getPath(IFile file) { 172 StringBuffer result= new StringBuffer (file.getProject().getName()); 173 String projectRelativePath= file.getParent().getProjectRelativePath().toString(); 174 if (projectRelativePath.length() > 0) { 175 result.append('/'); 176 result.append(projectRelativePath); 177 } 178 return result.toString(); 179 } 180 } | Popular Tags |