View Javadoc
1   /*
2    * #%L
3    * JAXX :: Runtime
4    * %%
5    * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package jaxx.runtime.swing.renderer;
23  
24  import org.nuiton.decorator.Decorator;
25  
26  import javax.swing.JTable;
27  import javax.swing.table.DefaultTableCellRenderer;
28  import javax.swing.table.TableCellRenderer;
29  import java.awt.Component;
30  import javax.swing.JComponent;
31  
32  /**
33   * A {@link TableCellRenderer} which compute text with the given {@link #decorator}
34   * and leave the hand to the {@link #delegate} to perform the visual renderer.
35   *
36   * @author Tony Chemit - chemit@codelutin.com
37   * @since 1.7.2 (was previously {@code jaxx.runtime.swing.DecoratorTableCellRenderer}).
38   */
39  public class DecoratorTableCellRenderer implements TableCellRenderer {
40  
41      /** Delegate cell renderer */
42      protected TableCellRenderer delegate;
43  
44      /** Decorator to produce text to render */
45      protected Decorator<?> decorator;
46  
47      protected boolean showToolTipText = false;
48  
49      public DecoratorTableCellRenderer(Decorator<?> decorator) {
50          this(new DefaultTableCellRenderer(), decorator, false);
51      }
52  
53      public DecoratorTableCellRenderer(Decorator<?> decorator, boolean showToolTipText) {
54          this(new DefaultTableCellRenderer(), decorator, showToolTipText);
55      }
56  
57      public DecoratorTableCellRenderer(TableCellRenderer delegate, Decorator<?> decorator) {
58          this(new DefaultTableCellRenderer(), decorator, false);
59      }
60  
61      public DecoratorTableCellRenderer(TableCellRenderer delegate,
62                                        Decorator<?> decorator,
63                                        boolean showToolTipText) {
64          this.delegate = delegate;
65          this.decorator = decorator;
66          this.showToolTipText = showToolTipText;
67      }
68  
69      @Override
70      public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasfocus, int row, int column) {
71          String text = null;
72          if (value != null) {
73              text = decorator.toString(value);
74          }
75          JComponent result =
76                  (JComponent) delegate.getTableCellRendererComponent(table,
77                                                                      text,
78                                                                      isSelected,
79                                                                      hasfocus,
80                                                                      row,
81                                                                      column);
82          if (showToolTipText) {
83              result.setToolTipText(text);
84          }
85          return result;
86      }
87  }