Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4121bf8bc508978544411fe277f68c21b381193d (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
/*******************************************************************************
 * Copyright (c) 2011 CEA LIST.
 * 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:
 *     Nicolas Bros (Mia-Software) - Bug 366367 - To be able to change the "CanBePresentedInTheTable" query
 *     Gregoire Dupe (Mia-Software) - Bug 373248 - Clean PMD errors
 *     Gregoire Dupe (Mia-Software) - Bug 374903 - [Table] ITableWidget.setLoadedFacetSets
 *******************************************************************************/
package org.eclipse.papyrus.emf.facet.util.ui.internal.dialog;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.papyrus.emf.facet.util.ui.internal.Messages;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.dialog.IDialogCallback;
import org.eclipse.papyrus.emf.facet.util.ui.internal.exported.dialog.IQuestionDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * A yes/no dialog, that replicates the JFace MessageDialog#openQuestion, but is designed to be easily testable with JUnit
 * 
 * @since 0.2
 */
public class QuestionDialog implements IQuestionDialog {

	private static final int SHELL_WIDTH = 450;
	private static final int SHELL_HEIGHT = 170;

	private final Shell parentShell;
	private final List<Runnable> listeners = new ArrayList<Runnable>();
	private Boolean result = null;
	private Shell shell;
	private Button btnYes;
	private Button btnNo;
	private final String title;
	private final String message;
	private IDialogCallback<Boolean> callback;

	public QuestionDialog(final Shell parent, final String title,
			final String message, final IDialogCallback<Boolean> callback) {
		this.parentShell = parent;
		this.title = title;
		this.message = message;
		this.callback = callback;
		createContents();
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public final void open() {
		this.shell.layout();
		// center the shell on its parent (if not parent shell then it will not be centered)
		if (this.parentShell != null) {
			final Rectangle parentShellBounds = this.parentShell.getBounds();
			final Point shellSize = this.shell.getSize();
			this.shell.setLocation(
					parentShellBounds.x + (parentShellBounds.width - shellSize.x) / 2,
					parentShellBounds.y + (parentShellBounds.height - shellSize.y) / 2);
		}
		this.shell.open();
	}

	/** Create the contents of the dialog. */
	private void createContents() {
		final int buttonWidthHint = 80;
		this.shell = new Shell(this.parentShell, SWT.BORDER | SWT.RESIZE | SWT.TITLE
				| SWT.APPLICATION_MODAL);
		this.shell.setSize(QuestionDialog.SHELL_WIDTH,
				QuestionDialog.SHELL_HEIGHT);
		this.shell.setText(this.title);
		final GridLayout shellLayout = new GridLayout(2, false);

		// constants to make checkstyle happy
		final int marginLeft = 5;
		final int marginTop = 8;
		final int verticalSpacing = 10;
		final int horizontalSpacing = 15;
		shellLayout.marginLeft = marginLeft;
		shellLayout.marginTop = marginTop;
		shellLayout.verticalSpacing = verticalSpacing;
		shellLayout.horizontalSpacing = horizontalSpacing;

		this.shell.setLayout(shellLayout);

		final Label image = new Label(this.shell, SWT.NONE);
		image.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
		image.setImage(Display.getDefault().getSystemImage(SWT.ICON_QUESTION));

		final Label labelMessage = new Label(this.shell, SWT.WRAP);
		labelMessage.setText(this.message);
		labelMessage.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Composite buttonsComposite = new Composite(this.shell, SWT.NONE);
		final int buttonsCompositeColumns = 3;
		buttonsComposite.setLayout(new GridLayout(buttonsCompositeColumns, false));
		buttonsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));

		final Composite paddingComposite = new Composite(buttonsComposite, SWT.NONE);
		final GridData paddingCompositeGridData = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		paddingCompositeGridData.heightHint = 0;
		paddingComposite.setLayoutData(paddingCompositeGridData);

		this.btnYes = new Button(buttonsComposite, SWT.NONE);
		final GridData button1GridData = new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1);
		button1GridData.widthHint = buttonWidthHint;
		this.btnYes.setLayoutData(button1GridData);
		this.btnYes.setText(Messages.QuestionDialog_yes);
		this.btnYes.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				pressYes();
			}
		});

		this.btnNo = new Button(buttonsComposite, SWT.NONE);
		final GridData button2GridData = new GridData(SWT.LEFT, SWT.BOTTOM, false, false, 1, 1);
		button2GridData.widthHint = buttonWidthHint;
		this.btnNo.setLayoutData(button2GridData);
		this.btnNo.setText(Messages.QuestionDialog_no);
		this.btnNo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(final SelectionEvent e) {
				pressNo();
			}
		});
	}

	public final void pressYes() {
		if (this.shell.isDisposed()) {
			throw new UnsupportedOperationException("Dialog is disposed"); //$NON-NLS-1$
		}
		this.result = Boolean.TRUE;
		this.shell.dispose();
		notifyListeners();
	}

	public final void pressNo() {
		if (this.shell.isDisposed()) {
			throw new UnsupportedOperationException("Dialog is disposed"); //$NON-NLS-1$
		}
		this.result = Boolean.FALSE;
		this.shell.dispose();
		notifyListeners();
	}

	private synchronized void notifyListeners() {
		if (this.callback != null) {
			this.callback.committed(getResult());
		}
		for (final Runnable listener : new ArrayList<Runnable>(this.listeners)) {
			listener.run();
		}
	}

	public synchronized void addCloseListener(final Runnable runnable) {
		this.listeners.add(runnable);
	}

	public final Boolean getResult() {
		return this.result;
	}
	
	public final Shell getShell() {
		return this.shell;
	}
}

Back to the top