Search in sources :

Example 1 with ApplyWhenCondition

use of net.sourceforge.usbdm.packageParser.ApplyWhenCondition in project usbdm-eclipse-plugins by podonoghue.

the class UsbdmOptionsPanel method validate.

/**
 *    Validates control & returns error message
 *
 *    @return
 *
 *    @return Error message (null if none)
 *
 *    @throws Exception
 */
public String validate() {
    if (fButtonMap == null) {
        // Button not yet created
        return null;
    }
    // Propagate dependencies between buttons
    // Bit of a hack - do the page multiple times to propagate dependency changes
    Set<Entry<String, Button>> buttonSet = fButtonMap.entrySet();
    for (int i = 0; i < 5; i++) {
        // Check and update all buttons and record if any changed
        boolean noChanges = true;
        for (Entry<String, Button> entry : buttonSet) {
            Button button = entry.getValue();
            ProjectVariable projectVariable = fVariableMap.get(entry.getKey());
            boolean enabled = true;
            try {
                ApplyWhenCondition applyWhenCondition = projectVariable.getRequirement();
                // if (projectVariable.getId().equals("projectOptionValue.KSDK-usb-audio-generator")) {
                // applyWhenCondition.setVerbose(true);
                // }
                enabled = applyWhenCondition.enabled(fDevice, fOptionMap, fButtonMap);
            } catch (Exception e) {
                e.printStackTrace();
                return e.getMessage();
            }
            if (button.isEnabled() != enabled) {
                button.setEnabled(enabled);
                noChanges = false;
            }
            if (!button.isEnabled()) {
                button.setSelection(false);
            }
        }
        if (noChanges) {
            break;
        }
    }
    // Check for at least one radio button selected in each group
    for (Group group : fGroupList) {
        boolean radioButtonSelected = false;
        boolean radioButtonPresent = false;
        for (Control child : group.getChildren()) {
            if (child instanceof Button) {
                Button button = (Button) child;
                if (button.isEnabled() && (button.getStyle() & SWT.RADIO) != 0) {
                    radioButtonPresent = true;
                    if (radioButtonSelected) {
                        // Clear multiple button selection
                        button.setSelection(false);
                    }
                    radioButtonSelected = radioButtonSelected || button.getSelection();
                }
            }
        }
        if (radioButtonPresent && !radioButtonSelected) {
            // Prompt to select a radio button
            return "Select " + group.getText();
        }
    }
    return null;
}
Also used : WizardGroup(net.sourceforge.usbdm.packageParser.WizardGroup) Group(org.eclipse.swt.widgets.Group) Entry(java.util.Map.Entry) Control(org.eclipse.swt.widgets.Control) ProjectVariable(net.sourceforge.usbdm.packageParser.ProjectVariable) Button(org.eclipse.swt.widgets.Button) ApplyWhenCondition(net.sourceforge.usbdm.packageParser.ApplyWhenCondition)

Aggregations

Entry (java.util.Map.Entry)1 ApplyWhenCondition (net.sourceforge.usbdm.packageParser.ApplyWhenCondition)1 ProjectVariable (net.sourceforge.usbdm.packageParser.ProjectVariable)1 WizardGroup (net.sourceforge.usbdm.packageParser.WizardGroup)1 Button (org.eclipse.swt.widgets.Button)1 Control (org.eclipse.swt.widgets.Control)1 Group (org.eclipse.swt.widgets.Group)1