1 package org.simantics.spreadsheet.ui;
4 import javax.swing.table.*;
5 import javax.swing.event.MouseInputAdapter;
7 import java.awt.event.MouseEvent;
9 public class TableRowResizer extends MouseInputAdapter
11 public static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR);
13 private int mouseYOffset, resizingRow;
14 private Cursor otherCursor = resizeCursor;
17 public TableRowResizer(JTable table){
19 table.addMouseListener(this);
20 table.addMouseMotionListener(this);
23 private int getResizingRow(Point p){
24 return getResizingRow(p, table.rowAtPoint(p));
27 private int getResizingRow(Point p, int row){
31 int col = table.columnAtPoint(p);
34 Rectangle r = table.getCellRect(row, col, true);
39 int midPoint = r.y + r.height / 2;
40 int rowIndex = (p.y < midPoint) ? row - 1 : row;
45 public void mousePressed(MouseEvent e){
46 Point p = e.getPoint();
48 resizingRow = getResizingRow(p);
49 mouseYOffset = p.y - table.getRowHeight(resizingRow);
52 private void swapCursor(){
53 Cursor tmp = table.getCursor();
54 table.setCursor(otherCursor);
58 public void mouseMoved(MouseEvent e){
59 if((getResizingRow(e.getPoint())>=0)
60 != (table.getCursor() == resizeCursor)){
65 private boolean dragging = false;
67 public void mouseDragged(MouseEvent e){
68 int mouseY = e.getY();
71 int newHeight = mouseY - mouseYOffset;
73 table.setRowHeight(resizingRow, newHeight);
80 public void mouseReleased(MouseEvent e) {
82 int mouseY = e.getY();
83 int newHeight = mouseY - mouseYOffset;
84 System.out.println(newHeight);
85 onResize(resizingRow, newHeight);
90 public void onResize(int row, int height) {