1 11 package org.eclipse.pde.internal.ui.compare; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Comparator ; 16 17 import org.eclipse.compare.CompareConfiguration; 18 import org.eclipse.compare.ITypedElement; 19 import org.eclipse.compare.structuremergeviewer.DiffNode; 20 import org.eclipse.compare.structuremergeviewer.ICompareInput; 21 import org.eclipse.compare.structuremergeviewer.IStructureComparator; 22 import org.eclipse.compare.structuremergeviewer.StructureDiffViewer; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.core.runtime.NullProgressMonitor; 25 import org.eclipse.core.runtime.SubProgressMonitor; 26 import org.eclipse.jface.viewers.Viewer; 27 import org.eclipse.jface.viewers.ViewerComparator; 28 import org.eclipse.pde.internal.ui.PDEUIMessages; 29 import org.eclipse.swt.widgets.Composite; 30 31 42 public class XMLStructureViewer extends StructureDiffViewer { 43 44 class XMLComparator extends ViewerComparator { 45 46 public XMLComparator() { 47 super(); 48 } 49 50 public int category(Object node) { 51 if (node instanceof DiffNode) { 52 Object o = ((DiffNode) node).getId(); 53 if (o instanceof XMLNode) { 54 String xmlType= ((XMLNode) o).getXMLType(); 55 if (xmlType.equals(XMLStructureCreator.TYPE_ATTRIBUTE)) 56 return 1; 57 if (xmlType.equals(XMLStructureCreator.TYPE_ELEMENT) 58 || xmlType.equals(XMLStructureCreator.TYPE_TEXT) 59 || xmlType.equals(XMLStructureCreator.TYPE_EXTENSION) 60 || xmlType.equals(XMLStructureCreator.TYPE_EXTENSIONPOINT)) 61 return 2; 62 } 63 } 64 return 0; 65 } 66 67 public void sort(final Viewer viewer, Object [] elements) { 68 if (elements != null 69 && elements.length > 0 70 && elements[0] instanceof DiffNode) { 71 Object o = ((DiffNode) elements[0]).getId(); 72 if (o instanceof XMLNode) { 73 XMLNode parent = ((XMLNode) o).getParent(); 74 String sig = parent.getSignature(); 75 if (sig.endsWith(XMLStructureCreator.SIGN_ELEMENT)) { 76 final ArrayList originalTree = 77 new ArrayList (Arrays.asList(parent.getChildren())); 78 Arrays.sort(elements, new Comparator () { 79 public int compare(Object a, Object b) { 80 return XMLComparator.this.compare( 81 (DiffNode) a, 82 (DiffNode) b, 83 originalTree); 84 } 85 }); 86 return; 87 } 88 } 89 } 90 super.sort(viewer, elements); 91 } 92 93 private int compare(DiffNode a, DiffNode b, ArrayList originalTree) { 94 95 int index_a = originalTree.indexOf(a.getId()); 96 int index_b = originalTree.indexOf(b.getId()); 97 if (index_a < index_b) 98 return -1; 99 return 1; 100 } 101 } 102 103 109 public XMLStructureViewer(Composite parent, CompareConfiguration configuration) { 110 super(parent, configuration); 111 setStructureCreator(new XMLStructureCreator()); 112 setComparator(new XMLComparator()); 113 } 114 115 116 protected XMLStructureCreator getXMLStructureCreator() { 117 return (XMLStructureCreator) getStructureCreator(); 118 } 119 120 121 124 protected void compareInputChanged(ICompareInput input) { 125 if (input != null) { 126 ITypedElement t = input.getLeft(); 127 if (t != null) { 128 String fileExtension = t.getType(); 129 getXMLStructureCreator().setFileExtension(fileExtension); 130 } 131 } 132 133 getXMLStructureCreator().initIdMaps(); 134 super.compareInputChanged(input); 135 136 } 137 138 protected void preDiffHook( 139 IStructureComparator ancestor, 140 IStructureComparator left, 141 IStructureComparator right, IProgressMonitor monitor) { 142 if (left != null && right != null) { 143 performMatching((XMLNode)left, (XMLNode)right, (XMLNode)ancestor, monitor); 144 } 145 } 146 147 148 private void performMatching(final XMLNode left, final XMLNode right, 149 final XMLNode ancestor, IProgressMonitor monitor) { 150 151 if (monitor == null) 152 monitor = new NullProgressMonitor(); 153 154 int totalWork; 155 if (ancestor != null) 156 totalWork = 1; 157 else 158 totalWork = 3; 159 monitor.beginTask(PDEUIMessages.XMLStructureViewer_taskName, totalWork); 160 161 AbstractMatching m = new OrderedMatching(); 162 try { 163 m.match(left, right, false, monitor); 164 if (ancestor != null) { 165 m.match(left, ancestor, true, 166 new SubProgressMonitor(monitor, 1)); 167 m.match(right, ancestor, true, 168 new SubProgressMonitor(monitor, 1)); 169 } 170 } finally { 171 monitor.done(); 172 } 173 } 174 175 } 176 | Popular Tags |