[Schmitzm-commits] r1574 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri May 6 18:08:53 CEST 2011


Author: mojays
Date: 2011-05-06 18:08:52 +0200 (Fri, 06 May 2011)
New Revision: 1574

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DialogManager.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
Log:
DialogManager: new method showInstanceFor(.)
SelectableJTable: new functionality for automatically growing columns

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DialogManager.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DialogManager.java	2011-05-05 16:19:57 UTC (rev 1573)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/DialogManager.java	2011-05-06 16:08:52 UTC (rev 1574)
@@ -158,6 +158,19 @@
 	}
 
 	/**
+	 * Determines a dialog instance from cache (or creates a new one) and
+	 * shows the dialog immediately in front.
+	 */
+	public DIALOG showInstanceFor(KEY key, Component owner, Object... constArgs) {
+	  DIALOG dialog = getInstanceFor(key, owner, constArgs);
+	  if ( dialog != null ) {
+	    dialog.setVisible(true);
+	    dialog.toFront();
+	  }
+	  return dialog;    
+	}
+	  
+	/**
 	 * Disposes all open instances and removes them from the cache.
 	 * 
 	 * @return <code>true</code> if at least one window has been disposed.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java	2011-05-05 16:19:57 UTC (rev 1573)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java	2011-05-06 16:08:52 UTC (rev 1574)
@@ -29,10 +29,13 @@
  ******************************************************************************/
 package de.schmitzm.swing;
 
+import java.awt.Component;
 import java.util.Vector;
 
 import javax.swing.JTable;
 import javax.swing.ListSelectionModel;
+import javax.swing.table.TableCellRenderer;
+import javax.swing.table.TableColumn;
 import javax.swing.table.TableColumnModel;
 import javax.swing.table.TableModel;
 
@@ -45,6 +48,11 @@
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
  */
 public class SelectableJTable extends JTable {
+  /** Indicates whether column width automatically grows with
+   *  data (Default: false). */ 
+  protected boolean autoGrowColumns = false;
+
+
   /**
    * Creates an empty table.
    */
@@ -129,4 +137,57 @@
       toggle = true;
     super.changeSelection(row, col, toggle, extend);
   }
+  
+  /**
+   * Sets whether column width automatically grows with data.
+   * The default is {@code false} because of performance reasons.
+   */
+  public void setAutoGrowColums(boolean autoGrow) {
+    boolean adjustColumns = !hasAutoGrowColumns() && autoGrow; 
+    this.autoGrowColumns = autoGrow;
+    if ( adjustColumns )
+      fitColumsAccordingToHeader();
+  }
+  
+  /**
+   * Checks whether column width automatically grows with data. 
+   */
+  public boolean hasAutoGrowColumns() {
+    return autoGrowColumns;
+  }
+
+  /**
+   * Sets the preferred column width according to the column headers.
+   * This method might be invoked when the table data changes completely. 
+   */
+  public void fitColumsAccordingToHeader() {
+    final TableCellRenderer renderer = getTableHeader().getDefaultRenderer();
+    for (int i = 0; i < getColumnCount(); ++i)
+      getColumnModel().getColumn(i).setPreferredWidth(
+          renderer.getTableCellRendererComponent(
+              this,
+              getModel().getColumnName(i),
+              false,
+              false,
+              0, i).getPreferredSize().width);
+  }
+
+  /**
+   * If {@link #setAutoGrowColums(boolean)} is set the preferred
+   * column width is increased automatically with larger data.
+   */
+  @Override
+  public Component prepareRenderer(final TableCellRenderer renderer, final int row, final int column) {
+    final Component prepareRenderer = super.prepareRenderer(renderer, row, column);
+    if ( !hasAutoGrowColumns() )
+      return prepareRenderer;
+    // if current cell is broader, extend the table column  
+    final TableColumn tableColumn = getColumnModel().getColumn(column);
+    tableColumn.setPreferredWidth(
+        Math.max(
+            prepareRenderer.getPreferredSize().width,
+            tableColumn.getPreferredWidth()
+        ));
+    return prepareRenderer;
+  }
 }



More information about the Schmitzm-commits mailing list