Search in sources :

Example 1 with MaskFormatter

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

the class Util method formatCpfCNPJ.

public static String formatCpfCNPJ(String str) {
    str = str.replaceAll("[\\D]", "");
    String pattern;
    if (str.length() <= 11) {
        str = String.format("%011d", Long.parseLong(str));
        pattern = "###.###.###-##";
    } else {
        str = String.format("%014d", Long.parseLong(str));
        pattern = "##.###.###/####-##";
    }
    try {
        MaskFormatter mask = new MaskFormatter(pattern);
        mask.setValueContainsLiteralCharacters(false);
        return mask.valueToString(str);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter) ParseException(java.text.ParseException)

Example 2 with MaskFormatter

use of javax.swing.text.MaskFormatter in project ramus by Vitaliy-Yakovchuk.

the class IDEF0ViewPlugin method createRegisterAction.

private ActionDescriptor createRegisterAction() {
    ActionDescriptor register = new ActionDescriptor();
    register.setMenu("Help");
    register.setAction(new AbstractAction() {

        {
            putValue(ACTION_COMMAND_KEY, "RegisterApplication");
        }

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            JPanel panel = new JPanel(new BorderLayout());
            double[][] size = { { 5, TableLayout.FILL, TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM, TableLayout.FILL, TableLayout.MINIMUM, TableLayout.FILL, 5 }, { 5, TableLayout.MINIMUM, 5, TableLayout.MINIMUM, 5, TableLayout.MINIMUM, 5, TableLayout.MINIMUM, 5 } };
            JPanel center = new JPanel(new TableLayout(size));
            final JTextField regName = new JTextField();
            center.add(new JLabel(ResourceLoader.getString("RegName")), "1,1,9,1");
            center.add(regName, "1,3,9,3");
            center.add(new JLabel(ResourceLoader.getString("SerialNumber")), "1,5,9,5");
            final JTextField[] fields = new JTextField[5];
            for (int i = 0; i < 5; i++) {
                try {
                    fields[i] = new JFormattedTextField(new MaskFormatter("AAAAA"));
                } catch (ParseException e1) {
                }
                fields[i].setPreferredSize(new Dimension(60, fields[i].getPreferredSize().height));
                center.add(fields[i], (i * 2 + 1) + ", 7");
                center.add(new JLabel("-"), (i * 2 + 2) + ", 7");
                final Action oldPaste = fields[i].getActionMap().get("paste");
                AbstractAction paste = new AbstractAction() {

                    @Override
                    public void actionPerformed(java.awt.event.ActionEvent e) {
                        String text = getClipboard();
                        if (text != null) {
                            String trim = text.trim();
                            if (trim.length() == 29) {
                                String[] strings = trim.split("-");
                                boolean ok = true;
                                for (String s : strings) {
                                    if (s.length() != 5)
                                        ok = false;
                                    for (char c : s.toCharArray()) {
                                        if (!Character.isLetterOrDigit(c))
                                            ok = false;
                                    }
                                }
                                if (ok) {
                                    for (int i = 0; i < 5; i++) {
                                        fields[i].setText(strings[i].toUpperCase());
                                    }
                                    return;
                                }
                            }
                        }
                        oldPaste.actionPerformed(e);
                    }
                };
                fields[i].getActionMap().put("paste-from-clipboard", paste);
                fields[i].getActionMap().put("paste", paste);
            }
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    fields[0].requestFocus();
                }
            });
            panel.add(center, BorderLayout.SOUTH);
            BaseDialog dialog = new BaseDialog(framework.getMainFrame(), true) {

                private int count = 0;

                @Override
                protected void onOk() {
                    final StringBuffer sb = new StringBuffer();
                    for (JTextField field : fields) {
                        sb.append(field.getText().trim());
                        sb.append("-");
                    }
                    String serial = sb.toString().substring(0, sb.length() - 1);
                    if (new SerialCheker().check(serial)) {
                        Metadata.DEMO_REGISTERED = true;
                        com.ramussoft.gui.common.prefrence.Options.setString("REGISTERED_VERSION", Boolean.toString(true));
                        Options.setString("SERIAL", serial);
                        for (ElementListener el : framework.getEngine().getElementListeners(null)) {
                            if (el instanceof DemoChecker) {
                                DemoChecker dc = (DemoChecker) el;
                                framework.getEngine().removeElementListener(null, el);
                                dc.getFramework().propertyChanged("EnableSaveActions");
                                String uc = " " + GlobalResourcesManager.getString("UnregisteredCopy");
                                String title = framework.getMainFrame().getTitle().replace(uc, "");
                                framework.getMainFrame().setTitle(title);
                            }
                        }
                        com.ramussoft.gui.common.prefrence.Options.setString("REG_NAME", regName.getText());
                        Metadata.REGISTERED_FOR = regName.getText();
                        super.onOk();
                        SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                JOptionPane.showMessageDialog(framework.getMainFrame(), MessageFormat.format(ResourceLoader.getString("SerialIsRight"), Metadata.getApplicationName()));
                            }
                        });
                    } else {
                        try {
                            getOKButton().setEnabled(false);
                            Thread.sleep(3000 * count);
                            count++;
                            SwingUtilities.invokeLater(new Runnable() {

                                @Override
                                public void run() {
                                    JOptionPane.showMessageDialog(framework.getMainFrame(), ResourceLoader.getString("SerialIsWrong"));
                                }
                            });
                            getOKButton().setEnabled(true);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            };
            dialog.setTitle(ResourceLoader.getString("RegisterApplication"));
            dialog.setMainPane(panel);
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setResizable(false);
            dialog.setVisible(true);
        }
    });
    return register;
}
Also used : JPanel(javax.swing.JPanel) AbstractAction(javax.swing.AbstractAction) Action(javax.swing.Action) DemoChecker(com.ramussoft.pb.idef.elements.DemoChecker) MaskFormatter(javax.swing.text.MaskFormatter) JTextField(javax.swing.JTextField) BorderLayout(java.awt.BorderLayout) ElementListener(com.ramussoft.common.event.ElementListener) AbstractAction(javax.swing.AbstractAction) TableLayout(info.clearthought.layout.TableLayout) SerialCheker(com.ramussoft.pb.frames.components.SerialCheker) JFormattedTextField(javax.swing.JFormattedTextField) JLabel(javax.swing.JLabel) Dimension(java.awt.Dimension) ParseException(java.text.ParseException)

Example 3 with MaskFormatter

use of javax.swing.text.MaskFormatter in project PCUGameManager by lantzelot-swe.

the class SaveStatePanel method getTimeField.

private JFormattedTextField getTimeField() {
    if (timeField == null) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter("##:##:##");
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
            System.exit(-1);
        }
        formatter.setPlaceholderCharacter('0');
        timeField = new JFormattedTextField(formatter);
        timeField.addKeyListener(new KeyAdapter() {

            /**
             * Handles increase/decrease of time value with arrow buttons
             *
             * @param e keyevent
             */
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    increaseTime(timeField.getCaretPosition());
                } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    decreaseTime(timeField.getCaretPosition());
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                // Only notify of change when text is altered: value is not committed before focus is lost.
                if (!timeField.getText().equals(timeField.getValue())) {
                    stateModel.notifyChange();
                }
            }
        });
        timeField.addFocusListener(new FocusAdapter() {

            @Override
            public void focusLost(FocusEvent e) {
                try {
                    timeField.commitEdit();
                } catch (ParseException e1) {
                    ExceptionHandler.handleException(e1, "");
                }
                setTimeToModel();
            }
        });
        timeField.setPreferredSize(new Dimension(55, 20));
        timeField.setMinimumSize(new Dimension(55, 20));
    }
    return timeField;
}
Also used : KeyEvent(java.awt.event.KeyEvent) FocusAdapter(java.awt.event.FocusAdapter) KeyAdapter(java.awt.event.KeyAdapter) JFormattedTextField(javax.swing.JFormattedTextField) MaskFormatter(javax.swing.text.MaskFormatter) ParseException(java.text.ParseException) Dimension(java.awt.Dimension) ParseException(java.text.ParseException) FocusEvent(java.awt.event.FocusEvent)

Example 4 with MaskFormatter

use of javax.swing.text.MaskFormatter in project MeteoInfo by meteoinfo.

the class JTextFieldDateEditor method setMaskVisible.

/**
 * Sets the mask visible.
 *
 * @param isMaskVisible
 *            true, if the mask should be visible
 */
public void setMaskVisible(boolean isMaskVisible) {
    this.isMaskVisible = isMaskVisible;
    if (isMaskVisible) {
        if (maskFormatter == null) {
            try {
                maskFormatter = new MaskFormatter(createMaskFromDatePattern(datePattern));
                maskFormatter.setPlaceholderCharacter(this.placeholder);
                maskFormatter.install(this);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : MaskFormatter(javax.swing.text.MaskFormatter) ParseException(java.text.ParseException)

Example 5 with MaskFormatter

use of javax.swing.text.MaskFormatter in project mdw-designer by CenturyLinkCloud.

the class FormWidgetDialog method createOneValidator.

private int createOneValidator(String value, int yOffset) {
    JTextField ftt;
    if (useMask) {
        String mask = value;
        int m = mask.length();
        for (int j = 0; j < m; j++) {
            if (!Character.isLetter(mask.charAt(j))) {
                mask = mask.substring(0, j);
                break;
            }
        }
        try {
            MaskFormatter fmter = new MaskFormatter(validatorMasks.get(mask));
            fmter.setPlaceholderCharacter('_');
            ftt = new JFormattedTextField(fmter);
        } catch (Exception e) {
            ftt = new JFormattedTextField();
        }
    } else {
        ftt = new JTextField();
    }
    ftt.setText(value);
    ftt.setFont(inputFont);
    ftt.setBounds(2, yOffset, 290, 20);
    JCheckBox sel = new JCheckBox();
    sel.setBounds(300, yOffset, 20, 20);
    validatorTable.add(sel);
    int yOffs = yOffset + 22;
    validatorTable.add(ftt);
    validatorEditors.add(ftt);
    validatorSelectors.add(sel);
    return yOffs;
}
Also used : JCheckBox(javax.swing.JCheckBox) JFormattedTextField(javax.swing.JFormattedTextField) MaskFormatter(javax.swing.text.MaskFormatter) JTextField(javax.swing.JTextField)

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