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-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2014-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui.renderer;
018
019import static org.opends.messages.AdminToolMessages.*;
020
021import java.awt.Component;
022
023import javax.swing.JComboBox;
024import javax.swing.JList;
025
026import org.forgerock.opendj.ldap.schema.AttributeType;
027import org.forgerock.opendj.ldap.schema.AttributeUsage;
028import org.forgerock.opendj.ldap.schema.MatchingRule;
029import org.forgerock.opendj.ldap.schema.ObjectClass;
030import org.forgerock.opendj.ldap.schema.ObjectClassType;
031import org.forgerock.opendj.ldap.schema.Syntax;
032
033/** The cell renderer to be used to render schema elements in a combo box. */
034public class SchemaElementComboBoxCellRenderer extends CustomListCellRenderer
035{
036  /**
037   * Constructor of the cell renderer.
038   * @param combo the combo box containing the elements to be rendered.
039   */
040  public SchemaElementComboBoxCellRenderer(JComboBox combo)
041  {
042    super(combo);
043  }
044
045  /**
046   * Constructor of the cell renderer.
047   * @param list the list containing the elements to be rendered.
048   */
049  public SchemaElementComboBoxCellRenderer(JList list)
050  {
051    super(list);
052  }
053
054  @Override
055  public Component getListCellRendererComponent(JList list, Object value,
056      int index, boolean isSelected, boolean cellHasFocus)
057  {
058    return super.getListCellRendererComponent(
059        list, getLabel(value), index, isSelected, cellHasFocus);
060  }
061
062  private Object getLabel(Object value)
063  {
064    if (value instanceof Syntax)
065    {
066      Syntax syntax = (Syntax) value;
067      return syntax.getName() != null ? syntax.getName() : syntax.getOID();
068    }
069    else if (value instanceof AttributeType)
070    {
071      return ((AttributeType) value).getNameOrOID();
072    }
073    else if (value instanceof ObjectClass)
074    {
075      return ((ObjectClass) value).getNameOrOID();
076    }
077    else if (value instanceof MatchingRule)
078    {
079      return ((MatchingRule) value).getNameOrOID();
080    }
081    else if (value instanceof AttributeUsage)
082    {
083      boolean isOperational = ((AttributeUsage)value).isOperational();
084      if (isOperational)
085      {
086        return INFO_CTRL_PANEL_ATTRIBUTE_USAGE_OPERATIONAL.get(value.toString());
087      }
088    }
089    else if (value instanceof ObjectClassType)
090    {
091      switch ((ObjectClassType)value)
092      {
093      case AUXILIARY:
094        return INFO_CTRL_PANEL_OBJECTCLASS_AUXILIARY_LABEL.get().toString();
095      case STRUCTURAL:
096        return INFO_CTRL_PANEL_OBJECTCLASS_STRUCTURAL_LABEL.get().toString();
097      case ABSTRACT:
098        return INFO_CTRL_PANEL_OBJECTCLASS_ABSTRACT_LABEL.get().toString();
099      }
100    }
101    return value;
102  }
103}