Example usage for java.text DecimalFormat parseObject

List of usage examples for java.text DecimalFormat parseObject

Introduction

In this page you can find the example usage for java.text DecimalFormat parseObject.

Prototype

@Override
public final Object parseObject(String source, ParsePosition pos) 

Source Link

Document

Parses text from a string to produce a Number.

Usage

From source file:NumericTextField.java

public NumericPlainDocument(AbstractDocument.Content content, DecimalFormat format) {
    super(content);
    setFormat(format);/*from   w  ww  . ja v a  2s. com*/

    try {
        format.parseObject(content.getString(0, content.length()), parsePos);
    } catch (Exception e) {
        throw new IllegalArgumentException("Initial content not a valid number");
    }

    if (parsePos.getIndex() != content.length() - 1) {
        throw new IllegalArgumentException("Initial content not a valid number");
    }
}

From source file:com.qcadoo.mes.productionScheduling.listeners.OrderTimePredictionListeners.java

@Transactional
public void changeRealizationTime(final ViewDefinitionState view, final ComponentState state,
        final String[] args) {
    FormComponent orderForm = (FormComponent) view.getComponentByReference(L_FORM);

    FieldComponent technologyLookup = (FieldComponent) view.getComponentByReference(OrderFields.TECHNOLOGY);
    FieldComponent plannedQuantityField = (FieldComponent) view
            .getComponentByReference(OrderFields.PLANNED_QUANTITY);
    FieldComponent dateFromField = (FieldComponent) view.getComponentByReference(OrderFields.DATE_FROM);
    FieldComponent dateToField = (FieldComponent) view.getComponentByReference(OrderFields.DATE_TO);
    FieldComponent productionLineLookup = (FieldComponent) view
            .getComponentByReference(OrderFields.PRODUCTION_LINE);

    boolean isGenerated = false;

    if (technologyLookup.getFieldValue() == null) {
        technologyLookup.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);

        return;//  ww  w.j  a  va2s.  com
    }

    if (!StringUtils.hasText((String) dateFromField.getFieldValue())) {
        dateFromField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);

        return;
    }

    if (!StringUtils.hasText((String) plannedQuantityField.getFieldValue())) {
        plannedQuantityField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);

        return;
    }

    if (productionLineLookup.getFieldValue() == null) {
        productionLineLookup.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);

        return;
    }

    BigDecimal quantity = null;
    Object value = plannedQuantityField.getFieldValue();

    if (value instanceof BigDecimal) {
        quantity = (BigDecimal) value;
    } else {
        try {
            ParsePosition parsePosition = new ParsePosition(0);
            String trimedValue = value.toString().replaceAll(" ", "");
            DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(view.getLocale());
            formatter.setParseBigDecimal(true);
            quantity = new BigDecimal(String.valueOf(formatter.parseObject(trimedValue, parsePosition)));
        } catch (NumberFormatException e) {
            plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidNumericFormat",
                    MessageType.FAILURE);

            return;
        }
    }

    int scale = quantity.scale();

    if (MAX != null && scale > MAX) {
        plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidScale.max", MessageType.FAILURE,
                MAX.toString());
        return;
    }

    int presicion = quantity.precision() - scale;

    if (MAX != null && presicion > MAX) {
        plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidPrecision.max",
                MessageType.FAILURE, MAX.toString());
        return;
    }

    if (BigDecimal.ZERO.compareTo(quantity) >= 0) {
        plannedQuantityField.addMessage("qcadooView.validate.field.error.outOfRange.toSmall",
                MessageType.FAILURE);
        return;
    }

    int maxPathTime = 0;

    Entity technology = dataDefinitionService
            .get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_TECHNOLOGY)
            .get((Long) technologyLookup.getFieldValue());
    Validate.notNull(technology, "technology is null");

    if (technology.getStringField(TechnologyFields.STATE).equals(TechnologyState.DRAFT.getStringValue())
            || technology.getStringField(TechnologyFields.STATE)
                    .equals(TechnologyState.OUTDATED.getStringValue())) {
        technologyLookup.addMessage("productionScheduling.technology.incorrectState", MessageType.FAILURE);

        return;
    }

    FieldComponent laborWorkTimeField = (FieldComponent) view
            .getComponentByReference(OrderFieldsPS.LABOR_WORK_TIME);
    FieldComponent machineWorkTimeField = (FieldComponent) view
            .getComponentByReference(OrderFieldsPS.MACHINE_WORK_TIME);
    FieldComponent includeTpzField = (FieldComponent) view.getComponentByReference(OrderFieldsPS.INCLUDE_TPZ);
    FieldComponent includeAdditionalTimeField = (FieldComponent) view
            .getComponentByReference(OrderFieldsPS.INCLUDE_ADDITIONAL_TIME);

    Boolean includeTpz = "1".equals(includeTpzField.getFieldValue());
    Boolean includeAdditionalTime = "1".equals(includeAdditionalTimeField.getFieldValue());

    Entity productionLine = dataDefinitionService
            .get(ProductionLinesConstants.PLUGIN_IDENTIFIER, ProductionLinesConstants.MODEL_PRODUCTION_LINE)
            .get((Long) productionLineLookup.getFieldValue());

    final Map<Long, BigDecimal> operationRuns = Maps.newHashMap();

    productQuantitiesService.getProductComponentQuantities(technology, quantity, operationRuns);

    OperationWorkTime workTime = operationWorkTimeService.estimateTotalWorkTimeForTechnology(technology,
            operationRuns, includeTpz, includeAdditionalTime, productionLine, true);

    laborWorkTimeField.setFieldValue(workTime.getLaborWorkTime());
    machineWorkTimeField.setFieldValue(workTime.getMachineWorkTime());

    maxPathTime = orderRealizationTimeService.estimateOperationTimeConsumption(
            technology.getTreeField(TechnologyFields.OPERATION_COMPONENTS).getRoot(), quantity, includeTpz,
            includeAdditionalTime, productionLine);

    if (maxPathTime > OrderRealizationTimeService.MAX_REALIZATION_TIME) {
        state.addMessage("orders.validate.global.error.RealizationTimeIsToLong", MessageType.FAILURE);

        dateToField.setFieldValue(null);
    } else {
        Date startTime = DateUtils.parseDate(dateFromField.getFieldValue());

        if (startTime == null) {
            dateFromField.addMessage("orders.validate.global.error.dateFromIsNull", MessageType.FAILURE);
        } else {
            Date stopTime = shiftsService.findDateToForOrder(startTime, maxPathTime);

            if (stopTime == null) {
                orderForm.addMessage("productionScheduling.timenorms.isZero", MessageType.FAILURE, false);

                dateToField.setFieldValue(null);
            } else {
                dateToField.setFieldValue(orderRealizationTimeService.setDateToField(stopTime));

                startTime = shiftsService.findDateFromForOrder(stopTime, maxPathTime);

                scheduleOperationComponents(technology.getId(), startTime);

                isGenerated = true;
            }

            if (startTime != null) {
                orderForm.addMessage("orders.dateFrom.info.dateFromSetToFirstPossible", MessageType.INFO,
                        false);
            }
        }
    }

    laborWorkTimeField.requestComponentUpdateState();
    machineWorkTimeField.requestComponentUpdateState();
    dateFromField.requestComponentUpdateState();
    dateToField.requestComponentUpdateState();

    orderForm.setEntity(orderForm.getEntity());

    state.performEvent(view, "refresh", new String[0]);

    if (isGenerated) {
        orderForm.addMessage("productionScheduling.info.calculationGenerated", MessageType.SUCCESS);
    }
}