Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2014 the original author or authors
*
* Licensed 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 com.foreach.across.modules.web.menu;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/**
* A PathBasedMenuBuilder can be used to define menu items in a non-hierarchical way.
* When building the actual Menu, the path will be used to determine parent and sub-menus.
* Any new prefix will be used as root of a sub menu.
*/
public class PathBasedMenuBuilder
{
private final PathBasedMenuBuilder parent;
private final PathBasedMenuItemBuilder rootBuilder;
private final Map itemBuilders;
private final Map moves;
private final MenuItemBuilderProcessor itemProcessor;
public PathBasedMenuBuilder() {
this( null, MenuItemBuilderProcessor.NoOpProcessor );
}
public PathBasedMenuBuilder( MenuItemBuilderProcessor itemProcessor ) {
this( null, itemProcessor );
}
public PathBasedMenuBuilder( PathBasedMenuBuilder parent,
MenuItemBuilderProcessor itemProcessor ) {
this.parent = parent;
this.itemProcessor = itemProcessor;
if ( parent != null ) {
rootBuilder = parent.rootBuilder;
itemBuilders = parent.itemBuilders;
moves = parent.moves;
}
else {
rootBuilder = new PathBasedMenuItemBuilder( null, this );
itemBuilders = new TreeMap<>();
moves = new TreeMap<>();
}
}
/**
* @return A new builder with the specified processor.
*/
public PathBasedMenuBuilder builder( MenuItemBuilderProcessor processor ) {
Assert.notNull( "A processor must be specified - try using NoOpProcessor if you want to clear it." );
return new PathBasedMenuBuilder( this, processor );
}
/**
* @return The parent PathBasedMenuBuilder or the current one if there is no parent.
*/
public PathBasedMenuBuilder and() {
return parent != null ? parent : this;
}
public PathBasedMenuItemBuilder root( String rootPath ) {
Assert.notNull( "Root path must not be null." );
rootBuilder.path = rootPath;
return rootBuilder;
}
public PathBasedMenuItemBuilder item( String path ) {
Assert.notNull( path, "A Menu item must have a valid path." );
PathBasedMenuItemBuilder itemBuilder = itemBuilders.get( path );
if ( itemBuilder == null ) {
itemBuilder = new PathBasedMenuItemBuilder( path, this );
itemBuilders.put( itemBuilder.getPath(), itemBuilder );
}
return itemBuilder;
}
public PathBasedMenuItemBuilder group( String path ) {
return item( path ).group( true );
}
public PathBasedMenuItemBuilder group( String path, String title ) {
return item( path, title ).group( true );
}
public PathBasedMenuItemBuilder item( String path, String title ) {
return item( path ).title( title );
}
public PathBasedMenuItemBuilder item( String path, String title, String url ) {
return item( path ).title( title ).url( url );
}
/**
* @return A newly constructed Menu instance.
*/
public Menu build() {
Menu root = rootBuilder.build();
Menu current = root;
Map builderMap = itemBuilders;
if ( !moves.isEmpty() ) {
builderMap = new TreeMap<>();
for ( PathBasedMenuItemBuilder itemBuilder : itemBuilders.values() ) {
String newPath = determineActualPath( itemBuilder.getPath() );
builderMap.put( newPath, itemBuilder );
}
}
Map