All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.premiumminds.webapp.wicket.bootstrap.select.BootstrapHierarchizedDropDownChoice Maven / Gradle / Ivy

/**
 * Copyright (C) 2016 Premium Minds.
 *
 * This file is part of pm-wicket-utils.
 *
 * pm-wicket-utils is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * pm-wicket-utils is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with pm-wicket-utils. If not, see .
 */
package com.premiumminds.webapp.wicket.bootstrap.select;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.util.ListModel;
import org.apache.wicket.util.string.AppendingStringBuffer;

public class BootstrapHierarchizedDropDownChoice> extends
		BootstrapDropDownChoice {
	private static final long serialVersionUID = 1437604672358804420L;
	
	private boolean disableParents = false;

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 */
	public BootstrapHierarchizedDropDownChoice(final String id)
	{
		super(id);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param choices
	 *            The collection of choices in the dropdown
	 */
	public BootstrapHierarchizedDropDownChoice(final String id, final List choices)
	{
		super(id, choices);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param renderer
	 *            The rendering engine
	 * @param choices
	 *            The collection of choices in the dropdown
	 */
	public BootstrapHierarchizedDropDownChoice(final String id, final List choices,
		final IChoiceRenderer renderer)
	{
		super(id, choices, renderer);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param model
	 *            See Component
	 * @param choices
	 *            The collection of choices in the dropdown
	 */
	public BootstrapHierarchizedDropDownChoice(final String id, IModel model, final List choices)
	{
		super(id, model, choices);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param model
	 *            See Component
	 * @param choices
	 *            The drop down choices
	 * @param renderer
	 *            The rendering engine
	 */
	public BootstrapHierarchizedDropDownChoice(final String id, IModel model, final List choices,
		final IChoiceRenderer renderer)
	{
		super(id, model, choices, renderer);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param choices
	 *            The collection of choices in the dropdown
	 */
	public BootstrapHierarchizedDropDownChoice(String id, IModel> choices)
	{
		super(id, choices);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param model
	 *            See Component
	 * @param choices
	 *            The drop down choices
	 */
	public BootstrapHierarchizedDropDownChoice(String id, IModel model, IModel> choices)
	{
		super(id, model, choices);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param choices
	 *            The drop down choices
	 * @param renderer
	 *            The rendering engine
	 */
	public BootstrapHierarchizedDropDownChoice(String id, IModel> choices,
		IChoiceRenderer renderer)
	{
		super(id, choices, renderer);
	}

	/**
	 * Constructor.
	 * 
	 * @param id
	 *            See Component
	 * @param model
	 *            See Component
	 * @param choices
	 *            The drop down choices
	 * @param renderer
	 *            The rendering engine
	 */
	public BootstrapHierarchizedDropDownChoice(String id, IModel model, IModel> choices,
		IChoiceRenderer renderer)
	{
		super(id, model, choices, renderer);
	}

	@Override
	protected void setOptionAttributes(AppendingStringBuffer buffer, T choice,
			int index, String selected) {
		super.setOptionAttributes(buffer, choice, index, selected);
		int depth = calculateDepth(choice);
		buffer.append(" class=\"treedepth").append(depth).append("\"");
		if(disableParents && choice.getChildren()!=null && !choice.getChildren().isEmpty())
			buffer.append(" disabled=\"disabled\"");
	}
	
	@Override
	public IModel> getChoicesModel() {
		List choices = convertChoices(super.getChoices());
		return new ListModel<>(choices);
	}
	
	public void setDisableParents(boolean disable){
		this.disableParents = disable;
	}
	
	public boolean isDisableParents(){
		return this.disableParents;
	}


	@SuppressWarnings("unchecked")
	private static > List convertChoices(Collection choices){
		ArrayList newList = new ArrayList();
		for(T elem : choices){
			newList.add(elem);
			if(elem.getChildren()!=null && !elem.getChildren().isEmpty()){
				newList.addAll((Collection) convertChoices(elem.getChildren()));
			}
		}
		
		return newList;
	}
	
	private static > int calculateDepth(T option){
		if(option.getParent()==null) return 0;
		else return 1+calculateDepth(option.getParent());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy