Search in sources :

Example 11 with MaskFormatter

use of javax.swing.text.MaskFormatter in project invesdwin-nowicket by invesdwin.

the class FormInput method validateUsPhoneNumber.

/**
 * Instead of using the MaskConverter in the Page, we just validate the value here with a custom validator.
 */
public String validateUsPhoneNumber(final String newValue) {
    try {
        final MaskFormatter maskFormatter = new MaskFormatter("(###) ###-####");
        maskFormatter.setValueClass(String.class);
        maskFormatter.setAllowsInvalid(true);
        maskFormatter.setValueContainsLiteralCharacters(true);
        final Object obj = maskFormatter.stringToValue(newValue);
        if (obj != null) {
            return null;
        }
    } catch (final ParseException e) {
    // ignore
    }
    // so we can provide a different message than "is not a valid string"
    return "does not match the mask";
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter) AValueObject(de.invesdwin.util.bean.AValueObject) ParseException(java.text.ParseException)

Example 12 with MaskFormatter

use of javax.swing.text.MaskFormatter in project asl-sensor-suite by usgs.

the class InputPanel method queryFDSN.

private void queryFDSN(int panelToLoad, FileOperationJButton seedButton) {
    // this section produces a selection box to make sure FDSN loading is user preference
    {
        JRadioButton local = new JRadioButton("Load from SEED file");
        JRadioButton fdsn = new JRadioButton("Load from FDSN");
        ButtonGroup group = new ButtonGroup();
        group.add(local);
        group.add(fdsn);
        local.setSelected(true);
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(local);
        panel.add(fdsn);
        int result = JOptionPane.showConfirmDialog(this, panel, "Select load method", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.CANCEL_OPTION) {
            return;
        }
        if (local.isSelected()) {
            loadData(panelToLoad, seedButton);
            return;
        }
    }
    try {
        MaskFormatter networkFormatter = new MaskFormatter("AA");
        // MaskFormatter stationFormatter = new MaskFormatter("UUUUU");
        MaskFormatter locationFormatter = new MaskFormatter("##");
        MaskFormatter channelFormatter = new MaskFormatter("UUA");
        JFormattedTextField networkField = new JFormattedTextField(networkFormatter);
        networkField.setText("");
        networkField.setMinimumSize(networkField.getPreferredSize());
        JTextField stationField = new JTextField();
        stationField.setText("");
        stationField.setMinimumSize(stationField.getPreferredSize());
        JFormattedTextField locationField = new JFormattedTextField(locationFormatter);
        locationField.setText("");
        locationField.setMinimumSize(locationField.getPreferredSize());
        JFormattedTextField channelField = new JFormattedTextField(channelFormatter);
        channelField.setText("");
        channelField.setMinimumSize(channelField.getPreferredSize());
        Date start = null;
        Date end = null;
        // set initial start and end times to be 2 days ago and 1 day ago at day start
        // such that there should be data for the full day's length that already exists
        Date defaultStartValue = Date.from(LocalDate.now().minusDays(2).atStartOfDay(ZoneOffset.UTC).toInstant());
        Date defaultEndValue = Date.from(LocalDate.now().minusDays(1).atStartOfDay(ZoneOffset.UTC).toInstant());
        // if there is any data, enforce range restriction on that limit
        if (dataStore.areAnyBlocksSet(activePlots)) {
            Pair<Long, Long> startAndEnd = dataStore.getCommonTime(activePlots);
            start = Date.from(Instant.ofEpochMilli(startAndEnd.getFirst()));
            defaultStartValue = start;
            end = Date.from(Instant.ofEpochMilli(startAndEnd.getSecond()));
            defaultEndValue = end;
        }
        JSpinner startPicker = timePickerFactory(start, end);
        JSpinner endPicker = timePickerFactory(start, end);
        SimpleDateFormat format = ((JSpinner.DateEditor) startPicker.getEditor()).getFormat();
        format.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
        format = ((JSpinner.DateEditor) endPicker.getEditor()).getFormat();
        format.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
        startPicker.setValue(defaultStartValue);
        endPicker.setValue(defaultEndValue);
        JPanel queryPanel = new JPanel();
        queryPanel.setLayout(new GridLayout(7, 2));
        queryPanel.add(new JLabel("NOTE:"));
        queryPanel.add(new JLabel("Wildcards are not supported!"));
        queryPanel.add(new JLabel("Network: (ex: IU)"));
        queryPanel.add(networkField);
        queryPanel.add(new JLabel("Station: (ex: ANMO)"));
        queryPanel.add(stationField);
        queryPanel.add(new JLabel("Location: (ex: 00)"));
        queryPanel.add(locationField);
        queryPanel.add(new JLabel("Channel: (ex: LHZ)"));
        queryPanel.add(channelField);
        queryPanel.add(new JLabel("Start time (UTC):"));
        queryPanel.add(startPicker);
        queryPanel.add(new JLabel("End time (UTC):"));
        queryPanel.add(endPicker);
        int result = JOptionPane.showConfirmDialog(this, queryPanel, "Set FDSN query parameters", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            String net = networkField.getText().toUpperCase();
            String sta = stationField.getText().replaceAll("\\s", "").toUpperCase();
            String loc = locationField.getText().replaceAll("\\s", "").toUpperCase();
            String cha = channelField.getText().toUpperCase();
            Date startDate = (Date) startPicker.getValue();
            long startMillis = startDate.toInstant().toEpochMilli();
            Date endDate = (Date) endPicker.getValue();
            long endMillis = endDate.toInstant().toEpochMilli();
            threadedDataFromFDSN(panelToLoad, net, sta, loc, cha, startMillis, endMillis);
        }
    } catch (ParseException e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(this, "ERROR CREATING FDSN QUERY DIALOG BOX", "FDSN ERROR", JOptionPane.ERROR_MESSAGE);
    }
}
Also used : JPanel(javax.swing.JPanel) JRadioButton(javax.swing.JRadioButton) BoxLayout(javax.swing.BoxLayout) JFormattedTextField(javax.swing.JFormattedTextField) JLabel(javax.swing.JLabel) MaskFormatter(javax.swing.text.MaskFormatter) JTextField(javax.swing.JTextField) Date(java.util.Date) LocalDate(java.time.LocalDate) GridLayout(java.awt.GridLayout) ButtonGroup(javax.swing.ButtonGroup) JSpinner(javax.swing.JSpinner) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 13 with MaskFormatter

use of javax.swing.text.MaskFormatter in project servoy-client by Servoy.

the class DataField method processKeyBinding.

/**
 * @see javax.swing.JComponent#processKeyBinding(javax.swing.KeyStroke,java.awt.event.KeyEvent, int, boolean)
 */
@Override
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
    if (!hasFocus()) {
        // FIX for tableview (JTable) that the field doesn't already have focus before it get's the first keyevent
        if (getFormatterFactory() instanceof DefaultFormatterFactory && ((DefaultFormatterFactory) getFormatterFactory()).getEditFormatter() != null) {
            DefaultFormatterFactory dff = (DefaultFormatterFactory) getFormatterFactory();
            setFormatter(dff.getEditFormatter());
        }
        setCaretPosition(getText().length());
        selectAll();
        eventExecutor.skipSelectOnEnter();
    }
    if (e.getKeyCode() == KeyEvent.VK_DECIMAL && isEditable() && Column.mapToDefaultType(dataType) == IColumnTypes.NUMBER) {
        if (e.getID() == KeyEvent.KEY_PRESSED) {
            decimalMode = true;
        } else {
            decimalMode = false;
        }
    } else if (decimalMode) {
        if (e.getID() == KeyEvent.KEY_TYPED) {
            replaceSelection(decimalSeparator);
            return true;
        }
    } else {
        decimalMode = false;
        AbstractFormatterFactory formatterFactory = getFormatterFactory();
        DefaultFormatter formatter = null;
        if (formatterFactory instanceof DefaultFormatterFactory) {
            DefaultFormatterFactory factory = (DefaultFormatterFactory) formatterFactory;
            AbstractFormatter editFormatter = factory.getEditFormatter();
            if (editFormatter == null)
                editFormatter = factory.getDefaultFormatter();
            if (editFormatter instanceof DefaultFormatter && !(editFormatter instanceof MaskFormatter)) {
                formatter = (DefaultFormatter) editFormatter;
            }
        }
        if (Column.mapToDefaultType(dataType) == IColumnTypes.DATETIME && formatter != null && formatter.getOverwriteMode() && isEditable()) {
            String selected = getSelectedText();
            if (selected == null || !getText().equals(selected)) {
                if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                    if (e.getID() == KeyEvent.KEY_RELEASED) {
                        Caret c = getCaret();
                        if (c.getDot() >= 0 && getText().length() > (c.getDot())) {
                            int dot = c.getDot();
                            setSelectionStart(c.getDot());
                            setSelectionEnd(c.getDot() + 1);
                            // $NON-NLS-1$
                            replaceSelection(" ");
                            c.setDot(dot);
                        }
                    }
                    return true;
                } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                    if (e.getID() == KeyEvent.KEY_RELEASED) {
                        Caret c = getCaret();
                        if (c.getDot() > 0) {
                            int dot = c.getDot();
                            setSelectionStart(c.getDot() - 1);
                            setSelectionEnd(c.getDot());
                            // $NON-NLS-1$
                            replaceSelection(" ");
                            c.setDot(dot - 1);
                            return true;
                        }
                        e.setKeyCode(KeyEvent.VK_LEFT);
                        e.setKeyChar(KeyEvent.CHAR_UNDEFINED);
                        ks = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ks.getModifiers(), ks.isOnKeyRelease());
                    }
                    return true;
                }
            }
        }
        if (e.getKeyCode() == KeyEvent.VK_INSERT && formatter != null) {
            if (e.getID() == KeyEvent.KEY_PRESSED && toggleOverwrite) {
                formatter.setOverwriteMode(!formatter.getOverwriteMode());
                toggleOverwrite = false;
                ((ISmartClientApplication) application).updateInsertModeIcon(this);
                int caretPos = getCaretPosition();
                if (formatter.getOverwriteMode()) {
                    setCaret(getOvertypeCaret());
                } else {
                    setCaret(getDefaultCaret());
                }
                setCaretPosition(caretPos);
            } else if (e.getID() == KeyEvent.KEY_RELEASED) {
                toggleOverwrite = true;
            }
        }
        return super.processKeyBinding(ks, e, condition, pressed);
    }
    return false;
}
Also used : DefaultFormatter(javax.swing.text.DefaultFormatter) FixedMaskFormatter(com.servoy.j2db.util.text.FixedMaskFormatter) ServoyMaskFormatter(com.servoy.j2db.util.text.ServoyMaskFormatter) MaskFormatter(javax.swing.text.MaskFormatter) DefaultFormatterFactory(javax.swing.text.DefaultFormatterFactory) Caret(javax.swing.text.Caret) Point(java.awt.Point)

Example 14 with MaskFormatter

use of javax.swing.text.MaskFormatter in project sandbox by murer.

the class Util method formatCEP.

public static String formatCEP(String str) {
    str = str.replaceAll("[\\D]", "");
    try {
        str = String.format("%08d", Long.parseLong(str));
        String pattern = "#####-###";
        try {
            MaskFormatter mask = new MaskFormatter(pattern);
            mask.setValueContainsLiteralCharacters(false);
            return mask.valueToString(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    } catch (NumberFormatException e) {
        throw new RuntimeException(e);
    }
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter) ParseException(java.text.ParseException)

Example 15 with MaskFormatter

use of javax.swing.text.MaskFormatter in project astroimagej by AstroImageJ.

the class Data_Processor method createFormatter.

protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter(s);
        formatter.setOverwriteMode(true);
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return formatter;
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter)

Aggregations

MaskFormatter (javax.swing.text.MaskFormatter)15 ParseException (java.text.ParseException)10 JFormattedTextField (javax.swing.JFormattedTextField)5 JTextField (javax.swing.JTextField)4 JLabel (javax.swing.JLabel)3 JPanel (javax.swing.JPanel)3 DefaultFormatter (javax.swing.text.DefaultFormatter)3 DefaultFormatterFactory (javax.swing.text.DefaultFormatterFactory)3 Dimension (java.awt.Dimension)2 GridLayout (java.awt.GridLayout)2 SimpleDateFormat (java.text.SimpleDateFormat)2 LocalDate (java.time.LocalDate)2 Date (java.util.Date)2 BoxLayout (javax.swing.BoxLayout)2 ButtonGroup (javax.swing.ButtonGroup)2 JRadioButton (javax.swing.JRadioButton)2 JSpinner (javax.swing.JSpinner)2 ElementListener (com.ramussoft.common.event.ElementListener)1 SerialCheker (com.ramussoft.pb.frames.components.SerialCheker)1 DemoChecker (com.ramussoft.pb.idef.elements.DemoChecker)1