001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.html.link;
018
019import org.apache.wicket.markup.ComponentTag;
020import org.apache.wicket.markup.MarkupStream;
021import org.apache.wicket.markup.html.WebMarkupContainer;
022import org.apache.wicket.model.IModel;
023
024/**
025 * Base class that that contains functionality for rendering disabled links.
026 * 
027 * @author Matej Knopp
028 */
029public abstract class AbstractLink extends WebMarkupContainer
030{
031        private static final long serialVersionUID = 1L;
032
033        /** this link's label/body. */
034        private IModel<?> bodyModel;
035
036        /**
037         * Construct.
038         * 
039         * @param id
040         */
041        public AbstractLink(String id)
042        {
043                this(id, null);
044        }
045
046        /**
047         * Construct.
048         * 
049         * @param id
050         *            the component id
051         * @param model
052         *            the link's model
053         */
054        public AbstractLink(String id, IModel<?> model)
055        {
056                super(id, model);
057        }
058
059        /**
060         * Renders this link's body.
061         * 
062         * @param markupStream
063         *            the markup stream
064         * @param openTag
065         *            the open part of this tag
066         * @see org.apache.wicket.Component#onComponentTagBody(MarkupStream, ComponentTag)
067         */
068        @Override
069        public void onComponentTagBody(final MarkupStream markupStream, final ComponentTag openTag)
070        {
071                // Get a copy of the body model from the getBody() method. This method could be overridden.
072                IModel<?> tmpBodyModel = getBody();
073
074                if ((tmpBodyModel != null) && (tmpBodyModel.getObject() != null))
075                {
076                        replaceComponentTagBody(markupStream, openTag,
077                                getDefaultModelObjectAsString(tmpBodyModel.getObject()));
078                }
079                else
080                {
081                        // Render the body of the link
082                        super.onComponentTagBody(markupStream, openTag);
083                }
084        }
085
086        /**
087         * Alters the tag so that the link renders as disabled.
088         * 
089         * This method is meant to be called from {@link #onComponentTag(ComponentTag)} method of the
090         * derived class.
091         * 
092         * @param tag
093         */
094        protected void disableLink(final ComponentTag tag)
095        {
096                // if the tag is a button or input
097                if ("button".equalsIgnoreCase(tag.getName()) || "input".equalsIgnoreCase(tag.getName()))
098                {
099                        tag.put("disabled", "disabled");
100                }
101                else
102                {
103                        // Remove any href from the old link
104                        tag.remove("href");
105
106                        tag.remove("onclick");
107                }
108        }
109
110        /**
111         * @return the link's body model
112         */
113        public IModel<?> getBody()
114        {
115                return bodyModel;
116        }
117
118        /**
119         * Sets the link's body model
120         * 
121         * @param bodyModel
122         * @return <code>this</code> for method chaining
123         */
124        public AbstractLink setBody(final IModel<?> bodyModel)
125        {
126                this.bodyModel = wrap(bodyModel);
127                return this;
128        }
129
130        @Override
131        protected void onDetach()
132        {
133                super.onDetach();
134
135                if (bodyModel != null)
136                {
137                        bodyModel.detach();
138                }
139        }
140}