View Javadoc
1   package net.logAnalyzer.gui.messages.cellrenderers;
2   
3   import java.awt.Component;
4   import java.awt.Font;
5   
6   import javax.swing.JLabel;
7   import javax.swing.JTable;
8   import javax.swing.UIManager;
9   import javax.swing.border.Border;
10  import javax.swing.border.EmptyBorder;
11  import javax.swing.table.TableCellRenderer;
12  
13  import net.logAnalyzer.converters.DoubleConverter;
14  import net.logAnalyzer.converters.LAConverter;
15  
16  /***
17   * This class defines a cell renderer displaying a value formatted by a
18   * {@link LAConverter}.
19   * 
20   * @author Karim REFEYTON
21   * @version 0.1
22   */
23  public class DefaultCellRenderer extends JLabel implements TableCellRenderer {
24      private static final long serialVersionUID = 1L;
25  
26      /***
27       * Default border.
28       */
29      public static final Border NOFOCUS_BORDER = new EmptyBorder(1, 1, 1, 1);
30  
31      /***
32       * Converter used to format cell value.
33       */
34      private LAConverter logconverter;
35  
36      /***
37       * All converters of the log.
38       */
39      private LAConverter[] logconverters;
40  
41      /***
42       * Constructs a new renderer. With the following style properties :
43       * <ul>
44       * <li>font is {@link Font#PLAIN}</li>
45       * <li>{@link javax.swing.JComponent#setOpaque(boolean)}is <tt>true</tt></li>
46       * <li>{@link JLabel#setHorizontalAlignment(int)}is RIGHT if converter is
47       * a {@link DoubleConverter}.</li>
48       * </ul>
49       * 
50       * @param logconverter
51       *            Column to render.
52       * @param logconverters
53       *            All columns.
54       */
55      public DefaultCellRenderer(LAConverter logconverter,
56              LAConverter[] logconverters) {
57          super();
58          this.logconverter = logconverter;
59          this.logconverters = logconverters;
60          Font font = this.getFont();
61          setFont(font.deriveFont(Font.PLAIN));
62          setOpaque(true);
63          if (logconverter instanceof DoubleConverter) {
64              setHorizontalAlignment(DefaultCellRenderer.RIGHT);
65          }
66      }
67  
68      /***
69       * Sets text, height, borders and colors of the cell regarding to value,
70       * selection and focus.
71       * 
72       * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
73       *      java.lang.Object, boolean, boolean, int, int)
74       */
75      public Component getTableCellRendererComponent(JTable table, Object value,
76              boolean isSelected, boolean hasFocus, int row, int column) {
77          // Sets text content
78          if (value != null && !(value instanceof String)) {
79              setText(logconverter.toString(value));
80          } else if (value != null) {
81              // Cut at the first newline character
82              String string = (String) value;
83              int posLF = string.indexOf('\n');
84              if (posLF >= 0) {
85                  string = string.substring(0, posLF) + "ΒΆ";
86              }
87              // Sets the new value
88              setText(string);
89          } else {
90              setText("");
91          }
92          // Shows the selection and focus
93          if (isSelected) {
94              setForeground(table.getSelectionForeground());
95              setBackground(table.getSelectionBackground());
96          } else {
97              setForeground(table.getForeground());
98              setBackground(table.getBackground());
99          }
100         if (hasFocus) {
101             setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
102             if (table.isCellEditable(row, column)) {
103                 setForeground(UIManager.getColor("Table.focusCellForeground"));
104                 setBackground(UIManager.getColor("Table.focusCellBackground"));
105             }
106         } else {
107             setBorder(NOFOCUS_BORDER);
108         }
109         return this;
110     }
111 
112     /***
113      * Returns the log converter rendered by the renderer.
114      * 
115      * @return Log converter.
116      */
117     protected LAConverter getConverter() {
118         return this.logconverter;
119     }
120 
121     /***
122      * Returns all log converters known by the renderer.
123      * 
124      * @return Log converter.
125      */
126     protected LAConverter[] getConverters() {
127         return this.logconverters;
128     }
129 }