Example usage for javax.swing.event DocumentListener changedUpdate

List of usage examples for javax.swing.event DocumentListener changedUpdate

Introduction

In this page you can find the example usage for javax.swing.event DocumentListener changedUpdate.

Prototype

public void changedUpdate(DocumentEvent e);

Source Link

Document

Gives notification that an attribute or set of attributes changed.

Usage

From source file:edu.ku.brc.af.ui.forms.validation.ValFormattedTextFieldSingle.java

/**
 * Sets the formatter./*from   ww  w .  j  a  v a2  s  . c om*/
 * @param dataObjFormatterName the formatter to use
 */
protected void setFormatterInternal(final UIFieldFormatterIFace formatterArg) {
    if (formatterArg != null && formatter != formatterArg) {
        formatter = formatterArg;

        fields = formatter.getFields();

        int requiredLength = formatter.getLength();
        bgStr = formatter.toPattern();

        document = new JFormattedDoc(this, formatter, requiredLength);
        document.setIgnoreLenForValidation(isPartialOK);
        setDocument(document);
        document.addDocumentListener(new DocumentAdaptor() {
            @Override
            protected void changed(DocumentEvent e) {
                isChanged = true;
                if (formatter.isLengthOK(getText().length())) {
                    setState(formatter.isValid(getText()) ? UIValidatable.ErrorType.Valid
                            : UIValidatable.ErrorType.Error);
                    repaint();
                }

                if (documentListeners != null) {
                    for (DocumentListener dl : documentListeners) {
                        dl.changedUpdate(null);
                    }
                }
            }
        });
    }
}

From source file:edu.ku.brc.af.ui.forms.validation.ValFormattedTextField.java

/**
 * Creates the various UI Components for the formatter.
 *//*from w w  w.  j a v  a  2s  . co m*/
protected void createUI() {
    CellConstraints cc = new CellConstraints();

    if (isViewOnly
            || (formatter != null && !formatter.isUserInputNeeded() && fields != null && fields.size() == 1)) {
        viewtextField = new JTextField();
        setControlSize(viewtextField);

        // Remove by rods 12/5/08 this messes thihngs up
        // values don't get inserted correctly, shouldn't be needed anyway

        //JFormattedDoc document = new JFormattedDoc(viewtextField, formatter, formatter.getFields().get(0));
        //viewtextField.setDocument(document);
        //document.addDocumentListener(this);
        //documents.add(document);

        ViewFactory.changeTextFieldUIForDisplay(viewtextField, false);
        PanelBuilder builder = new PanelBuilder(new FormLayout("1px,f:p:g,1px", "1px,f:p:g,1px"), this);
        builder.add(viewtextField, cc.xy(2, 2));
        bgColor = viewtextField.getBackground();

    } else {
        JTextField txt = new JTextField();

        Font txtFont = txt.getFont();

        Font font = new Font("Courier", Font.PLAIN, txtFont.getSize());

        BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = bi.createGraphics();
        g.setFont(font);
        FontMetrics fm = g.getFontMetrics(font);
        g.dispose();

        Insets ins = txt.getBorder().getBorderInsets(txt);
        int baseWidth = ins.left + ins.right;

        bgColor = txt.getBackground();

        StringBuilder sb = new StringBuilder("1px");
        for (UIFieldFormatterField f : fields) {
            sb.append(",");
            if (f.getType() == FieldType.separator || f.getType() == FieldType.constant) {
                sb.append('p');
            } else {
                sb.append(((fm.getMaxAdvance() * f.getSize()) + baseWidth) + "px");
            }
        }
        sb.append(",1px");
        PanelBuilder builder = new PanelBuilder(new FormLayout(sb.toString(), "1px,P:G,1px"), this);

        comps = new JComponent[fields.size()];
        int inx = 0;
        for (UIFieldFormatterField f : fields) {
            JComponent comp = null;
            JComponent tfToAdd = null;

            if (f.getType() == FieldType.separator || f.getType() == FieldType.constant) {
                comp = createLabel(f.getValue());
                if (f.getType() == FieldType.constant) {
                    comp.setBackground(Color.WHITE);
                    comp.setOpaque(true);
                }
                tfToAdd = comp;

            } else {
                JTextField tf = new BGTextField(f.getSize(), isViewOnly ? "" : f.getValue());
                tfToAdd = tf;

                if (inx == 0) {
                    tf.addKeyListener(new KeyAdapter() {
                        @Override
                        public void keyPressed(KeyEvent e) {
                            checkForPaste(e);
                        }
                    });
                }

                JFormattedDoc document = new JFormattedDoc(tf, formatter, f);
                tf.setDocument(document);
                document.addDocumentListener(new DocumentAdaptor() {
                    @Override
                    protected void changed(DocumentEvent e) {
                        isChanged = true;
                        if (!shouldIgnoreNotifyDoc) {
                            String fldStr = getText();
                            int len = StringUtils.isNotEmpty(fldStr) ? fldStr.length() : 0;
                            if (formatter != null && len > 0 && formatter.isLengthOK(len)) {
                                setState(formatter.isValid(fldStr) ? UIValidatable.ErrorType.Valid
                                        : UIValidatable.ErrorType.Error);
                                repaint();
                            }

                            //validateState();
                            if (changeListener != null) {
                                changeListener.stateChanged(new ChangeEvent(this));
                            }

                            if (documentListeners != null) {
                                for (DocumentListener dl : documentListeners) {
                                    dl.changedUpdate(null);
                                }
                            }
                        }
                        currCachedValue = null;
                    }
                });
                documents.add(document);

                addFocusAdapter(tf);

                comp = tf;
                comp.setFont(font);

                if (f.isIncrementer()) {
                    editTF = tf;
                    cardLayout = new CardLayout();
                    cardPanel = new JPanel(cardLayout);
                    cardPanel.add("edit", tf);

                    viewTF = new BGTextField(f.getSize(), isViewOnly ? "" : f.getValue());
                    viewTF.setDocument(document);
                    cardPanel.add("view", viewTF);

                    cardLayout.show(cardPanel, "view");
                    comp = cardPanel;
                    tfToAdd = cardPanel;
                }
            }

            setControlSize(tfToAdd);
            builder.add(comp, cc.xy(inx + 2, 2));
            comps[inx] = tfToAdd;
            inx++;
        }
    }
}