001    /* sillyview : a free model-view-controller system for Java
002     * Copyright (C) 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/sillyview/sillyview/src/net/sourceforge/sillyview/HTMLPaneView.java,v 1.2 2004/05/15 01:58:51 tj_willis Exp $
019     */
020    package net.sourceforge.sillyview;
021    
022    import javax.swing.*;
023    import javax.swing.event.HyperlinkListener;
024    import javax.swing.border.*;
025    import java.util.Map;
026    import java.util.Set;
027    
028    /**
029     * This class connects a HTMLPane as a view to a WidgetModel.  The text of 
030     * the HTMLPane will be the WidgetModel.toString().  If you want to handle 
031     * HyperlinkEvents such as when a user clicks a link or sublits a form, use 
032     * <code>setToken(HYPERLINK_LISTENER,yourclass)</code>.
033     *
034     * The text will be updated whenever setToken() or setTokens() is called.
035     *
036     *
037     * @author <a href="mailto:tj_willis@users.sourceforge.net">T.J. Willis</a>
038     * @version 1.0
039     */
040    public class HTMLPaneView 
041        extends HTMLPane 
042        implements WidgetView 
043    {
044    
045        /**
046         * The model this view gets its data from.
047         *
048         */
049        protected WidgetModel mod;
050    
051        /**
052         * A token for setting the view's hyperlink listener.
053         *
054         */
055        public static final String HYPERLINK_LISTENER = "<@_HYPERLINK_LISTENER>";
056    
057        /**
058         * Creates a new <code>HTMLPaneView</code> instance backed by the given
059         * model.
060         *
061         * @param model a <code>WidgetModel</code> value
062         */
063        public HTMLPaneView (WidgetModel model) {
064            super ((String) (model.getCurrentModel ()));
065            mod = model;
066        }
067    
068        /**
069         * Adds all the name/value pairs and then updates the model.  If
070         * the named token already exists, replace the value.  This is more
071         * efficient than many calls to setToken(), because model.getCurrentModel
072         * is only called once.
073         *
074         */
075        public final void addTokens (final Map < Object, Object > props) {
076            if (props == null) {
077                return;             // FIXME: clear all tokens in model
078            }
079            Set keys = props.keySet ();
080            for (Object k : keys) {
081                String key = k.toString ();
082                mod.setToken (key, props.get (key));
083            }
084            setText (mod.getCurrentModel ().toString ());
085    
086        }
087    
088        /**
089         * Sets an individual token to the given value.  Most will be
090         * sent to the backing model.
091         *
092         * @param key an <code>Object</code> value
093         * @param value an <code>Object</code> value
094         */
095        public final void setToken (final Object key, Object value) {
096            if (key == null)
097                return;
098    
099            String ti = "";
100            if (value != null)
101                ti = value.toString ();
102    
103            String la = key.toString ();
104    
105            if (HYPERLINK_LISTENER.equals (la)) {
106                addHyperlinkListener ((HyperlinkListener) value);
107                return;
108            }
109            mod.setToken (key, value);
110            setText (mod.getCurrentModel ().toString ());
111        }
112    
113        /**
114         * Gets the named value.  Most values will be retrieved by the
115         * backing model.
116         *
117         * @param key an <code>Object</code> value
118         * @return an <code>Object</code> value
119         */
120        public final Object getValue (final Object key) {
121            if (key == null)
122                return null;
123            String la = key.toString ();
124            //  if(TITLE.equals(la))
125            //      return title;
126    
127            return mod.getValue (key);
128        }
129    
130        /**
131         * Gets the backing model.
132         *
133         * @return a <code>WidgetModel</code> value
134         */
135        public final WidgetModel getModel () {
136            return mod;
137        }
138    
139        /**
140         * Sets the backing model.
141         *
142         * @param newModel a <code>WidgetModel</code> value
143         */
144        public final void setModel (WidgetModel newModel) {
145            mod = newModel;
146        }
147    
148    
149    
150    }