View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
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          // normal handler is a protected inner class of parent
57          delegateHandler = new MouseInputHandler();
58  
59          return this;
60      }
61  
62      // interface MouseInputListener
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             // workaround bug - control key removes an existing selection
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