Skip to content
Snippets Groups Projects
Commit 94b1ffe3 authored by Vincent Aravantinos's avatar Vincent Aravantinos
Browse files

Base for constraint verifiers

refs 2334
parent 64bfdee6
No related branches found
No related tags found
No related merge requests found
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2015 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.extension.base;
import java.util.List;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.fortiss.tooling.kernel.model.constraints.ErrorVerificationStatus;
import org.fortiss.tooling.kernel.model.constraints.FailVerificationStatus;
import org.fortiss.tooling.kernel.model.constraints.IConstraint;
import org.fortiss.tooling.kernel.model.constraints.IConstraintVerificationStatus;
import org.fortiss.tooling.kernel.model.constraints.OutdatedVerificationStatus;
import org.fortiss.tooling.kernel.service.IConstraintVerificationService;
import org.fortiss.tooling.kernel.service.IConstraintVerificationService.IFix;
import org.fortiss.tooling.kernel.ui.extension.IConstraintVerifierUI;
/**
* Base class for constraint verification GUI.
*
* @author vincent
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 3FA7D98F2CAEDA4B41362486FCA29373
*/
public class ConstraintVerifierUIBase<T extends IConstraint> implements IConstraintVerifierUI<T> {
/** {@inheritDoc} */
@Override
public boolean openStatus(IConstraintVerificationStatus status) {
if(!canOpen(status)) {
return false;
}
if(status instanceof OutdatedVerificationStatus) {
IConstraint constraint = status.getConstraint();
IConstraintVerificationService.INSTANCE.verify(constraint);
IConstraintVerificationStatus currentStatus = constraint.getVerificationStatus();
// If the constraint verification resulted in an error, we display it immediately.
if(currentStatus instanceof ErrorVerificationStatus & canOpen(currentStatus)) {
openStatus(currentStatus);
}
return true;
}
if(status instanceof FailVerificationStatus || status instanceof ErrorVerificationStatus) {
List<IFix> fixes = IConstraintVerificationService.INSTANCE.fixes(status);
Shell shell = Display.getCurrent().getActiveShell();
int result = new FixDialog(shell, status, getMessage(status), fixes).open();
if(fixes.size() == 1 && result == 0) {
// get(0) because the size is equal to 1 by the previous test
fixes.get(0).runFix(status);
} else if(result > 1) {
fixes.get(result - 2).runFix(status);
}
return true;
}
return false;
}
/** {@inheritDoc} */
@Override
public boolean canOpen(IConstraintVerificationStatus status) {
return(status instanceof OutdatedVerificationStatus);
}
/** Dialog presenting the error/failure and the possible fix(es). */
private static class FixDialog extends MessageDialog {
/** List of possible fixes. */
List<IFix> fixes;
/** Constructor. */
public FixDialog(Shell parentShell, IConstraintVerificationStatus s, String msg,
List<IFix> fixes) {
super(parentShell, getTitle(s), null, msg, getIconType(s), getButtons(fixes), 1);
this.fixes = fixes;
}
/**
* @param fixes
* @return Buttons to display: "OK" is always displayed. In addition, if there is *only one
* fix*, we provide one button for this fix. If there is more than one possible fix,
* they will displayed instead horizontally (via getCustomArea).
*/
private static String[] getButtons(List<IFix> fixes) {
String[] res = new String[fixes.size() == 1 ? 2 : 1];
if(fixes.size() == 1) {
// get(0) because the size is equal to 1 by the previous test
res[0] = "Fix: " + fixes.get(0).getDescription();
}
res[res.length - 1] = "OK";
return res;
}
/**
* @param s
* @return the icon type corresponding to <code>s</code>.
*/
private static String getTitle(IConstraintVerificationStatus s) {
if(s instanceof FailVerificationStatus) {
return "Verification of constraint failed";
}
return "Error during constraint verification";
}
/**
* @param s
* @return the icon type corresponding to <code>s</code>.
*/
private static int getIconType(IConstraintVerificationStatus s) {
return s instanceof FailVerificationStatus ? MessageDialog.WARNING : ERROR;
}
/** {@inheritDoc} */
@Override
protected Control createCustomArea(Composite parent) {
if(fixes.size() <= 1) {
return null;
}
Group buttonGroup = new Group(parent, SWT.NONE);
buttonGroup.setLayout(new GridLayout());
buttonGroup.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, true, false));
buttonGroup.setText("Possible fixes");
for(IFix fix : fixes) {
Button button = new Button(buttonGroup, SWT.None);
button.setText(fix.getDescription());
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent event) {
buttonPressed(fixes.indexOf(fix) + 1);
}
});
}
// Does not matter what we return as long as it's not null
return buttonGroup;
}
}
/** {@inheritDoc} */
@Override
public String getMessage(IConstraintVerificationStatus status) {
// By default, no message. To be specialized by inheriting classes.
return null;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment