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.panel;
018
019import java.lang.reflect.Constructor;
020
021import org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel;
022import org.apache.wicket.util.lang.Args;
023
024
025/**
026 * Simple factory that creates instances of a {@link BreadCrumbPanel bread crumb panel} based on the
027 * class it is constructed with.
028 * 
029 * @author Eelco Hillenius
030 */
031public final class BreadCrumbPanelFactory implements IBreadCrumbPanelFactory
032{
033        private static final long serialVersionUID = 1L;
034
035        /** Class to construct. */
036        private final Class<? extends BreadCrumbPanel> panelClass;
037
038        /**
039         * Construct.
040         * 
041         * @param panelClass
042         *            The class to use for creating instances. Must be of type {@link BreadCrumbPanel},
043         *            and must have constructor
044         *            {@link BreadCrumbPanel#BreadCrumbPanel(String, IBreadCrumbModel)}
045         */
046        public BreadCrumbPanelFactory(final Class<? extends BreadCrumbPanel> panelClass)
047        {
048                Args.notNull(panelClass, "panelClass");
049
050                if (!BreadCrumbPanel.class.isAssignableFrom(panelClass))
051                {
052                        throw new IllegalArgumentException("argument panelClass (" + panelClass +
053                                ") must extend class " + BreadCrumbPanel.class.getName());
054                }
055
056
057                this.panelClass = panelClass;
058
059                // check whether it has the proper constructor
060                getConstructor();
061        }
062
063        /**
064         * @see org.apache.wicket.extensions.breadcrumb.panel.IBreadCrumbPanelFactory#create(java.lang.String,
065         *      org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel)
066         */
067        @Override
068        public final BreadCrumbPanel create(final String componentId,
069                final IBreadCrumbModel breadCrumbModel)
070        {
071                Constructor<? extends BreadCrumbPanel> ctor = getConstructor();
072                try
073                {
074                        return ctor.newInstance(componentId, breadCrumbModel);
075                }
076                catch (Exception e)
077                {
078                        throw new RuntimeException(e);
079                }
080        }
081
082        /**
083         * Gets the proper constructor of the panel class.
084         * 
085         * @return The constructor.
086         */
087        private Constructor<? extends BreadCrumbPanel> getConstructor()
088        {
089                try
090                {
091                        return panelClass.getConstructor(String.class, IBreadCrumbModel.class);
092                }
093                catch (SecurityException e)
094                {
095                        throw new RuntimeException(e);
096                }
097                catch (NoSuchMethodException e)
098                {
099                        throw new RuntimeException(e);
100                }
101        }
102}