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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Jun 8 15:38:00 CEST 2011


Author: mojays
Date: 2011-06-08 15:37:58 +0200 (Wed, 08 Jun 2011)
New Revision: 1604

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DateTimeRenderer.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MultiLineTextRenderer.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MutableTable.java
Log:
Several new gui utilities for table cell rendering and editing.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-06-04 17:05:23 UTC (rev 1603)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2011-06-08 13:37:58 UTC (rev 1604)
@@ -201,7 +201,34 @@
 			formatter = new SimpleDateFormat(formatPattern);
 		return formatter.format(new Date(System.currentTimeMillis()));
 	}
+	
+	/**
+	 * Returns a new date which combines the date and time
+	 * information of several date objects.
+	 * @param date date the date information is taken from
+	 * @param time date the time information is taken from
+	 */
+	public static Date combineDate(Date date, Date time) {
+	  Calendar dateCal = GregorianCalendar.getInstance();
+	  dateCal.setTime( date != null ? date : new Date() );
+	  Calendar timeCal = GregorianCalendar.getInstance();
+	  timeCal.setTime( time != null ? time : new Date() );
+	  
+      Calendar combinedDateCal = GregorianCalendar.getInstance();
 
+      // combine date and time for new date object
+      combinedDateCal.set( Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY) );
+      combinedDateCal.set( Calendar.MINUTE, timeCal.get(Calendar.MINUTE) );
+      combinedDateCal.set( Calendar.SECOND, timeCal.get(Calendar.SECOND) );
+      combinedDateCal.set( Calendar.MILLISECOND, timeCal.get(Calendar.MILLISECOND) );
+
+      combinedDateCal.set( Calendar.DATE, dateCal.get(Calendar.DATE) );
+      combinedDateCal.set( Calendar.MONTH, dateCal.get(Calendar.MONTH) );
+      combinedDateCal.set( Calendar.YEAR, dateCal.get(Calendar.YEAR) );
+
+      return combinedDateCal.getTime();
+	}
+
 	/**
 	 * Returns an exception stack as string.
 	 */

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java	2011-06-04 17:05:23 UTC (rev 1603)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/ComponentRenderer.java	2011-06-08 13:37:58 UTC (rev 1604)
@@ -32,6 +32,7 @@
 import java.awt.Component;
 
 import javax.swing.AbstractButton;
+import javax.swing.JScrollPane;
 import javax.swing.JTable;
 import javax.swing.SwingConstants;
 import javax.swing.table.DefaultTableCellRenderer;
@@ -203,6 +204,53 @@
   }
 
   /**
+   * Dieser Renderer stellt das jeweilige Objekt in einem {@link javax.swing.JTextArea} dar.
+   * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+   * @version 1.0
+   */
+  public static class JTextArea extends ComponentRenderer {
+    private int rows = 5;
+    
+    /**
+     * Erzeugt einen neuen Zellen-Renderer in Form einer {@link JTextArea}.
+     * @param rows Anzahl an Zeilen
+     */
+    public JTextArea(int rows) {
+      super();
+      this.rows = rows;
+    }
+
+    /**
+     * Erzeugt einen neuen Zellen-Renderer in Form einer {@link JTextArea}
+     * mit 5 Zeilen.
+     */
+    public JTextArea() {
+      this(5);
+    }
+
+    /**
+     * Liefert ein {@link javax.swing.JTextArea}, das als Inhalt den Wert der
+     * Tabellenzelle hat.
+     * @param table      Tabelle in der die Zelle liegt
+     * @param value      Zu renderndes Objekt
+     * @param isSelected gibt an ob die Zelle aktuell selektiert ist
+     * @param hasFocus   gibt an ob die Zelle aktuell den Fokus besitzt
+     * @param row        Tabellenzeile der Zelle
+     * @param column     Tabellenspalte der Zelle
+     */
+    public Component createRendererComponent( JTable table,
+                                              Object value,
+                                              boolean isSelected,
+                                              boolean hasFocus,
+                                              int row,
+                                              int column) {
+      javax.swing.JTextArea textArea = new javax.swing.JTextArea(value != null ? value.toString() : "");
+      textArea.setRows(this.rows);
+      return new JScrollPane(textArea);
+    }
+  }
+
+  /**
    * Dieser Renderer stellt das jeweilige Objekt in einer {@link javax.swing.JCheckBox} dar.
    * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
    * @version 1.0

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DateTimeRenderer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DateTimeRenderer.java	2011-06-04 17:05:23 UTC (rev 1603)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/DateTimeRenderer.java	2011-06-08 13:37:58 UTC (rev 1604)
@@ -0,0 +1,49 @@
+package de.schmitzm.swing.table;
+
+import java.awt.Component;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableCellRenderer;
+
+
+/**
+ * Renderer which formats a date or time according to the
+ * given format.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class DateTimeRenderer extends DefaultTableCellRenderer {
+  protected DateFormat dateFormat = null;
+
+  /**
+   * Creates a new renderer
+   * @param formatPattern date/time format pattern
+   */
+  public DateTimeRenderer(String formatPattern) {
+    this( new SimpleDateFormat(formatPattern) );
+  }
+
+  /**
+   * Creates a new renderer
+   * @param dateFormat date/time format 
+   */
+  public DateTimeRenderer(DateFormat dateFormat) {
+    super();
+    this.dateFormat = dateFormat;
+  }
+  
+  /**
+   * Formats the given Daten to formated string and calls the
+   * super method.
+   */
+  @Override
+  public Component getTableCellRendererComponent(JTable table, Object value,
+      boolean isSelected, boolean hasFocus, int row, int column) {
+    if ( value instanceof Date)
+      value = dateFormat.format((Date)value);
+    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
+  }
+
+}

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MultiLineTextRenderer.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MultiLineTextRenderer.java	2011-06-04 17:05:23 UTC (rev 1603)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MultiLineTextRenderer.java	2011-06-08 13:37:58 UTC (rev 1604)
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * 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.Component;
+import java.text.NumberFormat;
+
+import javax.swing.JLabel;
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableCellRenderer;
+
+/**
+ * Diese Klasse stellt einen Renderer fuer Tabellenzellen dar, in denen
+ * in einer Zeile ein mehr-zeiliger Text dargestellt werden soll.<br>
+ * Standardmaessig werden der Darstellung des Strings einfach die
+ * Zeilen hintereinander gehaengt.<br>
+ * Dieser Renderer ersetzt die Zeilenumbrueche durch Leerzeichen.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ * @version 1.0
+ */
+public class MultiLineTextRenderer extends DefaultTableCellRenderer.UIResource {
+  @Override
+  public Component getTableCellRendererComponent(JTable table, Object value,
+      boolean isSelected, boolean hasFocus, int row, int column) {
+    String strValue = (value != null) ? value.toString() : "";
+    strValue = strValue.replace('\n', ' ');
+    return super.getTableCellRendererComponent(table, strValue, isSelected, hasFocus, row, column);
+  }
+}

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MutableTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MutableTable.java	2011-06-04 17:05:23 UTC (rev 1603)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/MutableTable.java	2011-06-08 13:37:58 UTC (rev 1604)
@@ -189,7 +189,7 @@
   protected void performChange() {
     int selCol[] = this.getSelectedColumns();
     int selRow[] = this.getSelectedRows();
-
+    
     for (int r=0; r<selRow.length; r++) {
       // werden Zellen einzeln selektiert, wird fuer jede Zelle
       // einzeln die Aenderungsaktion ausgefuehrt, sonst nur einmal

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java	2011-06-04 17:05:23 UTC (rev 1603)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java	2011-06-08 13:37:58 UTC (rev 1604)
@@ -0,0 +1,61 @@
+package de.schmitzm.swing.table;
+
+import java.awt.Component;
+import java.util.Date;
+
+import javax.swing.AbstractCellEditor;
+import javax.swing.JComponent;
+import javax.swing.JSpinner;
+import javax.swing.JTable;
+import javax.swing.SpinnerDateModel;
+import javax.swing.table.TableCellEditor;
+
+/**
+ * {@link TableCellEditor} to edit a time value by using a spinner
+ * component.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class TimeEditor extends AbstractCellEditor implements TableCellEditor {
+  /** Model for the spinner to maintain the time. */
+  protected SpinnerDateModel model = null;
+  /** Spinner to edit the time */
+  protected JSpinner timeSpinner = null;
+  /** Editor component for the spinner. */
+  protected JComponent editor = null;
+
+  /**
+   * Creates a new editor.
+   */
+  public TimeEditor(String formatPattern) {
+    model = new SpinnerDateModel();
+    timeSpinner = new JSpinner(model);
+    editor = new JSpinner.DateEditor(timeSpinner, formatPattern);
+    timeSpinner.setEditor(editor);
+  }
+
+  /**
+   * Creates a new editor with format "HH:mm:ss".
+   */
+  public TimeEditor() {
+    this("HH:mm:ss");
+  }
+  
+  /**
+   * Returns the current date value of the spinner.
+   */
+  @Override
+  public Object getCellEditorValue() {
+    return (Date)timeSpinner.getValue();
+  }
+  
+  /**
+   * Returns the {@link #editor} component.
+   */
+  @Override
+  public Component getTableCellEditorComponent(JTable table, Object value,
+      boolean isSelected, int row, int column) {
+    timeSpinner.setValue(value);
+    return timeSpinner;
+  }
+  
+}
\ No newline at end of file



More information about the Schmitzm-commits mailing list