Search in sources :

Example 1 with SettingsModelDateTime

use of org.knime.time.util.SettingsModelDateTime in project knime-core by knime.

the class CreateDateTimeNodeDialog method updateWarningLabel.

private void updateWarningLabel() {
    m_warningLabel.setText("");
    // === check period duration field ===
    if (m_dialogCompDuration.getModel().isEnabled() && m_updateWarningLabel) {
        final DateTimeType selectedItem = (DateTimeType) m_typeCombobox.getSelectedItem();
        Duration duration = null;
        Period period = null;
        if (((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue() == null) {
            m_warningLabel.setText("Please enter an interval.");
            return;
        }
        try {
            duration = DurationPeriodFormatUtils.parseDuration(((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue());
            if (selectedItem.equals(DateTimeType.LOCAL_DATE)) {
                m_warningLabel.setText("A time-based duration cannot be applied to a local date.");
                return;
            }
        } catch (DateTimeParseException e1) {
            try {
                period = DurationPeriodFormatUtils.parsePeriod(((SettingsModelString) m_dialogCompDuration.getModel()).getStringValue());
                if (selectedItem.equals(DateTimeType.LOCAL_TIME)) {
                    m_warningLabel.setText("A date-based duration cannot be applied to a local time.");
                    return;
                }
            } catch (DateTimeParseException e2) {
                m_warningLabel.setText("Value does not represent a duration.");
                return;
            }
        }
        if (((SettingsModelString) m_dialogCompRowNrOptionSelection.getModel()).getStringValue().equals(RowNrMode.Variable.name())) {
            // === check that duration is not zero and row number variable ===
            if ((duration != null && duration.isZero()) || (period != null && period.isZero())) {
                m_warningLabel.setText("Interval must not be zero.");
            }
            // === check that start is before end and duration positive and vice versa ===
            final Temporal start = ((SettingsModelBoolean) m_dialogCompStartUseExecTime.getModel()).getBooleanValue() ? CreateDateTimeNodeModel.getTemporalExecTimeWithFormat(((SettingsModelDateTime) m_dialogCompEnd.getModel()).getSelectedDateTime()) : ((SettingsModelDateTime) m_dialogCompStart.getModel()).getSelectedDateTime();
            final Temporal end = ((SettingsModelBoolean) m_dialogCompEndUseExecTime.getModel()).getBooleanValue() ? CreateDateTimeNodeModel.getTemporalExecTimeWithFormat(((SettingsModelDateTime) m_dialogCompStart.getModel()).getSelectedDateTime()) : ((SettingsModelDateTime) m_dialogCompEnd.getModel()).getSelectedDateTime();
            if (!selectedItem.equals(DateTimeType.LOCAL_TIME)) {
                final boolean isStartBeforeEnd;
                final boolean isEqual;
                if (selectedItem.equals(DateTimeType.LOCAL_DATE)) {
                    isStartBeforeEnd = ((LocalDate) start).isBefore((LocalDate) end);
                    isEqual = ((LocalDate) start).isEqual((LocalDate) end);
                } else if (selectedItem.equals(DateTimeType.LOCAL_DATE_TIME)) {
                    isStartBeforeEnd = ((LocalDateTime) start).isBefore((LocalDateTime) end);
                    isEqual = ((LocalDateTime) start).isEqual((LocalDateTime) end);
                } else {
                    isStartBeforeEnd = (((ZonedDateTime) start)).toLocalDateTime().isBefore(((LocalDateTime) end));
                    isEqual = (((ZonedDateTime) start)).toLocalDateTime().isEqual(((LocalDateTime) end));
                }
                final boolean isDurationNegative;
                if (duration != null) {
                    isDurationNegative = duration.isNegative();
                } else if (period != null) {
                    isDurationNegative = period.isNegative();
                } else {
                    throw new IllegalStateException("Either duration or period must not be null!");
                }
                if (isStartBeforeEnd && isDurationNegative) {
                    m_warningLabel.setText("Start is before end, but the interval is negative.");
                    return;
                }
                if (!isStartBeforeEnd && !isEqual && !isDurationNegative) {
                    m_warningLabel.setText("Start is after end, but the interval is positive.");
                    return;
                }
            }
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeParseException(java.time.format.DateTimeParseException) DateTimeType(org.knime.time.util.DateTimeType) Temporal(java.time.temporal.Temporal) ZonedDateTime(java.time.ZonedDateTime) SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime) Period(java.time.Period) Duration(java.time.Duration) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDate(java.time.LocalDate)

Example 2 with SettingsModelDateTime

use of org.knime.time.util.SettingsModelDateTime in project knime-core by knime.

the class CreateDateTimeNodeModel method createStartModel.

/**
 * @return the date&time model, used in both dialog and model.
 */
static SettingsModelDateTime createStartModel(final SettingsModelBoolean useExecTimeModel) {
    final SettingsModelDateTime settingsModelDateTime = new SettingsModelDateTime("start", LocalDateTime.now().withNano(0).minusYears(1).minusHours(1));
    useExecTimeModel.addChangeListener(l -> settingsModelDateTime.setEnabled(!useExecTimeModel.getBooleanValue()));
    return settingsModelDateTime;
}
Also used : SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime)

Example 3 with SettingsModelDateTime

use of org.knime.time.util.SettingsModelDateTime in project knime-core by knime.

the class CreateDateTimeNodeModel method createEndModel.

/**
 * @return the date&time model, used in both dialog and model.
 */
static SettingsModelDateTime createEndModel(final SettingsModelString rowNrOptionSelectionModel, final SettingsModelString durationOrEndSelectionModel, final SettingsModelBoolean useExecTimeModel) {
    final SettingsModelDateTime settingsModelDateTime = new SettingsModelDateTime("end", LocalDateTime.now().withNano(0));
    final ChangeListener changeListener = e -> {
        if (durationOrEndSelectionModel.getStringValue() != null && rowNrOptionSelectionModel.getStringValue() != null) {
            settingsModelDateTime.setEnabled((rowNrOptionSelectionModel.getStringValue().equals(RowNrMode.Variable.name()) || (rowNrOptionSelectionModel.getStringValue().equals(RowNrMode.Fixed.name()) && durationOrEndSelectionModel.getStringValue().equals(EndMode.End.name()))) && !useExecTimeModel.getBooleanValue());
        }
    };
    rowNrOptionSelectionModel.addChangeListener(changeListener);
    durationOrEndSelectionModel.addChangeListener(changeListener);
    useExecTimeModel.addChangeListener(changeListener);
    changeListener.stateChanged(new ChangeEvent(rowNrOptionSelectionModel));
    return settingsModelDateTime;
}
Also used : NodeSettingsRO(org.knime.core.node.NodeSettingsRO) RowKey(org.knime.core.data.RowKey) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) SettingsModelLongBounded(org.knime.core.node.defaultnodesettings.SettingsModelLongBounded) LocalDateTimeCellFactory(org.knime.core.data.time.localdatetime.LocalDateTimeCellFactory) ExecutionContext(org.knime.core.node.ExecutionContext) ZonedDateTimeCellFactory(org.knime.core.data.time.zoneddatetime.ZonedDateTimeCellFactory) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Duration(java.time.Duration) LocalTime(java.time.LocalTime) TemporalAmount(java.time.temporal.TemporalAmount) DateTimeType(org.knime.time.util.DateTimeType) ChangeListener(javax.swing.event.ChangeListener) DataCell(org.knime.core.data.DataCell) DateTimeException(java.time.DateTimeException) Period(java.time.Period) ChangeEvent(javax.swing.event.ChangeEvent) DurationPeriodFormatUtils(org.knime.time.util.DurationPeriodFormatUtils) DefaultRow(org.knime.core.data.def.DefaultRow) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) LocalDateCellFactory(org.knime.core.data.time.localdate.LocalDateCellFactory) LocalTimeCellFactory(org.knime.core.data.time.localtime.LocalTimeCellFactory) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) IOException(java.io.IOException) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) NodeModel(org.knime.core.node.NodeModel) File(java.io.File) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DateTimeParseException(java.time.format.DateTimeParseException) BufferedDataTable(org.knime.core.node.BufferedDataTable) ChronoUnit(java.time.temporal.ChronoUnit) SettingsModelLong(org.knime.core.node.defaultnodesettings.SettingsModelLong) LocalDate(java.time.LocalDate) SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime) Temporal(java.time.temporal.Temporal) ChangeEvent(javax.swing.event.ChangeEvent) SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime) ChangeListener(javax.swing.event.ChangeListener)

Example 4 with SettingsModelDateTime

use of org.knime.time.util.SettingsModelDateTime in project knime-core by knime.

the class DateTimeBasedRowFilterNodeDialog method checkStartAndEnd.

/**
 * Checks if start and end string can be parsed with the set format and locale
 */
private void checkStartAndEnd() {
    if (m_dialogCompStartDateTime.getModel().isEnabled() && m_dialogCompEndDateTime.getModel().isEnabled() && ((SettingsModelString) m_dialogCompEndSelection.getModel()).getStringValue().equals(EndMode.DateTime.name())) {
        boolean isStartAfterEnd = false;
        final DataType dataType = m_dialogCompColSelection.getSelectedAsSpec().getType();
        if (dataType.isCompatible(LocalDateValue.class)) {
            isStartAfterEnd = ((SettingsModelDateTime) m_dialogCompStartDateTime.getModel()).getLocalDate().isAfter(((SettingsModelDateTime) m_dialogCompEndDateTime.getModel()).getLocalDate());
        }
        if (dataType.isCompatible(LocalTimeValue.class)) {
            isStartAfterEnd = ((SettingsModelDateTime) m_dialogCompStartDateTime.getModel()).getLocalTime().isAfter(((SettingsModelDateTime) m_dialogCompEndDateTime.getModel()).getLocalTime());
        }
        if (dataType.isCompatible(LocalDateTimeValue.class)) {
            isStartAfterEnd = ((SettingsModelDateTime) m_dialogCompStartDateTime.getModel()).getLocalDateTime().isAfter(((SettingsModelDateTime) m_dialogCompEndDateTime.getModel()).getLocalDateTime());
        }
        if (dataType.isCompatible(ZonedDateTimeValue.class)) {
            isStartAfterEnd = ((SettingsModelDateTime) m_dialogCompStartDateTime.getModel()).getZonedDateTime().isAfter(((SettingsModelDateTime) m_dialogCompEndDateTime.getModel()).getZonedDateTime());
        }
        if (isStartAfterEnd) {
            m_typeWarningLabel.setText("End date must not be before start date!");
        }
    }
}
Also used : SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime) DataType(org.knime.core.data.DataType) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Aggregations

SettingsModelDateTime (org.knime.time.util.SettingsModelDateTime)4 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 Duration (java.time.Duration)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 Period (java.time.Period)2 ZonedDateTime (java.time.ZonedDateTime)2 DateTimeParseException (java.time.format.DateTimeParseException)2 Temporal (java.time.temporal.Temporal)2 DateTimeType (org.knime.time.util.DateTimeType)2 File (java.io.File)1 IOException (java.io.IOException)1 DateTimeException (java.time.DateTimeException)1 LocalTime (java.time.LocalTime)1 ChronoUnit (java.time.temporal.ChronoUnit)1 TemporalAmount (java.time.temporal.TemporalAmount)1 ChangeEvent (javax.swing.event.ChangeEvent)1 ChangeListener (javax.swing.event.ChangeListener)1 DataCell (org.knime.core.data.DataCell)1 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)1