Search in sources :

Example 1 with UpdateStringLengthFacetCommand

use of org.eclipse.wst.xsd.ui.internal.common.commands.UpdateStringLengthFacetCommand in project webtools.sourceediting by eclipse.

the class XSDFacetSection method doHandleEvent.

protected void doHandleEvent(Event event) {
    super.doHandleEvent(event);
    Command command = null;
    boolean doUpdateMax = false, doUpdateMin = false;
    boolean doSetInput = false;
    String minValue = minLengthText.getText().trim();
    String maxValue = maxLengthText.getText().trim();
    XSDLengthFacet lengthFacet = xsdSimpleTypeDefinition.getLengthFacet();
    XSDMinLengthFacet minLengthFacet = xsdSimpleTypeDefinition.getMinLengthFacet();
    XSDMaxLengthFacet maxLengthFacet = xsdSimpleTypeDefinition.getMaxLengthFacet();
    XSDMinInclusiveFacet minInclusiveFacet = xsdSimpleTypeDefinition.getMinInclusiveFacet();
    XSDMinExclusiveFacet minExclusiveFacet = xsdSimpleTypeDefinition.getMinExclusiveFacet();
    XSDMaxInclusiveFacet maxInclusiveFacet = xsdSimpleTypeDefinition.getMaxInclusiveFacet();
    XSDMaxExclusiveFacet maxExclusiveFacet = xsdSimpleTypeDefinition.getMaxExclusiveFacet();
    String currentMinInclusive = null, currentMinExclusive = null, currentMaxInclusive = null, currentMaxExclusive = null;
    if (minInclusiveFacet != null) {
        currentMinInclusive = minInclusiveFacet.getLexicalValue();
    }
    if (minExclusiveFacet != null) {
        currentMinExclusive = minExclusiveFacet.getLexicalValue();
    }
    if (maxInclusiveFacet != null) {
        currentMaxInclusive = maxInclusiveFacet.getLexicalValue();
    }
    if (maxExclusiveFacet != null) {
        currentMaxExclusive = maxExclusiveFacet.getLexicalValue();
    }
    String currentLength = null, currentMin = null, currentMax = null;
    if (lengthFacet != null) {
        currentLength = lengthFacet.getLexicalValue();
    }
    if (minLengthFacet != null) {
        currentMin = minLengthFacet.getLexicalValue();
    }
    if (maxLengthFacet != null) {
        currentMax = maxLengthFacet.getLexicalValue();
    }
    if (event.widget == minLengthText) {
        try {
            if (minValue.length() > 0) {
                if (!isNumericBaseType) {
                    Number big = new BigInteger(minValue);
                    big.toString();
                    if (minLengthFacet != null) {
                        if (minValue.equals(currentMin) || minValue.equals(currentLength))
                            return;
                    } else {
                        if (maxValue != null && minValue.equals(maxValue) && lengthFacet != null) {
                            return;
                        }
                    }
                } else {
                    if (// $NON-NLS-1$
                    xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("double").equals(xsdSimpleTypeDefinition.getBaseType()) || // $NON-NLS-1$
                    xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("float").equals(xsdSimpleTypeDefinition.getBaseType()) || // $NON-NLS-1$
                    xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("decimal").equals(xsdSimpleTypeDefinition.getBaseType())) {
                        BigDecimal bigDecimal = new BigDecimal(minValue);
                        bigDecimal.toString();
                        if ((currentMinInclusive != null && minValue.equals(currentMinInclusive)) || (currentMinExclusive != null && minValue.equals(currentMinExclusive))) {
                            return;
                        }
                    } else {
                        Number big = new BigInteger(minValue);
                        big.toString();
                    }
                    minimumInclusiveCheckbox.setEnabled(true);
                }
            } else {
                if (!isNumericBaseType) {
                    if (currentMin == null && currentLength == null)
                        return;
                } else {
                    if (currentMinInclusive == null && minimumInclusiveCheckbox.getSelection()) {
                        return;
                    } else if (currentMinExclusive == null && !minimumInclusiveCheckbox.getSelection()) {
                        return;
                    }
                }
                minimumInclusiveCheckbox.setEnabled(false);
                minValue = null;
            }
            doUpdateMin = true;
        } catch (NumberFormatException e) {
            // TODO show error message
            doUpdateMin = false;
        }
    }
    if (event.widget == maxLengthText) {
        try {
            if (maxValue.length() > 0) {
                if (!isNumericBaseType) {
                    Number big = new BigInteger(maxValue);
                    big.toString();
                    if (maxLengthFacet != null) {
                        if (maxValue.equals(currentMax) || maxValue.equals(currentLength))
                            return;
                    } else {
                        if (minValue != null && maxValue.equals(minValue) && lengthFacet != null) {
                            return;
                        }
                    }
                } else {
                    if (// $NON-NLS-1$
                    xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("double").equals(xsdSimpleTypeDefinition.getBaseType()) || // $NON-NLS-1$
                    xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("float").equals(xsdSimpleTypeDefinition.getBaseType()) || // $NON-NLS-1$
                    xsdSchema.getSchemaForSchema().resolveSimpleTypeDefinition("decimal").equals(xsdSimpleTypeDefinition.getBaseType())) {
                        BigDecimal bigDecimal = new BigDecimal(maxValue);
                        bigDecimal.toString();
                    } else {
                        Number big = new BigInteger(maxValue);
                        big.toString();
                    }
                    maximumInclusiveCheckbox.setEnabled(true);
                }
            } else {
                if (!isNumericBaseType) {
                    if (currentMax == null && currentLength == null)
                        return;
                } else {
                    if (currentMaxInclusive == null && maximumInclusiveCheckbox.getSelection()) {
                        return;
                    } else if (currentMaxExclusive == null && !maximumInclusiveCheckbox.getSelection()) {
                        return;
                    }
                    maximumInclusiveCheckbox.setEnabled(false);
                }
                maxValue = null;
            }
            doUpdateMax = true;
        } catch (NumberFormatException e) {
            doUpdateMax = false;
        // TODO show error message
        }
    }
    if (XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001.equals(xsdSimpleTypeDefinition.getTargetNamespace()) && (doUpdateMax || doUpdateMin)) {
        XSDSimpleTypeDefinition anonymousSimpleType = null;
        CompoundCommand compoundCommand = new CompoundCommand();
        ChangeToLocalSimpleTypeCommand changeToAnonymousCommand = null;
        if (input instanceof XSDFeature) {
            anonymousSimpleType = XSDCommonUIUtils.getAnonymousSimpleType((XSDFeature) input, xsdSimpleTypeDefinition);
            if (anonymousSimpleType == null) {
                anonymousSimpleType = XSDFactory.eINSTANCE.createXSDSimpleTypeDefinition();
                anonymousSimpleType.setBaseTypeDefinition(xsdSimpleTypeDefinition);
                changeToAnonymousCommand = new ChangeToLocalSimpleTypeCommand(Messages._UI_ACTION_CONSTRAIN_LENGTH, (XSDFeature) input);
                changeToAnonymousCommand.setAnonymousSimpleType(anonymousSimpleType);
                compoundCommand.add(changeToAnonymousCommand);
                doSetInput = true;
            }
            if (!isNumericBaseType) {
                // $NON-NLS-1$
                UpdateStringLengthFacetCommand updateCommand = new UpdateStringLengthFacetCommand("", anonymousSimpleType);
                if (doUpdateMax) {
                    updateCommand.setMax(maxValue);
                }
                if (doUpdateMin) {
                    updateCommand.setMin(minValue);
                }
                compoundCommand.add(updateCommand);
            } else {
                UpdateNumericBoundsFacetCommand updateCommand = new UpdateNumericBoundsFacetCommand(Messages._UI_ACTION_UPDATE_BOUNDS, anonymousSimpleType, minimumInclusiveCheckbox.getSelection(), maximumInclusiveCheckbox.getSelection());
                if (doUpdateMax) {
                    updateCommand.setMax(maxValue);
                }
                if (doUpdateMin) {
                    updateCommand.setMin(minValue);
                }
                compoundCommand.add(updateCommand);
            }
            command = compoundCommand;
            getCommandStack().execute(command);
        } else if (input instanceof XSDSimpleTypeDefinition) {
            if (!isNumericBaseType) {
                // $NON-NLS-1$
                UpdateStringLengthFacetCommand updateCommand = new UpdateStringLengthFacetCommand("", xsdSimpleTypeDefinition);
                if (doUpdateMax) {
                    updateCommand.setMax(maxValue);
                }
                if (doUpdateMin) {
                    updateCommand.setMin(minValue);
                }
                command = updateCommand;
            } else {
                UpdateNumericBoundsFacetCommand updateCommand = new UpdateNumericBoundsFacetCommand(Messages._UI_ACTION_UPDATE_BOUNDS, xsdSimpleTypeDefinition, minimumInclusiveCheckbox.getSelection(), maximumInclusiveCheckbox.getSelection());
                if (doUpdateMax) {
                    updateCommand.setMax(maxValue);
                }
                if (doUpdateMin) {
                    updateCommand.setMin(minValue);
                }
                command = updateCommand;
            }
            getCommandStack().execute(command);
        }
    } else {
        if (!isNumericBaseType) {
            // $NON-NLS-1$
            UpdateStringLengthFacetCommand updateCommand = new UpdateStringLengthFacetCommand("", xsdSimpleTypeDefinition);
            if (doUpdateMax) {
                updateCommand.setMax(maxValue);
            }
            if (doUpdateMin) {
                updateCommand.setMin(minValue);
            }
            getCommandStack().execute(updateCommand);
        } else {
            UpdateNumericBoundsFacetCommand updateCommand = new UpdateNumericBoundsFacetCommand(Messages._UI_ACTION_UPDATE_BOUNDS, xsdSimpleTypeDefinition, minimumInclusiveCheckbox.getSelection(), maximumInclusiveCheckbox.getSelection());
            if (doUpdateMax) {
                updateCommand.setMax(maxValue);
            }
            if (doUpdateMin) {
                updateCommand.setMin(minValue);
            }
            getCommandStack().execute(updateCommand);
        }
    }
    refresh();
    if (doSetInput)
        setInput(getPart(), getSelection());
}
Also used : UpdateStringLengthFacetCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateStringLengthFacetCommand) XSDMinInclusiveFacet(org.eclipse.xsd.XSDMinInclusiveFacet) XSDSimpleTypeDefinition(org.eclipse.xsd.XSDSimpleTypeDefinition) UpdateNumericBoundsFacetCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateNumericBoundsFacetCommand) BigDecimal(java.math.BigDecimal) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) XSDLengthFacet(org.eclipse.xsd.XSDLengthFacet) XSDFeature(org.eclipse.xsd.XSDFeature) UpdateStringLengthFacetCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateStringLengthFacetCommand) CompoundCommand(org.eclipse.gef.commands.CompoundCommand) Command(org.eclipse.gef.commands.Command) ChangeToLocalSimpleTypeCommand(org.eclipse.wst.xsd.ui.internal.common.commands.ChangeToLocalSimpleTypeCommand) UpdateNumericBoundsFacetCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateNumericBoundsFacetCommand) UpdateXSDWhiteSpaceFacetCommand(org.eclipse.wst.xsd.ui.internal.common.commands.UpdateXSDWhiteSpaceFacetCommand) XSDMaxExclusiveFacet(org.eclipse.xsd.XSDMaxExclusiveFacet) ChangeToLocalSimpleTypeCommand(org.eclipse.wst.xsd.ui.internal.common.commands.ChangeToLocalSimpleTypeCommand) BigInteger(java.math.BigInteger) XSDMinLengthFacet(org.eclipse.xsd.XSDMinLengthFacet) XSDMaxLengthFacet(org.eclipse.xsd.XSDMaxLengthFacet) XSDMinExclusiveFacet(org.eclipse.xsd.XSDMinExclusiveFacet) XSDMaxInclusiveFacet(org.eclipse.xsd.XSDMaxInclusiveFacet)

Aggregations

BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 Command (org.eclipse.gef.commands.Command)1 CompoundCommand (org.eclipse.gef.commands.CompoundCommand)1 ChangeToLocalSimpleTypeCommand (org.eclipse.wst.xsd.ui.internal.common.commands.ChangeToLocalSimpleTypeCommand)1 UpdateNumericBoundsFacetCommand (org.eclipse.wst.xsd.ui.internal.common.commands.UpdateNumericBoundsFacetCommand)1 UpdateStringLengthFacetCommand (org.eclipse.wst.xsd.ui.internal.common.commands.UpdateStringLengthFacetCommand)1 UpdateXSDWhiteSpaceFacetCommand (org.eclipse.wst.xsd.ui.internal.common.commands.UpdateXSDWhiteSpaceFacetCommand)1 XSDFeature (org.eclipse.xsd.XSDFeature)1 XSDLengthFacet (org.eclipse.xsd.XSDLengthFacet)1 XSDMaxExclusiveFacet (org.eclipse.xsd.XSDMaxExclusiveFacet)1 XSDMaxInclusiveFacet (org.eclipse.xsd.XSDMaxInclusiveFacet)1 XSDMaxLengthFacet (org.eclipse.xsd.XSDMaxLengthFacet)1 XSDMinExclusiveFacet (org.eclipse.xsd.XSDMinExclusiveFacet)1 XSDMinInclusiveFacet (org.eclipse.xsd.XSDMinInclusiveFacet)1 XSDMinLengthFacet (org.eclipse.xsd.XSDMinLengthFacet)1 XSDSimpleTypeDefinition (org.eclipse.xsd.XSDSimpleTypeDefinition)1