package de.memtext.rights; import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import javax.swing.tree.DefaultMutableTreeNode; import de.memtext.tree.TreeEntryI; public class AllowableHierarchyUtil { /** * Entfernt unerlaubte Blätter und Äste, in denen es gar keine erlaubten * Knoten gibt * * @param root * must also extend DefaultMutableTreeNode */ public static void removeUnallowedNodes(AllowableHierarchy root) { if (!(root instanceof DefaultMutableTreeNode)) throw new IllegalArgumentException(" must be a node!"); DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) root; boolean needAnotherRound = true, killedOne = false; while (needAnotherRound) { killedOne = false; for (Enumeration en = rootNode.breadthFirstEnumeration(); en.hasMoreElements();) { Object o = en.nextElement(); if (o == root) continue; AllowableHierarchy ah = (AllowableHierarchy) o; if (!ah.isAnyDescendantAllowed()) { // System.out.println("removing: "+ah); DefaultMutableTreeNode node = ((DefaultMutableTreeNode) ah); markParentsAsNotAllowed(node); node.removeFromParent(); killedOne = true; break; } } if (killedOne == false) needAnotherRound = false; } } private static void markParentsAsNotAllowed(DefaultMutableTreeNode node) { if (!node.isRoot() && node instanceof Allowable) { Allowable al = null; if (node.getParent() instanceof Allowable) al = (Allowable) node.getParent(); al.setAllowed(false); markParentsAsNotAllowed((DefaultMutableTreeNode) node.getParent()); } } /** * Restrukturiert einen Baum, so dass nicht erlaubte "Zwischenäste" entfernt * werden. root - noterl - erl3 * * wird root - erl3 * * @param root */ public static void reorg(AllowableHierarchy root) { if (!(root instanceof DefaultMutableTreeNode)) throw new IllegalArgumentException(" must be a node!"); DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) root; boolean needAnotherRound = true; List nodeList = new LinkedList(); while (needAnotherRound) { nodeList.clear(); needAnotherRound = false; for (int i = 0; i < rootNode.getChildCount(); i++) { AllowableHierarchy ah = (AllowableHierarchy) rootNode.getChildAt(i); if (!ah.isAllowed()) { needAnotherRound = true; ((DefaultMutableTreeNode) ah).removeFromParent(); for (int i2 = 0; i2 < ((DefaultMutableTreeNode) ah).getChildCount(); i2++) nodeList.add(((DefaultMutableTreeNode) ah).getChildAt(i2)); } } for (Iterator it = nodeList.iterator(); it.hasNext();) { rootNode.add((DefaultMutableTreeNode) it.next()); } } } public static void markNotAllowedNodes(AllowableHierarchy root, String mark) { if (!(root instanceof DefaultMutableTreeNode)) throw new IllegalArgumentException(" must be a node!"); DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) root; int i = 0; for (Enumeration en = rootNode.breadthFirstEnumeration(); en.hasMoreElements();) { AllowableHierarchy ah = (AllowableHierarchy) en.nextElement(); if (!ah.isAllowed()) { TreeEntryI te = (TreeEntryI) ah; if (!te.getName().endsWith(mark)) te.setName(te.getName() + mark); } } } } // Created on 24.04.2005 at 08:05:45