001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.form;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.tapestry.IActionListener;
019    import org.apache.tapestry.IForm;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.listener.ListenerInvoker;
023    import org.apache.tapestry.services.DataSqueezer;
024    
025    /**
026     * Implements a hidden field within a {@link Form}. [ <a
027     * href="../../../../../ComponentReference/Hidden.html">Component Reference </a>]
028     * 
029     * @author Howard Lewis Ship
030     * @author Paul Ferraro
031     */
032    public abstract class Hidden extends AbstractFormComponent
033    {
034        /**
035         * Returns false.
036         */
037    
038        protected boolean getCanTakeFocus()
039        {
040            return false;
041        }
042    
043        /**
044         * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
045         *      org.apache.tapestry.IRequestCycle)
046         */
047        protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
048        {
049            IForm form = getForm();
050            String externalValue = null;
051    
052            if (getEncode())
053            {
054                Object value = getValue();
055    
056                try
057                {
058                    externalValue = getDataSqueezer().squeeze(value);
059                }
060                catch (Exception e)
061                {
062                    throw new ApplicationRuntimeException(e.getMessage(), this, null, e);
063                }
064            }
065            else
066                externalValue = (String) getBinding("value").getObject(String.class);
067            
068            form.addHiddenValue(getName(), getClientId(), externalValue);
069        }
070    
071        /**
072         * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IRequestCycle)
073         */
074        protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
075        {
076            String parameter = cycle.getParameter(getName());
077    
078            Object value = parameter;
079    
080            if (getEncode())
081            {
082                try
083                {
084                    value = getDataSqueezer().unsqueeze(parameter);
085                }
086                catch (Exception ex)
087                {
088                    throw new ApplicationRuntimeException(ex.getMessage(), this, null, ex);
089                }
090            }
091    
092            // A listener is not always necessary ... it's easy to code
093            // the synchronization as a side-effect of the accessor method.
094    
095            setValue(value);
096    
097            getListenerInvoker().invokeListener(getListener(), this, cycle);
098        }
099    
100        /** @since 2.2 * */
101        public abstract DataSqueezer getDataSqueezer();
102    
103        public abstract Object getValue();
104    
105        public abstract void setValue(Object value);
106    
107        public abstract IActionListener getListener();
108    
109        /**
110         * Injected.
111         * 
112         * @since 4.0
113         */
114    
115        public abstract ListenerInvoker getListenerInvoker();
116    
117        /**
118         * Returns false. Hidden components are never disabled.
119         * 
120         * @since 2.2
121         */
122        public boolean isDisabled()
123        {
124            return false;
125        }
126    
127        /**
128         * Returns true if the compent encodes object values using a
129         * {@link org.apache.tapestry.util.io.DataSqueezerImpl}, false if values are always Strings.
130         * 
131         * @since 2.2
132         */
133        public abstract boolean getEncode();
134    }