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