001 /* 002 * Copyright 2002-2004 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 005 * use this file except in compliance with the License. You may obtain a copy of 006 * the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 013 * License for the specific language governing permissions and limitations under 014 * the License. 015 */ 016 package org.springframework.richclient.table; 017 018 import java.awt.Point; 019 import java.awt.Rectangle; 020 021 import javax.swing.JTable; 022 import javax.swing.event.TableModelEvent; 023 import javax.swing.table.TableModel; 024 025 /** 026 * VisibleTableModelEvent adds the method isVisible to test if the cell 027 * identified by the event is visible. 028 */ 029 public class VisibleTableModelEvent extends TableModelEvent { 030 private Point tmpPoint; 031 032 // This implementation caches the information for one JTable, it is 033 // certainly possible to cache it for more than one should 034 // you have this need. 035 private boolean valid; 036 037 private int firstVisRow; 038 039 private int lastVisRow; 040 041 private int firstVisCol; 042 043 private int lastVisCol; 044 045 public VisibleTableModelEvent(TableModel source) { 046 super(source, 0, 0, 0, UPDATE); 047 tmpPoint = new Point(); 048 } 049 050 /** 051 * Resets the underlying fields of the TableModelEvent. This assumes no ONE 052 * is going to cache the TableModelEvent. 053 */ 054 public void set(int row, int col) { 055 firstRow = row; 056 lastRow = row; 057 column = col; 058 } 059 060 /** 061 * Invoked to indicate the visible rows/columns need to be recalculated 062 * again. 063 */ 064 public void reset() { 065 valid = false; 066 } 067 068 public boolean isVisible(JTable table) { 069 if (!valid) { 070 // Determine the visible region of the table. 071 Rectangle visRect = table.getVisibleRect(); 072 073 tmpPoint.x = visRect.x; 074 tmpPoint.y = visRect.y; 075 firstVisCol = table.columnAtPoint(tmpPoint); 076 firstVisRow = table.rowAtPoint(tmpPoint); 077 078 tmpPoint.x += visRect.width; 079 tmpPoint.y += visRect.height; 080 lastVisCol = table.columnAtPoint(tmpPoint); 081 if (lastVisCol == -1) { 082 lastVisCol = table.getColumnCount() - 1; 083 } 084 if ((lastVisRow = table.rowAtPoint(tmpPoint)) == -1) { 085 lastVisRow = table.getRowCount(); 086 } 087 valid = true; 088 } 089 return (firstRow >= firstVisRow && firstRow <= lastVisRow && column >= firstVisCol && column <= lastVisCol); 090 } 091 }