KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > viewer > tableviewer > AttributeValueCellRenderer


1 package com.ca.directory.jxplorer.viewer.tableviewer;
2
3 import java.awt.Component JavaDoc;
4 import javax.swing.JTable JavaDoc;
5 import javax.swing.table.*;
6 import java.awt.*;
7
8 import com.ca.commons.cbutil.CBIntText;
9
10     /*
11      * A minimal extension of DefaultTableCellRenderer allowing
12      * us to set some rows to bold font and others to normal font.
13      *
14      */

15                                                
16 public class AttributeValueCellRenderer extends DefaultTableCellRenderer
17
18 {
19     Font normalFont;
20     Font boldFont;
21     Font boldBlueFont;
22     
23     /**
24      * constructor calls the super class constructor, and
25      * initialises the fonts.
26      */

27      
28     public AttributeValueCellRenderer()
29     {
30         super();
31         normalFont = this.getFont();
32         boldFont = normalFont.deriveFont(java.awt.Font.BOLD);
33         boldBlueFont = normalFont.deriveFont(java.awt.Font.BOLD);
34     }
35
36     /**
37      * Intercepts byte array/binary attribute values, and substitutes the
38      * string '(non string data)' for display to the user...
39      */

40      
41     protected void setValue(Object JavaDoc value) // I wonder what the performance hit here is...
42
{
43         if (value instanceof AttributeValue)
44         {
45             if (((AttributeValue)value).isBinary() && (((AttributeValue)value).isEmpty()==false))
46                 value = CBIntText.get("(non string data)");
47             else
48             {
49                 // truncate long strings for initial display
50
String JavaDoc stringVal = value.toString();
51                 if (stringVal.length() > 100)
52                     value = truncateLongString(stringVal);
53                     
54                 if (stringVal.substring(0,6).toLowerCase().startsWith("<html>"))
55                     value = " " + stringVal;
56             }
57         }
58         super.setValue(value);
59     }
60
61     public String JavaDoc truncateLongString(String JavaDoc truncateMe)
62     {
63         return truncateMe.substring(0, 100) + "...";
64     }
65
66     /**
67      * intercepts the super classes returned component, and sets the
68      * font on it before returning.
69      */

70      
71     public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
72         boolean isSelected, boolean hasFocus, int row, int column)
73     {
74         if (value instanceof AttributeValue)
75         {
76             
77             AttributeValue attVal = (AttributeValue)value;
78             String JavaDoc attString = attVal.toString();
79             if (attString.length() > 100)
80                 attString = truncateLongString(attString);
81             
82             Component JavaDoc c = super.getTableCellRendererComponent(table, attString, isSelected, hasFocus, row, column);
83             
84             if (attVal.isNaming())
85             {
86                 c.setForeground(((isSelected)?Color.white:Color.blue));
87                 c.setFont(boldFont);
88             }
89             else
90             {
91                 c.setForeground(((isSelected)?Color.white:Color.black));
92                 c.setFont(normalFont);
93             }
94             return c;
95             
96         }
97         else
98             return super.getTableCellRendererComponent(table, new String JavaDoc("error"), isSelected, hasFocus, row, column);
99     }
100 }
Popular Tags