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.BorderLayout;
031import java.awt.Component;
032import java.awt.Font;
033
034import javax.swing.JComboBox;
035import javax.swing.JList;
036import javax.swing.JPanel;
037import javax.swing.JSeparator;
038import javax.swing.ListCellRenderer;
039import javax.swing.border.EmptyBorder;
040
041import org.opends.guitools.controlpanel.datamodel.CategorizedComboBoxElement;
042import org.opends.guitools.controlpanel.ui.StatusGenericPanel;
043
044/**
045 * A renderer used in the Control Panel that deals with
046 * CategorizedComboBoxElement elements.  It can be used to render JList and
047 * JComboBoxes.
048 *
049 */
050public class CustomListCellRenderer implements ListCellRenderer
051{
052  private ListCellRenderer defaultRenderer;
053
054  /**
055   * The separator used to render a non-selectable separator in the combo box.
056   */
057  protected Component separator;
058
059  /**
060   * The default font.
061   */
062  protected Font defaultFont;
063
064  /**
065   * The category font.
066   */
067  protected Font categoryFont;
068
069  /**
070   * Constructor of a renderer to be used with a combo box.
071   * @param combo the combo box containing the elements to be rendered.
072   */
073  public CustomListCellRenderer(JComboBox combo)
074  {
075    this(combo.getRenderer());
076  }
077
078  /**
079   * Constructor of a renderer to be used with a list.
080   * @param list the list to be rendered.
081   */
082  public CustomListCellRenderer(JList list)
083  {
084    this(list.getCellRenderer());
085  }
086
087  private CustomListCellRenderer(ListCellRenderer defaultRenderer)
088  {
089    this.defaultRenderer = defaultRenderer;
090    JSeparator sep = new JSeparator();
091    separator = new JPanel(new BorderLayout());
092    ((JPanel)separator).setOpaque(false);
093    ((JPanel)separator).add(sep, BorderLayout.CENTER);
094    ((JPanel)separator).setBorder(new EmptyBorder(5, 3, 5, 3));
095  }
096
097  /** {@inheritDoc} */
098  public Component getListCellRendererComponent(JList list, Object value,
099      int index, boolean isSelected, boolean cellHasFocus)
100  {
101    Component comp;
102    if (StatusGenericPanel.COMBO_SEPARATOR.equals(value))
103    {
104      return separator;
105    }
106    else if (value instanceof CategorizedComboBoxElement)
107    {
108      CategorizedComboBoxElement element = (CategorizedComboBoxElement)value;
109      String name = getStringValue(element);
110      boolean isRegular =
111        element.getType() == CategorizedComboBoxElement.Type.REGULAR;
112      if (isRegular)
113      {
114        name = "    "+name;
115      }
116      comp = defaultRenderer.getListCellRendererComponent(list, name, index,
117          isSelected && isRegular, cellHasFocus);
118      if (defaultFont == null)
119      {
120        defaultFont = comp.getFont();
121        categoryFont = defaultFont.deriveFont(Font.BOLD | Font.ITALIC);
122      }
123      if (element.getType() == CategorizedComboBoxElement.Type.REGULAR)
124      {
125        comp.setFont(defaultFont);
126      }
127      else
128      {
129        comp.setFont(categoryFont);
130      }
131    }
132    else
133    {
134      comp = defaultRenderer.getListCellRendererComponent(list, value, index,
135          isSelected, cellHasFocus);
136      if (defaultFont == null)
137      {
138        defaultFont = comp.getFont();
139        categoryFont = defaultFont.deriveFont(Font.BOLD | Font.ITALIC);
140      }
141      comp.setFont(defaultFont);
142    }
143    return comp;
144  }
145
146  /**
147   * Returns the String value for a given CategorizedComboBoxElement.
148   * @param desc the combo box element.
149   * @return the String value for a given CategorizedComboBoxElement.
150   */
151  protected String getStringValue(CategorizedComboBoxElement desc)
152  {
153    return String.valueOf(desc.getValue());
154  }
155}