FileDocCategorySizeDatePackage
TreeNode.javaAPI DocJMF 2.1.1e24051Mon May 12 12:20:32 BST 2003jmapps.ui

TreeNode

public class TreeNode extends Vector

Fields Summary
public static final int
MARGIN_HORZ
public static final int
MARGIN_VERT
public static final int
BOXSIZE
public static final String
ACTION_NODE_ADDED
public static final String
ACTION_NODE_REMOVED
public static final String
ACTION_NODE_EXPANDED
public static final String
ACTION_NODE_COLLAPSED
public static final String
ACTION_NODE_SETCURRENT
public static final String
ACTION_NODE_RESETCURRENT
public static final Color
colorBg
public static final Color
colorCurBg
public static final Color
colorFg
public static final Color
colorCurFg
public static final Color
colorLines
private Component
componentOwner
private Vector
vectorActionListeners
private boolean
boolExpanded
private boolean
boolCurrent
private boolean
boolVisible
private Image
imageTreeElement
private Image
imageTreeElementCur
private int
nWidthImage
private int
nHeightImage
private Rectangle
rectElement
private Rectangle
rectElementFull
private String
strName
private Object
objUserData
private TreeNode
nodeOwner
Constructors Summary
public TreeNode(String stringName, Component clWindowOwner)



            
        super ();

        this.strName = new String ( stringName );
        this.componentOwner = clWindowOwner;
        rectElement = new Rectangle ( 0, 0, 0, 0 );
        rectElementFull = new Rectangle ( 0, 0, 0, 0 );

        setImage ( ImageArea.loadImage ( "treeNode.gif", clWindowOwner, true ) );
        setImageCur ( ImageArea.loadImage ( "treeNodeCur.gif", clWindowOwner, true ) );
	
Methods Summary
public voidaddActionListener(java.awt.event.ActionListener listener)

        if ( listener == null )
            return;
        vectorActionListeners.addElement ( listener );
    
public jmapps.ui.TreeNodeaddSubElement(jmapps.ui.TreeNode node)

        ActionEvent     event;

        if ( node.nodeOwner != null )
            node.nodeOwner.removeSubElement ( node );
        addElement ( node );
        node.nodeOwner = this;

        event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_ADDED );
        fireActionEvent ( event );

        return ( node );
    
public jmapps.ui.TreeNodeaddSubElement(java.lang.String stringName)

        TreeNode    node;

        node = new TreeNode ( stringName, componentOwner );
        addSubElement ( node );
        return ( node );
    
public voidaddToTreeElList(java.util.Vector cw_vectorTreeElList)

        int         i;
        int         nSize;
        TreeNode    node;

        if ( boolVisible == true )
            cw_vectorTreeElList.addElement ( this );

        if ( boolExpanded == false )
            return;

        nSize = getImmediateSubElementsCount ();
        for ( i = 0;  i < nSize;  i++ ) {
            node = (TreeNode) getSubElement ( i );
            node.addToTreeElList ( cw_vectorTreeElList );
        }
    
public voidcomputeImageSize()

        if ( boolCurrent == false  &&  imageTreeElement != null ) {
            nWidthImage = imageTreeElement.getWidth ( componentOwner );
            nHeightImage = imageTreeElement.getHeight ( componentOwner );
        }
        else if ( boolCurrent == true  &&  imageTreeElementCur != null ) {
            nWidthImage = imageTreeElementCur.getWidth ( componentOwner );
            nHeightImage = imageTreeElementCur.getHeight ( componentOwner );
        }
        else {
            nWidthImage = 0;
            nHeightImage = 0;
        }
    
public voiddestroyAllSubElements()

        int         nSize;
        TreeNode    node;

        nSize = size ();
        while ( nSize > 0 ) {
            nSize--;
            destroySubElement ( nSize );
        }
    
public voiddestroySubElement(jmapps.ui.TreeNode node)

        node.destroyAllSubElements ();
        node.nodeOwner = null;
        removeSubElement ( node );
    
public voiddestroySubElement(int nIndex)

        int         nSize;
        TreeNode    node;

        nSize = size ();
        if ( nIndex >= nSize  ||  nIndex < 0 )
            return;
        node = (TreeNode) elementAt ( nIndex );
        destroySubElement ( node );
    
public voiddrawDot(java.awt.Graphics graphics, int nX, int nY)

        if ( (nX + nY) % 2 == 0 )
            graphics.drawLine ( nX, nY, nX, nY );
    
public voiddrawDottedLine(java.awt.Graphics graphics, int nX1, int nY1, int nX2, int nY2)

        int     nX, nY;
        double  dDiv;

        if ( nX1 == nX2  &&  nY1 == nY2 ) {
            drawDot ( graphics, nX1, nY1 );
            return;
        }

        if ( nX1 > nX2 ) {
            nX = nX1;
            nX1 = nX2;
            nX2 = nX;
        }
        if ( nY1 > nY2 ) {
            nY = nY1;
            nY1 = nY2;
            nY2 = nY;
        }

        if ( nX2 - nX1 > nY2 - nY1 ) {
            dDiv = (double)(nY2 - nY1) / (nX2 - nX1);
            for ( nX = nX1;   nX <= nX2;   nX++ ) {
                nY = (int)Math.rint ( nY1 + (nX - nX1) * dDiv );
                drawDot ( graphics, nX, nY );
            }
        }
        else {
            dDiv = (nX2 - nX1) / (nY2 - nY1);
            for ( nY = nY1;   nY <= nY2;   nY++ ) {
                nX = (int)Math.rint ( nX1 + (nY - nY1) * dDiv );
                drawDot ( graphics, nX, nY );
            }
        }
    
public voiddrawDottedRectangle(java.awt.Graphics graphics, int nX, int nY, int nWidth, int nHeight)

        drawDottedLine ( graphics, nX, nY, nX + nWidth - 1, nY );
        drawDottedLine ( graphics, nX + nWidth - 1, nY, nX + nWidth - 1, nY + nHeight - 1 );
        drawDottedLine ( graphics, nX + nWidth - 1, nY + nHeight - 1, nX, nY + nHeight - 1 );
        drawDottedLine ( graphics, nX, nY + nHeight - 1, nX, nY );
    
public voiddrawElement(java.awt.Graphics graphics, int nOffsetX, int nOffsetY)

        int             nX;
        int             nY;
        int             nWidth;
        int             nHeight;
        int             nSize;
        Rectangle       rect;
        Rectangle       rectLastSubElement;
        FontMetrics     fontMetrics;
        TreeNode        node;
        Image           imageDraw;



        nSize = getImmediateSubElementsCount ();
        rect = new Rectangle ( rectElement );
        rect.x -= nOffsetX;
        rect.y -= nOffsetY;
        fontMetrics = graphics.getFontMetrics ();

        graphics.setColor ( colorLines );

        if ( nSize > 0 ) {
            nX = rect.x + MARGIN_HORZ;
            nY = rect.y + (rect.height - BOXSIZE) / 2;
            nWidth = BOXSIZE;
            nHeight = BOXSIZE;
            graphics.setColor ( colorBg );
            graphics.fillRect ( nX, nY, nWidth, nHeight );
            graphics.setColor ( colorLines );
            graphics.drawRect ( nX, nY, nWidth, nHeight );

            nX = rect.x + MARGIN_HORZ + BOXSIZE;
            nY = rect.y + rect.height / 2;
            drawDottedLine ( graphics, nX, nY, nX + MARGIN_HORZ, nY );
        }
        else {
            nX = rect.x + MARGIN_HORZ + BOXSIZE / 2;
            nY = rect.y + rect.height / 2;
            drawDottedLine ( graphics, nX, nY, nX + MARGIN_HORZ, nY );
        }

        if ( nSize > 0  &&  boolExpanded == true ) {
            node = (TreeNode) getSubElement ( nSize - 1 );
            rectLastSubElement = node.getNodeBounds ();

            nX = rect.x + 2 * MARGIN_HORZ + BOXSIZE + nWidthImage / 2;
            nY = rect.y + (rect.height - nHeightImage) / 2 + nHeightImage + 1;
            nHeight = rectLastSubElement.y - nOffsetY - nY + rectLastSubElement.height / 2;
            drawDottedLine ( graphics, nX, nY, nX, nY + nHeight );
        }

        graphics.setColor ( colorFg );

        if ( nSize > 0 ) {
            nX = rect.x + MARGIN_HORZ + 2;
            nY = rect.y + rect.height / 2;
            nWidth = BOXSIZE - 2 * 2;
            graphics.drawLine ( nX, nY, nX + nWidth, nY );
            if ( boolExpanded == false ) {
                nX = rect.x + MARGIN_HORZ + BOXSIZE / 2;
                nY = rect.y + (rect.height - BOXSIZE) / 2 + 2;
                nHeight = BOXSIZE - 2 * 2;
                graphics.drawLine ( nX, nY, nX, nY + nHeight );
            }
        }

        nX = rect.x + 2 * MARGIN_HORZ + BOXSIZE;
        nY = rect.y + (rect.height - nHeightImage) / 2;
		
        if ( boolCurrent == true )
            imageDraw = imageTreeElementCur;
        else
            imageDraw = imageTreeElement;

        if ( imageDraw != null )
            graphics.drawImage ( imageDraw, nX, nY, colorBg, componentOwner );

        if ( nWidthImage < 0  ||  nHeightImage < 0 ) {
            nWidthImage = imageDraw.getWidth ( componentOwner );
            nHeightImage = imageDraw.getHeight ( componentOwner );
        }

        nX = rect.x + 3 * MARGIN_HORZ + BOXSIZE + nWidthImage - MARGIN_HORZ / 2;
        nY = rect.y + 2;
        nWidth = fontMetrics.stringWidth ( strName ) + MARGIN_HORZ;
        nHeight = rect.height - 3;
        if ( boolCurrent == true ) {
            graphics.setColor ( colorCurBg );
            graphics.fillRect ( nX, nY, nWidth, nHeight );
            graphics.setColor ( colorCurFg );
        }
        else {
            graphics.setColor ( colorFg );
        }

        if ( boolCurrent == true  &&  hasOwnerFocus() )
            drawDottedRectangle ( graphics, nX, nY, nWidth, nHeight );

        nHeight = fontMetrics.getAscent ();
        nX = rect.x + 3 * MARGIN_HORZ + BOXSIZE + nWidthImage;
        nY = rect.y + (rect.height - nHeight) / 2 + nHeight;
        graphics.drawString ( strName, nX, nY );
    
public jmapps.ui.TreeNodefindElement(java.lang.String stringFullPath)

        int         i;
        int         nSize;
        TreeNode    node = null;


        if ( stringFullPath.equals("/" + strName) )
            return ( this );
        if ( !(stringFullPath.startsWith("/"+strName)) )
            return ( null );

        stringFullPath = stringFullPath.substring ( strName.length() + 1 );
        nSize = size ();
        for ( i = 0;  i < nSize  &&  node == null;  i++ ) {
            node = (TreeNode) elementAt ( i );
            node = node.findElement ( stringFullPath );
        }
        return ( node );
    
private voidfireActionEvent(java.awt.event.ActionEvent event)

        int             i;
        int             nCount;
        ActionListener  listener;

        nCount = vectorActionListeners.size ();
        for ( i = 0;  i < nCount;  i++ ) {
            listener = (ActionListener) vectorActionListeners.elementAt ( i );
            listener.actionPerformed ( event );
        }
    
public jmapps.ui.TreeNodegetCurrent()

        int         i;
        int         nSize;
        TreeNode    node;
        TreeNode    nodeCurrent = null;

        if ( boolCurrent == true )
            return ( this );
        nSize = getImmediateSubElementsCount ();
        for ( i = 0;  i < nSize  &&  nodeCurrent == null;  i++ ) {
            node = (TreeNode) getSubElement ( i );
            nodeCurrent = node.getCurrent ();
        }
        return ( nodeCurrent );
    
public intgetExpandedSubElementsCount()

        int     i;
        int     nCount;
        int     nSize;
        Object  object;

        nSize = size ();
        nCount = nSize;
        for ( i = 0;  i < nSize;  i++ ) {
            object = elementAt ( i );
            if ( object != null  &&  object instanceof TreeNode  &&  ((TreeNode)object).isExpanded() )
                nCount += ((TreeNode)object).getExpandedSubElementsCount ();
        }
        return ( nCount );
    
public java.lang.StringgetFullPathName()

        String  stringName;

        if ( nodeOwner != null )
            stringName = nodeOwner.getFullPathName ();
        else
            stringName = new String ();
        stringName += "/" + strName;
        return ( stringName );
    
public intgetImmediateSubElementsCount()

        int	nSize;

        nSize = size ();
        return ( nSize );
    
public java.lang.StringgetName()

        return ( strName );
    
public java.awt.RectanglegetNodeBounds()

        return ( new Rectangle(rectElement) );
    
public java.awt.RectanglegetNodeBoundsFull()

        return ( new Rectangle(rectElementFull) );
    
public jmapps.ui.TreeNodegetOwner()

        return ( nodeOwner );
    
public intgetRecursiveSubElementsCount()

        int     i;
        int     nCount;
        int     nSize;
        Object  object;

        nSize = size ();
        nCount = nSize;
        for ( i = 0;  i < nSize;  i++ ) {
            object = elementAt ( i );
            if ( object != null  &&  object instanceof TreeNode )
                nCount += ((TreeNode)object).getRecursiveSubElementsCount ();
        }
        return ( nCount );
    
public jmapps.ui.TreeNodegetRoot()

        TreeNode    nodeRoot;

        nodeRoot = this;
        while ( nodeRoot.getOwner() != null )
            nodeRoot = nodeRoot.getOwner ();
        return ( nodeRoot );
    
public jmapps.ui.TreeNodegetSubElement(java.lang.String stringName)

        int         i;
        int         nSize;
        TreeNode    node;
        String      stringElementName = null;


        nSize = size ();
        for ( i = 0;  i < nSize;  i++ ) {
            node = (TreeNode) elementAt ( i );
            stringElementName = node.getName ();
            if ( stringElementName.equals (stringName) )
                return ( node );
        }
        return ( null );
    
public jmapps.ui.TreeNodegetSubElement(int nIndex)

        int         nSize;
        TreeNode    node;

        nSize = size ();
        if ( nIndex >= nSize  ||  nIndex < 0 )
            return ( null );
        node = (TreeNode) elementAt ( nIndex );
        return ( node );
    
public java.lang.ObjectgetUserData()

        return ( objUserData );
    
private booleanhasOwnerFocus()

        boolean     boolResult = false;
        Component   component;
        Window      window;

        if ( componentOwner == null )
            return ( false );

        component = componentOwner;
        while ( component != null ) {
            if ( component instanceof Window ) {
                boolResult = (((Window)component).getFocusOwner() == componentOwner);
                break;
            }
            component = component.getParent ();
        }
        return ( boolResult );
    
public jmapps.ui.TreeNodeinsertSubElement(int nIndex, jmapps.ui.TreeNode node)

        int     nSize;

        if ( node.nodeOwner != null )
            node.nodeOwner.removeSubElement ( node );

        nSize = size ();
        if ( nIndex < 0 )
            nIndex = 0;
        if ( nIndex > nSize )
            nIndex = nSize;
        insertElementAt ( node, nIndex );
        node.nodeOwner = this;
        return ( node );
    
public jmapps.ui.TreeNodeinsertSubElement(int nIndex, java.lang.String stringName)

        TreeNode    node;

        node = new TreeNode ( stringName, componentOwner );
        insertSubElement ( nIndex, node );
        return ( node );
    
public booleanisExpanded()

        return ( boolExpanded );
    
public booleanisRecursiveSubElement(java.lang.Object object)

        boolean     boolResult = false;
        int         i;
        int         nSize;
        Object      objectSub;

        if ( object == this )
            return ( true );
        nSize = size ();
        for ( i = 0;  i < nSize  &&  boolResult == false;  i++ ) {
            objectSub = elementAt ( i );
            if ( object == objectSub )
                return ( true );
            if ( objectSub != null  &&  objectSub instanceof TreeNode )
                boolResult = ((TreeNode)objectSub).isRecursiveSubElement ( object );
        }
        return ( boolResult );
	
public booleanisSubElement(java.lang.String stringName)

        int         i;
        int         nSize;
        TreeNode    node;
        String      stringElementName = null;


        nSize = size ();
        for ( i = 0;  i < nSize;  i++ ) {
            node = (TreeNode) elementAt ( i );
            stringElementName = node.getName ();
            if ( stringElementName.equals(stringName) )
                return ( true );
        }
        return ( false );
    
public booleanisVisible()

        return ( boolVisible );
    
public voidonMouseDoubleclick(java.awt.Point pointMouse)

        int         i;
        int         nSize;
        TreeNode    node;

        nSize = getImmediateSubElementsCount ();
        if ( !(rectElementFull.contains (pointMouse)) )
            return;
        if ( rectElement.contains (pointMouse)
                    &&  pointMouse.x >= rectElement.x + 2 * MARGIN_HORZ + BOXSIZE
                    &&  nSize > 0 ) {
            switchExpanded ();
            return;
        }
        if ( boolExpanded == false )
            return;

        for ( i = 0;  i < nSize;  i++ ) {
            node = (TreeNode) getSubElement ( i );
            node.onMouseDoubleclick ( pointMouse );
        }
    
public voidonMousePressedLeft(java.awt.Point pointMouse, boolean boolShift, boolean boolCtrl, boolean boolAlt)

        int         i;
        int         nSize;
        TreeNode    node;

        nSize = getImmediateSubElementsCount ();
        if ( !(rectElementFull.contains (pointMouse)) )
            return;
        if ( rectElement.contains (pointMouse) ) {
            if ( pointMouse.x >= rectElement.x + 2 * MARGIN_HORZ + BOXSIZE )
                setCurrent ( true );
            else if ( nSize > 0 )
                switchExpanded ();
            return;
        }
        if ( boolExpanded == false )
            return;

        for ( i = 0;  i < nSize;  i++ ) {
            node = (TreeNode) getSubElement ( i );
            node.onMousePressedLeft ( pointMouse, boolShift, boolCtrl, boolAlt );
        }
    
public voidonMousePressedRight(java.awt.Point pointMouse, boolean boolShift, boolean boolCtrl, boolean boolAlt)

    
public java.awt.RectanglerecomputeLayout(java.awt.Rectangle rect, java.awt.FontMetrics fontMetrics)

        int         i;
        int         nSize;
        int         nOffsetX;
        int         nOffsetY;
        int         nHeight;
        TreeNode    node;
        Rectangle   rectSubElement;


        computeImageSize ();

        rectElement.x = rect.x;
        rectElement.y = rect.y;

        if ( boolVisible == true ) {
            rectElement.height = Math.max ( fontMetrics.getHeight(), BOXSIZE + 2 * MARGIN_VERT );
            rectElement.height = Math.max ( rectElement.height, nHeightImage + 2 * MARGIN_VERT );
            rectElement.width = BOXSIZE + nWidthImage + fontMetrics.stringWidth(strName) + 4 * MARGIN_HORZ;
        }
        else {
            rectElement.height = 0;
            rectElement.width = 0;
        }
        rectElementFull.setBounds ( rectElement );

        if ( boolExpanded == true ) {
            if ( boolVisible == true )
                nOffsetX = BOXSIZE / 2 + MARGIN_HORZ + nWidthImage / 2;
            else
                nOffsetX = 0;
            nOffsetY = rectElement.height;

            rectSubElement = new Rectangle ( rect );
            rectSubElement.x += nOffsetX;
            rectSubElement.y += nOffsetY;

            nSize = getImmediateSubElementsCount ();
            for ( i = 0;  i < nSize;  i++ ) {
                node = (TreeNode) getSubElement ( i );
                rectSubElement = node.recomputeLayout ( rectSubElement, fontMetrics );
                rectElementFull.height += rectSubElement.height;
                rectSubElement.y += rectSubElement.height;
                if ( rectElementFull.width < nOffsetX + rectSubElement.width )
                    rectElementFull.width = nOffsetX + rectSubElement.width;
            }
        }
        return ( new Rectangle(rectElementFull) );
    
public voidremoveSubElement(jmapps.ui.TreeNode node)

        boolean         boolResult;
        ActionEvent     event;

        boolResult = removeElement ( node );
        if ( boolResult == true ) {
            node.nodeOwner = null;
            event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_REMOVED );
            fireActionEvent ( event );
        }
    
public voidrewmoveActionListener(java.awt.event.ActionListener listener)

        if ( listener == null )
            return;
        vectorActionListeners.removeElement ( listener );
    
public voidsetCurrent(boolean boolCurrent)

        TreeNode	node;
        ActionEvent event;

        if ( this.boolCurrent == boolCurrent )
            return;

        if ( boolCurrent == true ) {
            node = (TreeNode) getRoot ();
            node = node.getCurrent ();
            if ( node != null )
                node.setCurrent ( false );

            node = (TreeNode) nodeOwner;
            while ( node != null  &&  node instanceof TreeNode ) {
                if ( node.isExpanded() == false )
                    node.setExpanded ( true );
                node = (TreeNode) node.nodeOwner;
            }
            event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_SETCURRENT );
        }
        else {
            event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_RESETCURRENT );
        }

        this.boolCurrent = boolCurrent;
        computeImageSize ();
        fireActionEvent ( event );
    
public voidsetExpanded(boolean boolExpanded)

        if ( this.boolExpanded != boolExpanded )
            switchExpanded ();
    
public voidsetImage(java.awt.Image imageNew)

        imageTreeElement = imageNew;
        computeImageSize ();
    
public voidsetImageCur(java.awt.Image imageNew)

        imageTreeElementCur = imageNew;
        computeImageSize ();
    
public voidsetUserData(java.lang.Object objectData)

        this.objUserData = objectData;
    
public voidsetVisible(boolean boolVisible)

        if ( this.boolVisible == boolVisible )
			return;

        this.boolVisible = boolVisible;
        if ( boolVisible == false  &&  boolExpanded == false )
            switchExpanded ();
    
public voidsortSubElements()

        int         i, j;
        int         nSize;
        TreeNode    node1;
        TreeNode    node2;
        String      stringName1;
        String      stringName2;
        int         nResult;


        nSize = size ();
        for ( i = 0;  i < nSize;  i++ ) {
            node1 = (TreeNode) elementAt ( i );
            stringName1 = node1.getName ();
            removeElementAt ( i );
            for ( j = 0;  j < i;  j++ ) {
                node2 = (TreeNode) elementAt ( j );
                stringName2 = node2.getName ();
                nResult = stringName1.compareTo ( stringName2 );
                if ( nResult < 0 )
                    break;
            }
            insertElementAt ( node1, j );
            node1.sortSubElements ();
        }
    
public booleanswitchExpanded()

        ActionEvent     event;

        if ( boolExpanded == true ) {
            boolExpanded = false;
            event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_COLLAPSED );
        }
        else {
            boolExpanded = true;
            event = new ActionEvent ( this, ActionEvent.ACTION_PERFORMED, ACTION_NODE_EXPANDED );
        }
        fireActionEvent ( event );
        return ( boolExpanded );