// --------------------- RowHeightResizer.java --------------------------
/************************************************************************
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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.
************************************************************************/
// awt
import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
// swingx
import javax.swing.JTable;
import javax.swing.event.MouseInputAdapter;
/**
* RowHeightResizer class provides support for resizing rows
* in a javax.swing.JTable and extends
* javax.swing.event.MouseInputAdapter.
*
* @author Thomas Kellerer
* @see Sun Java Developer's Forum Member[thomas.kellerer]
* @see See post
*/
public class RowHeightResizer extends MouseInputAdapter
{
private JTable table;
private boolean active;
private boolean rowSelectionAllowed;
private int row;
private int startY;
private int startHeight;
private static final int PIXELS = 5;
private Cursor lastCursor;
private static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
/**
* Class constructor sets the reference to this table and adds
* mouse listeners to this component.
*
* @param table javax.swing.JTable reference for this instance.
*/
public RowHeightResizer(JTable table)
{
this.table = table;
this.table.addMouseListener(this);
this.table.addMouseMotionListener(this);
this.row = -1;
}
/**
* Checks for null table reference, returns
* and does nothing if true; otherwise, the mouse listeners are
* removed.
*/
public void done()
{
if (this.table == null) return;
this.table.removeMouseListener(this);
this.table.removeMouseMotionListener(this);
}
/**
* Detects mouse movements.
*
* @param e java.awt.event.MouseEvent
*
*/
public void mouseMoved(MouseEvent e)
{
Point p = e.getPoint();
if (this.isMouseOverRowMargin(p))
{
if (this.lastCursor == null)
{
this.lastCursor = this.table.getCursor();
}
this.table.setCursor(resizeCursor);
}
else
{
this.table.setCursor(this.lastCursor);
}
}
/**
* Detects mouse-pressed events.
*
* @param e java.awt.event.MouseEvent reference of this event.
*
*/
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
if (this.isMouseOverRowMargin(p))
{
this.active = true;
this.startY = p.y;
this.startHeight = table.getRowHeight(row);
this.rowSelectionAllowed = this.table.getRowSelectionAllowed();
this.table.setRowSelectionAllowed(false);
}
}
/**
* Detects dragged event for this mouse event.
*
* @param e java.awt.event.MouseEvent reference of this event.
*/
public void mouseDragged(MouseEvent e)
{
if (!active) return;
int newHeight = startHeight + e.getY() - startY;
newHeight = Math.max(1, newHeight);
this.table.setRowHeight(row, newHeight);
}
/**
* Detects mouse released event.
*
* @param e java.awt.event.MouseEvent reference of this event.
*/
public void mouseReleased(MouseEvent e)
{
if (!active) return;
this.table.setRowSelectionAllowed(this.rowSelectionAllowed);
this.active = false;
this.row = -1;
}
/**
* Indicates whether mouse is over the row margin.
*
* @param e java.util.Point location of this row margin.
* @return boolean value indicating whether this mouse is over the row margin.
*/
private boolean isMouseOverRowMargin(Point p)
{
if (!table.isEnabled()) return false;
this.row = table.rowAtPoint(p);
int column = table.columnAtPoint(p);
if (row == -1 || column == -1) return false;
Rectangle r = table.getCellRect(row, column, true);
if (p.y >= r.y + r.height - PIXELS)
{
return true;
}
return false;
}
}