/plugins/LogViewer/tags/rel-0-8/src/java/logviewer/LogTableCellRenderer.java

# · Java · 207 lines · 111 code · 27 blank · 69 comment · 11 complexity · 943d17b75b4cbca224892d0f301d8efe MD5 · raw file

  1. package logviewer;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import javax.swing.text.BadLocationException;
  5. import javax.swing.text.Document;
  6. import javax.swing.*;
  7. import javax.swing.table.TableCellRenderer;
  8. import java.util.regex.*;
  9. /**
  10. * Cell renderer that uses a JTextArea as the renderer rather than a JLabel.
  11. * This allows text in individual cells to wrap, which a label won't do without
  12. * some work.
  13. *
  14. * The validate, revalidate, repaint, and firePropertyChange methods have been
  15. * overridden as no-ops for performance reasons.
  16. *
  17. * @version $Revision: 1156 $
  18. */
  19. public class LogTableCellRenderer extends JTextArea implements TableCellRenderer {
  20. private String highlightRegex = null;
  21. private Pattern pattern = null;
  22. private Matcher matcher = null;
  23. public LogTableCellRenderer() {
  24. setLineWrap( true );
  25. setWrapStyleWord( true );
  26. }
  27. public void setWordWrap( boolean wrap ) {
  28. setLineWrap( wrap );
  29. setWrapStyleWord( wrap );
  30. }
  31. public void toggleWordWrap() {
  32. setLineWrap( !getLineWrap() );
  33. setWrapStyleWord( !getWrapStyleWord() );
  34. }
  35. public boolean getWordWrap() {
  36. return getLineWrap();
  37. }
  38. /**
  39. * Overridden as no-op for performance.
  40. */
  41. public void validate() {
  42. // no-op
  43. }
  44. /**
  45. * Overridden as no-op for performance.
  46. */
  47. public void revalidate() {
  48. // no-op
  49. }
  50. /**
  51. * Overridden as no-op for performance.
  52. */
  53. public void repaint() {
  54. // no-op
  55. }
  56. /**
  57. * Overridden as no-op for performance.
  58. */
  59. public void repaint( int x,
  60. int y,
  61. int width,
  62. int height ) {
  63. // no-op
  64. }
  65. /**
  66. * Overridden as no-op for performance.
  67. */
  68. public void repaint( long tm,
  69. int x,
  70. int y,
  71. int width,
  72. int height ) {
  73. // no-op
  74. }
  75. /**
  76. * Overridden as no-op for performance.
  77. */
  78. public void firePropertyChange( String propertyName,
  79. byte oldValue,
  80. byte newValue ) {
  81. // no-op
  82. }
  83. /**
  84. * Overridden as no-op for performance.
  85. */
  86. public void firePropertyChange( String propertyName,
  87. char oldValue,
  88. char newValue ) {
  89. // no-op
  90. }
  91. /**
  92. * Overridden as no-op for performance.
  93. */
  94. public void firePropertyChange( String propertyName,
  95. short oldValue,
  96. short newValue ) {
  97. // no-op
  98. }
  99. /**
  100. * Overridden as no-op for performance.
  101. */
  102. public void firePropertyChange( String propertyName,
  103. int oldValue,
  104. int newValue ) {
  105. // no-op
  106. }
  107. /**
  108. * Overridden as no-op for performance.
  109. */
  110. public void firePropertyChange( String propertyName,
  111. long oldValue,
  112. long newValue ) {
  113. // no-op
  114. }
  115. /**
  116. * Overridden as no-op for performance.
  117. */
  118. public void firePropertyChange( String propertyName,
  119. float oldValue,
  120. float newValue ) {
  121. // no-op
  122. }
  123. /**
  124. * Overridden as no-op for performance.
  125. */
  126. public void firePropertyChange( String propertyName,
  127. double oldValue,
  128. double newValue ) {
  129. // no-op
  130. }
  131. /**
  132. * Overridden as almost a no-op for performance. "lineWrap" and "wrapStyleWord"
  133. * properties are handled, all others are ignored.
  134. */
  135. public void firePropertyChange( String propertyName,
  136. boolean oldValue,
  137. boolean newValue ) {
  138. if ( "lineWrap".equals( propertyName ) || "wrapStyleWord".equals( propertyName ) )
  139. super.firePropertyChange( propertyName, oldValue, newValue );
  140. }
  141. /**
  142. * Set a regular expression to identify a cell to be highlighted. This is used
  143. * at least by the "Find" functionality, cells that are found are highlighed.
  144. */
  145. public void setHighlightRegex( String regex ) {
  146. highlightRegex = regex;
  147. pattern = Pattern.compile( highlightRegex, Pattern.DOTALL );
  148. }
  149. /**
  150. * @return true if this cell has text that should be highlighted.
  151. */
  152. private boolean shouldHighlight( ) {
  153. if (pattern != null)
  154. matcher = pattern.matcher( getText() );
  155. if ( matcher != null ) {
  156. return matcher.find();
  157. }
  158. return false;
  159. }
  160. public Component getTableCellRendererComponent( JTable table, Object
  161. value, boolean isSelected, boolean hasFocus, int row, int column ) {
  162. if ( value == null )
  163. value = "";
  164. setText( value.toString() );
  165. setSize( table.getColumnModel().getColumn( column ).getWidth(), getPreferredSize().height );
  166. if ( table.getRowHeight( row ) < getPreferredSize().height ) {
  167. table.setRowHeight( row, getPreferredSize().height );
  168. }
  169. boolean shouldHighlight = shouldHighlight();
  170. if ( isSelected ) {
  171. setForeground( shouldHighlight ? Color.YELLOW : table.getSelectionForeground() );
  172. setBackground( table.getSelectionBackground() );
  173. }
  174. else {
  175. setForeground( table.getForeground() );
  176. setBackground( shouldHighlight ? Color.YELLOW : table.getBackground() );
  177. }
  178. return this;
  179. }
  180. }