Java tutorial
/* * Copyright 2012 AMG.lab, a Bull Group Company * * 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 org.xlcloud.console.entitlements.nodes; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; import org.primefaces.model.DefaultTreeNode; import org.primefaces.model.TreeNode; import org.xlcloud.console.entitlements.CompositeEntitlementDictionary; import org.xlcloud.console.entitlements.EntitlementDictionary; import org.xlcloud.console.entitlements.nodes.predicates.EntitlementNodePredicate; /** * Represents the single entitlement tree node. * Entitlement dictionaries are wrapped by this object. * @author "Konrad Krl", AMG.net */ public class EntitlementNode extends DefaultTreeNode { private static final long serialVersionUID = -2172444051807728183L; /** * Instantiates new entitlement tree node * and adds a node to the parent node. * @param entitlementDictionary entitlement dictionary which should be wrapped * @param visibilityPredicate indicates whether the entitlement node should be visible * @param selectionPredicate indicates whether the entitlement node should be selected by default * @param parent parent tree node */ public EntitlementNode(EntitlementDictionary entitlementDictionary, EntitlementNodePredicate visibilityPredicate, EntitlementNodePredicate selectionPredicate, TreeNode parent) { super(entitlementDictionary, parent); if (entitlementDictionary instanceof CompositeEntitlementDictionary) { instantiateChildNodes((CompositeEntitlementDictionary) entitlementDictionary, visibilityPredicate, selectionPredicate); } if (this.isSelectable()) { this.initializeNodeSelection(entitlementDictionary, selectionPredicate); } } /** * Instantiates new entitlement tree node * and adds a node to the parent node. * The tree node is unselected by default. * @param entitlementDictionary entitlement dicionary which should be wrapped * @param visibilityPredicate indicates whether the entitlement node should be visible * @param parent parent tree node */ public EntitlementNode(EntitlementDictionary entitlementDictionary, EntitlementNodePredicate visibilityPredicate, TreeNode parent) { this(entitlementDictionary, visibilityPredicate, null, parent); } private void initializeNodeSelection(EntitlementDictionary entitlementDictionary, EntitlementNodePredicate selectionPredicate) { if (this.isLeaf()) { if (selectionPredicate != null) { this.setSelected(selectionPredicate.evaluate(entitlementDictionary)); } } else { this.setSelected(hasAllChildrenSelected()); } } private void instantiateChildNodes(CompositeEntitlementDictionary compositeDictionary, EntitlementNodePredicate visibilityPredicate, EntitlementNodePredicate selectionPredicate) { for (EntitlementDictionary child : compositeDictionary.getIncludedEntitlementDictionaries()) { if (visibilityPredicate.evaluate(child)) { new EntitlementNode(child, visibilityPredicate, selectionPredicate, this); } } } private boolean hasAllChildrenSelected() { if (this.isLeaf()) { return this.isSelected(); } else { return forAllChildren(new Predicate() { @Override public boolean evaluate(Object object) { return ((EntitlementNode) object).isSelected(); } }); } } private boolean forAllChildren(Predicate predicate) { return (CollectionUtils.countMatches(getChildren(), predicate) == getChildCount()); } }