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    package net.databinder.components.hib;
020    
021    import java.io.Serializable;
022    
023    import net.databinder.models.hib.HibernateObjectModel;
024    
025    import org.apache.wicket.markup.html.panel.Panel;
026    import org.apache.wicket.model.BoundCompoundPropertyModel;
027    
028    /**
029     * Panel subclass to be tied to a single persistent object. Similar to
030     * DataForm, this component wraps its model in a BoundCompoundProperty model
031     * and provides access methods to the nested model.
032     * @see DataForm
033     * @author Nathan Hamblen
034     */
035    public class DataPanel extends Panel {
036    
037            /**
038             * Create panel with an existing persistent object model.
039             * @param id Wicket id
040             * @param model to be wrapped in a BoundCompoundPropertyModel
041             */
042            public DataPanel(String id, HibernateObjectModel model) {
043                    super(id, new BoundCompoundPropertyModel(model));
044            }
045    
046            /**
047             * Instantiates this panel with a persistent object of the given class and id.
048             * @param id Wicket id
049             * @param modelClass for the persistent object
050             * @param persistentObjectId id of the persistent object
051             */
052            public DataPanel(String id, Class modelClass, Serializable persistentObjectId) {
053                    super(id, new BoundCompoundPropertyModel(new HibernateObjectModel(modelClass, persistentObjectId)));
054            }
055    
056            /**
057             * Create panel without a model. Use this constructor only if the intended model
058             * is not available at construction time of DataPanel subclass.
059             * @param id Wicket id
060             * @see DataPanel#setPersistentObject(Object object)
061             */
062            protected DataPanel(String id) {
063                    super(id);
064            }
065    
066            /**
067             * @return the nested model, casted to the expected IModel subclass.
068             */
069            protected HibernateObjectModel getPersistentObjectModel() {
070                    try {
071                            return (HibernateObjectModel) getBindingModel().getChainedModel();
072                    } catch (ClassCastException c) {
073                            throw new RuntimeException("DataPanel's nested model was not a HibernateObjectModel", c);
074                    }
075            }
076    
077            /**
078             * Change the persistent model object of this panel.
079             * @param object  to attach to this panel
080             * @return this panel, for chaining
081             */
082            public DataPanel setPersistentObject(Object object) {
083                    getPersistentObjectModel().setObject(object);
084                    return this;
085            }
086    
087            /**
088             * Set the persistant object model. Use this when a DataPanel subclass has
089             * been constructed without a model.
090             * @param model to back this panel
091             * @return this panel, for chaining
092             */
093            protected DataPanel setPersistentObjectModel(HibernateObjectModel model) {
094                    setModel(new BoundCompoundPropertyModel(model));
095                    return this;
096            }
097    
098            /**
099             * @return this panel's model for binding components to expressions.
100             */
101            protected BoundCompoundPropertyModel getBindingModel() {
102                    return (BoundCompoundPropertyModel) getModel();
103            }
104    }