001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2008 Sun Microsystems, Inc.
015 * Portions Copyright 2015-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui.renderer;
018
019import java.awt.BorderLayout;
020import java.awt.Component;
021import java.awt.Font;
022
023import javax.swing.JComboBox;
024import javax.swing.JList;
025import javax.swing.JPanel;
026import javax.swing.JSeparator;
027import javax.swing.ListCellRenderer;
028import javax.swing.border.EmptyBorder;
029
030import org.opends.guitools.controlpanel.datamodel.CategorizedComboBoxElement;
031import org.opends.guitools.controlpanel.ui.StatusGenericPanel;
032
033/**
034 * A renderer used in the Control Panel that deals with
035 * CategorizedComboBoxElement elements.  It can be used to render JList and
036 * JComboBoxes.
037 */
038public class CustomListCellRenderer implements ListCellRenderer
039{
040  private ListCellRenderer defaultRenderer;
041  /** The separator used to render a non-selectable separator in the combo box. */
042  private Component separator;
043  /** The default font. */
044  protected Font defaultFont;
045  /** The category font. */
046  private Font categoryFont;
047
048  /**
049   * Constructor of a renderer to be used with a combo box.
050   * @param combo the combo box containing the elements to be rendered.
051   */
052  public CustomListCellRenderer(JComboBox combo)
053  {
054    this(combo.getRenderer());
055  }
056
057  /**
058   * Constructor of a renderer to be used with a list.
059   * @param list the list to be rendered.
060   */
061  public CustomListCellRenderer(JList list)
062  {
063    this(list.getCellRenderer());
064  }
065
066  private CustomListCellRenderer(ListCellRenderer defaultRenderer)
067  {
068    this.defaultRenderer = defaultRenderer;
069    JSeparator sep = new JSeparator();
070    separator = new JPanel(new BorderLayout());
071    ((JPanel)separator).setOpaque(false);
072    ((JPanel)separator).add(sep, BorderLayout.CENTER);
073    ((JPanel)separator).setBorder(new EmptyBorder(5, 3, 5, 3));
074  }
075
076  @Override
077  public Component getListCellRendererComponent(JList list, Object value,
078      int index, boolean isSelected, boolean cellHasFocus)
079  {
080    Component comp;
081    if (StatusGenericPanel.COMBO_SEPARATOR.equals(value))
082    {
083      return separator;
084    }
085    else if (value instanceof CategorizedComboBoxElement)
086    {
087      CategorizedComboBoxElement element = (CategorizedComboBoxElement)value;
088      String name = getStringValue(element);
089      boolean isRegular =
090        element.getType() == CategorizedComboBoxElement.Type.REGULAR;
091      if (isRegular)
092      {
093        name = "    "+name;
094      }
095      comp = defaultRenderer.getListCellRendererComponent(list, name, index,
096          isSelected && isRegular, cellHasFocus);
097      if (defaultFont == null)
098      {
099        defaultFont = comp.getFont();
100        categoryFont = defaultFont.deriveFont(Font.BOLD | Font.ITALIC);
101      }
102      if (element.getType() == CategorizedComboBoxElement.Type.REGULAR)
103      {
104        comp.setFont(defaultFont);
105      }
106      else
107      {
108        comp.setFont(categoryFont);
109      }
110    }
111    else
112    {
113      comp = defaultRenderer.getListCellRendererComponent(list, value, index,
114          isSelected, cellHasFocus);
115      if (defaultFont == null)
116      {
117        defaultFont = comp.getFont();
118        categoryFont = defaultFont.deriveFont(Font.BOLD | Font.ITALIC);
119      }
120      comp.setFont(defaultFont);
121    }
122    return comp;
123  }
124
125  /**
126   * Returns the String value for a given CategorizedComboBoxElement.
127   * @param desc the combo box element.
128   * @return the String value for a given CategorizedComboBoxElement.
129   */
130  protected String getStringValue(CategorizedComboBoxElement desc)
131  {
132    return String.valueOf(desc.getValue());
133  }
134}