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-2015 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.Syntax;
027import org.forgerock.opendj.ldap.schema.MatchingRule;
028import org.forgerock.opendj.ldap.schema.AttributeUsage;
029import org.opends.server.types.CommonSchemaElements;
030import org.forgerock.opendj.ldap.schema.ObjectClassType;
031
032/**
033 * The cell renderer to be used to render schema elements in a combo box.
034 */
035public class SchemaElementComboBoxCellRenderer extends CustomListCellRenderer
036{
037  /**
038   * Constructor of the cell renderer.
039   * @param combo the combo box containing the elements to be rendered.
040   */
041  public SchemaElementComboBoxCellRenderer(JComboBox combo)
042  {
043    super(combo);
044  }
045
046  /**
047   * Constructor of the cell renderer.
048   * @param list the list containing the elements to be rendered.
049   */
050  public SchemaElementComboBoxCellRenderer(JList list)
051  {
052    super(list);
053  }
054
055  /** {@inheritDoc} */
056  public Component getListCellRendererComponent(JList list, Object value,
057      int index, boolean isSelected, boolean cellHasFocus)
058  {
059    if (value instanceof Syntax)
060    {
061      String syntaxName = ((Syntax)value).getName();
062      if (syntaxName == null)
063      {
064        value = ((Syntax)value).getOID();
065      }
066      else
067      {
068        value = syntaxName;
069      }
070    }
071    else if (value instanceof CommonSchemaElements)
072    {
073      value = ((CommonSchemaElements)value).getNameOrOID();
074    }
075    else if (value instanceof MatchingRule)
076    {
077      value = ((MatchingRule)value).getNameOrOID();
078    }
079    else if (value instanceof AttributeUsage)
080    {
081      boolean isOperational = ((AttributeUsage)value).isOperational();
082      if (isOperational)
083      {
084        value = INFO_CTRL_PANEL_ATTRIBUTE_USAGE_OPERATIONAL.get(
085            value.toString());
086      }
087    }
088    else if (value instanceof ObjectClassType)
089    {
090      switch ((ObjectClassType)value)
091      {
092      case AUXILIARY:
093        value = INFO_CTRL_PANEL_OBJECTCLASS_AUXILIARY_LABEL.get().toString();
094        break;
095      case STRUCTURAL:
096        value = INFO_CTRL_PANEL_OBJECTCLASS_STRUCTURAL_LABEL.get().toString();
097        break;
098      case ABSTRACT:
099        value = INFO_CTRL_PANEL_OBJECTCLASS_ABSTRACT_LABEL.get().toString();
100        break;
101      }
102    }
103    return super.getListCellRendererComponent(
104        list, value, index, isSelected, cellHasFocus);
105  }
106}