package votorola.a.web.wic; // Copyright 2008-2009, 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.util.*; import java.util.concurrent.*; import org.apache.wicket.Page; import votorola.g.lang.*; import votorola.g.web.wic.*; /** A navigation bar containing a series of tabs. */ public abstract @ThreadSafe class NavBar { // - N a v - B a r -------------------------------------------------------------------- /** The index of the default tab. The implementation of this method in class NavBar * returns zero, which corresponds to the left-hand tab. */ public int defaultTabIndex() { return 0; } /** The hash key for this bar's session scope. */ HashKey hashKey() { return hashKey; } private final HashKey hashKey = new HashKey(); // /** The runner for the last page viewed beneath this bar; or, if none was viewed, the // * runner of the default tab. // * // * @see #defaultTabIndex() // */ // public final RequestCycleRunner lastRunner( VRequestCycle cycle ) // { // final SessionScope sessionScope = VSession.get().scopeNavBar(); // final int lastIndexOnPath; // { // Integer i = sessionScope.getLastIndexOnPath( hashKey ); // lastIndexOnPath = i == null? defaultTabIndex(): i; // } // final NavTab lastTabOnPath = tabList().get( lastIndexOnPath ); // if( lastTabOnPath instanceof SuperTab ) // { // return ((SuperTab)lastTabOnPath).subBar().lastRunner( cycle ); // } // else return lastTabOnPath.runner( cycle ); // } ///// runners removed on migration to stateless pages /** The tab that controls the visibility of this bar. * * @return the super-tab; or null, if this bar is always visible */ public abstract SuperTab superTab(); /** A list of the navigation tabs contained within this bar. */ public abstract List tabList(); // /** Returns the first tab for a particular page type, within this bar. // * // * @throws IllegalStateException if this bar contains no tab for the specified page type // */ // public final NavTab tabForPageType( Class pageClass, VRequestCycle cycle ) // { // for( NavTab tab: tabList() ) // { // if( !tab.bookmark(cycle).pageClass().equals( pageClass )) continue; // // return tab; // } // throw new IllegalStateException(); // } /** Returns the first tab of a particular tab type, within this bar. * * @throws IllegalStateException if this bar contains no tab of the specified type */ public final NavTab tabOfType( Class pageClass, VRequestCycle cycle ) { for( NavTab tab: tabList() ) { if( !pageClass.isInstance( tab )) continue; return tab; } throw new IllegalStateException(); } // ==================================================================================== /** Session scope for instances of NavBar. * * @see VSession#scopeNavBar() */ static @ThreadSafe class SessionScope implements java.io.Serializable { private static final long serialVersionUID = 0L; /** Constructs a SessionScope. */ SessionScope( VSession session ) { this.session = session; } private final VSession session; // -------------------------------------------------------------------------------- /** @see #putLastIndexOnPath(NavBar.HashKey,Integer) */ Integer getLastIndexOnPath( HashKey key ) { return lastIndexOnPathMap.get( key ); } private final ConcurrentHashMap lastIndexOnPathMap = new ConcurrentHashMap( /*initial capacity*/32, /*load factor*/0.75f, /*concurrency*/1 ); // only one writer (or reader) typically /** @see #getLastIndexOnPath(NavBar.HashKey) * @see #removeLastIndexOnPath(NavBar.HashKey) */ Integer putLastIndexOnPath( HashKey key, Integer index ) { try{ return lastIndexOnPathMap.put( key, index ); } finally{ session.dirty(); } // per Session API } /** @see #putLastIndexOnPath(NavBar.HashKey,Integer) */ Integer removeLastIndexOnPath( HashKey key ) { try{ return lastIndexOnPathMap.remove( key ); } finally{ session.dirty(); } // per Session API } } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** A serializeable hash key for instances of NavBar. */ private static @ThreadSafe final class HashKey implements java.io.Serializable {} }