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