]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.spreadsheet.ui/src/org/simantics/spreadsheet/ui/TableRowResizer.java
Database backwards compatibility fix for fail-safe import changes
[simantics/platform.git] / bundles / org.simantics.spreadsheet.ui / src / org / simantics / spreadsheet / ui / TableRowResizer.java
1 package org.simantics.spreadsheet.ui;
2
3 import javax.swing.*;
4 import javax.swing.table.*;
5 import javax.swing.event.MouseInputAdapter;
6 import java.awt.*;
7 import java.awt.event.MouseEvent;
8
9 public class TableRowResizer extends MouseInputAdapter
10
11     public static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR); 
12
13     private int mouseYOffset, resizingRow; 
14     private Cursor otherCursor = resizeCursor; 
15     private JTable table; 
16
17     public TableRowResizer(JTable table){ 
18         this.table = table; 
19         table.addMouseListener(this); 
20         table.addMouseMotionListener(this); 
21     } 
22
23     private int getResizingRow(Point p){ 
24         return getResizingRow(p, table.rowAtPoint(p)); 
25     } 
26
27     private int getResizingRow(Point p, int row){ 
28         if(row == -1){ 
29             return -1; 
30         } 
31         int col = table.columnAtPoint(p); 
32         if(col==-1) 
33             return -1; 
34         Rectangle r = table.getCellRect(row, col, true); 
35         r.grow(0, -3); 
36         if(r.contains(p)) 
37             return -1; 
38
39         int midPoint = r.y + r.height / 2; 
40         int rowIndex = (p.y < midPoint) ? row - 1 : row; 
41
42         return rowIndex; 
43     } 
44
45     public void mousePressed(MouseEvent e){ 
46         Point p = e.getPoint(); 
47
48         resizingRow = getResizingRow(p); 
49         mouseYOffset = p.y - table.getRowHeight(resizingRow); 
50     } 
51
52     private void swapCursor(){ 
53         Cursor tmp = table.getCursor(); 
54         table.setCursor(otherCursor); 
55         otherCursor = tmp; 
56     }
57
58     public void mouseMoved(MouseEvent e){
59         if((getResizingRow(e.getPoint())>=0)
60            != (table.getCursor() == resizeCursor)){
61             swapCursor();
62         }
63     }
64
65     private boolean dragging = false;
66     
67     public void mouseDragged(MouseEvent e){
68         int mouseY = e.getY();
69
70         if(resizingRow >= 0){
71             int newHeight = mouseY - mouseYOffset;
72             if(newHeight > 0) {
73                 table.setRowHeight(resizingRow, newHeight);
74             }
75         }
76         dragging = true;
77     }
78     
79    @Override
80    public void mouseReleased(MouseEvent e) {
81        if (dragging) {
82            int mouseY = e.getY();
83            int newHeight = mouseY - mouseYOffset;
84            System.out.println(newHeight);
85            onResize(resizingRow, newHeight);
86            dragging = false;
87        }
88    }
89     
90     public void onResize(int row, int height) {
91         
92     }
93     
94 }