Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d9bb9563d32043f1d41f7cc4c8c5fc596fac5e71 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * Copyright (c) 2000, 2011 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.compare.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.ResourceBundle;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.ITypedElement;
import org.eclipse.compare.structuremergeviewer.ICompareInput;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.PlatformUI;

import com.ibm.icu.text.MessageFormat;

/**
 * A simple compare viewer for binary files.
 * Shows the position of the first non-matching byte.
 */
public class BinaryCompareViewer extends AbstractViewer {

	private static final String BUNDLE_NAME = "org.eclipse.compare.internal.BinaryCompareViewerResources"; //$NON-NLS-1$
	
	private static final int EOF = -1;
	private ICompareInput fInput;
	private ResourceBundle fBundle;
	private boolean fLeftIsLocal;

	private Composite fComposite;
	private Label fMessage;
	
	public BinaryCompareViewer(Composite parent, final CompareConfiguration cc) {
		
		PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, ICompareContextIds.BINARY_COMPARE_VIEW);

		fBundle= ResourceBundle.getBundle(BUNDLE_NAME);
		
		fComposite= new Composite(parent, SWT.NONE);
		RowLayout rowLayout = new RowLayout();
		rowLayout.type = SWT.VERTICAL;
		fComposite.setLayout(rowLayout);

		fMessage= new Label(fComposite, SWT.WRAP);
		fComposite.setData(CompareUI.COMPARE_VIEWER_TITLE, Utilities.getString(fBundle, "title")); //$NON-NLS-1$
		
		fLeftIsLocal= Utilities.getBoolean(cc, "LEFT_IS_LOCAL", false); //$NON-NLS-1$
		
		if (cc != null && cc.getContainer() instanceof CompareEditorInput) {
			Label compareAsTextLabel = new Label(fComposite, SWT.WRAP);
			compareAsTextLabel
					.setText(Utilities.getString(fBundle, "compareAsText")); //$NON-NLS-1$
		}
	}

	public Control getControl() {
		return fComposite;
	}

	public void setInput(Object input) {
		if (fComposite != null && input instanceof ICompareInput) {
			fInput= (ICompareInput) input;

			InputStream left= null;
			InputStream right= null;
			
			String message= null;
			try {
				left= getStream(fInput.getLeft());
				right= getStream(fInput.getRight());
				
				if (left != null && right != null) {
					int pos= 0;
					while (true) {
						int l= left.read();
						int r= right.read();
						if (l != r) {
							String format= Utilities.getString(fBundle, "diffMessageFormat"); //$NON-NLS-1$
							message= MessageFormat.format(format, new String[] { Integer.toString(pos) } );
							break;
						}
						if (l == EOF)
							break;
						pos++;
					}
				} else if (left == null && right == null) {
					message= Utilities.getString(fBundle, "deleteConflictMessage"); //$NON-NLS-1$
				} else if (left == null) {
					if (fLeftIsLocal)
						message= Utilities.getString(fBundle, "deletedMessage"); //$NON-NLS-1$
					else
						message= Utilities.getString(fBundle, "addedMessage"); //$NON-NLS-1$
				} else if (right == null) {
					if (fLeftIsLocal)
						message= Utilities.getString(fBundle, "addedMessage"); //$NON-NLS-1$
					else
						message= Utilities.getString(fBundle, "deletedMessage"); //$NON-NLS-1$
				}
			} catch (CoreException ex) {
				message = Utilities.getString(fBundle, "errorMessage"); //$NON-NLS-1$
				CompareUIPlugin.log(ex);
			} catch (IOException ex) {
				message = Utilities.getString(fBundle, "errorMessage"); //$NON-NLS-1$
				CompareUIPlugin.log(ex);
			} finally {
				Utilities.close(left);
				Utilities.close(right);
			}
			if (message != null)
				fMessage.setText(message);
			fComposite.layout();
		}
	}

	public Object getInput() {
		return fInput;
	}

	private InputStream getStream(ITypedElement input) throws CoreException {
		if (input instanceof IStreamContentAccessor)
			return ((IStreamContentAccessor)input).getContents();
		return null;
	}
}

Back to the top