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            String id = getClientId();
069    
070            form.addHiddenValue(getName(), id, externalValue);
071        }
072    
073        /**
074         * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IRequestCycle)
075         */
076        protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
077        {
078            String parameter = cycle.getParameter(getName());
079    
080            Object value = parameter;
081    
082            if (getEncode())
083            {
084                try
085                {
086                    value = getDataSqueezer().unsqueeze(parameter);
087                }
088                catch (Exception ex)
089                {
090                    throw new ApplicationRuntimeException(ex.getMessage(), this, null, ex);
091                }
092            }
093    
094            // A listener is not always necessary ... it's easy to code
095            // the synchronization as a side-effect of the accessor method.
096    
097            setValue(value);
098    
099            getListenerInvoker().invokeListener(getListener(), this, cycle);
100        }
101    
102        /** @since 2.2 * */
103        public abstract DataSqueezer getDataSqueezer();
104    
105        public abstract Object getValue();
106    
107        public abstract void setValue(Object value);
108    
109        public abstract IActionListener getListener();
110    
111        /**
112         * Injected.
113         * 
114         * @since 4.0
115         */
116    
117        public abstract ListenerInvoker getListenerInvoker();
118    
119        /**
120         * Returns false. Hidden components are never disabled.
121         * 
122         * @since 2.2
123         */
124        public boolean isDisabled()
125        {
126            return false;
127        }
128    
129        /**
130         * Returns true if the compent encodes object values using a
131         * {@link org.apache.tapestry.util.io.DataSqueezerImpl}, false if values are always Strings.
132         * 
133         * @since 2.2
134         */
135        public abstract boolean getEncode();
136    }