001    /*
002     * Databinder: a simple bridge from Wicket to Hibernate
003     * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
004    
005     * This library is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU Lesser General Public
007     * License as published by the Free Software Foundation; either
008     * version 2.1 of the License, or (at your option) any later version.
009     *
010     * This library is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * Lesser General Public License for more details.
014     *
015     * You should have received a copy of the GNU Lesser General Public
016     * License along with this library; if not, write to the Free Software
017     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
018     */
019    
020    package net.databinder.models;
021    
022    import org.apache.wicket.Component;
023    import org.apache.wicket.model.IModel;
024    
025    /**
026     * Adaptation of Wicket's LoadableDetachableModel that does not extend a
027     * read-only model and permits changing the underlying object.
028     */
029    public abstract class LoadableWritableModel  implements IModel {
030    
031            private transient boolean attached = false;
032            private transient Object tempModelObject;
033    
034            public LoadableWritableModel() {
035            }
036    
037            public final void detach() {
038                    if (attached) {
039                            attached = false;
040                            tempModelObject = null;
041                            onDetach();
042                    }
043            }
044    
045            public Object getObject() {
046                    if (!attached) {
047                            attached = true;
048                            tempModelObject = load();
049    
050                            onAttach();
051                    }
052                    return tempModelObject;
053            }
054    
055            public final boolean isAttached() {
056                    return attached;
057            }
058    
059            /**
060             * Called by subclass when the model object is readily available. Saves a later
061             * (possibly expensive) call to load().
062             * @param object
063             */
064            protected void setTempModelObject(Object object) {
065                    attached = true;
066                    tempModelObject = object;
067            }
068    
069            public String toString() {
070                    StringBuffer sb = new StringBuffer(super.toString());
071                    sb.append(":attached=").append(attached).append(":tempModelObject=[")
072                                    .append(this.tempModelObject).append("]");
073                    return sb.toString();
074            }
075    
076            protected abstract Object load();
077    
078            /**
079             * Called when attaching, after load().
080             */
081            protected void onAttach() {
082            }
083    
084            /**
085             * Called when detaching.
086             */
087            protected void onDetach() {
088            }
089    }