[Schmitzm-commits] r65 - in trunk/src/schmitzm: geotools/gui swing swing/table

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Apr 19 20:57:42 CEST 2009


Author: mojays
Date: 2009-04-19 20:57:41 +0200 (Sun, 19 Apr 2009)
New Revision: 65

Added:
   trunk/src/schmitzm/swing/table/SelectionTableModel.java
Modified:
   trunk/src/schmitzm/geotools/gui/FeatureCollectionTableModel.java
   trunk/src/schmitzm/geotools/gui/FeatureTablePane.java
   trunk/src/schmitzm/swing/SortedJTable.java
Log:
SortedJTable
- because of undefined behavior, class does no longer overwrite getter/setter, but instead defines new method to access the indexes according to the model
new SelectionTableModel
- extends an other TableModel by an automatic check box column to select rows individually (without Strg/Shift key)

Modified: trunk/src/schmitzm/geotools/gui/FeatureCollectionTableModel.java
===================================================================
--- trunk/src/schmitzm/geotools/gui/FeatureCollectionTableModel.java	2009-04-19 11:30:02 UTC (rev 64)
+++ trunk/src/schmitzm/geotools/gui/FeatureCollectionTableModel.java	2009-04-19 18:57:41 UTC (rev 65)
@@ -52,6 +52,8 @@
   protected Feature[] featureArray;
   /** Spaltennamen. Wird nur beim Aufruf von {@link #reorganize} befuellt. */
   protected String[] colNames = null;
+  /** Spalten-Typen. Wird nur beim Aufruf von {@link #reorganize} befuellt. */
+  protected Class[] colClass = null;
   /** Bestimmt die angezeigten Features */
   protected AttributeTypeFilter attrFilter = AttributeTypeFilter.ALL;
 
@@ -97,9 +99,10 @@
    */
   protected void reorganize(boolean fireTableStructureChanged) {
     featureArray = FeatureUtil.featuresToArray(featureTable);
-    if ( featureArray == null || featureArray.length == 0 )
+    if ( featureArray == null || featureArray.length == 0 ) {
       colNames = new String[0];
-    else {
+      colClass = new Class[0];
+    } else {
       // Struktur der Tabelle von erstem Feature uebernehmen
       FeatureType ft = featureArray[0].getFeatureType();
       // Pruefen, welche Attribute angezeigt werden
@@ -111,11 +114,13 @@
       }
       // Namen und Attribut-Indizes der angezeigten Spalten ermitteln
       colNames      = new String[ attrTypes.size() ];
+      colClass      = new Class[ attrTypes.size() ];
       attrIdxForCol = new int[ attrTypes.size() ];
       for (int i=0; i<colNames.length; i++) {
         int idx = ft.find( attrTypes.elementAt(i) );
         attrIdxForCol[i] = idx;
         colNames[i]      = ft.getAttributeType(idx).getLocalName();
+        colClass[i]      = ft.getAttributeType(idx).getBinding();
       }
     }
     if ( fireTableStructureChanged )
@@ -140,7 +145,7 @@
    * erst nach erneutem {@link #reorganize()} in das Tabellen-Modell uebernommen!
    */
   public FeatureCollection getFeatureCollection() {
-      return featureTable;
+    return featureTable;
   }
 
   /**
@@ -196,11 +201,14 @@
       return featureArray[row].getAttribute( attrIdxForCol[col] );
   }
   
+  /**
+   * Liefert die Klasse des Feature-Attributs.
+   * @param col Spaltennummer (Attribut), beginnen bei 0
+   */
   @Override
-	public Class<?> getColumnClass(int columnIndex) {
-		// TODO Hey Martin! Check what the original Datatype is in the FeatureType and return that class. At the moment its sorting the prices 10EUR, 2EUR 22EUR, 9EUR ...  
-		return super.getColumnClass(columnIndex);
-	}
+  public Class<?> getColumnClass(int col) {
+    return colClass[col];
+  }
 
   /**
    * Liefert Features der Tabelle als Array.

Modified: trunk/src/schmitzm/geotools/gui/FeatureTablePane.java
===================================================================
--- trunk/src/schmitzm/geotools/gui/FeatureTablePane.java	2009-04-19 11:30:02 UTC (rev 64)
+++ trunk/src/schmitzm/geotools/gui/FeatureTablePane.java	2009-04-19 18:57:41 UTC (rev 65)
@@ -47,6 +47,7 @@
 import schmitzm.swing.MultiSplitPane;
 import schmitzm.swing.SortedJTable;
 import schmitzm.swing.SwingUtil;
+import schmitzm.swing.table.SelectionTableModel;
 import skrueger.geotools.StyledFeatureCollectionTableModel;
 
 /**
@@ -63,7 +64,7 @@
  */
 public class FeatureTablePane extends JPanel {
   /** Tabelle in der die Features angezeigt werden. */
-  protected JTable featuresTable = null;
+  protected SortedJTable featuresTable = null;
   /** Tabellen-Modell der Feature-Tabelle. */
   protected FeatureCollectionTableModel featuresTableModel = null;
   /** Preview-Bereich fuer die in der Tabelle selektierten Features. */
@@ -187,7 +188,10 @@
     }
 
     // Tabelle fuer FeatureCollection
-    featuresTable = new SortedJTable(featuresTableModel);
+//    featuresTable = new JTable();
+//    featuresTable.setAutoCreateRowSorter(true);
+    featuresTable = new SortedJTable();
+    featuresTable.setModel( new SelectionTableModel(featuresTableModel, featuresTable) );
     featuresTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
     featuresTable.setColumnSelectionAllowed(false);
     featuresTable.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
@@ -262,7 +266,7 @@
    * Liefert die in der Tabelle selektierten Features.
    */
   public FeatureCollection getSelectedFeatures() {
-    return featuresTableModel.getFeaturesAsCollection( featuresTable.getSelectedRows() );
+    return featuresTableModel.getFeaturesAsCollection( featuresTable.getSelectedModelRows() );
   }
 
   /**

Modified: trunk/src/schmitzm/swing/SortedJTable.java
===================================================================
--- trunk/src/schmitzm/swing/SortedJTable.java	2009-04-19 11:30:02 UTC (rev 64)
+++ trunk/src/schmitzm/swing/SortedJTable.java	2009-04-19 18:57:41 UTC (rev 65)
@@ -23,7 +23,11 @@
  * {@link JTable#convertRowIndexToModel(int)} and {@link JTable#convertRowIndexToView(int)}
  * is not necessary:
  * <ul>
- *   <li></li>
+ *   <li>{@link #getEditingRow()}</li>
+ *   <li>{@link #getSelectedRow()}</li>
+ *   <li>{@link #getSelectedRows()}</li>
+ *   <li>{@link #addRowSelectionInterval(int, int)}</li>
+ *   <li>{@link #setRowSelectionInterval(int, int)}</li>
  * </ul>   
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
  */
@@ -98,53 +102,79 @@
    * Returns the index of the currently edited row according to
    * the table model.
    */
-  public int getEditingRow() {
-    return convertRowIndexToModel(super.getEditingRow());
+  public int getEditingModelRow() {
+    return convertRowIndexToModel(getEditingRow());
   }
 
   /**
    * Returns the index of the selected row according to
    * the table model.
    */
-  @Override
-  public int getSelectedRow() {
-    return convertRowIndexToModel(super.getSelectedRow());
+  public int getSelectedModelRow() {
+    return convertRowIndexToModel(getSelectedRow());
   }
 
   /**
    * Returns the indices of the selected rows according to
    * the table model.
    */
-  @Override
-  public int[] getSelectedRows() {
+  public int[] getSelectedModelRows() {
     int[] rows = super.getSelectedRows();
     for (int i=0; i<rows.length; i++)
       rows[i] = convertRowIndexToModel(rows[i]);
     return rows;
   }
   
-//  /**
-//   * Extends the current selection by the given index interval
-//   * according to the table model.
-//   */
-//  public void addRowSelectionInterval(int index0, int index1) {
-//    for (int i=index0; i<=index1; i++) {
-//      int viewIdx = convertRowIndexToView(i);
-//      super.addRowSelectionInterval(viewIdx, viewIdx);
-//    }
-//  }
-//
-//  /**
-//   * Sets the current selection to the given index interval
-//   * according to the table model.
-//   */
-//  public void setRowSelectionInterval(int index0, int index1) {
-//    clearSelection();
-//    for (int i=index0; i<=index1; i++) {
-//      int viewIdx = convertRowIndexToView(i);
-//      super.addRowSelectionInterval(viewIdx, viewIdx);
-//    }
-//  }
-//  
+  /**
+   * Changes the selected cells.
+   * @param row row index
+   * @param col column index
+   * @param toggle see {@link #changeSelection(int, int, boolean, boolean)}
+   * @param extend see {@link #changeSelection(int, int, boolean, boolean)}
+   */
+  public void changeSelectionModel(int row, int col, boolean toggle, boolean extend) {
+    row = convertRowIndexToView(row);
+    changeSelection(row, col, toggle, extend);
+  }
+  
+  /**
+   * Extends the current selection by the given index interval
+   * according to the table model.
+   * @param index0 start index
+   * @param index1 end index (inclusive)
+   */
+  public void addModelRowSelectionInterval(int index0, int index1) {
+    for (int i=index0; i<=index1; i++) {
+      int viewIdx = convertRowIndexToView(i);
+      addRowSelectionInterval(viewIdx, viewIdx);
+    }
+  }
 
+  /**
+   * Sets the current selection to the given index interval
+   * according to the table model.
+   * @param index0 start index
+   * @param index1 end index (inclusive)
+   */
+  public void setModelRowSelectionInterval(int index0, int index1) {
+    clearSelection();
+    for (int i=index0; i<=index1; i++) {
+      int viewIdx = convertRowIndexToView(i);
+      addRowSelectionInterval(viewIdx, viewIdx);
+    }
+  }
+  
+  /**
+   * Removes the given index interval of rows from the current selection
+   * according to the table model.
+   * @param index0 start index
+   * @param index1 end index (inclusive)
+   */
+  public void removeModelRowSelectionInterval(int index0, int index1) {
+    for (int i=index0; i<=index1; i++) {
+      int viewIdx = convertRowIndexToView(i);
+      removeRowSelectionInterval(viewIdx, viewIdx);
+    }
+  }
+
 }

Added: trunk/src/schmitzm/swing/table/SelectionTableModel.java
===================================================================
--- trunk/src/schmitzm/swing/table/SelectionTableModel.java	2009-04-19 11:30:02 UTC (rev 64)
+++ trunk/src/schmitzm/swing/table/SelectionTableModel.java	2009-04-19 18:57:41 UTC (rev 65)
@@ -0,0 +1,288 @@
+/** 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.table;
+
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.MouseListener;
+
+import javax.swing.JTable;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+import javax.swing.event.TableModelListener;
+import javax.swing.table.AbstractTableModel;
+import javax.swing.table.TableModel;
+
+/**
+ * This table model extends an other {@link TableModel} by inserting an
+ * additional column 0 which shows and controls the row selection.
+ * To realize this functionality it is necessary to connect this table model
+ * to an explicit {@link JTable}.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a> (University of Bonn/Germany)
+ *
+ */
+public class SelectionTableModel extends AbstractTableModel {
+
+  /** Holds the table which controls the selection. */
+  protected JTable table = null;
+  /** Holds the base data. */
+  protected TableModel model = null;
+  
+  
+  /**
+   * Creates a new table model
+   * @param model provides the data
+   * @param table used to determine the row selection 
+   */
+  public SelectionTableModel(TableModel model, JTable 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);
+  }
+
+  /**
+   * Adds a listener to the list that's notified each time a change
+   * to the data model occurs.
+   */
+  @Override
+  public void addTableModelListener(TableModelListener listener) {
+    model.addTableModelListener(listener);
+  }
+  
+  /**
+   * Removes a listener from the list that's notified each time a change
+   * to the data model occurs.
+   */
+  @Override
+  public void removeTableModelListener(TableModelListener listener) {
+    model.removeTableModelListener(listener);
+  }
+
+  /**
+   * Returns the type of a column.
+   * @return {@code Boolean.class} for column 0
+   */
+  @Override
+  public Class getColumnClass(int col) {
+    if ( col == 0 )
+      return Boolean.class;
+   return model.getColumnClass(col - 1); 
+  }
+  
+  /**
+   * Returns the type of a column.
+   * @return {@code Boolean.class} for column 0
+   */
+  @Override
+  public int getColumnCount() {
+   return model.getColumnCount() + 1; 
+  }
+  
+  /**
+   * Returns the name of a column.
+   * @return empty string for column 0
+   */
+  @Override
+  public String getColumnName(int col) {
+    if ( col == 0 )
+      return "";
+   return model.getColumnName(col - 1); 
+  }
+  
+  /**
+   * Returns the number of rows in the model.
+   */
+  @Override
+  public int getRowCount() {
+    return model.getRowCount();
+  }
+
+  /**
+   * Returns the value for a cell.
+   * @param row row index
+   * @param col column index
+   */
+  @Override
+  public Object getValueAt(int row, int col) {
+    if ( col == 0 )
+      return table.isRowSelected( table.convertRowIndexToView(row) );
+    return model.getValueAt(row, col - 1);
+  }
+  
+  /**
+   * Sets a value for a cell.
+   * @param value new cell value
+   * @param row row index
+   * @param col column index
+   */
+  @Override
+  public void setValueAt(Object value, int row, int col) {
+    if ( col == 0 ) {
+      if ( value instanceof Boolean ) {
+        boolean select   = ((Boolean)value).booleanValue();
+        int     tableRow = table.convertRowIndexToView(row);
+        if ( select )
+          table.addRowSelectionInterval(tableRow, tableRow);
+        else
+          table.removeRowSelectionInterval(tableRow, tableRow);
+      }
+    } else
+      model.setValueAt(value, row, col - 1);
+  }
+
+  /**
+   * Returns the value for a cell.
+   * @param row row index
+   * @param col column index
+   * @param {@code true} for column 0
+   */
+  @Override
+  public boolean isCellEditable(int row, int col) {
+    if ( col == 0 )
+      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) {}
+    
+    /**
+     * 
+     */
+    @Override
+    public void valueChanged(ListSelectionEvent e) {
+    }
+  }
+}



More information about the Schmitzm-commits mailing list