Author: ehillenius Date: Fri Sep 21 18:20:49 2007 New Revision: 578344 URL: http://svn.apache.org/viewvc?rev=578344&view=rev Log: added a little bit more indirection to make default implementations more accesible.
Added: wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/DefaultBreadCrumbsModel.java wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbParticipantDelegate.java Modified: wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/BreadCrumbBar.java wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbPanel.java Modified: wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/BreadCrumbBar.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/BreadCrumbBar.java?rev=578344&r1=578343&r2=578344&view=diff ============================================================================== --- wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/BreadCrumbBar.java (original) +++ wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/BreadCrumbBar.java Fri Sep 21 18:20:49 2007 @@ -38,10 +38,13 @@ * first / second / third * </pre> * - * This component also functions as a implementation of - * [EMAIL PROTECTED] IBreadCrumbModel bread crumb model}. This component holds the state - * as well as doing the rendering. Override and provide your own markup file if - * you want to work with other elements, e.g. uls instead of spans. + * <p> + * Delegates how the bread crumb model works to [EMAIL PROTECTED] DefaultBreadCrumbsModel}. + * </p> + * <p> + * Override and provide your own markup file if you want to work with other + * elements, e.g. uls instead of spans. + * </p> * * @author Eelco Hillenius */ @@ -184,14 +187,7 @@ private static final long serialVersionUID = 1L; - /** The currently active participant, if any (possibly null). */ - private IBreadCrumbParticipant activeParticipant = null; - - /** Holds the current list of crumbs. */ - private List crumbs = new ArrayList(); - - /** listeners utility. */ - private final BreadCrumbModelListenerSupport listenerSupport = new BreadCrumbModelListenerSupport(); + private final IBreadCrumbModel decorated; /** * Construct. @@ -202,25 +198,27 @@ public BreadCrumbBar(String id) { super(id); + this.decorated = new DefaultBreadCrumbsModel(); BreadCrumbsListView breadCrumbsListView = new BreadCrumbsListView("crumbs"); addListener(breadCrumbsListView); add(breadCrumbsListView); } + /** * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#addListener(org.apache.wicket.extensions.breadcrumb.IBreadCrumbModelListener) */ - public final void addListener(IBreadCrumbModelListener listener) + public void addListener(IBreadCrumbModelListener listener) { - this.listenerSupport.addListener(listener); + decorated.addListener(listener); } /** * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#allBreadCrumbParticipants() */ - public final List allBreadCrumbParticipants() + public List allBreadCrumbParticipants() { - return crumbs; + return decorated.allBreadCrumbParticipants(); } /** @@ -228,85 +226,23 @@ */ public IBreadCrumbParticipant getActive() { - return activeParticipant; + return decorated.getActive(); } /** * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#removeListener(org.apache.wicket.extensions.breadcrumb.IBreadCrumbModelListener) */ - public final void removeListener(IBreadCrumbModelListener listener) + public void removeListener(IBreadCrumbModelListener listener) { - this.listenerSupport.removeListener(listener); + decorated.removeListener(listener); } /** * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#setActive(org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant) */ - public final void setActive(final IBreadCrumbParticipant breadCrumbParticipant) - { - // see if the bread crumb was already added, and if so, - // clean up the stack after (on top of) this bred crumb - // and notify listeners of the removal - int len = crumbs.size() - 1; - int i = len; - while (i > -1) - { - IBreadCrumbParticipant temp = (IBreadCrumbParticipant)crumbs.get(i); - - // if we found the bread crumb - if (breadCrumbParticipant.equals(temp)) - { - // remove the bread crumbs after this one - int j = len; - while (j > i) - { - // remove and fire event - IBreadCrumbParticipant removed = (IBreadCrumbParticipant)crumbs.remove(j--); - listenerSupport.fireBreadCrumbRemoved(removed); - } - - // activate the bread crumb participant - activate(breadCrumbParticipant); - - // we're done; the provided bread crumb is on top - // and the content is replaced, so just return this function - return; - } - - i--; - } - - // arriving here means we weren't able to find the bread crumb - // add the new crumb - crumbs.add(breadCrumbParticipant); - - // and notify listeners - listenerSupport.fireBreadCrumbAdded(breadCrumbParticipant); - - // activate the bread crumb participant - activate(breadCrumbParticipant); - } - - /** - * Activates the bread crumb participant. - * - * @param breadCrumbParticipant - * The participant to activate - */ - protected final void activate(final IBreadCrumbParticipant breadCrumbParticipant) + public void setActive(IBreadCrumbParticipant breadCrumbParticipant) { - // get old value - IBreadCrumbParticipant previousParticipant = this.activeParticipant; - - // and set the provided participant as the active one - this.activeParticipant = breadCrumbParticipant; - - // fire bread crumb activated event - listenerSupport.fireBreadCrumbActivated(previousParticipant, breadCrumbParticipant); - - // signal the bread crumb participant that it is selected as the - // currently active one - breadCrumbParticipant.onActivate(previousParticipant); + decorated.setActive(breadCrumbParticipant); } /** @@ -350,7 +286,7 @@ protected void onDetach() { super.onDetach(); - for (Iterator i = crumbs.iterator(); i.hasNext();) + for (Iterator i = decorated.allBreadCrumbParticipants().iterator(); i.hasNext();) { IBreadCrumbParticipant crumb = (IBreadCrumbParticipant)i.next(); if (crumb instanceof Component) Added: wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/DefaultBreadCrumbsModel.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/DefaultBreadCrumbsModel.java?rev=578344&view=auto ============================================================================== --- wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/DefaultBreadCrumbsModel.java (added) +++ wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/DefaultBreadCrumbsModel.java Fri Sep 21 18:20:49 2007 @@ -0,0 +1,149 @@ +/* + * 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.wicket.extensions.breadcrumb; + +import java.util.ArrayList; +import java.util.List; + +/** + * Default breadcrumb model implementation that should be good for 99% of the + * use cases out there. + * + * @author eelcohillenius + */ +public class DefaultBreadCrumbsModel implements IBreadCrumbModel +{ + private static final long serialVersionUID = 1L; + + /** The currently active participant, if any (possibly null). */ + private IBreadCrumbParticipant activeParticipant = null; + + /** Holds the current list of crumbs. */ + private List crumbs = new ArrayList(); + + /** listeners utility. */ + private final BreadCrumbModelListenerSupport listenerSupport = new BreadCrumbModelListenerSupport(); + + /** + * Construct. + */ + public DefaultBreadCrumbsModel() + { + } + + /** + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#addListener(org.apache.wicket.extensions.breadcrumb.IBreadCrumbModelListener) + */ + public final void addListener(IBreadCrumbModelListener listener) + { + this.listenerSupport.addListener(listener); + } + + /** + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#allBreadCrumbParticipants() + */ + public final List allBreadCrumbParticipants() + { + return crumbs; + } + + /** + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#getActive() + */ + public IBreadCrumbParticipant getActive() + { + return activeParticipant; + } + + /** + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#removeListener(org.apache.wicket.extensions.breadcrumb.IBreadCrumbModelListener) + */ + public final void removeListener(IBreadCrumbModelListener listener) + { + this.listenerSupport.removeListener(listener); + } + + /** + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel#setActive(org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant) + */ + public final void setActive(final IBreadCrumbParticipant breadCrumbParticipant) + { + // see if the bread crumb was already added, and if so, + // clean up the stack after (on top of) this bred crumb + // and notify listeners of the removal + int len = crumbs.size() - 1; + int i = len; + while (i > -1) + { + IBreadCrumbParticipant temp = (IBreadCrumbParticipant)crumbs.get(i); + + // if we found the bread crumb + if (breadCrumbParticipant.equals(temp)) + { + // remove the bread crumbs after this one + int j = len; + while (j > i) + { + // remove and fire event + IBreadCrumbParticipant removed = (IBreadCrumbParticipant)crumbs.remove(j--); + listenerSupport.fireBreadCrumbRemoved(removed); + } + + // activate the bread crumb participant + activate(breadCrumbParticipant); + + // we're done; the provided bread crumb is on top + // and the content is replaced, so just return this function + return; + } + + i--; + } + + // arriving here means we weren't able to find the bread crumb + // add the new crumb + crumbs.add(breadCrumbParticipant); + + // and notify listeners + listenerSupport.fireBreadCrumbAdded(breadCrumbParticipant); + + // activate the bread crumb participant + activate(breadCrumbParticipant); + } + + /** + * Activates the bread crumb participant. + * + * @param breadCrumbParticipant + * The participant to activate + */ + protected final void activate(final IBreadCrumbParticipant breadCrumbParticipant) + { + // get old value + IBreadCrumbParticipant previousParticipant = this.activeParticipant; + + // and set the provided participant as the active one + this.activeParticipant = breadCrumbParticipant; + + // fire bread crumb activated event + listenerSupport.fireBreadCrumbActivated(previousParticipant, breadCrumbParticipant); + + // signal the bread crumb participant that it is selected as the + // currently active one + breadCrumbParticipant.onActivate(previousParticipant); + } +} Modified: wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbPanel.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbPanel.java?rev=578344&r1=578343&r2=578344&view=diff ============================================================================== --- wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbPanel.java (original) +++ wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbPanel.java Fri Sep 21 18:20:49 2007 @@ -17,7 +17,6 @@ package org.apache.wicket.extensions.breadcrumb.panel; import org.apache.wicket.Component; -import org.apache.wicket.MarkupContainer; import org.apache.wicket.extensions.breadcrumb.BreadCrumbLink; import org.apache.wicket.extensions.breadcrumb.IBreadCrumbModel; import org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant; @@ -64,6 +63,19 @@ private IBreadCrumbModel breadCrumbModel; /** + * Implementation of the participant. + */ + private final IBreadCrumbParticipant decorated = new BreadCrumbParticipantDelegate(this) + { + private static final long serialVersionUID = 1L; + + public String getTitle() + { + return BreadCrumbPanel.this.getTitle(); + } + }; + + /** * Construct. * * @param id @@ -158,70 +170,15 @@ */ public Component getComponent() { - return this; + return decorated.getComponent(); } /** - * If the previous participant is not null (and a component, which it should - * be), replace that component on it's parent with this one. - * * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant#onActivate(org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant) */ public void onActivate(IBreadCrumbParticipant previous) { - if (previous != null) - { - MarkupContainer parent = previous.getComponent().getParent(); - if (parent != null) - { - final String thisId = getId(); - if (parent.get(thisId) != null) - { - parent.replace(this); - } - else - { - // try to search downwards to match the id - // NOTE unfortunately, we can't rely on the path pre 2.0 - Component c = (Component)parent.visitChildren(new IVisitor() - { - public Object component(Component component) - { - if (component.getId().equals(thisId)) - { - return component; - } - return IVisitor.CONTINUE_TRAVERSAL; - } - }); - if (c == null) - { - // not found... do a reverse search (upwards) - c = (Component)parent.visitParents(Component.class, new IVisitor() - { - public Object component(Component component) - { - if (component.getId().equals(thisId)) - { - return component; - } - return IVisitor.CONTINUE_TRAVERSAL; - } - }); - } - - // replace if found - if (c != null) - { - c.replaceWith(this); - } - } - } - } - else if (getParent() != null) - { - getParent().replace(this); - } + decorated.onActivate(previous); } /** Added: wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbParticipantDelegate.java URL: http://svn.apache.org/viewvc/wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbParticipantDelegate.java?rev=578344&view=auto ============================================================================== --- wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbParticipantDelegate.java (added) +++ wicket/trunk/jdk-1.4/wicket-extensions/src/main/java/org/apache/wicket/extensions/breadcrumb/panel/BreadCrumbParticipantDelegate.java Fri Sep 21 18:20:49 2007 @@ -0,0 +1,125 @@ +/* + * 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.wicket.extensions.breadcrumb.panel; + +import org.apache.wicket.Component; +import org.apache.wicket.MarkupContainer; +import org.apache.wicket.Component.IVisitor; +import org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant; +import org.apache.wicket.markup.html.panel.Panel; + +/** + * Base implementation for [EMAIL PROTECTED] Panel}/ [EMAIL PROTECTED] Component} based + * [EMAIL PROTECTED] IBreadCrumbParticipant} that decouples the implementation from the + * actual panel class. + * + * @author eelcohillenius + */ +public abstract class BreadCrumbParticipantDelegate implements IBreadCrumbParticipant +{ + private static final long serialVersionUID = 1L; + + private final Component component; + + /** + * Construct. + * + * @param component + */ + public BreadCrumbParticipantDelegate(Component component) + { + if (component == null) + { + throw new IllegalArgumentException("component must be not null"); + } + this.component = component; + } + + /** + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant#getComponent() + */ + public Component getComponent() + { + return component; + } + + /** + * If the previous participant is not null (and a component, which it should + * be), replace that component on it's parent with this one. + * + * @see org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant#onActivate(org.apache.wicket.extensions.breadcrumb.IBreadCrumbParticipant) + */ + public void onActivate(IBreadCrumbParticipant previous) + { + if (previous != null) + { + MarkupContainer parent = previous.getComponent().getParent(); + if (parent != null) + { + final String thisId = component.getId(); + if (parent.get(thisId) != null) + { + parent.replace(component); + } + else + { + // try to search downwards to match the id + // NOTE unfortunately, we can't rely on the path pre 2.0 + Component c = (Component)parent.visitChildren(new IVisitor() + { + public Object component(Component component) + { + if (component.getId().equals(thisId)) + { + return component; + } + return IVisitor.CONTINUE_TRAVERSAL; + } + }); + if (c == null) + { + // not found... do a reverse search (upwards) + c = (Component)parent.visitParents(Component.class, new IVisitor() + { + public Object component(Component component) + { + if (component.getId().equals(thisId)) + { + return component; + } + return IVisitor.CONTINUE_TRAVERSAL; + } + }); + } + + // replace if found + if (c != null) + { + c.replaceWith(component); + } + } + } + } + else if (component.getParent() != null) + { + component.getParent().replace(component); + } + } +}