/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

package org.apache.isis.viewer.wicket.ui.app.registry;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

import org.apache.isis.viewer.wicket.ui.ComponentFactory;
import org.apache.isis.viewer.wicket.ui.ComponentType;

/**
 * Defines an API for registering {@link ComponentFactory}s.
 * 
 * <p>
 * As used by {@link ComponentFactoryListDefault}.
 */
public interface ComponentFactoryRegistrar {
    
    public static class ComponentFactoryList implements Iterable<ComponentFactory> {
        private final List<ComponentFactory> componentFactories = Lists.newArrayList();

        public void add(ComponentFactory componentFactory) {
            componentFactories.add(componentFactory);
        }

        public void replace(final ComponentFactory replacementComponentFactory) {
            removeExisting(matching(replacementComponentFactory.getComponentType()));
            add(replacementComponentFactory);
        }

        public void replace(final Class<? extends ComponentFactory> toReplace, final ComponentFactory replacementComponentFactory) {
            removeExisting(matching(toReplace));
            add(replacementComponentFactory);
        }

        private void removeExisting(final Predicate<ComponentFactory> predicate) {
            final Collection<ComponentFactory> matching = Collections2.filter(componentFactories, predicate);
            componentFactories.removeAll(matching);
        }

        private static Predicate<ComponentFactory> matching(final ComponentType componentType) {
            return new Predicate<ComponentFactory>() {
                @Override
                public boolean apply(ComponentFactory input) {
                    return input.getComponentType() == componentType;
                }
            };
        }

        private static Predicate<ComponentFactory> matching(final Class<? extends ComponentFactory> toReplace) {
            return new Predicate<ComponentFactory>() {
                @Override
                public boolean apply(ComponentFactory input) {
                    return toReplace.isAssignableFrom(input.getClass());
                }
            };
        }

        @Override
        public Iterator<ComponentFactory> iterator() {
            return componentFactories.iterator();
        }
    }

    void addComponentFactories(ComponentFactoryList componentFactoryList);
}

@@ -880,37 +880,8 @@
 y;%0A%0A
-import java.util.Collection;%0A
 impo
@@ -972,55 +972,8 @@
 te;%0A
-import com.google.common.collect.Collections2;%0A
 impo
@@ -1965,32 +1965,56 @@
 y) %7B%0A           
+ int indexOfOldFactory =
  removeExisting(
@@ -2039,36 +2039,58 @@
 ));%0A            
-add(
+insert(indexOfOldFactory, 
 replacementCompo
@@ -2135,16 +2135,420 @@
 te void 
+insert(final int indexToInsertInto, final ComponentFactory replacementComponentFactory) %7B%0A            if (indexToInsertInto %3E -1 && indexToInsertInto %3C componentFactories.size()) %7B%0A                componentFactories.add(indexToInsertInto, replacementComponentFactory);%0A            %7D else %7B%0A                componentFactories.add(replacementComponentFactory);%0A            %7D%0A        %7D%0A%0A        private int 
 removeEx
@@ -2617,106 +2617,221 @@
     
-f
 in
-al Collection%3CComponentFactory%3E matching = Collections2.filter(componentFactories, predicate);%0A
+t indexOfFirst = -1;%0A            for (int i = 0; i %3C componentFactories.size(); i++) %7B%0A                ComponentFactory factory = componentFactories.get(i);%0A                if (predicate.apply(factory)) %7B%0A      
     
@@ -2830,32 +2830,34 @@
                 
+  
 componentFactori
@@ -2869,21 +2869,212 @@
 move
-All(matching)
+(i);%0A                    if (indexOfFirst == -1) %7B%0A                        indexOfFirst = i;%0A                    %7D%0A                    i--;%0A                %7D%0A            %7D%0A%0A            return indexOfFirst
 ;%0A  

patch for Jeremy Gurr