alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.pde.internal.ui.compare;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.*;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.swt.widgets.Composite;

public class PluginStructureViewer extends StructureDiffViewer {

	public PluginStructureViewer(Composite parent, CompareConfiguration config) {
		super(parent, config);
		setStructureCreator(new PluginStructureCreator());
		setComparator(new ViewerComparator() {
			public int compare(Viewer viewer, Object e1, Object e2) {
				if (e1 instanceof DiffNode) {
					if (e2 instanceof DiffNode) {
						ITypedElement e1Element = ((DiffNode) e1).getAncestor();
						ITypedElement e2Element = ((DiffNode) e2).getAncestor();
						if (!(e1Element instanceof DocumentRangeNode))
							e1Element = ((DiffNode) e1).getLeft();
						if (!(e2Element instanceof DocumentRangeNode))
							e2Element = ((DiffNode) e2).getLeft();
						if (e1Element instanceof DocumentRangeNode && e2Element instanceof DocumentRangeNode) {
							float e1off = getRelativeOffset(((DocumentRangeNode) e1Element));
							float e2off = getRelativeOffset(((DocumentRangeNode) e2Element));
							return e1off - e2off < 0 ? -1 : 1;
						}
						return 0;
					}
					return -1;
				}
				return 1;
			}

			// we may be comparing the ancestor to the left (local) document
			// since the lengths may be different, base the comparison on the relative position in the doc
			private float getRelativeOffset(DocumentRangeNode node) {
				float absoluteOffset = node.getRange().getOffset();
				float documentLength = node.getDocument().getLength();
				return absoluteOffset / documentLength;
			}
		});
	}
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.