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-2009 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027
028package org.opends.guitools.controlpanel.ui.renderer;
029
030import java.awt.Component;
031
032import javax.swing.BorderFactory;
033import javax.swing.JComponent;
034import javax.swing.JTable;
035import javax.swing.border.Border;
036import javax.swing.table.DefaultTableCellRenderer;
037import javax.swing.table.TableCellRenderer;
038
039import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
040import org.opends.guitools.controlpanel.ui.components.LabelWithHelpIcon;
041import org.opends.guitools.controlpanel.util.Utilities;
042import org.forgerock.i18n.LocalizableMessage;
043
044/**
045 * Class used to render the tables.
046 */
047public class CustomCellRenderer extends LabelWithHelpIcon
048implements TableCellRenderer
049{
050  private static final long serialVersionUID = -8604332267021523835L;
051  /**
052   * The border of the first column.
053   */
054  protected static final Border column0Border =
055    BorderFactory.createCompoundBorder(
056      BorderFactory.createMatteBorder(0, 1, 0, 0,
057          ColorAndFontConstants.gridColor),
058          BorderFactory.createEmptyBorder(4, 4, 4, 4));
059  /**
060   * The default border.
061   */
062  public static final Border defaultBorder =
063    BorderFactory.createEmptyBorder(4, 4, 4, 4);
064  private static Border defaultFocusBorder;
065
066  /**
067   * Default constructor.
068   */
069  public CustomCellRenderer()
070  {
071    super(LocalizableMessage.EMPTY, null);
072    setHelpIconVisible(false);
073    setFont(ColorAndFontConstants.tableFont);
074    setOpaque(true);
075    setBackground(ColorAndFontConstants.treeBackground);
076    setForeground(ColorAndFontConstants.treeForeground);
077  }
078
079  /** {@inheritDoc} */
080  public Component getTableCellRendererComponent(JTable table, Object value,
081      boolean isSelected, boolean hasFocus, int row, int column) {
082    if (value instanceof String)
083    {
084      String s = (String)value;
085      if (s.indexOf("<html>") == 0)
086      {
087        value = "<html>"+
088        Utilities.applyFont(s.substring(6), ColorAndFontConstants.tableFont);
089      }
090      setText((String)value);
091    }
092    else
093    {
094      setText(String.valueOf(value));
095    }
096
097    if (hasFocus)
098    {
099      setBorder(getDefaultFocusBorder(table, value, isSelected, row, column));
100    }
101    else if (column == 0)
102    {
103      setBorder(column0Border);
104    }
105    else
106    {
107      setBorder(defaultBorder);
108    }
109    return this;
110  }
111
112  /**
113   * Returns the border to be used for a given cell in a table.
114   * @param table the table.
115   * @param value the value to be rendered.
116   * @param isSelected whether the row is selected or not.
117   * @param row the row number of the cell.
118   * @param column the column number of the cell.
119   * @return the border to be used for a given cell in a table.
120   */
121  public static Border getDefaultFocusBorder(JTable table, Object value,
122      boolean isSelected, int row, int column)
123  {
124    if (defaultFocusBorder == null)
125    {
126      DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
127      JComponent comp = (JComponent)
128      renderer.getTableCellRendererComponent(table, value, isSelected,
129          true, row, column);
130      Border border = comp.getBorder();
131      if (border != null)
132      {
133        defaultFocusBorder = border;
134      }
135      else
136      {
137        defaultFocusBorder = defaultBorder;
138      }
139    }
140    return defaultFocusBorder;
141  }
142}