001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS.
026 */
027
028package org.opends.guitools.controlpanel.ui.renderer;
029
030import java.awt.Component;
031import java.awt.Font;
032
033import javax.swing.JTree;
034
035import org.opends.guitools.controlpanel.browser.BrowserController;
036import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
037import org.opends.guitools.controlpanel.ui.nodes.BasicNode;
038
039/**
040 * The renderer used to render the nodes in the LDAP entry browser.
041 *
042 */
043public class BrowserCellRenderer extends TreeCellRenderer {
044
045  private static final long serialVersionUID = 6756291700611741513L;
046  Font defaultFont = ColorAndFontConstants.treeFont;
047  Font italicFont = defaultFont.deriveFont(Font.ITALIC);
048  Font boldFont = defaultFont.deriveFont(Font.BOLD);
049  Font italicBoldFont = defaultFont.deriveFont(Font.ITALIC|Font.BOLD);
050  BasicNode inspectedNode;
051
052  /**
053   * Sets which is the inspected node.  This method simply marks the selected
054   * node in the tree so that it can have a different rendering.  This is
055   * useful for instance when the right panel has a list of entries to which
056   * the menu action apply, to make a difference between the selected node in
057   * the tree (to which the action in the main menu will not apply) and the
058   * selected nodes in the right pane.
059   * @param node the selected node.
060   */
061  public void setInspectedNode(BasicNode node) {
062    inspectedNode = node;
063  }
064
065  /** {@inheritDoc} */
066  public Component getTreeCellRendererComponent(
067      JTree tree,
068      Object value,
069      boolean isSelected,
070    boolean isExpanded,
071    boolean isLeaf,
072    int row,
073      boolean cellHasFocus)
074  {
075    BasicNode node = (BasicNode)value;
076    super.getTreeCellRendererComponent(tree, node, isSelected,
077        isExpanded, isLeaf,
078        row, cellHasFocus);
079
080    setIcon(node.getIcon());
081    setText(node.getDisplayName());
082
083    Font newFont = defaultFont;
084    int style = node.getFontStyle();
085    if (node == inspectedNode) {
086      style |= Font.BOLD;
087    }
088    if ((style & Font.ITALIC & Font.BOLD) != 0) {
089      newFont = italicBoldFont;
090    }
091    else if ((style & Font.ITALIC) != 0) {
092      newFont = italicFont;
093    }
094    else if ((style & Font.BOLD) != 0) {
095      newFont = boldFont;
096    }
097    else {
098      newFont = defaultFont;
099    }
100    setFont(newFont);
101    return this;
102  }
103
104
105  /**
106   * Returns the row height for the provided browser controller.
107   * @param controller the browser controller.
108   * @return the row height for the provided browser controller.
109   */
110  public static int calculateRowHeight(BrowserController controller) {
111    return 16;
112  }
113}