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