示例#1
0
    /* (non-Javadoc)
     * @see javax.swing.tree.DefaultTreeCellRenderer#getTreeCellRendererComponent(javax.swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean)
     */
    public Component getTreeCellRendererComponent(
        JTree tree,
        Object value,
        boolean sel,
        boolean expanded,
        boolean leaf,
        int row,
        boolean hasFocus) {

      super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);

      Folder f = getFolderForNode(value);

      if (f != null) {
        String folderName = f.getTitle();

        if (folderName == null || folderName.length() == 0) {
          folderName = f.getDataItemName();
        }

        if (f.getFolderIcon() != null) {
          setIcon(f.getFolderIcon());
        } else {
          setText(folderName);
        }
        setToolTipText(f.getTooltip());
      } else {
        setToolTipText(null); // no tool tip
      }

      return this;
    }
示例#2
0
  /**
   * Create a new JTree using the root node as the root of the tree
   *
   * @return the newly created JTree
   */
  private JTree getFolderTree() {

    if (rootNode == null) {
      throw new RuntimeException(Messages.getString(BUNDLE_NAME, "RootFolder.10")); // $NON-NLS-1$
    }

    final JTree tree = new JTree(rootNode);

    // this tree is not editable
    tree.setEditable(false);

    // setup some default properties
    tree.putClientProperty("JTree.lineStyle", "Angled"); // $NON-NLS-1$ //$NON-NLS-2$
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.setShowsRootHandles(showHandles);

    // set the cell renderer to display text and/or images
    tree.setCellRenderer(new FolderIconRenderer());

    /* when the user clicks on a tree node then load the Folder for the node
     * that is clicked
     */
    tree.addTreeSelectionListener(
        new TreeSelectionListener() {

          public void valueChanged(TreeSelectionEvent e) {
            @SuppressWarnings("unused")
            TreePath treePath = e.getNewLeadSelectionPath();
            DefaultMutableTreeNode node =
                (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

            if (node == null) {
              return;
            }

            Object nodeInfo = node.getUserObject();
            loadNodeObject(nodeInfo);
          }
        });

    // the maximum hieght of each row in the JTree
    int maxRowHeight = -1;

    /* determine the maximum height of the nodes in the JTree by getting the
     * maximum hieght of each Folder node in the tree.  It is helpful if using images
     * for Folder nodes that all images are the same height.
     */
    Enumeration<TreeNode> e = rootNode.breadthFirstEnumeration();
    DefaultMutableTreeNode node;

    Folder f = getFolderForNode(rootNode);

    // determine the maximum height of the root Folder node
    if (f != null) {
      ImageIcon icon;
      if ((icon = f.getFolderIcon()) != null) {
        maxRowHeight = Math.max(maxRowHeight, icon.getIconHeight());
      }
    }

    // determine the maximum height of all child nodes
    for (; e.hasMoreElements(); ) {
      node = (DefaultMutableTreeNode) e.nextElement();
      f = getFolderForNode(node);

      if (f != null) {
        ImageIcon icon;
        if ((icon = f.getFolderIcon()) != null) {
          maxRowHeight = Math.max(maxRowHeight, icon.getIconHeight());
        }
      }
    }

    // set the rowheight of the JTree to the maximum of the current height and the
    // maximum Folder node height
    int rowHeight = tree.getRowHeight();
    if (maxRowHeight > rowHeight) {
      tree.setRowHeight(maxRowHeight);
    }

    return tree;
  }