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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon Mar 12 15:55:09 CET 2012


Author: mojays
Date: 2012-03-12 15:55:09 +0100 (Mon, 12 Mar 2012)
New Revision: 1900

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/PercentageEditor.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/SpinnerEditor.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java
Log:
SortableJTable: new method getEditingModelColumn(.); handle editing column conversion if no cell is being edited
new SpinnerEditor
new PercentageEditor

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java	2012-03-12 00:03:28 UTC (rev 1899)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SortableJTable.java	2012-03-12 14:55:09 UTC (rev 1900)
@@ -163,10 +163,22 @@
    * the table model.
    */
   public int getEditingModelRow() {
+    if ( getEditingRow() < 0 )
+      return -1;
     return convertRowIndexToModel(getEditingRow());
   }
 
   /**
+   * Returns the index of the currently edited column according to
+   * the table model.
+   */
+  public int getEditingModelColumn() {
+    if ( getEditingColumn() < 0 )
+      return -1;
+    return convertColumnIndexToModel(getEditingColumn());
+  }
+
+  /**
    * Returns the index of the selected row according to
    * the table model.
    */

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/PercentageEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/PercentageEditor.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/PercentageEditor.java	2012-03-12 14:55:09 UTC (rev 1900)
@@ -0,0 +1,47 @@
+package de.schmitzm.swing.table;
+
+import java.awt.Component;
+import java.text.DecimalFormat;
+
+import javax.swing.JSpinner;
+import javax.swing.JTable;
+import javax.swing.SpinnerModel;
+import javax.swing.SpinnerNumberModel;
+import javax.swing.table.TableCellEditor;
+
+/**
+ * {@link TableCellEditor} to edit a percentage value by using a spinner
+ * component.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class PercentageEditor extends SpinnerEditor {
+  /**
+   * Creates a new editor.
+   */
+  public PercentageEditor(String formatPattern) {
+    super(formatPattern);
+  }
+  
+
+  /**
+   * Creates the spinner model for the editor.
+   */
+  public SpinnerModel createSpinnerModel(String percentageDecimalFormatPattern) {
+    DecimalFormat format = new DecimalFormat(percentageDecimalFormatPattern);
+    
+    int fracDigits = format.getMinimumFractionDigits();
+    if ( percentageDecimalFormatPattern.contains("%") )
+      fracDigits += 2;
+    double stepSize = Math.pow(10, -fracDigits);
+    return new SpinnerNumberModel(0, 0, 1, stepSize);
+  }
+  
+  /**
+   * Creates the spinner editor.
+   */
+  @Override
+  public JSpinner.DefaultEditor createEditor(JSpinner spinner, String percentageDecimalFormatPattern)  {
+    return new JSpinner.NumberEditor(spinner, percentageDecimalFormatPattern);
+  }
+ 
+}
\ No newline at end of file

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/SpinnerEditor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/SpinnerEditor.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/SpinnerEditor.java	2012-03-12 14:55:09 UTC (rev 1900)
@@ -0,0 +1,105 @@
+package de.schmitzm.swing.table;
+
+import java.awt.Component;
+import java.text.Format;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Date;
+
+import javax.swing.AbstractCellEditor;
+import javax.swing.JSpinner;
+import javax.swing.JSpinner.DefaultEditor;
+import javax.swing.JTable;
+import javax.swing.SpinnerModel;
+import javax.swing.table.TableCellEditor;
+import javax.swing.table.TableCellRenderer;
+
+import de.schmitzm.swing.SwingUtil;
+
+/**
+ * {@link TableCellEditor} to edit a numeric value by using a spinner
+ * component.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public abstract class SpinnerEditor extends AbstractCellEditor implements TableCellEditor {
+  /** Model for the spinner to maintain the value. */
+  protected SpinnerModel spinnerModel = null;
+  /** Spinner to edit the value */
+  protected JSpinner spinner = null;
+  /** Editor component for the spinner. */
+  protected JSpinner.DefaultEditor spinnerEditor = null;
+
+  /**
+   * Creates a new editor.
+   */
+  public SpinnerEditor(String formatPattern) {
+    spinnerModel   = createSpinnerModel(formatPattern);
+    spinner        = new JSpinner(spinnerModel);
+    spinnerEditor  = createEditor(spinner,formatPattern);
+// Better: call DefaultEditor.commit() on stopCellEditing() (see below)
+//    ((DefaultFormatter)editor.getTextField().getFormatter()).setCommitsOnValidEdit(true);
+    spinner.setEditor(spinnerEditor);
+    SwingUtil.initComponentForCellEditor(spinner);
+  }
+  
+  /**
+   * Creates the spinner editor.
+   */
+  public abstract JSpinner.DefaultEditor createEditor(JSpinner spinner, String formatPattern);
+
+  /**
+   * Creates the spinner model for the editor.
+   */
+  public abstract SpinnerModel createSpinnerModel(String formatPattern);
+  
+  /**
+   * Returns the current date value of the spinner.
+   */
+  @Override
+  public Object getCellEditorValue() {
+    return spinner.getValue();
+  }
+  
+  /**
+   * Returns the {@link #spinner} component.
+   */
+  @Override
+  public Component getTableCellEditorComponent(JTable table, Object value,
+      boolean isSelected, int row, int column) {
+    spinner.setValue(value);
+    return spinner;
+  }
+  
+//  /**
+//   * Returns the {@link #spinner} component.
+//   */
+//  @Override
+//  public Component getTableCellRendererComponent(JTable table, Object value,
+//      boolean isSelected, boolean hasFocus, int row, int column) {
+//    spinner.setValue(value);
+//    return spinner;
+//  }
+
+  /**
+   * Calls {@link DefaultEditor#commitEdit()} before invoking super method.
+   * @return {@code false} if {@link DefaultEditor#commitEdit()} causes an
+   *         exception
+   */
+  @Override
+  public boolean stopCellEditing() {
+    try {
+      spinnerEditor.commitEdit();
+      return super.stopCellEditing();
+    } catch (ParseException e) {
+      return false;
+    }
+  }
+  
+  /**
+   * Returns the editor spinner.
+   */
+  public JSpinner getSpinner() {
+    return spinner;
+  }
+  
+}
\ No newline at end of file



More information about the Schmitzm-commits mailing list