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;
018
019import java.lang.ref.WeakReference;
020import java.util.Locale;
021
022import org.apache.wicket.MarkupContainer;
023
024/**
025 * Because Component has a reference to its parents, which eventually is the Page, keeping a "copy"
026 * of a component is very expensive. ContainerInfo shall be used instead of MarkupContainer whenever
027 * a small subset of the container's information is required.
028 * 
029 * @author Juergen Donnerstag
030 */
031public class ContainerInfo
032{
033        private final WeakReference<Class<?>> containerClassRef;
034        private final Locale locale;
035        private final String style;
036        private final String variation;
037        private final MarkupType markupType;
038
039        /**
040         * Construct.
041         * 
042         * @param container
043         *            The container to create the information from
044         */
045        public ContainerInfo(final MarkupContainer container)
046        {
047                this(container.getClass(), container.getLocale(), container.getStyle(),
048                        container.getVariation(), container.getMarkupType());
049        }
050
051        /**
052         * Construct.
053         * 
054         * @param containerClass
055         *            the real container class (could be a parent class)
056         * @param container
057         *            The container to create the information from
058         */
059        public ContainerInfo(final Class<?> containerClass, final MarkupContainer container)
060        {
061                this(containerClass != null ? containerClass : container.getClass(), container.getLocale(), container.getStyle(),
062                        container.getVariation(), container.getMarkupType());
063        }
064
065        /**
066         * Construct.
067         * 
068         * @param containerClass
069         * @param locale
070         * @param style
071         * @param variation
072         * @param markupType
073         */
074        public ContainerInfo(final Class<?> containerClass, final Locale locale, final String style,
075                final String variation, final MarkupType markupType)
076        {
077                super();
078                containerClassRef = new WeakReference<Class<?>>(containerClass);
079                this.locale = locale;
080                this.style = style;
081                this.variation = variation;
082                this.markupType = markupType;
083        }
084
085        /**
086         * 
087         * @return The container class
088         */
089        public Class<?> getContainerClass()
090        {
091                return containerClassRef.get();
092        }
093
094        /**
095         * 
096         * @return The container markup type (== file extension)
097         */
098        public String getFileExtension()
099        {
100                return markupType != null ? markupType.getExtension() : null;
101        }
102
103        /**
104         * 
105         * @return The container locale
106         */
107        public Locale getLocale()
108        {
109                return locale;
110        }
111
112        /**
113         * 
114         * @return The container style
115         */
116        public String getStyle()
117        {
118                return style;
119        }
120
121        /**
122         * 
123         * @return The containers variation
124         */
125        public String getVariation()
126        {
127                return variation;
128        }
129
130        /**
131         * 
132         * @see java.lang.Object#toString()
133         */
134        @Override
135        public String toString()
136        {
137                Class<?> classRef = containerClassRef.get();
138                return (classRef != null ? classRef.getName() : "null class") + ":" + locale + ":" + style +
139                        ":" + markupType;
140        }
141}