Package

Source Code of ProjectTreeModel$CheckRenderer$TreeLabel

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.ColorUIResource;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import fbench.FBIconLibrary;

public class ProjectTreeModel extends JTree {
  /**
   *
   */
  private static final long serialVersionUID = 5716349484811378870L;

  public class CheckRenderer extends JPanel implements TreeCellRenderer {
    /**
     *
     */
    private static final long serialVersionUID = 3987440114119024156L;

    protected JCheckBox check;

    protected TreeLabel label;

    public CheckRenderer() {
      setLayout(null);
      add(check = new JCheckBox());
      add(label = new TreeLabel());
      check.setBackground(UIManager.getColor("Tree.textBackground"));
      label.setForeground(UIManager.getColor("Tree.textForeground"));
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
        boolean isSelected, boolean expanded, boolean leaf, int row,
        boolean hasFocus) {
      String stringValue = tree.convertValueToText(value, isSelected,
          expanded, leaf, row, hasFocus);
      setEnabled(tree.isEnabled());
      if (value instanceof CheckNode) {
        check.setVisible(true);
        check.setSelected(((CheckNode) value).isSelected());
      } else {
        check.setVisible(false);
      }
      label.setFont(tree.getFont());
      label.setText(stringValue);
      label.setSelected(isSelected);
      label.setFocus(hasFocus);
      Icon icon = stringValue.contains(".") ? FBIconLibrary
          .getIcon(stringValue) : null;
      if (icon == null) {
        if (leaf)
          icon = UIManager.getIcon("Tree.leafIcon");
        else if (expanded)
          icon = UIManager.getIcon("Tree.openIcon");
        else
          icon = UIManager.getIcon("Tree.closedIcon");
      }
      label.setIcon(icon);
      return this;
    }

    public Dimension getPreferredSize() {
      Dimension dCheck = check.getPreferredSize();
      Dimension dLabel = label.getPreferredSize();
      return new Dimension(dCheck.width + dLabel.width,
          (dCheck.height < dLabel.height ? dLabel.height
              : dCheck.height));
    }

    public Dimension getCheckSize() {
      Dimension dim = check.isVisible() ? check.getPreferredSize()
          : new Dimension();
      return dim;
    }

    public void doLayout() {
      Dimension dCheck = getCheckSize();
      Dimension dLabel = label.getPreferredSize();
      int yCheck = 0;
      int yLabel = 0;
      if (dCheck.height < dLabel.height) {
        yCheck = (dLabel.height - dCheck.height) / 2;
      } else {
        yLabel = (dCheck.height - dLabel.height) / 2;
      }
      check.setLocation(0, yCheck);
      check.setBounds(0, yCheck, dCheck.width, dCheck.height);
      label.setLocation(dCheck.width, yLabel);
      label.setBounds(dCheck.width, yLabel, dLabel.width, dLabel.height);
    }

    public void setBackground(Color color) {
      if (color instanceof ColorUIResource)
        color = null;
      super.setBackground(color);
    }

    public class TreeLabel extends JLabel {
      boolean isSelected;
      boolean hasFocus;

      public TreeLabel() {
      }

      public void setBackground(Color color) {
        if (color instanceof ColorUIResource)
          color = null;
        super.setBackground(color);
      }

      public void paint(Graphics g) {
        String str;
        if ((str = getText()) != null) {
          if (0 < str.length()) {
            if (isSelected) {
              g.setColor(UIManager
                  .getColor("Tree.selectionBackground"));
            } else {
              g.setColor(UIManager
                  .getColor("Tree.textBackground"));
            }
            Dimension d = getPreferredSize();
            int imageOffset = 0;
            Icon currentI = getIcon();
            if (currentI != null) {
              imageOffset = currentI.getIconWidth()
                  + Math.max(0, getIconTextGap() - 1);
            }
            g.fillRect(imageOffset, 0, d.width - 1 - imageOffset,
                d.height);
            if (hasFocus) {
              g.setColor(UIManager
                  .getColor("Tree.selectionBorderColor"));
              g.drawRect(imageOffset, 0, d.width - 1
                  - imageOffset, d.height - 1);
            }
          }
        }
        super.paint(g);
      }

      public Dimension getPreferredSize() {
        Dimension retDimension = super.getPreferredSize();
        if (retDimension != null) {
          retDimension = new Dimension(retDimension.width + 3,
              retDimension.height);
        }
        return retDimension;
      }

      public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
      }

      public void setFocus(boolean hasFocus) {
        this.hasFocus = hasFocus;
      }
    }

    public int getRowHeight() {
      return check.getPreferredSize().height;
    }
  }

  protected DefaultMutableTreeNode root = null;

  protected CheckRenderer renderer = null;

  protected DefaultMutableTreeNode selected = null;
 
  protected ProjectBuilder2 projectBuilder = null;

  public ProjectTreeModel(DefaultMutableTreeNode root, ProjectBuilder2 projectBuilder) {
    super(root);
    this.root = root;
    this.projectBuilder = projectBuilder;

    this.addMouseListener(new MouseAdapter() {
      public void mouseReleased(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        ProjectTreeModel tree = ProjectTreeModel.this;
        int row = tree.getRowForLocation(x, y);
        TreePath path = tree.getPathForRow(row);
        // TreePath path = tree.getSelectionPath();
        if (path != null) {
          Object lastNode = path.getLastPathComponent();
          if (lastNode instanceof CheckNode) {
            CheckNode node = (CheckNode) lastNode;
            Dimension d = renderer.getCheckSize();
            Rectangle rect = tree.getUI().getPathBounds(tree, path);

            boolean isSelected = node.isSelected();
            if (node != selected && x - rect.x < d.width
                && y - rect.y < d.height || node == selected)
              isSelected = !isSelected;

            node.setSelected(isSelected);
            ProjectTreeModel.this.projectBuilder.handleNodeSelected(node);
            if (node.getSelectionMode() == CheckNode.DIG_IN_SELECTION) {
              if (isSelected) {
                tree.expandPath(path);
              } else {
                tree.collapsePath(path);
              }
            }
            ((DefaultTreeModel) tree.getModel()).nodeChanged(node);
            // I need to revalidate if node is root...?
            if (row == 0) {
              tree.revalidate();
              tree.repaint();
            }
          }
          selected = (DefaultMutableTreeNode) lastNode;
        }
      }
    });
    renderer = new CheckRenderer();
    this.setCellRenderer(renderer);
    this.setRowHeight(renderer.getRowHeight());
    this.setShowsRootHandles(true);
  }

  public void addNode(String text, File file, boolean allowsChildren,
      boolean isSelected) {
    text = new String(text).replace(File.separator, "\\");
    String[] parts = text.split("\\\\", 100);
    DefaultMutableTreeNode current = root;

    for (int i = 0; i < parts.length - 1; i++) {
      if (parts[i].equals(""))
        continue;

      DefaultMutableTreeNode child = null;
      boolean found = false;
      Enumeration children = current.children();
      while (children.hasMoreElements()) {
        child = (DefaultMutableTreeNode) children.nextElement();

        if (child.toString().equals(parts[i])) {
          current = child;
          found = true;
          break;
        }
      }

      if (!found) {
        child = new DefaultMutableTreeNode(parts[i]);
        current.add(child);
        current = child;
      }
    }

    DefaultMutableTreeNode child = null;
    Enumeration children = current.children();
    while (children.hasMoreElements()) {
      child = (DefaultMutableTreeNode) children.nextElement();
      if (child.toString().equals(file.getName()))
        return;
    }
   
    CheckNode node = new CheckNode(parts[parts.length - 1], file,
        allowsChildren, isSelected);
    // node.setUserObject(file);
    current.add(node);
    invalidate();
  }

  public void expandAll(boolean expand) {
    expandAll(new TreePath(root), expand);
  }

  private void expandAll(TreePath parent, boolean expand) {
    // Traverse children
    TreeNode node = (TreeNode) parent.getLastPathComponent();
    if (node.getChildCount() >= 0) {
      for (Enumeration e = node.children(); e.hasMoreElements();) {
        TreeNode n = (TreeNode) e.nextElement();
        TreePath path = parent.pathByAddingChild(n);
        expandAll(path, expand);
      }
    }

    if (expand) {
      expandPath(parent);
    } else {
      collapsePath(parent);
    }
  }

  public File[] getSelectedFiles() {
    Vector<File> vector = new Vector<File>();
    getSelectedFiles(root, vector);
    File[] array = new File[vector.size()];
    vector.toArray(array);
    return array;
  }

  private void getSelectedFiles(DefaultMutableTreeNode node,
      Vector<File> vector) {
    if (node instanceof CheckNode) {
      CheckNode checkNode = (CheckNode) node;
      if (checkNode.isSelected)
        vector.add(checkNode.getFile());
    }

    Enumeration enumeration = node.children();
    while (enumeration.hasMoreElements()) {
      getSelectedFiles(
          (DefaultMutableTreeNode) enumeration.nextElement(), vector);
    }
  }
}
TOP

Related Classes of ProjectTreeModel$CheckRenderer$TreeLabel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.