001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * FormValidator.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.commonswing;
030:
031: import java.awt.event.ActionEvent;
032: import java.awt.event.ActionListener;
033: import java.awt.event.ItemEvent;
034: import java.awt.event.ItemListener;
035: import java.beans.PropertyChangeEvent;
036: import java.beans.PropertyChangeListener;
037: import javax.swing.AbstractButton;
038: import javax.swing.Action;
039: import javax.swing.JComboBox;
040: import javax.swing.event.DocumentEvent;
041: import javax.swing.event.DocumentListener;
042: import javax.swing.text.Document;
043: import javax.swing.text.JTextComponent;
044:
045: public abstract class FormValidator {
046:
047: private class FormTextfieldListener implements DocumentListener,
048: PropertyChangeListener {
049: public FormTextfieldListener() {
050: }
051:
052: /**
053: * This method gets called when a bound property is changed.
054: *
055: * @param evt A PropertyChangeEvent object describing the event source and the
056: * property that has changed.
057: */
058: public void propertyChange(final PropertyChangeEvent evt) {
059: if (DOCUMENT_PROPERTY_NAME.equals(evt.getPropertyName())) {
060: final Document olddoc = (Document) evt.getOldValue();
061: olddoc.removeDocumentListener(this );
062: final Document newdoc = (Document) evt.getOldValue();
063: newdoc.addDocumentListener(this );
064: }
065: }
066:
067: /**
068: * Gives notification that an attribute or set of attributes changed.
069: *
070: * @param e the document event
071: */
072: public void changedUpdate(final DocumentEvent e) {
073: handleValidate();
074: }
075:
076: /**
077: * Gives notification that there was an insert into the document. The range given by
078: * the DocumentEvent bounds the freshly inserted region.
079: *
080: * @param e the document event
081: */
082: public void insertUpdate(final DocumentEvent e) {
083: handleValidate();
084: }
085:
086: /**
087: * Gives notification that a portion of the document has been removed. The range is
088: * given in terms of what the view last saw (that is, before updating sticky
089: * positions).
090: *
091: * @param e the document event
092: */
093: public void removeUpdate(final DocumentEvent e) {
094: handleValidate();
095: }
096: }
097:
098: private class FormActionListener implements ActionListener {
099: public FormActionListener() {
100: }
101:
102: /**
103: * Invoked when an action occurs.
104: */
105: public void actionPerformed(final ActionEvent e) {
106: handleValidate();
107: }
108: }
109:
110: private class FormItemListener implements ItemListener {
111: public FormItemListener() {
112: }
113:
114: /**
115: * Invoked when an item has been selected or deselected by the user. The code written
116: * for this method performs the operations that need to occur when an item is selected
117: * (or deselected).
118: */
119: public void itemStateChanged(final ItemEvent e) {
120: handleValidate();
121: }
122: }
123:
124: private FormTextfieldListener formTextfieldListener;
125: private FormActionListener actionListener;
126: private static final String DOCUMENT_PROPERTY_NAME = "document"; //$NON-NLS-1$
127: private FormItemListener itemListener;
128: private boolean enabled;
129:
130: protected FormValidator() {
131: this .formTextfieldListener = new FormTextfieldListener();
132: this .actionListener = new FormActionListener();
133: this .itemListener = new FormItemListener();
134: }
135:
136: public void registerTextField(final JTextComponent textField) {
137: textField.getDocument().addDocumentListener(
138: formTextfieldListener);
139: textField.addPropertyChangeListener(DOCUMENT_PROPERTY_NAME,
140: formTextfieldListener);
141: }
142:
143: public void registerButton(final AbstractButton bt) {
144: bt.addActionListener(actionListener);
145: }
146:
147: public void registerComboBox(final JComboBox bt) {
148: bt.addItemListener(itemListener);
149: }
150:
151: public abstract Action getConfirmAction();
152:
153: protected final void handleValidate() {
154: final Action confirmAction = getConfirmAction();
155: if (confirmAction == null || enabled == false) {
156: return;
157: }
158:
159: if (performValidate() == false) {
160: confirmAction.setEnabled(false);
161: } else {
162: confirmAction.setEnabled(true);
163: }
164: }
165:
166: public boolean isEnabled() {
167: return enabled;
168: }
169:
170: public void setEnabled(final boolean enabled) {
171: this .enabled = enabled;
172: }
173:
174: public abstract boolean performValidate();
175: }
|