Source for org.jfree.report.modules.gui.swing.common.FormValidator

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: FormValidator.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.gui.swing.common;
  32: 
  33: import java.awt.event.ActionEvent;
  34: import java.awt.event.ActionListener;
  35: import java.awt.event.ItemEvent;
  36: import java.awt.event.ItemListener;
  37: import java.beans.PropertyChangeEvent;
  38: import java.beans.PropertyChangeListener;
  39: import javax.swing.AbstractButton;
  40: import javax.swing.Action;
  41: import javax.swing.JComboBox;
  42: import javax.swing.event.DocumentEvent;
  43: import javax.swing.event.DocumentListener;
  44: import javax.swing.text.Document;
  45: import javax.swing.text.JTextComponent;
  46: 
  47: public abstract class FormValidator
  48: {
  49: 
  50:   private class FormTextfieldListener
  51:           implements DocumentListener, PropertyChangeListener
  52:   {
  53:     private FormTextfieldListener ()
  54:     {
  55:     }
  56: 
  57:     /**
  58:      * This method gets called when a bound property is changed.
  59:      *
  60:      * @param evt A PropertyChangeEvent object describing the event source and the
  61:      *            property that has changed.
  62:      */
  63:     public void propertyChange (final PropertyChangeEvent evt)
  64:     {
  65:       if (DOCUMENT_PROPERTY_NAME.equals(evt.getPropertyName()))
  66:       {
  67:         final Document olddoc = (Document) evt.getOldValue();
  68:         olddoc.removeDocumentListener(this);
  69:         final Document newdoc = (Document) evt.getOldValue();
  70:         newdoc.addDocumentListener(this);
  71:       }
  72:     }
  73: 
  74:     /**
  75:      * Gives notification that an attribute or set of attributes changed.
  76:      *
  77:      * @param e the document event
  78:      */
  79:     public void changedUpdate (final DocumentEvent e)
  80:     {
  81:       handleValidate();
  82:     }
  83: 
  84:     /**
  85:      * Gives notification that there was an insert into the document.  The range given by
  86:      * the DocumentEvent bounds the freshly inserted region.
  87:      *
  88:      * @param e the document event
  89:      */
  90:     public void insertUpdate (final DocumentEvent e)
  91:     {
  92:       handleValidate();
  93:     }
  94: 
  95:     /**
  96:      * Gives notification that a portion of the document has been removed.  The range is
  97:      * given in terms of what the view last saw (that is, before updating sticky
  98:      * positions).
  99:      *
 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: }