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