[Schmitzm-commits] r2149 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . table

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 28 17:55:35 CET 2012


Author: mojays
Date: 2012-11-28 17:55:35 +0100 (Wed, 28 Nov 2012)
New Revision: 2149

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/HighlightCellRenderer.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
Log:
HighlightCellRenderer: moved from WIME to SCHMITZM
SwingUtil: handle piped renderer in .getRendererdTableCellValue(.)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-11-28 16:28:28 UTC (rev 2148)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-11-28 16:55:35 UTC (rev 2149)
@@ -118,6 +118,7 @@
 import javax.swing.UIDefaults;
 import javax.swing.UIManager;
 import javax.swing.plaf.FontUIResource;
+import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.TableCellEditor;
 import javax.swing.table.TableCellRenderer;
 import javax.swing.text.JTextComponent;
@@ -143,6 +144,7 @@
 import de.schmitzm.swing.input.ManualInputOption;
 import de.schmitzm.swing.input.MultipleOptionPane;
 import de.schmitzm.swing.table.ComponentRenderer;
+import de.schmitzm.swing.table.HighlightCellRenderer;
 
 /**
  * Diese Klasse beinhaltet statische Hilfsfunktionen fuer das Arbeiten mit
@@ -2652,6 +2654,25 @@
       cb.setDisabledIcon( SwingUtil.scaleImage(SwingUtil.ICON_CHECKBOX_DISABLED_UNCHECKED, scaleFactor) );
       cb.setDisabledSelectedIcon( SwingUtil.scaleImage(SwingUtil.ICON_CHECKBOX_DISABLED_CHECKED, scaleFactor) );
     }
+    
+    /**
+     * Returns a table cell value as rendered (if {@link DefaultTableCellRenderer} is
+     * used).
+     * @param table a table
+     * @param row row index
+     * @param col column index
+     * @return {@code JTable.getValueAt(int, int).toString()} if no {@link DefaultTableCellRenderer}
+     *         is used for cell
+     */
+    public static String getRendererdTableCellValue(JTable table, int row, int col) {
+      TableCellRenderer rend = table.getCellRenderer(row, col);
+      if ( rend instanceof HighlightCellRenderer )
+        rend = ((HighlightCellRenderer)rend).getPipedRenderer();
+      if ( rend instanceof DefaultTableCellRenderer )
+        return ((DefaultTableCellRenderer)rend).getText();
+      Object cellValue = table.getValueAt(row, col);
+      return cellValue != null ? cellValue.toString() : null; 
+    }
 
 
     /**

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/HighlightCellRenderer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/HighlightCellRenderer.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/HighlightCellRenderer.java	2012-11-28 16:55:35 UTC (rev 2149)
@@ -0,0 +1,205 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.swing.table;
+
+import java.awt.Color;
+import java.awt.Component;
+
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableCellRenderer;
+import javax.swing.table.TableCellRenderer;
+
+/**
+ * Pipes another {@link TableCellRenderer} and sets a highlighting background color
+ * to its rendering component, if the cell contains a specific pattern.
+ * @author Martin O.J. Schmitz
+ */
+public class HighlightCellRenderer implements TableCellRenderer {
+  protected TableCellRenderer pipedRenderer = null;
+  protected String pattern = null;
+  protected boolean exact = false;
+  protected Color highlight = Color.YELLOW;
+  private   Color defaultBackground = null;
+  
+  /**
+   * Creates a new renderer with not-exact pattern check and yellow
+   * highlight color. A pattern is not yes set.
+   */
+  public HighlightCellRenderer() {
+    this(false, Color.YELLOW);
+  }
+
+  /**
+   * Creates a new renderer. A pattern is not yes set.
+   * @param exact indicated whether pattern check is exact (TRUE = complete cell value
+   *              must match; FALSE = cell value must contain pattern); check always
+   *              is case-insensitive!
+   * @param highlight highlight color if cell matches pattern
+   */
+  public HighlightCellRenderer(boolean exact, Color highlight) {
+    this((String)null, exact, highlight);
+  }
+
+
+  /**
+   * Creates a new renderer. 
+   * @param pattern Pattern to check the cell with
+   * @param exact indicated whether pattern check is exact (TRUE = complete cell value
+   *              must match; FALSE = cell value must contain pattern); check always
+   *              is case-insensitive!
+   * @param highlight highlight color if cell matches pattern
+   */
+  public HighlightCellRenderer(String pattern, boolean exact, Color highlight) {
+    this(null,pattern,exact,highlight);
+  }
+
+  /**
+   * Creates a new renderer with not-exact pattern check and yellow
+   * highlight color. A pattern is not yes set. The piped renderer
+   * is used to create the cell component.
+   */
+  public HighlightCellRenderer(TableCellRenderer pipedRenderer) {
+    this(pipedRenderer, false, Color.YELLOW);
+  }
+
+  /**
+   * Creates a new renderer. A pattern is not yes set. The piped renderer
+   * is used to create the cell component.
+   * @param exact indicated whether pattern check is exact (TRUE = complete cell value
+   *              must match; FALSE = cell value must contain pattern); check always
+   *              is case-insensitive!
+   * @param highlight highlight color if cell matches pattern
+   */
+  public HighlightCellRenderer(TableCellRenderer pipedRenderer, boolean exact, Color highlight) {
+    this(pipedRenderer, null, exact, highlight);
+  }
+
+  /**
+   * Creates a new renderer. The piped renderer is used to create the cell component.
+   * @param pattern Pattern to check the cell with
+   * @param exact indicated whether pattern check is exact (TRUE = complete cell value
+   *              must match; FALSE = cell value must contain pattern); check always
+   *              is case-insensitive!
+   * @param highlight highlight color if cell matches pattern
+   */
+  public HighlightCellRenderer(TableCellRenderer pipedRenderer, String pattern, boolean exact, Color highlight) {
+    if ( pipedRenderer == null )
+      pipedRenderer = new DefaultTableCellRenderer();
+    this.pipedRenderer = pipedRenderer;
+    setPattern(pattern);
+    setExact(exact);
+    setHighlight(highlight);
+  }
+
+  /**
+   * Creates the rendering component for a cell. Depending of the cell value, the
+   * background color is set to highlight matched pattern.
+   */
+  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
+    Component comp = pipedRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
+    // Ignore selected rows!
+    if ( isSelected )
+      return comp;
+
+    // remember the default background for non-highlighted
+    // cells
+    if ( defaultBackground == null )
+      defaultBackground = comp.getBackground();
+    
+    // If available, compare pattern with the rendered text (instead of "value")
+    // to include e.g. leading zeros in comparation!
+    Object cellValue = value;
+    if ( comp instanceof DefaultTableCellRenderer )
+      cellValue = ((DefaultTableCellRenderer)comp).getText();
+ 
+    comp.setBackground(defaultBackground);
+    if ( cellValue != null && getPattern() != null && !"".equals(getPattern())) {
+      final String valueStr = cellValue.toString();
+      final String pattern = getPattern();
+      if ( isExact() && valueStr.equalsIgnoreCase(pattern) ||
+           !isExact() && valueStr.toUpperCase().contains(pattern.toUpperCase()) ) {
+        comp.setBackground( getHighlight() );
+      }
+    }
+    return comp;
+  }
+
+  /**
+   * Sets the pattern to match the cell(s) with.
+   */
+  public void setPattern(String pattern) {
+    this.pattern = pattern;
+  }
+  
+  /**
+   * Returns the pattern the cell(s) are matched.
+   */
+  public String getPattern() {
+    return pattern;
+  }
+  
+  /**
+   * Sets whether pattern match is exact (complete cell value has to
+   * match the pattern).
+   */
+  public void setExact(boolean exact) {
+    this.exact = exact;
+  } 
+
+  /**
+   * Returns whether pattern match is exact (complete cell value has to
+   * match the pattern).
+   */
+  public boolean isExact() {
+    return exact;
+  }
+
+  /**
+   * Sets the highlight color.
+   */
+  public void setHighlight(Color color) {
+    this.highlight = color;
+  }
+  
+  /**
+   * Returns the highlight color.
+   */
+  public Color getHighlight() {
+    return highlight;
+  }
+  
+  /**
+   * Returns the piped {@link TableCellRenderer}
+   */
+  public TableCellRenderer getPipedRenderer() {
+    return pipedRenderer;
+  }
+}
+



More information about the Schmitzm-commits mailing list