Search in sources :

Example 1 with DateFormatter

use of javax.swing.text.DateFormatter in project knime-core by knime.

the class DialogComponentDateTimeSelection method updateSpinnerFormat.

/**
 * Allows the user to determine by himself whether milliseconds shall be used or not. Checks if the text in the text
 * field of the time spinner contains milliseconds or not and updates the format of the spinner editor accordingly.
 */
private void updateSpinnerFormat() {
    final JFormattedTextField field = (JFormattedTextField) m_editor.getComponent(0);
    try {
        DateTimeFormatter.ofPattern(TIME_FORMAT_WITH_MS).parse(field.getText());
        if (!m_useMillis) {
            setUseMillis(true);
            ((DateFormatter) m_editor.getTextField().getFormatter()).setFormat(new SimpleDateFormat(TIME_FORMAT_WITH_MS, Locale.getDefault()));
            updateModel();
        }
    } catch (final DateTimeParseException iae) {
        try {
            DateTimeFormatter.ofPattern(TIME_FORMAT_WITHOUT_MS).parse(field.getText());
            if (m_useMillis) {
                setUseMillis(false);
                ((DateFormatter) m_editor.getTextField().getFormatter()).setFormat(new SimpleDateFormat(TIME_FORMAT_WITHOUT_MS, Locale.getDefault()));
                updateModel();
            }
        } catch (final DateTimeParseException iae2) {
        }
    }
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) JFormattedTextField(javax.swing.JFormattedTextField) DateFormatter(javax.swing.text.DateFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with DateFormatter

use of javax.swing.text.DateFormatter in project sldeditor by robward-scisys.

the class FieldConfigTimePeriod method createUIPanel.

/**
 * Creates the ui panel.
 *
 * @param fieldPanel the field panel
 * @param panelData the panel
 * @param index the index
 * @param title the title
 * @return the int
 */
private void createUIPanel(FieldPanel fieldPanel, TimePeriodPanel panelData, int index, String title) {
    panelData.panel = new JPanel();
    panelData.panel.setLayout(null);
    int row = 0;
    panelData.dateRadioButton = new JRadioButton();
    panelData.dateRadioButton.setBounds(2, getRowY(row), 20, BasePanel.WIDGET_HEIGHT);
    panelData.dateRadioButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            updateFields(panelData);
        }
    });
    panelData.panel.add(panelData.dateRadioButton);
    panelData.buttonGroup.add(panelData.dateRadioButton);
    JDatePanelImpl datePanel = new JDatePanelImpl(panelData.dateModel);
    panelData.datePicker = new JDatePickerImpl(datePanel);
    int xPos = getXPos();
    panelData.datePicker.setBounds(xPos + BasePanel.WIDGET_X_START, getRowY(row), BasePanel.WIDGET_STANDARD_WIDTH, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(panelData.datePicker);
    panelData.dateModel.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    // Time picker
    SpinnerDateModel model = new SpinnerDateModel();
    Calendar calendar = Calendar.getInstance();
    model.setValue(calendar.getTime());
    panelData.timePicker = new JSpinner(model);
    panelData.timePicker.setBounds(panelData.datePicker.getX() + panelData.datePicker.getWidth() + 10, getRowY(row), (int) (BasePanel.WIDGET_STANDARD_WIDTH * 0.75), BasePanel.WIDGET_HEIGHT);
    panelData.timePicker.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    panelData.panel.add(panelData.timePicker);
    panelData.timeEditor = new JSpinner.DateEditor(panelData.timePicker, "HH:mm:ss");
    panelData.timePicker.setEditor(panelData.timeEditor);
    DateFormatter formatter = (DateFormatter) panelData.timeEditor.getTextField().getFormatter();
    formatter.setAllowsInvalid(false);
    formatter.setOverwriteMode(true);
    // 
    // Date
    // 
    row++;
    panelData.durationRadioButton = new JRadioButton();
    panelData.durationRadioButton.setBounds(2, getRowY(row), 20, BasePanel.WIDGET_HEIGHT);
    panelData.durationRadioButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            updateFields(panelData);
        }
    });
    panelData.panel.add(panelData.durationRadioButton);
    panelData.buttonGroup.add(panelData.durationRadioButton);
    panelData.dateCheckbox = new JCheckBox(Localisation.getString(FieldConfigBase.class, "FieldConfigTimePeriod.date"));
    panelData.dateCheckbox.setBounds(20, getRowY(row), 55, BasePanel.WIDGET_HEIGHT);
    panelData.dateCheckbox.setSelected(true);
    panelData.dateCheckbox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean enabled = panelData.dateCheckbox.isSelected();
            panelData.yearSpinner.setEnabled(enabled);
            panelData.monthSpinner.setEnabled(enabled);
            panelData.daySpinner.setEnabled(enabled);
        }
    });
    panelData.panel.add(panelData.dateCheckbox);
    int x = xPos + BasePanel.WIDGET_X_START - 10;
    // Year
    JLabel yearLbl = new JLabel(Localisation.getField(FieldConfigBase.class, "FieldConfigTimePeriod.year"));
    yearLbl.setHorizontalAlignment(SwingConstants.LEADING);
    yearLbl.setBounds(x, getRowY(row), 40, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(yearLbl);
    panelData.yearSpinner = new JSpinner();
    x = x + 40;
    int spinnerWidth = (int) (BasePanel.WIDGET_STANDARD_WIDTH * 0.4);
    panelData.yearSpinner.setBounds(x, getRowY(row), spinnerWidth, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(panelData.yearSpinner);
    panelData.yearSpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    // Month
    x = x + spinnerWidth + 2;
    JLabel monthLbl = new JLabel(Localisation.getField(FieldConfigBase.class, "FieldConfigTimePeriod.month"));
    monthLbl.setHorizontalAlignment(SwingConstants.LEADING);
    monthLbl.setBounds(x, getRowY(row), 40, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(monthLbl);
    x = x + 40;
    panelData.monthSpinner = new JSpinner();
    panelData.monthSpinner.setBounds(x, getRowY(row), spinnerWidth, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(panelData.monthSpinner);
    panelData.monthSpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    // Day
    x = x + spinnerWidth + 2;
    JLabel dayLbl = new JLabel(Localisation.getField(FieldConfigBase.class, "FieldConfigTimePeriod.day"));
    dayLbl.setHorizontalAlignment(SwingConstants.LEADING);
    dayLbl.setBounds(x, getRowY(row), 40, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(dayLbl);
    x = x + 40;
    panelData.daySpinner = new JSpinner();
    panelData.daySpinner.setBounds(x, getRowY(row), spinnerWidth, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(panelData.daySpinner);
    panelData.daySpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    // 
    // Time
    // 
    row++;
    panelData.timeCheckbox = new JCheckBox(Localisation.getString(FieldConfigBase.class, "FieldConfigTimePeriod.time"));
    panelData.timeCheckbox.setBounds(20, getRowY(row), 55, BasePanel.WIDGET_HEIGHT);
    panelData.timeCheckbox.setSelected(true);
    panelData.timeCheckbox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            boolean enabled = panelData.timeCheckbox.isSelected();
            panelData.hourSpinner.setEnabled(enabled);
            panelData.minuteSpinner.setEnabled(enabled);
            panelData.secondSpinner.setEnabled(enabled);
        }
    });
    panelData.panel.add(panelData.timeCheckbox);
    x = xPos + BasePanel.WIDGET_X_START - 10;
    // Hours
    JLabel hoursLbl = new JLabel(Localisation.getField(FieldConfigBase.class, "FieldConfigTimePeriod.hours"));
    hoursLbl.setHorizontalAlignment(SwingConstants.LEADING);
    hoursLbl.setBounds(x, getRowY(row), 40, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(hoursLbl);
    panelData.hourSpinner = new JSpinner();
    x = x + 40;
    panelData.hourSpinner.setBounds(x, getRowY(row), spinnerWidth, BasePanel.WIDGET_HEIGHT);
    panelData.hourSpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    panelData.panel.add(panelData.hourSpinner);
    // Minutes
    x = x + spinnerWidth + 2;
    JLabel minuteLbl = new JLabel(Localisation.getField(FieldConfigBase.class, "FieldConfigTimePeriod.minutes"));
    minuteLbl.setHorizontalAlignment(SwingConstants.LEADING);
    minuteLbl.setBounds(x, getRowY(row), 40, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(minuteLbl);
    x = x + 40;
    panelData.minuteSpinner = new JSpinner();
    panelData.minuteSpinner.setBounds(x, getRowY(row), spinnerWidth, BasePanel.WIDGET_HEIGHT);
    panelData.minuteSpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    panelData.panel.add(panelData.minuteSpinner);
    // Seconds
    x = x + spinnerWidth + 2;
    JLabel secondLabel = new JLabel(Localisation.getField(FieldConfigBase.class, "FieldConfigTimePeriod.seconds"));
    secondLabel.setHorizontalAlignment(SwingConstants.LEADING);
    secondLabel.setBounds(x, getRowY(row), 40, BasePanel.WIDGET_HEIGHT);
    panelData.panel.add(secondLabel);
    x = x + 40;
    panelData.secondSpinner = new JSpinner();
    panelData.secondSpinner.setBounds(x, getRowY(row), spinnerWidth, BasePanel.WIDGET_HEIGHT);
    panelData.secondSpinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            valueUpdated();
        }
    });
    panelData.panel.add(panelData.secondSpinner);
    fieldPanel.add(panelData.panel);
    panelData.panel.setBounds(0, index * ((row * BasePanel.WIDGET_HEIGHT) + 50), BasePanel.FIELD_PANEL_WIDTH, (row + 2) * BasePanel.WIDGET_HEIGHT);
    Dimension preferredSize = new Dimension((int) panelData.panel.getPreferredSize().getWidth(), (row + 2) * BasePanel.WIDGET_HEIGHT);
    panelData.panel.setPreferredSize(preferredSize);
    panelData.panel.setBorder(BorderFactory.createTitledBorder(title));
}
Also used : JPanel(javax.swing.JPanel) JRadioButton(javax.swing.JRadioButton) ActionEvent(java.awt.event.ActionEvent) Calendar(java.util.Calendar) JDatePanelImpl(net.sourceforge.jdatepicker.impl.JDatePanelImpl) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) SpinnerDateModel(javax.swing.SpinnerDateModel) JDatePickerImpl(net.sourceforge.jdatepicker.impl.JDatePickerImpl) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) ChangeEvent(javax.swing.event.ChangeEvent) DateFormatter(javax.swing.text.DateFormatter) JSpinner(javax.swing.JSpinner) ChangeListener(javax.swing.event.ChangeListener)

Example 3 with DateFormatter

use of javax.swing.text.DateFormatter in project knime-core by knime.

the class DialogComponentDateTimeSelection method updateComponent.

/**
 * {@inheritDoc}
 */
@Override
protected void updateComponent() {
    final SettingsModelDateTime model = (SettingsModelDateTime) getModel();
    m_dateChooser.setDate(Date.from(model.getLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant()));
    m_dateCheckbox.setSelected(model.useDate() && m_displayOption.isShowDate());
    m_useMillis = model.useMillis();
    ((DateFormatter) m_editor.getTextField().getFormatter()).setFormat(new SimpleDateFormat(m_useMillis ? TIME_FORMAT_WITH_MS : TIME_FORMAT_WITHOUT_MS, Locale.getDefault()));
    m_timeSpinner.setValue(Date.from(model.getLocalTime().atDate(LocalDate.of(1970, 1, 1)).atZone(ZoneId.systemDefault()).toInstant()));
    m_timeCheckbox.setSelected(model.useTime() && m_displayOption.isShowTime());
    m_zoneComboBox.setSelectedItem(model.getZone().getId());
    m_zoneCheckbox.setSelected(model.useZone() && m_displayOption.isShowZone());
    setEnabledComponents(model.isEnabled());
}
Also used : DateFormatter(javax.swing.text.DateFormatter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with DateFormatter

use of javax.swing.text.DateFormatter in project sldeditor by robward-scisys.

the class FieldConfigDate method createUI.

/**
 * Creates the ui.
 */
/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.ui.detail.config.FieldConfigBase#createUI()
     */
@Override
public void createUI() {
    if (datePicker == null) {
        int xPos = getXPos();
        FieldPanel fieldPanel = createFieldPanel(xPos, getLabel());
        JDatePanelImpl datePanel = new JDatePanelImpl(dateModel);
        datePicker = new JDatePickerImpl(datePanel);
        datePicker.setBounds(xPos + BasePanel.WIDGET_X_START, 0, BasePanel.WIDGET_STANDARD_WIDTH, BasePanel.WIDGET_HEIGHT);
        fieldPanel.add(datePicker);
        dateModel.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                valueUpdated();
            }
        });
        // Time picker
        SpinnerDateModel model = new SpinnerDateModel();
        Calendar calendar = Calendar.getInstance();
        model.setValue(calendar.getTime());
        timePicker = new JSpinner(model);
        timePicker.setBounds(datePicker.getX() + datePicker.getWidth() + 10, 0, BasePanel.WIDGET_STANDARD_WIDTH, BasePanel.WIDGET_HEIGHT);
        timePicker.addChangeListener(new ChangeListener() {

            @Override
            public void stateChanged(ChangeEvent e) {
                valueUpdated();
            }
        });
        fieldPanel.add(timePicker);
        timeEditor = new JSpinner.DateEditor(timePicker, "HH:mm:ss");
        timePicker.setEditor(timeEditor);
        DateFormatter formatter = (DateFormatter) timeEditor.getTextField().getFormatter();
        formatter.setAllowsInvalid(false);
        formatter.setOverwriteMode(true);
        if (!isValueOnly()) {
            setAttributeSelectionPanel(fieldPanel.internalCreateAttrButton(Double.class, this, isRasterSymbol()));
        }
    }
}
Also used : Calendar(java.util.Calendar) JDatePanelImpl(net.sourceforge.jdatepicker.impl.JDatePanelImpl) SpinnerDateModel(javax.swing.SpinnerDateModel) JDatePickerImpl(net.sourceforge.jdatepicker.impl.JDatePickerImpl) ChangeEvent(javax.swing.event.ChangeEvent) DateFormatter(javax.swing.text.DateFormatter) JSpinner(javax.swing.JSpinner) ChangeListener(javax.swing.event.ChangeListener) FieldPanel(com.sldeditor.ui.widgets.FieldPanel)

Aggregations

DateFormatter (javax.swing.text.DateFormatter)4 SimpleDateFormat (java.text.SimpleDateFormat)2 Calendar (java.util.Calendar)2 JSpinner (javax.swing.JSpinner)2 SpinnerDateModel (javax.swing.SpinnerDateModel)2 ChangeEvent (javax.swing.event.ChangeEvent)2 ChangeListener (javax.swing.event.ChangeListener)2 JDatePanelImpl (net.sourceforge.jdatepicker.impl.JDatePanelImpl)2 JDatePickerImpl (net.sourceforge.jdatepicker.impl.JDatePickerImpl)2 FieldPanel (com.sldeditor.ui.widgets.FieldPanel)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 DateTimeParseException (java.time.format.DateTimeParseException)1 JCheckBox (javax.swing.JCheckBox)1 JFormattedTextField (javax.swing.JFormattedTextField)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 JRadioButton (javax.swing.JRadioButton)1