001: /*
002: * RowHeightResizer.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.awt.Cursor;
015: import java.awt.Point;
016: import java.awt.Rectangle;
017: import java.awt.event.MouseEvent;
018:
019: import javax.swing.JTable;
020: import javax.swing.event.MouseInputAdapter;
021:
022: public class RowHeightResizer extends MouseInputAdapter {
023: private JTable table;
024: private boolean active;
025: private boolean rowSelectionAllowed;
026: private int row;
027: private int startY;
028: private int startHeight;
029:
030: private static final int PIXELS = 5;
031: private Cursor lastCursor;
032: private static Cursor resizeCursor = Cursor
033: .getPredefinedCursor(Cursor.S_RESIZE_CURSOR);
034:
035: public RowHeightResizer(JTable table) {
036: this .table = table;
037: this .table.addMouseListener(this );
038: this .table.addMouseMotionListener(this );
039: this .row = -1;
040: }
041:
042: public void done() {
043: if (this .table == null)
044: return;
045: this .table.removeMouseListener(this );
046: this .table.removeMouseMotionListener(this );
047: }
048:
049: public void mouseMoved(MouseEvent e) {
050: Point p = e.getPoint();
051:
052: if (this .isMouseOverRowMargin(p)) {
053: if (this .lastCursor == null) {
054: this .lastCursor = this .table.getCursor();
055: }
056: this .table.setCursor(resizeCursor);
057: } else {
058: this .table.setCursor(this .lastCursor);
059: }
060: }
061:
062: public void mousePressed(MouseEvent e) {
063: Point p = e.getPoint();
064:
065: if (this .isMouseOverRowMargin(p)) {
066: this .active = true;
067: this .startY = p.y;
068: this .startHeight = table.getRowHeight(row);
069: this .rowSelectionAllowed = this .table
070: .getRowSelectionAllowed();
071: this .table.setRowSelectionAllowed(false);
072: }
073: }
074:
075: public void mouseDragged(MouseEvent e) {
076: if (!active)
077: return;
078:
079: int newHeight = startHeight + e.getY() - startY;
080: newHeight = Math.max(1, newHeight);
081: this .table.setRowHeight(row, newHeight);
082: }
083:
084: public void mouseReleased(MouseEvent e) {
085: if (!active)
086: return;
087:
088: this .table.setRowSelectionAllowed(this .rowSelectionAllowed);
089: this .active = false;
090: this .row = -1;
091: }
092:
093: private boolean isMouseOverRowMargin(Point p) {
094: if (!table.isEnabled())
095: return false;
096: this .row = table.rowAtPoint(p);
097: int column = table.columnAtPoint(p);
098:
099: if (row == -1 || column == -1)
100: return false;
101:
102: Rectangle r = table.getCellRect(row, column, true);
103:
104: if (p.y >= r.y + r.height - PIXELS) {
105: return true;
106: }
107: return false;
108: }
109:
110: }
|