Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bb419e530d4ab2bcd433758ea401a32e5729dff3 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * 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.lang.reflect.InvocationTargetException;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.CompareEditorInput;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * This is a dialog that can host a {@link CompareEditorInput}.
 * <p>
 * This class can be used as is or can be subclassed.
 *
 * @since 3.3
 */
public class CompareDialog extends TrayDialog implements IPropertyChangeListener {

	private final CompareEditorInput fCompareEditorInput;
	private Button fCommitButton;
	private Label statusLabel;
	boolean hasSettings = true;
	private final DialogCompareContainer fContainer = new DialogCompareContainer();

	private class DialogCompareContainer extends CompareContainer {

		@Override
		public void run(boolean fork, boolean cancelable,
				IRunnableWithProgress runnable) throws InvocationTargetException,
				InterruptedException {
			ProgressMonitorDialog dialog = new ProgressMonitorDialog(getShell());
			dialog.run(fork, cancelable, runnable);
		}

		@Override
		public void setStatusMessage(String message) {
			if (statusLabel != null && !statusLabel.isDisposed()) {
				if (message == null) {
					statusLabel.setText(""); //$NON-NLS-1$
				} else {
					statusLabel.setText(message);
				}
			}
		}
	}

	/**
	 * Create a dialog to host the given input.
	 * @param shell a shell
	 * @param input the dialog input
	 */
	public CompareDialog(Shell shell, CompareEditorInput input) {
		super(shell);
		setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
		Assert.isNotNull(input);
		fCompareEditorInput= input;
	}

	@Override
	public boolean close() {
		if (super.close()) {
			if (fCompareEditorInput != null)
				fCompareEditorInput.removePropertyChangeListener(this);
			return true;
		}
		return false;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		fCommitButton= createButton(parent, IDialogConstants.OK_ID, getOKButtonLabel(), true);
		fCommitButton.setEnabled(isOKEnabled());
		if (isCancelable()) {
			createButton(parent, IDialogConstants.CANCEL_ID, getCancelButtonLabel(), false);
		}
	}

	private String getCancelButtonLabel() {
		return fCompareEditorInput.getCancelButtonLabel();
	}

	private boolean isCancelable() {
		return isInputEditable() || isElementSelectionDialog();
	}

	private String getOKButtonLabel() {
		return fCompareEditorInput.getOKButtonLabel();
	}

	private boolean isElementSelectionDialog() {
		return fCompareEditorInput.isEditionSelectionDialog();
	}

	/**
	 * Return whether the compare editor input of this dialog is editable.
	 * By default, the input is editable if the compare configuration
	 * indicates that either the left or right sides are editable.
	 * Subclasses may override.
	 * @return whether the compare editor input of this dialog is editable
	 * @see CompareConfiguration#isLeftEditable()
	 * @see CompareConfiguration#isRightEditable()
	 */
	protected boolean isInputEditable() {
		return fCompareEditorInput.getCompareConfiguration().isLeftEditable()
			|| fCompareEditorInput.getCompareConfiguration().isRightEditable();
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {
		if (event.getProperty().equals(CompareEditorInput.DIRTY_STATE)
				|| event.getProperty().equals(CompareEditorInput.PROP_SELECTED_EDITION)) {
			if (fCommitButton != null && fCompareEditorInput != null)
				fCommitButton.setEnabled(isOKEnabled());
		} else if (event.getProperty().equals(CompareEditorInput.PROP_TITLE)) {
			getShell().setText(fCompareEditorInput.getTitle());
		} else if (event.getProperty().equals(CompareEditorInput.PROP_TITLE_IMAGE)) {
			getShell().setImage(fCompareEditorInput.getTitleImage());
		}
	}

	private boolean isOKEnabled() {
		if (isInputEditable())
			return fCompareEditorInput.isDirty();
		if (isElementSelectionDialog())
			return getSelectedElement() != null;
		return true;
	}

	private Object getSelectedElement() {
		return fCompareEditorInput.getSelectedEdition();
	}

	@Override
	protected Control createDialogArea(Composite parent2) {

		Composite parent= (Composite) super.createDialogArea(parent2);

		Control c= fCompareEditorInput.createContents(parent);
		c.setLayoutData(new GridData(GridData.FILL_BOTH));

		statusLabel = new Label(parent, SWT.NONE);
		statusLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		Shell shell= c.getShell();
		shell.setText(fCompareEditorInput.getTitle());
		shell.setImage(fCompareEditorInput.getTitleImage());
		applyDialogFont(parent);
		return parent;
	}

	@Override
	public int open() {
		// Before opening, set the container of the input and listen
		// for changes to the input
		fCompareEditorInput.addPropertyChangeListener(this);
		fCompareEditorInput.setContainer(fContainer);
		return super.open();
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == OK) {
			if (!fCompareEditorInput.okPressed())
				return;
		} else if (buttonId == CANCEL) {
			fCompareEditorInput.cancelPressed();
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected IDialogSettings getDialogBoundsSettings() {
		IDialogSettings compareSettings = CompareUIPlugin.getDefault().getDialogSettings();
		String sectionName = this.getClass().getName();
		IDialogSettings dialogSettings = compareSettings.getSection(sectionName);
		if (dialogSettings == null) {
			hasSettings = false;
			dialogSettings = compareSettings.addNewSection(sectionName);
		}
		return dialogSettings;
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		if (getHelpContextId() != null)
			PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell, getHelpContextId());
	}

	/**
	 * Return the help content id for this dialog or <code>null</code>.
	 * By default, a generic help content id is returned. Subclasses may
	 * override.
	 * @return the help content id for this dialog or <code>null</code>
	 */
	public String getHelpContextId() {
		return ICompareContextIds.COMPARE_DIALOG;
	}

	@Override
	protected Point getInitialSize() {
		Point initialSize = super.getInitialSize();
		if (hasSettings) {
			return initialSize;
		}
		return getDefaultSize();
	}

	/**
	 * If we don't have settings we need to come up with a reasonable default
	 * since we can't depend on the compare editor input layout returning a
	 * good default size.
	 * @return the default size of the dialog
	 */
	protected Point getDefaultSize() {
		int width= 0;
		int height= 0;
		Shell shell= getParentShell();
		if (shell != null) {
			Point parentSize= shell.getSize();
			width= parentSize.x-100;
			height= parentSize.y-100;
		}
		if (width < 700)
			width= 700;
		if (height < 500)
			height= 500;
		return new Point(width, height);
	}

	/**
	 * Return the compare editor input for this dialog.
	 * @return the compare editor input for this dialog
	 */
	protected final CompareEditorInput getInput() {
		return fCompareEditorInput;
	}

}

Back to the top