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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Oct 3 22:05:38 CEST 2012


Author: mojays
Date: 2012-10-03 22:05:38 +0200 (Wed, 03 Oct 2012)
New Revision: 2097

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ListTableModel.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
Log:
SelectableJTable: Methods to scroll to cell
ListTableModel: TableModel with 1 column to simply maintain a list

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ListTableModel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ListTableModel.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ListTableModel.java	2012-10-03 20:05:38 UTC (rev 2097)
@@ -0,0 +1,103 @@
+/**
+ * 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.lang;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.swing.JTable;
+import javax.swing.table.AbstractTableModel;
+import javax.swing.table.TableModel;
+
+import de.schmitzm.swing.SelectableJTable;
+
+/**
+ * Wrapper {@link TableModel} to show simple list data in a {@link JTable},
+ * especially a {@link SelectableJTable}.
+ * @author Martin O.J. Schmitz
+ *
+ */
+public class ListTableModel<E> extends AbstractTableModel {
+  protected List<E> data = new ArrayList<E>();
+  
+  /**
+   * Sets the content of the table. 
+   */
+  public void setData(List<E> data) {
+    this.data = data;
+    fireTableDataChanged();
+  }
+  
+  /**
+   * Sets the content of the table. 
+   */
+  public void setData(E[] data) {
+    setData(data == null ? null : Arrays.asList(data));
+  }
+
+  /**
+   * Sets the content of the table. 
+   */
+  public List<E> getData() {
+    return this.data;
+  }
+  
+  
+  /**
+   * Returns the number of elements in the list.
+   */
+  @Override
+  public int getRowCount() {
+    return data == null ? 0 : data.size();
+  }
+
+  /**
+   * Returns 1, because a list always has only 1 column.
+   */
+  @Override
+  public int getColumnCount() {
+    return 1;
+  }
+
+  /**
+   * Returns an element of the list.
+   * @param row list index of element
+   * @param col not used!
+   */
+  @Override
+  public Object getValueAt(int row, int col) {
+    if ( data == null )
+      return null;
+    E element = data.get(row);
+    return element;
+  }
+
+}

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java	2012-10-03 13:11:04 UTC (rev 2096)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SelectableJTable.java	2012-10-03 20:05:38 UTC (rev 2097)
@@ -280,4 +280,41 @@
   public void setAlternatingColumnBackgroundEnabled(boolean enabled) {
     this.greyedBackgroundEnabled = enabled;
   }
+
+  /**
+   * Scrolls so that a cell becomes visible.
+   * @param row table row
+   * @param col table cell
+   */
+  public void scrollToCell(int row, int col) {
+    scrollRectToVisible( getCellRect(row, col, true));
+  }
+  
+  /**
+   * Scrolls so that the selected cell becomes visible.
+   */
+  public void scrollToSelectedCell() {
+    scrollToCell( getSelectedRow(), getSelectedColumn());
+  }
+  
+  /**
+   * Scrolls so that a row becomes visible, horizontally scrolling 
+   * to the selected column.  
+   * @param row table row
+   */
+  public void scrollToRow(int row) {
+    int col = Math.max(getSelectedColumn(),0);
+    scrollToCell(row, col);
+  }
+
+  /**
+   * Scrolls so that a column becomes visible, vertically scrolling 
+   * to the selected row.  
+   * @param col table col
+   */
+  public void scrollToCol(int col) {
+    int row = Math.max(getSelectedRow(),0);
+    scrollToCell(row, col);
+  }
+
 }



More information about the Schmitzm-commits mailing list