Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 019a65a68aceeaec6e817b350c91b3bd8b640937 (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
/*******************************************************************************
 * Copyright (c) 2016 Obeo.
 * 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:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.ide.ui.internal.widgets.quickfix;

import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.eef.EEFValidationFixDescription;
import org.eclipse.eef.EEFValidationRuleDescription;
import org.eclipse.eef.EefPackage;
import org.eclipse.eef.core.api.controllers.InvalidValidationRuleResultData;
import org.eclipse.eef.ide.ui.internal.EEFIdeUiPlugin;
import org.eclipse.eef.ide.ui.internal.Messages;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.IMessage;

/**
 * The wizard page used to launch a quick fix.
 *
 * @author sbegaudeau
 */
public class EEFQuickFixPage extends WizardPage {
	/**
	 * The table viewer containing all the quick fixes available.
	 */
	private TableViewer quickFixesList;

	/**
	 * The validation rule.
	 */
	private EEFValidationRuleDescription validationRule;

	/**
	 * The invalid validation result data.
	 */
	private InvalidValidationRuleResultData data;

	/**
	 * The selected {@link IMessage}.
	 */
	private IMessage selectedMessage;

	/**
	 * The constructor.
	 *
	 * @param message
	 *            The message
	 * @param validationRule
	 *            The validation rule
	 * @param data
	 *            The invalid validation result data
	 */
	public EEFQuickFixPage(IMessage message, EEFValidationRuleDescription validationRule, InvalidValidationRuleResultData data) {
		super(message.getMessage());
		this.setTitle(Messages.EEFQuickFixPage_title);
		this.setDescription(MessageFormat.format(Messages.EEFQuickFixPage_description, message.getMessage()));
		this.validationRule = validationRule;
		this.data = data;
		this.selectedMessage = message;
	}

	/**
	 * Returns the selected {@link IMessage}.
	 *
	 * @return The selected {@link IMessage}
	 */
	public IMessage getSelectedMessage() {
		return this.selectedMessage;
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		this.initializeDialogUnits(parent);

		Composite control = new Composite(parent, SWT.NONE);
		control.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		this.setControl(control);

		FormLayout layout = new FormLayout();
		layout.marginHeight = this.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.marginWidth = this.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.spacing = this.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		control.setLayout(layout);

		Label label = new Label(control, SWT.NONE);
		label.setText(Messages.EEFQuickFixPage_label);
		label.setLayoutData(new FormData());

		// Create the list for the quick fixes
		this.quickFixesList = new TableViewer(control, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
		this.quickFixesList.setContentProvider(new EEFQuickFixTableContentProvider());
		this.quickFixesList.setLabelProvider(new EEFQuickFixTableLabelProvider());
		this.quickFixesList.setComparator(new EEFQuickFixTableComparator());
		this.quickFixesList.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				EEFQuickFixPage.this.setPageComplete(true);
			}
		});

		FormData listData = new FormData();
		listData.top = new FormAttachment(label, 0);
		listData.left = new FormAttachment(0);
		listData.right = new FormAttachment(100, 0);
		listData.height = convertHeightInCharsToPixels(10);
		this.quickFixesList.getControl().setLayoutData(listData);

		this.quickFixesList.setInput(this.validationRule);

		Dialog.applyDialogFont(control);

		// Select the first quick fix available
		Object firstElement = this.quickFixesList.getElementAt(0);
		if (firstElement != null) {
			this.quickFixesList.setSelection(new StructuredSelection(firstElement));
		} else {
			this.setMessage(Messages.EEFQuickFixWizard_noQuickFixAvailable, IMessageProvider.ERROR);
		}
	}

	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.jface.wizard.WizardPage#isPageComplete()
	 */
	@Override
	public boolean isPageComplete() {
		return this.quickFixesList != null && this.validationRule.getFixes().size() > 0 && !this.quickFixesList.getSelection().isEmpty();
	}

	/**
	 * Launch the selected quick fix in the list.
	 *
	 * @param monitor
	 *            The progress monitor
	 */
	public void performFinish(IProgressMonitor monitor) {
		try {
			this.getWizard().getContainer().run(false, true, (progressMonitor) -> {
				progressMonitor.beginTask(Messages.EEFQuickFixWizard_applyQuickFix, 1);
				this.getShell().getDisplay().readAndDispatch();
				if (progressMonitor.isCanceled()) {
					return;
				}

				ISelection selection = this.quickFixesList.getSelection();
				if (selection instanceof IStructuredSelection) {
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
					// Only one quick fix can be selected
					Object element = structuredSelection.getFirstElement();
					if (element instanceof EEFValidationFixDescription) {
						// Run the quick fix using the given eval
						EEFValidationFixDescription validationFix = (EEFValidationFixDescription) element;
						EAttribute expressionEAttribute = EefPackage.Literals.EEF_VALIDATION_FIX_DESCRIPTION__FIX_EXPRESSION;
						this.data.getEditingContextAdapter().performModelChange(() -> {
							this.data.getEval().logIfBlank(expressionEAttribute).call(validationFix.getFixExpression());
						});
					}
				}

				progressMonitor.worked(1);
			});
		} catch (InvocationTargetException e) {
			EEFIdeUiPlugin.getPlugin().error(e.getMessage(), e);
		} catch (InterruptedException e) {
			EEFIdeUiPlugin.getPlugin().error(e.getMessage(), e);
		}
	}
}

Back to the top