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

com.viaoa.html.OATreeNode Maven / Gradle / Ivy

/* 
This software and documentation is the confidential and proprietary 
information of ViaOA, Inc. ("Confidential Information").  
You shall not disclose such Confidential Information and shall use 
it only in accordance with the terms of the license agreement you 
entered into with ViaOA, Inc..

ViaOA, Inc. MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, OR NON-INFRINGEMENT. ViaOA, Inc. SHALL NOT BE LIABLE FOR ANY DAMAGES
SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
THIS SOFTWARE OR ITS DERIVATIVES.
 
Copyright (c) 2001 ViaOA, Inc.
All rights reserved.
*/ 
package com.viaoa.html;

import java.util.Vector;
import java.lang.reflect.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

import com.viaoa.annotation.OAProperty;
import com.viaoa.hub.*;
import com.viaoa.object.OAObject;
import com.viaoa.object.OAObjectInfoDelegate;
import com.viaoa.object.OAPropertyInfo;
import com.viaoa.util.*;
import java.util.*;

/** TreeNode for OATree.  */
public class OATreeNode extends OAHtmlComponent implements Cloneable {
    private static final long serialVersionUID = 1L;
    String propertyPath;  // property path after the path to a hub   ex: emps.dept.name => dept.name
    boolean titleFlag;
    Method[] methodsToHub; // methods to find Hub
    Method[] methodsToObject; // methods to find Object (when no hubs are in path)
    Method[] methodsToProperty;  // methods to get property value from object => name
    boolean showAll = true;  // if false then only the activeObject is shown, if true then all objects in hub are used
    OATreeNode[] treeNodeChildren = new OATreeNode[0];
    Hub hub;
    Hub updateHub;
    String font;
    String colorBackground;
    OATree tree; // set when added to tree or node
    Hub hubFilter;
    protected Vector vecColumns;

    public OATreeNode(String path) {
        this.propertyPath = path;
    }
    /** @param hub is hub to use for data and/or hub to update. */
    public OATreeNode(String path, Hub hub) {
        this(path, hub, hub);
    }

    /** @param property from hub */
    public void addColumn(String property, String heading, int columns) {
        OATableColumn tc = new OATableColumn(property, heading, columns);
        if (vecColumns == null) vecColumns = new Vector(5,3);
        vecColumns.add(tc);
    }

    protected void setTree(OATree tree) {
        this.tree = tree;
        for (int i=0; i < treeNodeChildren.length; i++) {
            OATreeNode node = treeNodeChildren[i];
            node.setTree(tree);
        }
    }
    
    /** @param hub is the list of objects that are valid for this tree node.  
        If an object is not found in this Hub, then it will not be displayed in the tree.
    */
    public void setFilter(Hub hub) {
        hubFilter = hub;
    }
    public Hub getFilter() {
        return hubFilter;
    }

    /** constructor for creating a root node.
        @param hub is hub to use for data.
        @param hubUpdate the hub to keep in sync (the shared hub)
    */
    public OATreeNode(String path, Hub hub, Hub hubUpdate) {
        this.hub = hub;
        this.updateHub = hubUpdate;
        this.propertyPath = path;
    }


    boolean bExpandingAll,bCollaspingAll;  // used by OATree
    public void expandAll() {
        if (tree != null) {
            bExpandingAll = true;
            tree.getHtml();
            bExpandingAll = false;
        }
    }
    public void collapseAll() {
        if (tree != null) {
            bCollaspingAll = true;
            tree.getHtml();
            bCollaspingAll = false;
        }
    }


    /** @param font String within the <font> tag.  Ex: size="1"  */
    public void setFont(String font) {
        this.font = font;
    }
    public String getFont() {
        return this.font;
    }
    public void setBackground(String background) {
        this.colorBackground = background;
    }
    public String getBackground() {
        return this.colorBackground;
    }

    protected void findMethods(Class clazz) {
        if (titleFlag) return;
        if (methodsToHub != null || methodsToProperty != null) return;
        
        int pos,prev;
        String path = propertyPath;
        if (path == null) path = "";
        methodsToHub = null;
        Vector vec = new Vector();
        for (pos=prev=0; pos >= 0; prev=pos+1) {
            pos = path.indexOf('.',prev);
            String name;
            if (pos >= 0) name = "get"+path.substring(prev,pos);
            else {
                name = path.substring(prev);
                if (name.length() == 0) name = "toString";
                else name = "get" + name;
            }
            
            // find method
            Method method = ClassModifier.getMethod(clazz, name);
            if (method == null) throw new RuntimeException("OATreeNode.findMethods() cant find method for \""+name+"\" in PropertyPath \""+propertyPath+"\"");
            vec.addElement(method);

            if ( Hub.class.equals(method.getReturnType())) {
                if (methodsToHub != null) {
                    throw new RuntimeException("OATreeNode.findMethods() propertyPath "+propertyPath+"\" has more then one hub");
                }
                methodsToHub = new Method[vec.size()];
                vec.copyInto(methodsToHub);
                vec.removeAllElements();
                
                // 20090703 was: clazz = Hub.getOAObjectInfo(clazz).getPropertyClass(name.substring(3));
                //new
                ArrayList al = OAObjectInfoDelegate.getOAObjectInfo(clazz).getPropertyInfos();
                for (OAPropertyInfo pi : al) {
                    if (pi.getName().equals(name.substring(3))) {
                        clazz = pi.getClassType();
                        break;
                    }
                }
            }
            else clazz = method.getReturnType();
        }
        if (methodsToHub == null) {
            methodsToProperty = new Method[1];
            methodsToProperty[0] = (Method) vec.elementAt(vec.size() - 1);
            vec.removeElementAt(vec.size() - 1);
            methodsToObject = new Method[vec.size()];
            vec.copyInto(methodsToObject);
        }
        else {
            methodsToProperty = new Method[vec.size()];
            vec.copyInto(methodsToProperty);
        }
    }


    public void add(OATreeNode node) {
        int x = treeNodeChildren.length;
        OATreeNode[] temp = new OATreeNode[x+1];
        System.arraycopy(treeNodeChildren, 0, temp, 0, x);
        treeNodeChildren = temp;
        treeNodeChildren[x] = node;
    }

    /** if false, then only the active object will be displayed in tree. */
    public boolean getShowAll() {
        return showAll;
    }
    public void setShowAll(boolean b) {
        showAll = b;
    }
    
    protected void getHtml(StringBuffer sb, OATree tree) {
        getHtml(sb, tree, null, null,null,false);
    }

    
    protected void getHtml(StringBuffer sb, OATree tree, Object object, OATreeData parent, Hub hubToUse, boolean bStartTable) {
        OATreeData data = parent;
        
        // see if any Children need displayed
        OATreeFormNode formNode = null;
        boolean bHasChildren = titleFlag;  // title nodes always need to be expanded
        for (int i=0; !bHasChildren && i < treeNodeChildren.length; i++) {
            OATreeNode node = treeNodeChildren[i];
            if (node instanceof OATreeFormNode) {
                formNode = (OATreeFormNode) node;
                continue;
            }
            if (object != null) node.findMethods(object.getClass());
            if (node.titleFlag) {
                bHasChildren = true;
                continue;
            }
            else if (node.methodsToHub != null) {
                Hub h;
                if (object == null) h = node.hub;
                else h = (Hub) ClassModifier.getPropertyValue(object, node.methodsToHub);
                if (h != null) {
                    h.loadAllData();
                    int x = h.getSize();
                    int j=0;
                    if (!showAll) {
                        j = hub.getPos();
                        if (j < 0) j = 0;
                        x = j+1;
                    }
                    for (; j");
            if (!bHasChildren) sb.append("");
            sb.append("");
        }
        
        if (titleFlag) {
            data = new OATreeData(parent, this, null, bIsExpanded, false);
            int pos = tree.addData(data);
            createHtmlRow(sb, tree, propertyPath, pos, (treeNodeChildren.length==0),bIsSelected,bIsExpanded,bHasChildren, true, formNode, false);
        }
        else if (object != null) {
            findMethods(object.getClass());
            String value;
            if (vecColumns != null) {
                value = "";
                int xx = vecColumns.size();
                for (int ii=0; ii tc.columns) s = s.substring(0,tc.columns);        
                    if (s.length() == 0) s = " ";
                    if (ii != 0) {
                        if (ii == (xx-1)) value += "";
                }
            }
            else {
                Object obj = object;
                // 06/24/01 methodsToObject has already been called before this method ...
                // if (methodsToObject != null) obj = ClassModifier.getPropertyValue(object, methodsToObject);
                value = OAConverter.toString(ClassModifier.getPropertyValue(obj, methodsToProperty),getFormat(methodsToProperty[methodsToProperty.length-1]));
            }
            
            boolean b = tree.isSelected(this, parent, object);
            boolean b2 = tree.isExpanded(this, parent, object);
            boolean b3 = tree.isFormExpanded(this, parent, object);
            data = new OATreeData(parent, this, object, b2, b3);
            data.hub = hubToUse;
            int pos = tree.addData(data);
            createHtmlRow(sb, tree, value, pos, (treeNodeChildren.length==0),bIsSelected,bIsExpanded,bHasChildren,false,formNode,bIsFormExpanded);
        }
        else if (hub != null) {
            hub.loadAllData();
            int x = hub.getSize();
            int j=0;
            if (!showAll) {
                j = hub.getPos();
                if (j < 0) j = 0;
                x = j+1;
            }
            createColumnHeading(sb);
            for (; j");
sb.append("");
sb.append("");

continue;//qqqqqqqqqqqqqqqqqqvvvvvvvvvvv                
            }
            if (!bIsExpanded) continue;
            if (object != null) node.findMethods(object.getClass());
            
            if (node.titleFlag) {
                node.getHtml(sb,tree,null,data, hubToUse,true);
                bEndTable = true;
            }
            else if (node.methodsToHub != null || node.hub != null) {
                Hub h;
                if (object == null) h = node.hub;
                else h = (Hub) ClassModifier.getPropertyValue(object, node.methodsToHub);
                h.loadAllData();
                int x = h.getSize();
                int j=0;
                if (!showAll) {
                    j = hub.getPos();
                    if (j < 0) j = 0;
                    x = j+1;
                }
                node.createColumnHeading(sb);
                boolean b = (node.vecColumns == null);
                for (; j");
        }
    }

    protected void createColumnHeading(StringBuffer sb) {
        if (vecColumns == null) return;

        // start a new table   
        sb.append("
"; else value += ""; } value += ""+s+""; if (ii != xx-1) value += ""); sb.append("
"); sb.append("Hello ..... "+node.propertyPath); sb.append("
"); sb.append("
"); String s, s2; sb.append(System.getProperty("line.separator")); sb.append(""); int x = vecColumns.size(); for (int i=0; i tc.columns) s = s.substring(0,tc.columns); int xx = s.length(); for ( ; xx < tc.columns; xx++) s += " "; sb.append(""); } sb.append(""); sb.append(""); } protected void createHtmlRow(StringBuffer sb, OATree tree, String value, int pos, boolean bLeaf, boolean bSelected, boolean bExpanded, boolean bHasChildren, boolean bTitle, OATreeFormNode formNode, boolean bFormExpanded) { String s, s2; sb.append(System.getProperty("line.separator")); sb.append(""); sb.append(""); } }









"+s+" 
"); if (!bLeaf) { if (!bHasChildren) { } else { if (bExpanded) { s = "oacommand_"+tree.name.length()+"_"+tree.name+"c"+pos; s2 = tree.getCollapseImageName(); } else { s = "oacommand_"+tree.name.length()+"_"+tree.name+"e"+pos; s2 = tree.getExpandImageName(); } sb.append(""); } s = "oacommand_"+tree.name.length()+"_"+tree.name+"s"+pos; s2 = tree.getNodeClosedImageName(); if (bSelected) s2 = tree.getNodeOpenedImageName(); } else { s = "oacommand_"+tree.name.length()+"_"+tree.name+"s"+pos; s2 = tree.getLeafImageName(); } if (formNode != null) { if (bFormExpanded) { s = "oacommand_"+tree.name.length()+"_"+tree.name+"C"+pos; s2 = tree.getExpandFormImageName(); } else { s = "oacommand_"+tree.name.length()+"_"+tree.name+"E"+pos; s2 = tree.getCollapseFormImageName(); } } sb.append(" "); sb.append(""); if (value == null) value = ""; for (int i=value.length(); i<8; i++) { value += " "; } s = (font == null) ? tree.font : font; if (s != null) value = "" + value + ""; if (bSelected) s = tree.selectColor; else s = colorBackground; if (s != null) { if (vecColumns == null) { s = ""; s += value; s += ""; value = s; } else { s = " bgcolor=\""+s+"\" "; value = Util.convert(value, ""; else s = ""; if (tree.bWrap || vecColumns != null) s += value; else s += ""+value+""; if (!bTitle) s += ""; sb.append(s); sb.append("