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.extensions.markup.html.repeater.tree.content;
018
019import java.util.Optional;
020
021import org.apache.wicket.Component;
022import org.apache.wicket.MarkupContainer;
023import org.apache.wicket.ajax.AjaxRequestTarget;
024import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
025import org.apache.wicket.behavior.Behavior;
026import org.apache.wicket.markup.ComponentTag;
027import org.apache.wicket.markup.html.basic.Label;
028import org.apache.wicket.markup.html.panel.Panel;
029import org.apache.wicket.model.IModel;
030
031/**
032 * A styled link with a label.
033 * 
034 * @see #newLinkComponent(String, IModel)
035 * @see #getStyleClass()
036 * 
037 * @author svenmeier
038 */
039public abstract class StyledLinkLabel<T> extends Panel
040{
041        private static final StyleBehavior STYLE_CLASS = new StyleBehavior();
042
043        private static final long serialVersionUID = 1L;
044
045        public StyledLinkLabel(String id, IModel<T> model)
046        {
047                super(id, model);
048
049                MarkupContainer link = newLinkComponent("link", model);
050                link.add(STYLE_CLASS);
051                add(link);
052
053                link.add(newLabelComponent("label", model));
054        }
055
056        @SuppressWarnings("unchecked")
057        public IModel<T> getModel()
058        {
059                return (IModel<T>)getDefaultModel();
060        }
061
062        public T getModelObject()
063        {
064                return getModel().getObject();
065        }
066
067        /**
068         * Hook method to create a new link component.
069         * 
070         * This default implementation returns an {@link AjaxFallbackLink} which invokes
071         * {@link #onClick(Optional)} only if {@link #isClickable()} returns <code>true</code>.
072         * 
073         * @see #isClickable()
074         * @see #onClick(Optional)
075         */
076        protected MarkupContainer newLinkComponent(String id, IModel<T> model)
077        {
078                return new AjaxFallbackLink<Void>(id)
079                {
080                        private static final long serialVersionUID = 1L;
081
082                        @Override
083                        public boolean isEnabled()
084                        {
085                                return StyledLinkLabel.this.isClickable();
086                        }
087
088                        @Override
089                        public void onClick(Optional<AjaxRequestTarget> targetOptional)
090                        {
091                                StyledLinkLabel.this.onClick(targetOptional);
092                        }
093                };
094        }
095
096        /**
097         * Hook method to create a new label component.
098         * 
099         * @param id
100         * @param model
101         * @return created component
102         * 
103         * @see #newLabelModel(IModel)
104         */
105        protected Component newLabelComponent(String id, IModel<T> model)
106        {
107                return new Label(id, newLabelModel(model));
108        }
109
110        /**
111         * Create the model for the label, defaults to the model itself.
112         * 
113         * @param model
114         * @return wrapping model
115         */
116        protected IModel<?> newLabelModel(IModel<T> model)
117        {
118                return model;
119        }
120
121        /**
122         * Get a style class for the link.
123         */
124        protected abstract String getStyleClass();
125
126        /**
127         * Clicking is disabled by default, override this method if you want your link to be enabled.
128         * 
129         * @see #newLinkComponent(String, IModel)
130         * @see #isClickable()
131         */
132        protected boolean isClickable()
133        {
134                return false;
135        }
136
137        /**
138         * Hook method to be notified of a click on the link.
139         * 
140         * @param target
141         * 
142         * @see #newLinkComponent(String, IModel)
143         * @see #isClickable()
144         */
145        protected void onClick(Optional<AjaxRequestTarget> target)
146        {
147        }
148
149        /**
150         * Behavior to add a style class attribute to a contained link.
151         */
152        private static class StyleBehavior extends Behavior
153        {
154                private static final long serialVersionUID = 1L;
155
156                @Override
157                public void onComponentTag(Component component, ComponentTag tag)
158                {
159                        StyledLinkLabel<?> parent = (StyledLinkLabel<?>)component.getParent();
160
161                        String styleClass = parent.getStyleClass();
162                        if (styleClass != null)
163                        {
164                                tag.put("class", styleClass);
165                        }
166                }
167        }
168}