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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Feb 10 16:34:19 CET 2012


Author: mojays
Date: 2012-02-10 16:34:19 +0100 (Fri, 10 Feb 2012)
New Revision: 1860

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/JDateChooserCellEditor.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java
Log:
new JDateChooserCellEditor: with fix for "terminateEditOnFocusLost" problem in jCalendar class
TimeEditor: BugFix for manual input; configuration with empty border
SwingUtil: method to create DefaultCellEditor for component with automatic table rendering configuration (no border)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-02-08 12:09:08 UTC (rev 1859)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-02-10 15:34:19 UTC (rev 1860)
@@ -70,6 +70,7 @@
 import javax.swing.AbstractAction;
 import javax.swing.AbstractButton;
 import javax.swing.Action;
+import javax.swing.BorderFactory;
 import javax.swing.CellEditor;
 import javax.swing.DefaultCellEditor;
 import javax.swing.Icon;
@@ -1712,7 +1713,7 @@
      */
     public static TableCellEditor createLimitedCellEditor(int limit, int style) {
       JTextField field = createLimitedTextField(limit, style);
-      return new DefaultCellEditor(field);
+      return createDefaultCellEditor(field);
     }
 
     /**
@@ -1849,6 +1850,38 @@
     }
     
     /**
+     * Creates a {@link DefaultCellEditor} for a {@link JComponent}. Also applies some
+     * configuration on the component (e.g. removing the borders).
+     * @param comp a {@link JTextField}, {@link JComboBox} or {@link JCheckBox}
+     * @exception IllegalArgumentException if an unsupported component is given
+     * @see
+     */
+    public static DefaultCellEditor createDefaultCellEditor(JComponent comp) {
+      initComponentForCellEditor(comp);
+      DefaultCellEditor editor = null;
+      if ( comp instanceof JTextField )
+       editor = new DefaultCellEditor((JTextField)comp);
+      else if ( comp instanceof JCheckBox )
+        editor = new DefaultCellEditor((JCheckBox)comp);
+      else if ( comp instanceof JComboBox )
+        editor = new DefaultCellEditor((JComboBox)comp);
+      else
+        throw new IllegalArgumentException("DefaultCellEditor can not be created for: "+LangUtil.getSimpleClassName(comp)+" (only JTextField, JComboBox, JCheckBox supported)");
+      return editor;
+    }
+    
+    /**
+     * Applies some configuration to the component to use it as {@link CellEditor}.
+     * For example an empty border is set.
+     */
+    public static void initComponentForCellEditor(JComponent comp) {
+      comp.setBorder( BorderFactory.createEmptyBorder() );
+    }
+    
+   
+    
+    
+    /**
      * Applies a client property to the table that the cell editing is automatically
      * finished on focus lost. 
      */

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/JDateChooserCellEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/JDateChooserCellEditor.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/JDateChooserCellEditor.java	2012-02-10 15:34:19 UTC (rev 1860)
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * 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 javax.swing.JTable;
+
+import com.toedter.calendar.JCalendar;
+import com.toedter.calendar.JDateChooser;
+
+import de.schmitzm.swing.SwingUtil;
+
+/**
+ * This class fixes a bug (or weird behavior) in {@link com.toedter.calendar.JDateChooserCellEditor}
+ * when the {@link JTable} client property "terminateEditOnFocusLost" is set to {@code true}.
+ * In this case the normal behavior when clicking on something in the {@link JCalendar} popup
+ * causes an immediate editing stop (because focus was lost) BEFORE the new date value
+ * was applied! To avoid this behavior the {@link #stopCellEditing()} method is overridden.<br>
+ * Furthermore this class
+ * <ul>
+ *   <li>implements a method {@link #getDateChooser()} to provide access to the component</li>
+ *   <li>applies some GUI configuration to make the cell editor look nicer (e.g. no border)</li>
+ * </ul>
+ * 
+ * @author Martin Schmitz
+ */
+public class JDateChooserCellEditor extends com.toedter.calendar.JDateChooserCellEditor {
+
+    /** Holds the date chooser component */
+    protected JDateChooser dateChooser;
+
+    /**
+     * Creates a new cell editor.
+     */
+    public JDateChooserCellEditor() {
+      super();
+      // determine the "private" super class component
+      dateChooser = (JDateChooser)getTableCellEditorComponent(null, null, false, 0, 0);
+      // apply some nicer GUI parameters
+      SwingUtil.initComponentForCellEditor(dateChooser.getDateEditor().getUiComponent());
+    }
+    
+    /**
+     * Returns the {@link JDateChooser} component.
+     * @return
+     */
+    public JDateChooser getDateChooser() {
+      return dateChooser;
+    }
+    
+    /**
+     * Major hack to avoid problems when the {@link JTable} client property "terminateEditOnFocusLost"
+     * is set to {@code true}. In this case the normal behavior when clicking on a date in {@link JCalendar}
+     * causes an immediate editing stop (because focus was lost) BEFORE the new date value was applied!<br>
+     * This hack only calls the super-method when the {@link JCalendar} is NOT showing!
+     */
+    @Override
+    public boolean stopCellEditing() {
+      if ( dateChooser.getJCalendar().isShowing() ) {
+        return true;
+      }
+      return super.stopCellEditing();
+    }
+}

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java	2012-02-08 12:09:08 UTC (rev 1859)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/TimeEditor.java	2012-02-10 15:34:19 UTC (rev 1860)
@@ -1,15 +1,18 @@
 package de.schmitzm.swing.table;
 
 import java.awt.Component;
+import java.text.ParseException;
 import java.util.Date;
 
 import javax.swing.AbstractCellEditor;
-import javax.swing.JComponent;
 import javax.swing.JSpinner;
+import javax.swing.JSpinner.DefaultEditor;
 import javax.swing.JTable;
 import javax.swing.SpinnerDateModel;
 import javax.swing.table.TableCellEditor;
 
+import de.schmitzm.swing.SwingUtil;
+
 /**
  * {@link TableCellEditor} to edit a time value by using a spinner
  * component.
@@ -21,7 +24,7 @@
   /** Spinner to edit the time */
   protected JSpinner timeSpinner = null;
   /** Editor component for the spinner. */
-  protected JComponent editor = null;
+  protected JSpinner.DateEditor editor = null;
 
   /**
    * Creates a new editor.
@@ -30,7 +33,10 @@
     model = new SpinnerDateModel();
     timeSpinner = new JSpinner(model);
     editor = new JSpinner.DateEditor(timeSpinner, formatPattern);
+// Better: call DefaultEditor.commit() on stopCellEditing() (see below)
+//    ((DefaultFormatter)editor.getTextField().getFormatter()).setCommitsOnValidEdit(true);
     timeSpinner.setEditor(editor);
+    SwingUtil.initComponentForCellEditor(timeSpinner);
   }
 
   /**
@@ -58,4 +64,19 @@
     return timeSpinner;
   }
   
+  /**
+   * Calls {@link DefaultEditor#commitEdit()} before invoking super method.
+   * @return {@code false} if {@link DefaultEditor#commitEdit()} causes an
+   *         exception
+   */
+  @Override
+  public boolean stopCellEditing() {
+    try {
+      editor.commitEdit();
+      return super.stopCellEditing();
+    } catch (ParseException e) {
+      return false;
+    }
+  }
+  
 }
\ No newline at end of file



More information about the Schmitzm-commits mailing list