Esempio n. 1
0
  @Override
  public Object stringToValue(String text) {
    Pattern pattern = getPattern();
    Matcher matcher = pattern.matcher(text);

    if (matcher.matches()) {
      setMatcher(matcher);
      // set text to be green
      dce.setForeground(foreground);
      dce.setBackground(backgound);

      return text;
    } else {
      // set text to be red when it's not correct
      dce.setForeground(warningForeground);
      dce.setBackground(warningBackground);

      if (discardOnInvalid) {
        return "";
      }

      return text;
    }
  }
  @Override
  public void installUI(JComponent c) {
    super.installUI(c);

    textFormattedField = (JFormattedTextField) c;

    textFormattedField.setFocusable(true);
    textFormattedField.setOpaque(false);
    textFormattedField.setMargin(new Insets(0, 0, 0, 0));
    textFormattedField.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    textFormattedField.setFont(AdobeLookAndFeel.fontBold);
    textFormattedField.setForeground(AdobeLookAndFeel.colorText);
    textFormattedField.setSelectionColor(new Color(100, 100, 100));
    textFormattedField.setCaretColor(AdobeLookAndFeel.colorText);

    mouseAdapter =
        new MouseAdapter() {
          @Override
          public void mouseReleased(MouseEvent e) {
            textFormattedField.repaint();
          }

          @Override
          public void mousePressed(MouseEvent e) {
            textFormattedField.repaint();
          }

          @Override
          public void mouseClicked(MouseEvent e) {
            textFormattedField.repaint();
          }
        };
    textFormattedField.addMouseListener(mouseAdapter);

    keyAdapter =
        new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent e) {
            textFormattedField.repaint();
          }
        };
    textFormattedField.addKeyListener(keyAdapter);

    focusListener =
        new FocusAdapter() {
          @Override
          public void focusGained(FocusEvent e) {
            textFormattedField.repaint();
          }

          @Override
          public void focusLost(FocusEvent e) {
            textFormattedField.repaint();
          }
        };
    textFormattedField.addFocusListener(focusListener);

    propertyChangeListener =
        new PropertyChangeListener() {
          @Override
          public void propertyChange(PropertyChangeEvent evt) {
            textFormattedField.repaint();
          }
        };
    textFormattedField.addPropertyChangeListener(
        AccessibleContext.ACCESSIBLE_STATE_PROPERTY, propertyChangeListener);
  }