alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */


package org.netbeans.modules.search.types;


import java.awt.Color;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.util.ResourceBundle;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.CompoundBorder;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.openide.awt.Mnemonics;
import org.openide.util.NbBundle;


/**
 * Customizer of TextType beans.
 *
 * @author  Petr Kuzel

 */
public class TextCustomizer extends JPanel implements Customizer {

    private TextType peer;
    private String lastSubstring = "";
    private String lastRegexp = "";

    /** Creates new form FullTextCustomizer */
    public TextCustomizer() {
        initComponents ();
        initAccessibility ();
        initTextFieldListeners();
        TitledBorder tb = new TitledBorder(getBorderLabel());
        tb.setBorder(new CompoundBorder());
        setBorder (tb);

        ButtonGroup group = new ButtonGroup();
        group.add(substringRadioButton);
        group.add(regexpRadioButton);

    }

    /** Allow derived customizers.
    */
    protected String getBorderLabel() {
        return null;
    }

    private void initAccessibility(){                
        ResourceBundle bundle = NbBundle.getBundle(TextCustomizer.class);
        substringTextField.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_PROP_SUBSTRING"));
        substringTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_PROP_SUBSTRING"));
        regexpTextField.getAccessibleContext().setAccessibleName(bundle.getString("ACSN_PROP_RE"));         
        regexpTextField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_PROP_RE"));         
        caseSensitiveCheckBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_CASE_SENSITIVE"));         
        wholeWordsCheckBox.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_WHOLE_WORDS"));         
        regexpRadioButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_RE"));         
        substringRadioButton.getAccessibleContext().setAccessibleDescription(bundle.getString("ACS_TEXT_LABEL_SUBSTRING"));         
    }
    
    /**
     */
    private void initTextFieldListeners() {
        class TextChangeListener implements DocumentListener {
            private JTextField textField;
            TextChangeListener(JTextField textField) {
                this.textField = textField;
            }
            public void changedUpdate(DocumentEvent e) {
                documentChanged();
            }
            public void insertUpdate(DocumentEvent e) {
                documentChanged();
            }
            public void removeUpdate(DocumentEvent e) {
                documentChanged();
            }
            private void documentChanged() {
                if (textField == substringTextField) {
                    substringChanged();
                } else {
                    regexpChanged();
                }
            }
        }
        substringTextField.getDocument().addDocumentListener(
                new TextChangeListener(substringTextField));
        regexpTextField.getDocument().addDocumentListener(
                new TextChangeListener(regexpTextField));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        substringRadioButton = new javax.swing.JRadioButton();
        regexpRadioButton = new javax.swing.JRadioButton();
        jPanel1 = new javax.swing.JPanel();
        caseSensitiveCheckBox = new javax.swing.JCheckBox();
        wholeWordsCheckBox = new javax.swing.JCheckBox();

        setLayout(new java.awt.GridBagLayout());

        substringRadioButton.setSelected(true);
        Mnemonics.setLocalizedText(substringRadioButton, NbBundle.getMessage(TextCustomizer.class, "TEXT_LABEL_SUBSTRING"));  //NOI18N
        substringRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                substringRadioButtonActionPerformed(evt);
            }
        });
        substringRadioButton.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                substringRadioButtonStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 0);
        add(substringRadioButton, gridBagConstraints);

        substringTextField.setColumns(20);
        substringTextField.setPreferredSize(new java.awt.Dimension (100, substringTextField.getPreferredSize ().height));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 11);
        add(substringTextField, gridBagConstraints);

        Mnemonics.setLocalizedText(regexpRadioButton, NbBundle.getMessage(TextCustomizer.class, "TEXT_LABEL_RE"));  //NOI18N
        regexpRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                regexpRadioButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 5, 0);
        add(regexpRadioButton, gridBagConstraints);

        regexpTextField.setColumns(20);
        regexpTextField.setPreferredSize(new java.awt.Dimension (100, regexpTextField.getPreferredSize ().height));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 5, 11);
        add(regexpTextField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        add(jPanel1, gridBagConstraints);

        Mnemonics.setLocalizedText(caseSensitiveCheckBox, NbBundle.getMessage(TextCustomizer.class, "TEXT_LABEL_CASE_SENSITIVE"));  //NOI18N
        caseSensitiveCheckBox.setBorder(null);
        caseSensitiveCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                caseSensitiveCheckBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 30, 0, 11);
        add(caseSensitiveCheckBox, gridBagConstraints);

        Mnemonics.setLocalizedText(wholeWordsCheckBox, NbBundle.getMessage(TextCustomizer.class, "TEXT_LABEL_WHOLE_WORDS"));  //NOI18N
        wholeWordsCheckBox.setBorder(null);
        wholeWordsCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wholeWordsCheckBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 30, 0, 11);
        add(wholeWordsCheckBox, gridBagConstraints);

    }//GEN-END:initComponents

    private void substringRadioButtonStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_substringRadioButtonStateChanged
        caseSensitiveCheckBox.setEnabled(substringRadioButton.isSelected());
        wholeWordsCheckBox.setEnabled(substringRadioButton.isSelected());
    }//GEN-LAST:event_substringRadioButtonStateChanged

    private void wholeWordsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wholeWordsCheckBoxActionPerformed
        peer.setWholeWords(wholeWordsCheckBox.isSelected());
    }//GEN-LAST:event_wholeWordsCheckBoxActionPerformed

    private void caseSensitiveCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_caseSensitiveCheckBoxActionPerformed
        peer.setCaseSensitive(caseSensitiveCheckBox.isSelected());
    }//GEN-LAST:event_caseSensitiveCheckBoxActionPerformed

    private void regexpRadioButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_regexpRadioButtonActionPerformed

        String text = regexpTextField.getText();
        try {
            peer.setRe(text);
        } catch (IllegalArgumentException ex) {
        }

    }//GEN-LAST:event_regexpRadioButtonActionPerformed

    private void substringRadioButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_substringRadioButtonActionPerformed

        peer.setMatchString(substringTextField.getText());

    }//GEN-LAST:event_substringRadioButtonActionPerformed

    /**
     * Called whenever contents of the Substring textfield changes.
     */
    private void substringChanged() {
        String text = substringTextField.getText();
        if (!text.equals(lastSubstring)) {
            peer.setMatchString(text);
            substringRadioButton.setSelected(true);
            lastSubstring = text;
        }
    }
    
    /**
     * Called whenever contents of the Regular Expression textfield
     * changes.
     */
    private void regexpChanged() {
        String text = regexpTextField.getText();
        if (!text.equals(lastRegexp)) {
            regexpRadioButton.setSelected(true);
            try {
                peer.setRe(text);
                regexpTextField.setForeground(getForegroundColor());
            } catch (IllegalArgumentException ex) {
                regexpTextField.setForeground(getErrorForegroundColor());
            }
            lastRegexp = text;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox caseSensitiveCheckBox;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton regexpRadioButton;
    private final javax.swing.JTextField regexpTextField = new javax.swing.JTextField();
    private javax.swing.JRadioButton substringRadioButton;
    private final javax.swing.JTextField substringTextField = new javax.swing.JTextField();
    private javax.swing.JCheckBox wholeWordsCheckBox;
    // End of variables declaration//GEN-END:variables

    /** Initialize customizer with proper values.
    */
    public void setObject(final Object obj) {

        peer = (TextType) obj;

        // set default coloring
        regexpTextField.setForeground(getForegroundColor());
        substringTextField.setForeground(getForegroundColor());

        /* Display in customizer. */

        substringTextField.setText(peer.getMatchString());
        lastSubstring = peer.getMatchString();
        caseSensitiveCheckBox.setSelected(peer.isCaseSensitive());
        wholeWordsCheckBox.setSelected(peer.getWholeWords());
        regexpTextField.setText(peer.getRe());
        lastRegexp = peer.getRe();

        if(!"".equals(peer.getRe())) // NOI18N
            regexpRadioButton.setSelected(true);
        if(!"".equals(peer.getMatchString())) // NOI18N
            substringRadioButton.setSelected(true);
    }

    public void addPropertyChangeListener(final PropertyChangeListener p1) {
    }

    public void removePropertyChangeListener(final PropertyChangeListener p1) {
    }

    public void requestFocus() {
        JTextField tf = regexpRadioButton.isSelected() ?
                          regexpTextField : substringTextField;
        int n = tf.getText().length();
        if (n > 0) {
            tf.setCaretPosition(0);
            tf.moveCaretPosition(n);
        }
        tf.requestFocus();
    }


    // colors

    private Color findColor (String key, Color defCol) {
        Color color = UIManager.getDefaults().getColor (key);
        if ( color != null ) {
            return color;
        }
        return defCol;
    }

    private Color getForegroundColor () {
        return findColor ("TextField.foreground", Color.black);
    }

    private Color getErrorForegroundColor () {
        return findColor ("TextField.errorForeground", Color.red);
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.