From 94b1ffe37613d79c7e6b842de8db70ee65fd8efc Mon Sep 17 00:00:00 2001 From: Vincent Aravantinos <aravantinos@fortiss.org> Date: Fri, 8 Apr 2016 16:11:48 +0000 Subject: [PATCH] Base for constraint verifiers refs 2334 --- .../base/ConstraintVerifierUIBase.java | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ConstraintVerifierUIBase.java diff --git a/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ConstraintVerifierUIBase.java b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ConstraintVerifierUIBase.java new file mode 100644 index 000000000..e4c6da999 --- /dev/null +++ b/org.fortiss.tooling.kernel.ui/trunk/src/org/fortiss/tooling/kernel/ui/extension/base/ConstraintVerifierUIBase.java @@ -0,0 +1,169 @@ +/*--------------------------------------------------------------------------+ +$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; + } +} -- GitLab