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.breadcrumb;
018
019import org.apache.wicket.extensions.breadcrumb.panel.BreadCrumbPanel;
020import org.apache.wicket.markup.html.link.Link;
021
022
023/**
024 * A link that when clicked will set the the active {@link IBreadCrumbParticipant bread crumb
025 * participant} to the one that is returned by {@link #getParticipant(String)}. It is used
026 * internally by {@link BreadCrumbBar the the bread crumb bar component}, and you can use it for
027 * rendering links e.g. with {@link BreadCrumbPanel bread crumb panel components}.
028 * 
029 * <p>
030 * When clicked, it registers a change for backbutton support.
031 * </p>
032 * 
033 * @author Eelco Hillenius
034 */
035public abstract class BreadCrumbLink extends Link<Void>
036{
037        private static final long serialVersionUID = 1L;
038
039        /** The bread crumb model. */
040        private final IBreadCrumbModel breadCrumbModel;
041
042        /**
043         * Construct.
044         * 
045         * @param id
046         *            The link id
047         * @param breadCrumbModel
048         *            The bread crumb model
049         */
050        public BreadCrumbLink(final String id, final IBreadCrumbModel breadCrumbModel)
051        {
052                super(id);
053                this.breadCrumbModel = breadCrumbModel;
054        }
055
056        /**
057         * @see org.apache.wicket.markup.html.link.Link#onClick()
058         */
059        @Override
060        public void onClick()
061        {
062                // get the currently active particpant
063                final IBreadCrumbParticipant active = breadCrumbModel.getActive();
064                if (active == null)
065                {
066                        throw new IllegalStateException("The model has no active bread crumb. Before using " +
067                                this + ", you have to have at least one bread crumb in the model");
068                }
069
070                // get the participant to set as active
071                final IBreadCrumbParticipant participant = getParticipant(active.getComponent().getId());
072
073                // add back button support
074                addStateChange();
075
076                // set the next participant as the active one
077                breadCrumbModel.setActive(participant);
078        }
079
080        /**
081         * Gets the {@link IBreadCrumbParticipant bread crumb participant} to be set active when the
082         * link is clicked.
083         * 
084         * @param componentId
085         *            When the participant creates it's own view, it typically should use this component
086         *            id for the component that is returned by
087         *            {@link IBreadCrumbParticipant#getComponent()}.
088         * @return The bread crumb participant
089         */
090        protected abstract IBreadCrumbParticipant getParticipant(String componentId);
091}