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.form.validation;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.core.util.string.CssUtils;
021import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
022import org.apache.wicket.feedback.FeedbackCollector;
023import org.apache.wicket.feedback.IFeedback;
024import org.apache.wicket.feedback.IFeedbackMessageFilter;
025import org.apache.wicket.markup.ComponentTag;
026import org.apache.wicket.markup.html.WebMarkupContainer;
027import org.apache.wicket.markup.html.panel.Panel;
028
029/**
030 * A panel that hides or shows itself depending on whether there are feedback messages for a given
031 * message filter. If a component is set using setIndicatorFor(Component), then the indicator is
032 * visible when the given component has an error. The default content for this indicator is a red
033 * star, but you can subclass this panel and provide your own markup to give any custom look you
034 * desire.
035 * 
036 * @author Jonathan Locke
037 */
038public class FormComponentFeedbackIndicator extends Panel implements IFeedback
039{
040        public static final String ERROR_CSS_CLASS_KEY = CssUtils
041                .key(FormComponentFeedbackIndicator.class, "error");
042
043        private static final long serialVersionUID = 1L;
044
045        /** The message filter for this indicator component */
046        private IFeedbackMessageFilter filter;
047
048        /**
049         * Constructor
050         * 
051         * @param id
052         *            See Component
053         */
054        public FormComponentFeedbackIndicator(final String id)
055        {
056                super(id);
057        }
058
059        /**
060         * @param component
061         *            The component to watch for messages
062         */
063        public void setIndicatorFor(final Component component)
064        {
065                filter = new ComponentFeedbackMessageFilter(component);
066        }
067
068        @Override
069        protected void onInitialize()
070        {
071                super.onInitialize();
072                add(new WebMarkupContainer("indicator")
073                {
074                        private static final long serialVersionUID = 1L;
075
076                        @Override
077                        protected void onComponentTag(ComponentTag tag)
078                        {
079                                super.onComponentTag(tag);
080                                tag.put("class", getString(ERROR_CSS_CLASS_KEY));
081                        }
082                });
083        }
084
085        /**
086         * Set the component's visibility according to the existence (or not) of feedback messages
087         */
088        @Override
089        public void onConfigure()
090        {
091                super.onConfigure();
092                // Get the messages for the current page
093                setVisible(new FeedbackCollector(getPage()).collect(getFeedbackMessageFilter()).size() > 0);
094        }
095
096        /**
097         * @return Let subclass specify some other filter
098         */
099        protected IFeedbackMessageFilter getFeedbackMessageFilter()
100        {
101                return filter;
102        }
103}