HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"JComponentTree"
Volume: 3 Issue: 10, p. 22

Download files assoicated with this article

	

Listing 1
 
public class ComponentTreeNode extends DefaultMutableTreeNode 
{ 
  public ComponentTreeNode(Component obj) 
  { 
    super(obj); 
  } 

  public ComponentTreeNode(Component obj, boolean allowsChildren) 
  { 
    super(obj, allowsChildren); 
  } 

  public Component getComponent() 
  { 
    return (Component)getUserObject(); 
  } 
} 

Listing 2
 
public interface ComponentTreeConstants 
{ 
  // Orientation constants 
  public static final int NORTH = 1; 
  public static final int SOUTH = 2; 
  public static final int EAST = 3; 
  public static final int WEST = 4; 

  // Justification constants 
  public static final int LEFT = 1; 
  public static final int CENTER = 2; 
  public static final int RIGHT = 3; 
  public static final int TOP = 1; 
  public static final int MIDDLE = 2; 
  public static final int BOTTOM = 3; 

  // Line type constants 
  public static final int SQUARE = 1; 
  public static final int STRAIGHT = 2; 
} 

Listing 3 

public Dimension preferredLayoutSize(Container container) 
{ 
  Dimension dim = getPreferredSize(getRoot()); 
  Insets insets = container.getInsets(); 
  int vertInsets = insets.top + insets.bottom; 
  int horzInsets = insets.left + insets.right; 
  dim.width += horzInsets; 
  dim.height += vertInsets; 
  return dim; 
} 

private Dimension getPreferredSize(ComponentTreeNode node) 
{ 
  if (!node.getComponent().isVisible()) 
    return new Dimension(0, 0); 

  Dimension dim = node.getComponent().getPreferredSize(); 
  Dimension preferredSize = 
    new Dimension(dim.width, dim.height); 

  int children = model.getChildCount(node); 
  if (direction == EAST || direction == WEST) 
  { 
    int width = 0; 
    int height = 0; 
    if (children > 0) 
    { 
      Dimension size; 
      ComponentTreeNode child; 
      for (int i = 0; i < children; i++) 
      { 
        child = (ComponentTreeNode)model.getChild(node, i); 
        if (child.getComponent().isVisible()) 
        { 
          size = getPreferredSize(child); 
          height += size.height + vgap; 
          if (size.width > width) 
          width = size.width; 
        } 
      } 
    } 
    preferredSize = new Dimension( 
      preferredSize.width + width + hgap, 
      Math.max(preferredSize.height, height - vgap)); 
  } 
  if (direction == NORTH || direction == SOUTH) 
  { 
    int width = 0; 
    int height = 0; 
    if (children > 0) 
    { 
      Dimension size; 
      ComponentTreeNode child; 
      for (int i = 0; i < children; i++) 
      { 
        child = (ComponentTreeNode)model.getChild(node, i); 
        if (child.getComponent().isVisible()) 
        { 
          size = getPreferredSize(child); 
          width += size.width + hgap; 
          if (size.height > height) 
     height = size.height; 
        } 
      } 
    } 
    preferredSize = new Dimension( 
      Math.max(preferredSize.width, width - hgap), 
      preferredSize.height + height + vgap); 
  } 
  return preferredSize; 
} 

Listing 4 

public void layoutContainer(Container container) 
{ 
  Insets insets = container.getInsets(); 
  Dimension dim = container.getSize(); 
  Dimension prefered = getPreferredSize(getRoot()); 
  int vertInsets = insets.top + insets.bottom; 
  int horzInsets = insets.left + insets.right; 

  if (direction == WEST) 
    layout(getRoot(), dim.width - insets.right, insets.top); 
  if (direction == NORTH) 
    layout(getRoot(), insets.left, dim.height - insets.bottom); 
  if (direction == EAST || direction == SOUTH) 
    layout(getRoot(), insets.left, insets.top); 
} 

public void layout(ComponentTreeNode node, int x, int y) 
{ 
  if (!node.getComponent().isVisible()) return; 

  int children = model.getChildCount(node); 
  if (direction == EAST || direction == WEST) 
  { 
    Dimension size = node.getComponent().getPreferredSize(); 

    Dimension down = getLayoutSize(node); 

    int pos = y; 
    if (alignment == MIDDLE) 
    { 
      pos = y + (down.height - size.height) / 2; 
    } 
    if (alignment == BOTTOM) 
    { 
      pos = y + (down.height - size.height); 
    } 
    if (direction == EAST) 
    { 
      node.getComponent(). 
        setBounds(x, pos, size.width, size.height); 
      x += Math.max(size.width, down.width) + hgap; 
    } 
    else 
    { 
      node.getComponent(). 
        setBounds(x - size.width, pos, size.width, size.height); 
      x -= Math.max(size.width, down.width) + hgap; 
    } 

    ComponentTreeNode child; 
    for (int i = 0; i < children; i++) 
    { 
      child = (ComponentTreeNode)model.getChild(node, i); 
      if (child.getComponent().isVisible()) 
      { 
        layout(child, x, y); 
        y += getLayoutSize(child).height + vgap; 
      } 
    } 
  } 
  if (direction == NORTH || direction == SOUTH) 
  { 
    Dimension size = 
      node.getComponent().getPreferredSize(); 
    Dimension right = getLayoutSize(node); 

    int pos = x; 
    if (alignment == CENTER) 
    { 
      pos = x + (right.width - size.width) / 2; 
    } 
    if (alignment == RIGHT) 
    { 
      pos = x + (right.width - size.width); 
    } 

    if (direction == SOUTH) 
    { 
      node.getComponent(). 
        setBounds(pos, y, size.width, size.height); 
      y += Math.max(size.height, right.height) + vgap; 
    } 
    else 
    { 
      node.getComponent(). 
        setBounds(pos, y - size.height, size.width, size.height); 
      y -= Math.max(size.height, right.height) + vgap; 
    } 

    ComponentTreeNode child; 
    for (int i = 0; i < children; i++) 
    { 
      child = (ComponentTreeNode)model.getChild(node, i); 
      if (child.getComponent().isVisible()) 
      { 
        layout(child, x, y); 
        x += getLayoutSize(child).width + hgap; 
      } 
    } 
  } 
} 

Listing 5. 

public void drawLines(Container cont, Graphics g) 
{ 
  Color dark = cont.getBackground().darker(); 
  Color lite = cont.getBackground().brighter(); 
  drawLines(getRoot(), g, dark, lite); 
} 

private void drawLines(ComponentTreeNode node, 
  Graphics g, Color dark, Color lite) 
{ 
  if (!node.getComponent().isVisible()) return; 
  int children = model.getChildCount(node); 
  if (direction == EAST || direction == WEST) 
  { 
    Rectangle dim = node.getComponent().getBounds(); 
    int x0, y0, x1, y1, x2, y2; 

    if (direction == EAST) 
    { 
      x0 = dim.x + dim.width; 
      x1 = x0 + hgap / 2; 
      x2 = x0 + hgap - 1; 
    } 
    else 
    { 
      x0 = dim.x; 
      x1 = x0 - hgap / 2; 
      x2 = x0 - hgap + 1; 
    } 
    y0 = dim.y; 
    y1 = dim.y + dim.height / 2; 

    ComponentTreeNode child; 
    for (int i = 0; i < children; i++) 
    { 
      child = (ComponentTreeNode)model.getChild(node, i); 
      if (child.getComponent().isVisible()) 
      { 
        Rectangle bounds = child.getComponent().getBounds(); 
        y2 = bounds.y + bounds.height / 2; 

        if (linetype == SQUARE) 
        { 
          drawLine(g, dark, lite, x0, y1, x1, y1); 
          drawLine(g, dark, lite, x1, y2, x2, y2); 
          if (y1 != y2) 
            drawLine(g, dark, lite, x1, y1, x1, y2); 
          if (i == 0) 
          { 
            if(alignment == LEFT) 
              y0 = Math.min(y2, y1); 
            if (alignment == RIGHT) 
              y0 = Math.max(y2, y1); 
          } 
          if (children >= 1  && alignment != CENTER) 
            drawLine(g, dark, lite, x1, y0, x1, y2); 
        } 
        else 
        { 
          drawLine(g, dark, lite, x0, y1, x2, y2); 
        } 

        drawLines(child, g, dark, lite); 
      } 
    } 
  } 
  if (direction == NORTH || direction == SOUTH) 
  { 
    Rectangle dim = node.getComponent().getBounds(); 
    int x0, y0, x1, y1, x2, y2; 

    if (direction == SOUTH) 
    { 
      y0 = dim.y + dim.height; 
      y1 = y0 + vgap / 2; 
      y2 = y0 + vgap - 1; 
    } 
    else 
    { 
      y0 = dim.y; 
      y1 = y0 - vgap / 2; 
      y2 = y0 - vgap + 1; 
    } 
    x0 = dim.x; 
    x1 = dim.x + dim.width / 2; 

    ComponentTreeNode child; 
    for (int i = 0; i < children; i++) 
    { 
      child = (ComponentTreeNode)model.getChild(node, i); 
      if (child.getComponent().isVisible() 
      { 
        Rectangle bounds = child.getComponent().getBounds(); 
        x2 = bounds.x + bounds.width / 2; 

        if (linetype == SQUARE) 
        { 
          drawLine(g, dark, lite, x1, y0, x1, y1); 
          drawLine(g, dark, lite, x2, y1, x2, y2); 
          if (x1 != x2) 
            drawLine(g, dark, lite, x1, y1, x2, y1); 
          if (i == 0) 
          { 
            if (alignment == LEFT) 
              x0 = Math.min(x2, x1); 
            if (alignment == RIGHT) 
              x0 = Math.max(x2, x1); 
          } 
          if (children >= 1 && alignment != CENTER) 
          drawLine(g, dark, lite, x0, y1, x2, y1); 
        } 
        else 
        { 
          drawLine(g, dark, lite, x1, y0, x2, y2); 
        } 
        drawLines(child, g, dark, lite); 
      } 
    } 
  } 
} 

private void drawLine(Graphics g, 
  Color dark, Color lite, int x1, int y1, int x2, int y2) 
{ 
  g.setColor(lite); 
  g.drawLine(x1, y1, x2, y2); 
  g.setColor(dark); 
  g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1); 
} 

Listing 6. 

public class JComponentTree extends JPanel 
  implements ComponentTreeConstants, TreeModelListener 
{ 
  protected ComponentTreeLayout treeLayout; 
  protected CellRendererPane pane; 

  public JComponentTree( 
    int direction, int alignment, int linetype, 
    int hgap, int vgap) 
  { 
    treeLayout = new ComponentTreeLayout(this, 
      direction, alignment, linetype, hgap, vgap); 
    setLayout(treeLayout); 
  } 

  // Other constructor variations not listed 

  public ComponentTreeNode addNode( 
    ComponentTreeNode parent, Component child) 
  { 
    ComponentTreeNode node = 
      new ComponentTreeNode(child); 
    if (parent == null) setRoot(node); 
    else treeLayout.addNode(parent, node); 
    add(child); 
    return node; 
  } 

  public void setDirection(int direction) 
  { 
    treeLayout.setDirection(direction); 
    setSize(getPreferredSize()); 
    doLayout(); 
    repaint(); 
  } 

  public int getDirection() 
  { 
    return treeLayout.getDirection(); 
  } 

  // Other field accessor methods not listed 

  public void paintComponent(Graphics g) 
  { 
    super.paintComponent(g); 
    treeLayout.drawLines(this, g); 
  } 

  public Insets getInsets() 
  { 
    return new Insets(10, 10, 10, 10); 
  } 

  public void treeNodesChanged(TreeModelEvent event) 
  { 
    doLayout(); 
    repaint(); 
  } 

  // Other TreeModelListener methods not listed 
} 


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.