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 2016 ForgeRock AS.
016 */
017
018package org.opends.guitools.controlpanel.ui.renderer;
019
020import javax.swing.JComboBox;
021
022import org.opends.guitools.controlpanel.datamodel.CategorizedComboBoxElement;
023import org.opends.guitools.controlpanel.datamodel.IndexDescriptor;
024import org.opends.guitools.controlpanel.datamodel.VLVIndexDescriptor;
025import org.opends.guitools.controlpanel.util.Utilities;
026
027/**
028 * The renderer to be used to render CategorizedComboBoxElement objects whose
029 * values are AbstraceIndexDescriptor objects.  It can be used with combo
030 * boxes.
031 */
032public class IndexComboBoxCellRenderer extends CustomListCellRenderer
033{
034  /**
035   * Constructor of the index renderer.
036   * @param combo the combo whose contents will be rendered.
037   */
038  public IndexComboBoxCellRenderer(JComboBox combo)
039  {
040    super(combo);
041  }
042
043  /**
044   * Returns the String value for a given CategorizedComboBoxElement.
045   * @param desc the combo box element.
046   * @return the String value for a given CategorizedComboBoxElement.
047   */
048  @Override
049  protected String getStringValue(CategorizedComboBoxElement desc)
050  {
051    String v;
052    Object value = desc.getValue();
053    if (value instanceof IndexDescriptor)
054    {
055      v = ((IndexDescriptor)value).getName();
056    }
057    else if (value instanceof VLVIndexDescriptor)
058    {
059      v = Utilities.getVLVNameInCellRenderer((VLVIndexDescriptor)value);
060    }
061    else
062    {
063      v = super.getStringValue(desc);
064    }
065    return v;
066  }
067}