001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: FormValidator.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    package org.jfree.report.modules.gui.swing.common;
032    
033    import java.awt.event.ActionEvent;
034    import java.awt.event.ActionListener;
035    import java.awt.event.ItemEvent;
036    import java.awt.event.ItemListener;
037    import java.beans.PropertyChangeEvent;
038    import java.beans.PropertyChangeListener;
039    import javax.swing.AbstractButton;
040    import javax.swing.Action;
041    import javax.swing.JComboBox;
042    import javax.swing.event.DocumentEvent;
043    import javax.swing.event.DocumentListener;
044    import javax.swing.text.Document;
045    import javax.swing.text.JTextComponent;
046    
047    public abstract class FormValidator
048    {
049    
050      private class FormTextfieldListener
051              implements DocumentListener, PropertyChangeListener
052      {
053        private FormTextfieldListener ()
054        {
055        }
056    
057        /**
058         * This method gets called when a bound property is changed.
059         *
060         * @param evt A PropertyChangeEvent object describing the event source and the
061         *            property that has changed.
062         */
063        public void propertyChange (final PropertyChangeEvent evt)
064        {
065          if (DOCUMENT_PROPERTY_NAME.equals(evt.getPropertyName()))
066          {
067            final Document olddoc = (Document) evt.getOldValue();
068            olddoc.removeDocumentListener(this);
069            final Document newdoc = (Document) evt.getOldValue();
070            newdoc.addDocumentListener(this);
071          }
072        }
073    
074        /**
075         * Gives notification that an attribute or set of attributes changed.
076         *
077         * @param e the document event
078         */
079        public void changedUpdate (final DocumentEvent e)
080        {
081          handleValidate();
082        }
083    
084        /**
085         * Gives notification that there was an insert into the document.  The range given by
086         * the DocumentEvent bounds the freshly inserted region.
087         *
088         * @param e the document event
089         */
090        public void insertUpdate (final DocumentEvent e)
091        {
092          handleValidate();
093        }
094    
095        /**
096         * Gives notification that a portion of the document has been removed.  The range is
097         * given in terms of what the view last saw (that is, before updating sticky
098         * positions).
099         *
100         * @param e the document event
101         */
102        public void removeUpdate (final DocumentEvent e)
103        {
104          handleValidate();
105        }
106      }
107    
108      private class FormActionListener implements ActionListener
109      {
110        private FormActionListener ()
111        {
112        }
113    
114        /**
115         * Invoked when an action occurs.
116         */
117        public void actionPerformed (final ActionEvent e)
118        {
119          handleValidate();
120        }
121      }
122    
123      private class FormItemListener implements ItemListener
124      {
125        private FormItemListener ()
126        {
127        }
128    
129        /**
130         * Invoked when an item has been selected or deselected by the user. The code written
131         * for this method performs the operations that need to occur when an item is selected
132         * (or deselected).
133         */
134        public void itemStateChanged (final ItemEvent e)
135        {
136          handleValidate();
137        }
138      }
139    
140      private FormTextfieldListener formTextfieldListener;
141      private FormActionListener actionListener;
142      private static final String DOCUMENT_PROPERTY_NAME = "document";
143      private FormItemListener itemListener;
144      private boolean enabled;
145    
146      protected FormValidator ()
147      {
148        this.formTextfieldListener = new FormTextfieldListener();
149        this.actionListener = new FormActionListener();
150        this.itemListener = new FormItemListener();
151      }
152    
153      public void registerTextField (final JTextComponent textField)
154      {
155        textField.getDocument().addDocumentListener(formTextfieldListener);
156        textField.addPropertyChangeListener(DOCUMENT_PROPERTY_NAME, formTextfieldListener);
157      }
158    
159      public void registerButton (final AbstractButton bt)
160      {
161        bt.addActionListener(actionListener);
162      }
163    
164      public void registerComboBox (final JComboBox bt)
165      {
166        bt.addItemListener(itemListener);
167      }
168    
169      public abstract Action getConfirmAction ();
170    
171      protected final void handleValidate ()
172      {
173        final Action confirmAction = getConfirmAction();
174        if (confirmAction == null || enabled == false)
175        {
176          return;
177        }
178    
179        if (performValidate() == false)
180        {
181          confirmAction.setEnabled(false);
182        }
183        else
184        {
185          confirmAction.setEnabled(true);
186        }
187      }
188    
189      public boolean isEnabled ()
190      {
191        return enabled;
192      }
193    
194      public void setEnabled (final boolean enabled)
195      {
196        this.enabled = enabled;
197      }
198    
199      public abstract boolean performValidate ();
200    }