    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * -----------------
    028     * DetailEditor.java
    029     * -----------------
    030     * (C) Copyright 2004, by Thomas Morgner and Contributors.
    031     *
    032     * Original Author:  Thomas Morgner;
    033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
    034     *
    035     * $Id: DetailEditor.java,v 1.3 2005/10/18 13:23:37 mungady Exp $
    036     *
    037     * Changes
    038     * -------
    039     * 07-Jun-2004 : Added JCommon header (DG);
    040     *
    041     */
    042
    043    package org.jfree.ui.tabbedui;
    044
    045    import javax.swing.JComponent;
    046
    047    /**
    048     * A detail editor.
    049     *
    050     * @author Thomas Morgner
    051     */
    052    public abstract class DetailEditor extends JComponent {
    053
    054        /** The object, that is edited. */
    055        private Object object;
    056        /** whether the edit process has been confirmed (user pressed OK). */
    057        private boolean confirmed;
    058
    059        /**
    060         * Creates a new editor.
    061         */
    062        public DetailEditor() {
    063            // nothing required
    064        }
    065
    066        /**
    067         * Updates the object.
    068         */
    069        public void update() {
    070            if (this.object == null) {
    071                throw new IllegalStateException();
    072            }
    073            else {
    074                updateObject(this.object);
    075            }
    076            setConfirmed(false);
    077        }
    078
    079        /**
    080         * Returns the object.
    081         *
    082         * @return The object.
    083         */
    084        public Object getObject() {
    085            return this.object;
    086        }
    087
    088        /**
    089         * Sets the object to be edited.
    090         *
    091         * @param object  the object.
    092         */
    093        public void setObject(final Object object) {
    094            if (object == null) {
    095                throw new NullPointerException();
    096            }
    097            this.object = object;
    098            setConfirmed(false);
    099            fillObject();
    100        }
    101
    102        /**
    103         * Parses an integer.
    104         *
    105         * @param text  the text.
    106         * @param def  the default value.
    107         *
    108         * @return The parsed integer, or the default value if the string didn't contain a
    109         *         value.
    110         */
    111        protected static int parseInt(final String text, final int def) {
    112            try {
    113                return Integer.parseInt(text);
    114            }
    115            catch (NumberFormatException fe) {
    116                return def;
    117            }
    118        }
    119
    120        /**
    121         * Clears the editor.
    122         */
    123        public abstract void clear();
    124
    125        /**
    126         * Edits the object. The object itself should not be modified, until
    127         * update or create was called.
    128         */
    129        protected abstract void fillObject();
    130
    131        /**
    132         * Updates the object.
    133         *
    134         * @param object  the object.
    135         */
    136        protected abstract void updateObject(Object object);
    137
    138        /**
    139         * Returns the confirmed flag.
    140         *
    141         * @return The confirmed flag.
    142         */
    143        public boolean isConfirmed() {
    144            return this.confirmed;
    145        }
    146
    147        /**
    148         * Sets the confirmed flag.
    149         *
    150         * @param confirmed  the confirmed flag.
    151         */
    152        protected void setConfirmed(final boolean confirmed) {
    153            final boolean oldConfirmed = this.confirmed;
    154            this.confirmed = confirmed;
    155            firePropertyChange("confirmed", oldConfirmed, confirmed);
    156        }
    157
    158
    159    }