01: /*******************************************************************************
02: * Copyright (c) 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.ui.compare;
11:
12: import org.eclipse.compare.CompareConfiguration;
13: import org.eclipse.compare.ITypedElement;
14: import org.eclipse.compare.structuremergeviewer.DiffNode;
15: import org.eclipse.compare.structuremergeviewer.DocumentRangeNode;
16: import org.eclipse.compare.structuremergeviewer.StructureDiffViewer;
17: import org.eclipse.jface.viewers.Viewer;
18: import org.eclipse.jface.viewers.ViewerComparator;
19: import org.eclipse.swt.widgets.Composite;
20:
21: public class PluginStructureViewer extends StructureDiffViewer {
22:
23: public PluginStructureViewer(Composite parent,
24: CompareConfiguration config) {
25: super (parent, config);
26: setStructureCreator(new PluginStructureCreator());
27: setComparator(new ViewerComparator() {
28: public int compare(Viewer viewer, Object e1, Object e2) {
29: if (e1 instanceof DiffNode) {
30: if (e2 instanceof DiffNode) {
31: ITypedElement e1Element = ((DiffNode) e1)
32: .getAncestor();
33: ITypedElement e2Element = ((DiffNode) e2)
34: .getAncestor();
35: if (!(e1Element instanceof DocumentRangeNode))
36: e1Element = ((DiffNode) e1).getLeft();
37: if (!(e2Element instanceof DocumentRangeNode))
38: e2Element = ((DiffNode) e2).getLeft();
39: if (e1Element instanceof DocumentRangeNode
40: && e2Element instanceof DocumentRangeNode) {
41: float e1off = getRelativeOffset(((DocumentRangeNode) e1Element));
42: float e2off = getRelativeOffset(((DocumentRangeNode) e2Element));
43: return e1off - e2off < 0 ? -1 : 1;
44: }
45: return 0;
46: }
47: return -1;
48: }
49: return 1;
50: }
51:
52: // we may be comparing the ancestor to the left (local) document
53: // since the lengths may be different, base the comparison on the relative position in the doc
54: private float getRelativeOffset(DocumentRangeNode node) {
55: float absoluteOffset = node.getRange().getOffset();
56: float documentLength = node.getDocument().getLength();
57: return absoluteOffset / documentLength;
58: }
59: });
60: }
61: }
|