View Javadoc

1   // %318955973:hoplugins.commons.ui.renderer%
2   package hoplugins.commons.ui.renderer;
3   
4   import java.awt.Component;
5   import java.awt.Insets;
6   
7   import javax.swing.JTable;
8   import javax.swing.JTextArea;
9   import javax.swing.table.TableCellRenderer;
10  
11  
12  /***
13   * TextArea table cell renderer. Allows multiple line table cells.
14   *
15   * @author <a href="mailto:kenmooda@users.sourceforge.net">Tommi Rautava</a>
16   */
17  public class TextAreaCellRenderer extends JTextArea implements TableCellRenderer {
18      //~ Constructors -------------------------------------------------------------------------------
19  
20      /***
21       * Constructor
22       */
23      public TextAreaCellRenderer() {
24          super();
25          this.setLineWrap(true);
26          this.setWrapStyleWord(true);
27          this.setAlignmentY(TextAreaCellRenderer.CENTER_ALIGNMENT);
28          this.setMargin(new Insets(0, 1, 0, 1));
29      }
30  
31      //~ Methods ------------------------------------------------------------------------------------
32  
33      /*
34       * (non-Javadoc)
35       *
36       * @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
37       *      java.lang.Object, boolean, boolean, int, int)
38       */
39      public Component getTableCellRendererComponent(JTable table, Object object, boolean isSelected,
40                                                     boolean hasFocus, int row, int column) {
41          // Set text.
42          this.setText((String) object);
43  
44          int height_wanted = (int) this.getPreferredSize().getHeight();
45  
46          if (height_wanted != table.getRowHeight(row)) {
47              table.setRowHeight(row, height_wanted);
48          }
49  
50          // Set background color.
51          if (isSelected) {
52              this.setBackground(table.getSelectionBackground());
53          } else {
54              this.setBackground(table.getBackground());
55          }
56  
57          return this;
58      }
59  }