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.protocol.http;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.wicket.Component;
025import org.apache.wicket.ajax.AjaxRequestTarget;
026import org.apache.wicket.markup.html.internal.Enclosure;
027import org.apache.wicket.markup.html.internal.InlineEnclosure;
028import org.apache.wicket.markup.parser.filter.InlineEnclosureHandler;
029import org.apache.wicket.util.visit.IVisit;
030import org.apache.wicket.util.visit.IVisitor;
031
032/**
033 * This listener adds Enclosures to AjaxTarget, where the child controller of the said Enclosure is
034 * already added. This is a part of the fix to the problem:
035 * 
036 * "Changing the visibility of a child component in Ajax callback method will not affect the entire
037 * enclosure but just the child component itself. This is because only the child component is added
038 * to the AjaxRequestTarget"
039 * 
040 * When used with an "Inline" Enclosure, this problem is fixed.
041 * 
042 * Syntax for inline enclosure:
043 * 
044 * <tr wicket:enclosure="controllingChildId">
045 * 
046 * In this example the tag used is tr, but any other tag could be used as well. The attribute
047 * "wicket:enclosure" is mandatory, and is used to recognise an inline enclosure. The value of the
048 * attribute, here "controllingChildId" can contain an id for the child (controller) element. If the
049 * said value is not given, the first element inside the enclosure will be used as a controller
050 * child. If there are no elements inside the enclosure, the parsing will fail.
051 * 
052 * 
053 * @see WebApplication
054 * @see InlineEnclosure
055 * @see InlineEnclosureHandler
056 *
057 * @author Joonas Hamalainen
058 */
059public class AjaxEnclosureListener implements AjaxRequestTarget.IListener
060{
061        /**
062         * Construct.
063         */
064        public AjaxEnclosureListener()
065        {
066        }
067
068        /**
069         * Try to find Enclosures that have their controllers added already, and add them to the target.
070         */
071        @Override
072        public void onBeforeRespond(final Map<String, Component> map, final AjaxRequestTarget target)
073        {
074                final List<String> keysToRemove = new ArrayList<>();
075
076                target.getPage().visitChildren(InlineEnclosure.class, new IVisitor<InlineEnclosure, Void>()
077                {
078                        @Override
079                        public void component(final InlineEnclosure enclosure, final IVisit<Void> visit)
080                        {
081                                Iterator<Map.Entry<String, Component>> entriesItor = map.entrySet().iterator();
082                                while (entriesItor.hasNext())
083                                {
084                                        Map.Entry<String, Component> entry = entriesItor.next();
085                                        Component component = entry.getValue();
086                                        if (isControllerOfEnclosure(component, enclosure))
087                                        {
088                                                final Component controller = component;
089                                                target.add(enclosure);
090                                                visit.dontGoDeeper();
091                                                enclosure.visitChildren(new IVisitor<Component, Void>() {
092                                                        @Override
093                                                        public void component(Component descendant, IVisit<Void> visit) {
094                                                                if (descendant == controller) {
095                                                                        // if the controlling component is in the enclosure we do not need to repaint it
096                                                                        // individually, it will be repainted as part of the enclosure repaint
097                                                                        keysToRemove.add(controller.getId());
098                                                                }
099                                                        }
100                                                });
101                                                break;
102                                        }
103                                }
104                        }
105                });
106
107                for (String key : keysToRemove)
108                {
109                        map.remove(key);
110                }
111        }
112
113        /**
114         * Check if a given component is the controlling child of a given enclosure
115         * 
116         * @param component
117         * @param enclosure
118         * @return true if the given component is the controlling child of the given InlineEnclosure
119         */
120        public static boolean isControllerOfEnclosure(final Component component,
121                final Enclosure enclosure)
122        {
123                return (enclosure.get(enclosure.getChildId()) == component || // #queue()
124                                enclosure.getParent().get(enclosure.getChildId()) == component); // #add()
125        }
126}