Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 50d8da27a5414c56f24e1a6dd799b6c33fc61d65 (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
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * 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.CompareUI;
import org.eclipse.compare.IStreamContentAccessor;
import org.eclipse.compare.contentmergeviewer.ContentMergeViewer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

/**
 */
public class ImageMergeViewer extends ContentMergeViewer {
	private static final String BUNDLE_NAME= "org.eclipse.compare.internal.ImageMergeViewerResources"; //$NON-NLS-1$

	private Object fLeftImage;
	private Object fRightImage;

	private ImageCanvas fAncestor;
	private ImageCanvas fLeft;
	private ImageCanvas fRight;

	public ImageMergeViewer(Composite parent, int styles, CompareConfiguration mp) {
		super(styles, ResourceBundle.getBundle(BUNDLE_NAME), mp);

		PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, ICompareContextIds.IMAGE_COMPARE_VIEW);

		buildControl(parent);
		String title= Utilities.getString(getResourceBundle(), "title"); //$NON-NLS-1$
		getControl().setData(CompareUI.COMPARE_VIEWER_TITLE, title);
	}

	@Override
	protected void updateContent(Object ancestor, Object left, Object right) {
		setInput(fAncestor, ancestor);

		fLeftImage= left;
		setInput(fLeft, left);

		fRightImage= right;
		setInput(fRight, right);
	}

	/*
	 * We can't modify the contents of either side we just return null.
	 */
	@Override
	protected byte[] getContents(boolean left) {
		return null;
	}

	@Override
	public void createControls(Composite composite) {
		fAncestor= new ImageCanvas(composite, SWT.NO_FOCUS);
		fLeft= new ImageCanvas(composite, SWT.NO_FOCUS);
		fRight= new ImageCanvas(composite, SWT.NO_FOCUS);
	}

	private static void setInput(ImageCanvas canvas, Object input) {
		if (canvas != null) {
			InputStream stream= null;
			try {
				if (input instanceof IStreamContentAccessor) {
					IStreamContentAccessor sca= (IStreamContentAccessor) input;
					if (sca != null) {
						try {
							stream= sca.getContents();
						} catch (CoreException e) {
							// NeedWork
						}
					}
				}

				Image image= null;
				Display display= canvas.getDisplay();
				if (stream != null) {
					try {
						image= new Image(display, stream);
					} catch (SWTException e) {
						// silently ignored
					}
				}

				canvas.setImage(image);
				if (image != null) {
					canvas.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
				} else {
					canvas.setBackground(null);
				}
			} finally {
				if (stream != null) {
					try {
						stream.close();
					} catch (IOException e) {
						// silently ignored
					}
				}
			}
		}
	}

	@Override
	protected void handleResizeAncestor(int x, int y, int width, int height) {
		if (width > 0) {
			fAncestor.setVisible(true);
			fAncestor.setBounds(x, y, width, height);
		} else {
			fAncestor.setVisible(false);
		}
	}

	@Override
	protected void handleResizeLeftRight(int x, int y, int width1, int centerWidth, int width2, int height) {
		fLeft.setBounds(x, y, width1, height);
		fRight.setBounds(x + width1 + centerWidth, y, width2, height);
	}

	@Override
	protected void copy(boolean leftToRight) {
		if (leftToRight) {
			fRightImage= fLeftImage;
			setInput(fRight, fRightImage);
			setRightDirty(true);
		} else {
			fLeftImage= fRightImage;
			setInput(fLeft, fLeftImage);
			setLeftDirty(true);
		}
	}
}

Back to the top