1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.ui.swing.components;
20
21 import java.awt.event.MouseEvent;
22
23 import javax.swing.event.MouseInputListener;
24 import javax.swing.plaf.basic.BasicTableUI;
25
26 /***
27 * BetterTableUI allows a JTable to be disabled by
28 * listening for MouseEvents and then forwarding them
29 * to the usual MouseInputHandler only if the table
30 * is enabled. <BR><BR>
31 *
32 * This class also works around a bug where an editable
33 * table's selection is changed when clicking in an edit
34 * cell while the control key is down. This typically
35 * happened while users were copying/pasting data from
36 * cell to cell. <BR><BR>
37 *
38 * To use, call <code>JTable.setUI()</code> on any
39 * JTable with a BetterTableUI as the parameter.
40 *
41 * @author michael@mpowers.net
42 * @version $Revision: 904 $
43 */
44 public class BetterTableUI extends BasicTableUI implements MouseInputListener
45 {
46 /***
47 * The listener to get all mouse events when the table is enabled.
48 */
49 protected MouseInputListener delegateHandler;
50
51 /***
52 * Overridden to set self as mouse listener and create delegate.
53 */
54 protected MouseInputListener createMouseInputListener()
55 {
56
57 delegateHandler = new MouseInputHandler();
58
59 return this;
60 }
61
62
63
64 public void mouseClicked(MouseEvent event)
65 {
66 if ( (table!=null) && (table.isEnabled()) )
67 {
68 delegateHandler.mouseClicked(event);
69 }
70 }
71
72 public void mouseDragged(MouseEvent event)
73 {
74 if ( (table!=null) && (table.isEnabled()) )
75 {
76 delegateHandler.mouseDragged(event);
77 }
78 }
79
80 public void mouseEntered(MouseEvent event)
81 {
82 if ( (table!=null) && (table.isEnabled()) )
83 {
84 delegateHandler.mouseEntered(event);
85 }
86 }
87
88 public void mouseExited(MouseEvent event)
89 {
90 if ( (table!=null) && (table.isEnabled()) )
91 {
92 delegateHandler.mouseExited(event);
93 }
94 }
95
96 public void mouseMoved(MouseEvent event)
97 {
98 if ( (table!=null) && (table.isEnabled()) )
99 {
100 delegateHandler.mouseMoved(event);
101 }
102 }
103
104 public void mousePressed(MouseEvent event)
105 {
106 if ( (table!=null) && (table.isEnabled()) )
107 {
108
109 if ( table.isEditing() && event.isControlDown() ) return;
110
111 delegateHandler.mousePressed(event);
112 }
113 }
114
115 public void mouseReleased(MouseEvent event)
116 {
117 if ( (table!=null) && (table.isEnabled()) )
118 {
119 delegateHandler.mouseReleased(event);
120 }
121 }
122 }
123