TreeControl.java

00001 /*
00002  * $Header: /cvsroot/jonas/jonas/src/org/objectweb/jonas/webapp/taglib/TreeControl.java,v 1.1 2003/10/16 12:17:28 antonma Exp $
00003  * $Revision: 1.1 $
00004  * $Date: 2003/10/16 12:17:28 $
00005  *
00006  * ====================================================================
00007  *
00008  * The Apache Software License, Version 1.1
00009  *
00010  * Copyright (c) 2001 The Apache Software Foundation.  All rights
00011  * reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions
00015  * are met:
00016  *
00017  * 1. Redistributions of source code must retain the above copyright
00018  *    notice, this list of conditions and the following disclaimer.
00019  *
00020  * 2. Redistributions in binary form must reproduce the above copyright
00021  *    notice, this list of conditions and the following disclaimer in
00022  *    the documentation and/or other materials provided with the
00023  *    distribution.
00024  *
00025  * 3. The end-user documentation included with the redistribution, if
00026  *    any, must include the following acknowlegement:
00027  *       "This product includes software developed by the
00028  *        Apache Software Foundation (http://www.apache.org/)."
00029  *    Alternately, this acknowlegement may appear in the software itself,
00030  *    if and wherever such third-party acknowlegements normally appear.
00031  *
00032  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
00033  *    Foundation" must not be used to endorse or promote products derived
00034  *    from this software without prior written permission. For written
00035  *    permission, please contact apache@apache.org.
00036  *
00037  * 5. Products derived from this software may not be called "Apache"
00038  *    nor may "Apache" appear in their names without prior written
00039  *    permission of the Apache Group.
00040  *
00041  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
00042  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00043  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00044  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
00045  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00046  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00047  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
00048  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00049  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00050  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00051  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00052  * SUCH DAMAGE.
00053  * ====================================================================
00054  *
00055  * This software consists of voluntary contributions made by many
00056  * individuals on behalf of the Apache Software Foundation.  For more
00057  * information on the Apache Software Foundation, please see
00058  * <http://www.apache.org/>.
00059  *
00060  */
00061 
00062 package org.objectweb.jonas.webapp.taglib;
00063 
00064 import java.io.Serializable;
00065 import java.util.HashMap;
00066 
00078 public class TreeControl implements Serializable {
00079     public static final String ID_PREFIX = "treenode";
00080 
00081 // ----------------------------------------------------------- Constructors
00082 
00086     public TreeControl() {
00087         super();
00088         setRoot(null);
00089     }
00090 
00096     public TreeControl(TreeControlNode root) {
00097         super();
00098         setRoot(root);
00099     }
00100 
00101 // ----------------------------------------------------- Instance Variables
00102 
00106     protected HashMap registry = new HashMap();
00107 
00111     protected TreeControlNode selected = null;
00112 
00113     // Id
00114     protected int mi_Id = 0;
00115 
00116 // ------------------------------------------------------------- Properties
00117 
00121     protected TreeControlNode root = null;
00122 
00123     public TreeControlNode getRoot() {
00124         return (this.root);
00125     }
00126 
00127     protected void setRoot(TreeControlNode root) {
00128         if (this.root != null) {
00129             removeNode(this.root);
00130         }
00131         if (root != null) {
00132             addNode(root);
00133         }
00134         root.setLast(true);
00135         this.root = root;
00136     }
00137 
00142     public int getWidth() {
00143         if (root == null) {
00144             return (0);
00145         }
00146         else {
00147             return (getWidth(root));
00148         }
00149     }
00150 
00151 // --------------------------------------------------------- Public Methods
00152 
00159     public TreeControlNode findNode(String name) {
00160         synchronized (registry) {
00161             return ((TreeControlNode) registry.get(name));
00162         }
00163     }
00164 
00172     public void selectNode(String name) {
00173         if (selected != null) {
00174             selected.setSelected(false);
00175             selected = null;
00176         }
00177         selected = findNode(name);
00178         if (selected != null) {
00179             selected.setSelected(true);
00180         }
00181     }
00182 
00188     public TreeControlNode getSelected() {
00189         return selected;
00190     }
00191 
00195     public void expandSelectedParents() {
00196         TreeControlNode oCur = getSelected();
00197         while (oCur != null){
00198             oCur = oCur.getParent();
00199             if (oCur != null) {
00200                 oCur.setExpanded(true);
00201             }
00202         }
00203     }
00204 
00205     public String newId() {
00206         StringBuffer sbRet = new StringBuffer(ID_PREFIX);
00207         sbRet.append(mi_Id);
00208         mi_Id++;
00209         return sbRet.toString();
00210     }
00211 
00212 // -------------------------------------------------------- Package Methods
00213 
00222     void addNode(TreeControlNode node)
00223         throws IllegalArgumentException {
00224         synchronized (registry) {
00225             String name = node.getName();
00226             if (registry.containsKey(name)) {
00227                 throw new IllegalArgumentException("Name '" + name + "' is not unique");
00228             }
00229             node.setTree(this);
00230             registry.put(name, node);
00231             // Refresh expand info
00232             autoRefresh(node);
00233         }
00234     }
00235 
00241     int getWidth(TreeControlNode node) {
00242         int width = node.getWidth();
00243         if (!node.isExpanded()) {
00244             return (width);
00245         }
00246         TreeControlNode children[] = node.findChildren();
00247         for (int i = 0; i < children.length; i++) {
00248             int current = getWidth(children[i]);
00249             if (current > width) {
00250                 width = current;
00251             }
00252         }
00253         return (width);
00254     }
00255 
00263     void removeNode(TreeControlNode node) {
00264         synchronized (registry) {
00265             TreeControlNode children[] = node.findChildren();
00266             for (int i = 0; i < children.length; i++) {
00267                 removeNode(children[i]);
00268             }
00269             TreeControlNode parent = node.getParent();
00270             if (parent != null) {
00271                 parent.removeChild(node);
00272             }
00273             node.setParent(null);
00274             node.setTree(null);
00275             if (node == this.root) {
00276                 this.root = null;
00277             }
00278             registry.remove(node.getName());
00279             // Save removed node in list
00280             addRemovedList(node);
00281         }
00282     }
00283 
00288     private HashMap m_RemovedList = null;
00289 
00293     public void disableAutoRefresh() {
00294         if (m_RemovedList != null) {
00295             m_RemovedList.clear();
00296         }
00297         m_RemovedList = null;
00298     }
00299 
00309     public void enableAutoRefresh() {
00310         m_RemovedList = new HashMap();
00311     }
00312 
00318     void addRemovedList(TreeControlNode p_RemovedNode) {
00319         if (m_RemovedList != null) {
00320             m_RemovedList.put(p_RemovedNode.getName(), p_RemovedNode);
00321         }
00322     }
00323 
00329     protected void autoRefresh(TreeControlNode p_AddedNode) {
00330         if (m_RemovedList != null) {
00331             TreeControlNode oRemove = (TreeControlNode) m_RemovedList.get(p_AddedNode.getName());
00332             if (oRemove != null) {
00333                 p_AddedNode.setExpanded(oRemove.isExpanded());
00334             }
00335         }
00336     }
00337 }

Generated on Tue Feb 15 15:05:34 2005 for JOnAS by  doxygen 1.3.9.1