[Schmitzm-commits] r69 - in trunk: dist src/schmitzm/swing src/schmitzm/swing/table

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Apr 20 14:12:50 CEST 2009


Author: mojays
Date: 2009-04-20 14:12:43 +0200 (Mon, 20 Apr 2009)
New Revision: 69

Added:
   trunk/src/schmitzm/swing/SelectableJTable.java
Modified:
   trunk/dist/schmitzm-src.zip
   trunk/dist/schmitzm.jar
   trunk/src/schmitzm/swing/SortableJTable.java
   trunk/src/schmitzm/swing/table/SelectionTableModel.java
Log:
SelectionTableModel:
- Problem with cleared selection (when selection column is clicked on) solved
- now using new class SelectableJTable
SortableJTable
- derived from SelectableJTable

Modified: trunk/dist/schmitzm-src.zip
===================================================================
(Binary files differ)

Modified: trunk/dist/schmitzm.jar
===================================================================
(Binary files differ)

Added: trunk/src/schmitzm/swing/SelectableJTable.java
===================================================================
--- trunk/src/schmitzm/swing/SelectableJTable.java	2009-04-20 10:04:49 UTC (rev 68)
+++ trunk/src/schmitzm/swing/SelectableJTable.java	2009-04-20 12:12:43 UTC (rev 69)
@@ -0,0 +1,113 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+    This library 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 2.1 of the License, or (at your option) any later version.
+    This library 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 Lesser General Public License for more details.
+    You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+    Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+    Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+    Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+package schmitzm.swing;
+
+import java.util.Vector;
+
+import javax.swing.JTable;
+import javax.swing.ListSelectionModel;
+import javax.swing.table.TableColumnModel;
+import javax.swing.table.TableModel;
+
+import schmitzm.swing.table.SelectionTableModel;
+
+/**
+ * Extends the {@link JTable} by redefining the {@link #changeSelection(int, int, boolean, boolean)}
+ * method. If a {@link SelectionTableModel} is used, a click on the selection column must not
+ * clear an existing selection.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ */
+public class SelectableJTable extends JTable {
+  /**
+   * Creates an empty table.
+   */
+  public SelectableJTable() {
+    super();
+    initTable();
+  }
+
+  /**
+   * Creates an empty table.
+   */
+  public SelectableJTable(int numRows, int numColumns) {
+    super(numRows, numColumns);
+    initTable();
+  }
+
+  /**
+   * Creates a new table.
+   */
+  public SelectableJTable(Object[][] rowData, Object[] columnNames) {
+    super(rowData, columnNames);
+    initTable();
+  }
+
+  /**
+   * Creates a new table.
+   */
+  public SelectableJTable(TableModel dm, TableColumnModel cm, ListSelectionModel sm) {
+    super(dm, cm, sm);
+    initTable();
+  }
+
+  /**
+   * Creates a new table.
+   */
+  public SelectableJTable(TableModel dm, TableColumnModel cm) {
+    super(dm, cm);
+    initTable();
+  }
+
+  /**
+   * Creates a new table.
+   */
+  public SelectableJTable(TableModel dm) {
+    super(dm);
+    initTable();
+  }
+
+  /**
+   * Creates a new table.
+   */
+  public SelectableJTable(Vector rowData, Vector columnNames) {
+    super(rowData, columnNames);
+    initTable();
+  }
+
+  /**
+   * Called by every constructor to initialize the extended
+   * functionalities. Currently does nothing.
+   */
+  protected void initTable() {
+  }
+
+  /**
+   * If a {@link SelectionTableModel} is used, a click on the selection column must not
+   * clear an existing selection. So if ...
+   * <ul>
+   *   <li>{@link #getModel()} is an {@link SelectionTableModel}
+   *   <li>{@code toggle} flag is {@code false}</li>
+   *   <li>{@code extend} flag is {@code false}</li>
+   *   <li>{@link #convertColumnIndexToModel(int) convertColumnIndexToModel(col)} is 0
+   * </ul>
+   * ... this method sets {@code toggle} to {@code true} (before calling the super-method)
+   * to simulate that the Strg/Ctrl-key is pressed.
+   */
+  @Override
+  public void changeSelection(int row, int col, boolean toggle, boolean extend) {
+    if ( getModel() instanceof SelectionTableModel &&
+         convertColumnIndexToModel(col) == 0 &&
+         !toggle &&
+         !extend )
+      toggle = true;
+    super.changeSelection(row, col, toggle, extend);
+  }
+}

Modified: trunk/src/schmitzm/swing/SortableJTable.java
===================================================================
--- trunk/src/schmitzm/swing/SortableJTable.java	2009-04-20 10:04:49 UTC (rev 68)
+++ trunk/src/schmitzm/swing/SortableJTable.java	2009-04-20 12:12:43 UTC (rev 69)
@@ -12,7 +12,6 @@
 
 import java.util.Vector;
 
-import javax.swing.JTable;
 import javax.swing.ListSelectionModel;
 import javax.swing.table.TableColumnModel;
 import javax.swing.table.TableModel;
@@ -33,7 +32,7 @@
  * </ul>   
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
  */
-public class SortableJTable extends JTable {
+public class SortableJTable extends SelectableJTable {
 
   /**
    * Creates an empty table.
@@ -91,7 +90,6 @@
     initTable();
   }
   
-  
   /**
    * Called by every constructor to initialize the extended
    * functionalities.
@@ -178,10 +176,4 @@
       removeRowSelectionInterval(viewIdx, viewIdx);
     }
   }
-  
-//  @Override
-//  public void clearSelection() {
-//    super.clearSelection();
-//  }
-
 }

Modified: trunk/src/schmitzm/swing/table/SelectionTableModel.java
===================================================================
--- trunk/src/schmitzm/swing/table/SelectionTableModel.java	2009-04-20 10:04:49 UTC (rev 68)
+++ trunk/src/schmitzm/swing/table/SelectionTableModel.java	2009-04-20 12:12:43 UTC (rev 69)
@@ -23,6 +23,8 @@
 import javax.swing.table.AbstractTableModel;
 import javax.swing.table.TableModel;
 
+import schmitzm.swing.SelectableJTable;
+
 /**
  * This table model extends an other {@link TableModel} by inserting an
  * additional column 0 which shows and controls the row selection.
@@ -34,9 +36,12 @@
 public class SelectionTableModel extends AbstractTableModel {
 
   /** Holds the table which controls the selection. */
-  protected JTable table = null;
+  protected SelectableJTable table = null;
   /** Holds the base data. */
   protected TableModel model = null;
+//!! SOLVED BY USING SelectableJTable !!
+//  /** Used to realize the selection column. */
+//  protected SelectionRestorer selectionRestorer = null;
   
   
   /**
@@ -44,24 +49,35 @@
    * @param model provides the data
    * @param table used to determine the row selection 
    */
-  public SelectionTableModel(TableModel model, JTable table) {
+  public SelectionTableModel(TableModel model, SelectableJTable table) {
     super();
     this.model = model;
     this.table = table;
     table.getColumnModel().setColumnSelectionAllowed(false);
     
-    // Goal: By selection column cells, rows should be selected individually,
-    //       without holding Strg/Shift key.
-    // Problem: Also on a click in the selection column, the selection
-    //          is first cleared
-    // Solution: After every key stroke and every mouse click, the current
-    //           selection is remembered. On click on selection column
-    //           (the selection is already cleared!) the selection is
-    //           restored. After that the click (TableModel.setValue(..)) is
-    //           progressed, which extends the selection.
-    SelectionRestorer.connectTo(table);
+//!! SOLVED BY USING SelectableJTable !!
+//    // Goal: By selection column cells, rows should be selected individually,
+//    //       without holding Strg/Shift key.
+//    // Problem: Also on a click in the selection column, the selection
+//    //          is first cleared
+//    // Solution: After every key stroke and every mouse click, the current
+//    //           selection is remembered. On click on selection column
+//    //           (the selection is already cleared!) the selection is
+//    //           restored. After that the click (TableModel.setValue(..)) is
+//    //           progressed, which extends the selection.
+//    this.selectionRestorer = new SelectionRestorer(table);
   }
 
+//!! SOLVED BY USING SelectableJTable !!
+//  /**
+//   * Forces a update of the {@link SelectionRestorer} to renew the
+//   * remembered selected cells.
+//   */
+//  public void updateSelectionRestorer() {
+//    if ( selectionRestorer != null )
+//      selectionRestorer.update();
+//  }
+  
   /**
    * Adds a listener to the list that's notified each time a change
    * to the data model occurs.
@@ -164,125 +180,141 @@
       return true;
     return model.isCellEditable(row, col - 1);
   }
-  
-  /**
-   * This listener must be added as {@link MouseListener} and {@link KeyListener}.
-   * It stored the current table selection (rows) on every mouse click and key
-   * stroke. On mouse click on selection column 0, the remembered selection
-   * is restored to avoid the selection clear.<br><br>
-   * 
-   * <b>Goal:</b> By selection column cells, rows should be selected individually,
-   *              without holding Strg/Shift key.<br>
-   * <b>Problem:</b> Also on a click in the selection column, the selection
-   *                 is first cleared.<br>
-   * <b>Solution:</b> After every key stroke and every mouse click, the current
-   *                  selection is remembered. On click on selection column
-   *                  (the selection is already cleared!) the selection is
-   *                  restored. After that the click (TableModel.setValue(..)) is
-   *                  progressed, which extends the selection.
-   */
-  private static class SelectionRestorer extends MouseAdapter implements KeyListener, ListSelectionListener {
-    /** Holds the table the selection is remembered from. */
-    protected JTable table = null;
-    /** Holds the remembered rows according to the model. */
-    protected int[] selectedModelRows = null;
 
-
-    /**
-     * Creates {@link SelectionRestorer} and adds it to a {@link JTable}
-     * as {@link MouseListener} and {@link KeyListener}.
-     * @param table a table
-     */
-    public static void connectTo(JTable table) {
-      // remove all previously connected SelectionRestorers
-      SelectionRestorer[] selRest = table.getListeners(SelectionRestorer.class);
-      for (SelectionRestorer selectionRestorer : selRest) {
-        table.removeMouseListener(selectionRestorer);
-        table.removeKeyListener(selectionRestorer);
-        table.getSelectionModel().removeListSelectionListener(selectionRestorer);
-      }
-      
-      SelectionRestorer selectionRestorer = new SelectionRestorer(table);
-      table.addMouseListener( selectionRestorer );
-      table.addKeyListener( selectionRestorer );
-      table.getSelectionModel().addListSelectionListener(selectionRestorer);
-    }
-    
-    /**
-     * Created a new selection rememberer.
-     * @param table a table
-     */
-    protected SelectionRestorer(JTable table) {
-      super();
-      this.table = table;
-    }
-
-    /**
-     * Stores the current selection (according to the model)
-     * in {@link #selectedModelRows}.
-     */
-    protected void storeSelection() {
-      selectedModelRows = table.getSelectedRows();
-      for (int i=0; i < selectedModelRows.length; i++)
-        selectedModelRows[i] = table.convertRowIndexToModel(selectedModelRows[i]);
-    }
-    
-    /**
-     * Restores the remembered selection.
-     */
-    protected void restoreSelection() {
-      if ( selectedModelRows == null )
-        return;
-      
-      for (int row : selectedModelRows) {
-        int tableRow = table.convertRowIndexToView(row);
-        table.addRowSelectionInterval(tableRow,tableRow);
-      }
-    }
-    
-    /**
-     * On mouse click (without holding Strg/Shift) the selection was
-     * cleared. So we restore the remembered selection.
-     */
-    @Override
-    public void mousePressed(MouseEvent e) {
-      int modelCol = table.convertColumnIndexToModel(
-                        table.columnAtPoint(e.getPoint()) );
-      if ( modelCol == 0 )
-        restoreSelection();
-    }
-    
-    /**
-     * After every click, remember the currently selected rows
-     * according to the model.
-     */
-    @Override
-    public void mouseReleased(MouseEvent e) {
-      storeSelection();
-    }
-
-    /**
-     * After every key stroke, remember the currently selected rows
-     * according to the model.
-     */
-    @Override
-    public void keyReleased(KeyEvent e) {
-      storeSelection();
-    }
-
-    /** Does nothing. */
-    @Override
-    public void keyPressed(KeyEvent e) {}
-    
-    /** Does nothing. */
-    @Override
-    public void keyTyped(KeyEvent e) {}
-    
-    /**
-     * Does currently nothing.
-     */
-    @Override
-    public void valueChanged(ListSelectionEvent e) {
-    }
-  }
+// !!! BY USING SelectableJTable THIS PROCEDURE IS NOT NEEDED ANYMORE !!!  
+//  /**
+//   * This listener must be added as {@link MouseListener} and {@link KeyListener}.
+//   * It stored the current table selection (rows) on every mouse click and key
+//   * stroke. On mouse click on selection column 0, the remembered selection
+//   * is restored to avoid the selection clear.<br><br>
+//   * 
+//   * <b>Goal:</b> By selection column cells, rows should be selected individually,
+//   *              without holding Strg/Shift key.<br>
+//   * <b>Problem:</b> Also on a click in the selection column, the selection
+//   *                 is first cleared.<br>
+//   * <b>Solution:</b> After every key stroke and every mouse click, the current
+//   *                  selection is remembered. On click on selection column
+//   *                  (the selection is already cleared!) the selection is
+//   *                  restored. After that the click (TableModel.setValue(..)) is
+//   *                  progressed, which extends the selection.
+//   */
+//  private static class SelectionRestorer extends MouseAdapter implements KeyListener, ListSelectionListener {
+//    /** Holds the table the selection is remembered from. */
+//    protected JTable table = null;
+//    /** Holds the remembered rows according to the model. */
+//    protected int[] selectedModelRows = null;
+//
+//    /**
+//     * Created a new selection rememberer and adds the listener to
+//     * the {@link JTable}.
+//     * @param table a table
+//     */
+//    public SelectionRestorer(JTable table) {
+//      super();
+//      connect(table);
+//    }
+//
+//    /**
+//     * Connects the {@link SelectionRestorer} to a {@link JTable}
+//     * as {@link MouseListener}, {@link KeyListener} and {@link ListSelectionListener}.
+//     * @param table a table
+//     */
+//    protected void connect(JTable table) {
+//      this.table = table;
+//      // remove all previously connected SelectionRestorers
+//      disconnect();
+//      // add Listeners
+//      table.addMouseListener(this);
+//      table.addKeyListener(this);
+//      table.getSelectionModel().addListSelectionListener(this);
+//    }
+//    
+//    /**
+//     * Disconnects the {@link SelectionRestorer} from the {@link JTable}.
+//     */
+//    public void disconnect() {
+//      // remove all previously connected SelectionRestorers
+//      SelectionRestorer[] selRest = table.getListeners(SelectionRestorer.class);
+//      for (SelectionRestorer selectionRestorer : selRest) {
+//        table.removeMouseListener(selectionRestorer);
+//        table.removeKeyListener(selectionRestorer);
+//        table.getSelectionModel().removeListSelectionListener(selectionRestorer);
+//      }
+//    }
+//
+//    /**
+//     * Updates the remembered selection. Calls {@link #storeSelection()}.
+//     */
+//    public void update() {
+//      storeSelection();
+//    }
+//    
+//    /**
+//     * Stores the current selection (according to the model)
+//     * in {@link #selectedModelRows}.
+//     */
+//    protected void storeSelection() {
+//      selectedModelRows = table.getSelectedRows();
+//      for (int i=0; i < selectedModelRows.length; i++)
+//        selectedModelRows[i] = table.convertRowIndexToModel(selectedModelRows[i]);
+//    }
+//    
+//    /**
+//     * Restores the remembered selection.
+//     */
+//    protected void restoreSelection() {
+//      if ( selectedModelRows == null )
+//        return;
+//      
+//      for (int row : selectedModelRows) {
+//        int tableRow = table.convertRowIndexToView(row);
+//        table.addRowSelectionInterval(tableRow,tableRow);
+//      }
+//    }
+//    
+//    /**
+//     * On mouse click (without holding Strg/Shift) the selection was
+//     * cleared. So we restore the remembered selection.
+//     */
+//    @Override
+//    public void mousePressed(MouseEvent e) {
+//      int modelCol = table.convertColumnIndexToModel(
+//                        table.columnAtPoint(e.getPoint()) );
+//      if ( modelCol == 0 )
+//        restoreSelection();
+//    }
+//    
+//    /**
+//     * After every click, remember the currently selected rows
+//     * according to the model.
+//     */
+//    @Override
+//    public void mouseReleased(MouseEvent e) {
+//      storeSelection();
+//    }
+//
+//    /**
+//     * After every key stroke, remember the currently selected rows
+//     * according to the model.
+//     */
+//    @Override
+//    public void keyReleased(KeyEvent e) {
+//      storeSelection();
+//    }
+//
+//    /** Does nothing. */
+//    @Override
+//    public void keyPressed(KeyEvent e) {}
+//    
+//    /** Does nothing. */
+//    @Override
+//    public void keyTyped(KeyEvent e) {}
+//    
+//    /**
+//     * Does currently nothing.
+//     */
+//    @Override
+//    public void valueChanged(ListSelectionEvent e) {
+//    }
+//  }
 }



More information about the Schmitzm-commits mailing list