summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/components/BetterTableUI.java
blob: cb1d48a7cb5dac3843ff725ccda42615b9fb6157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing.components;

import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputListener;
import javax.swing.plaf.basic.BasicTableUI;

/**
 * BetterTableUI allows a JTable to be disabled by listening for MouseEvents and
 * then forwarding them to the usual MouseInputHandler only if the table is
 * enabled. <BR>
 * <BR>
 *
 * This class also works around a bug where an editable table's selection is
 * changed when clicking in an edit cell while the control key is down. This
 * typically happened while users were copying/pasting data from cell to cell.
 * <BR>
 * <BR>
 *
 * To use, call <code>JTable.setUI()</code> on any JTable with a BetterTableUI
 * as the parameter.
 *
 * @author michael@mpowers.net
 * @version $Revision: 904 $
 */
public class BetterTableUI extends BasicTableUI implements MouseInputListener {
	/**
	 * The listener to get all mouse events when the table is enabled.
	 */
	protected MouseInputListener delegateHandler;

	/**
	 * Overridden to set self as mouse listener and create delegate.
	 */
	protected MouseInputListener createMouseInputListener() {
		// normal handler is a protected inner class of parent
		delegateHandler = new MouseInputHandler();

		return this;
	}

	// interface MouseInputListener

	public void mouseClicked(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			delegateHandler.mouseClicked(event);
		}
	}

	public void mouseDragged(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			delegateHandler.mouseDragged(event);
		}
	}

	public void mouseEntered(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			delegateHandler.mouseEntered(event);
		}
	}

	public void mouseExited(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			delegateHandler.mouseExited(event);
		}
	}

	public void mouseMoved(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			delegateHandler.mouseMoved(event);
		}
	}

	public void mousePressed(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			// workaround bug - control key removes an existing selection
			if (table.isEditing() && event.isControlDown())
				return;

			delegateHandler.mousePressed(event);
		}
	}

	public void mouseReleased(MouseEvent event) {
		if ((table != null) && (table.isEnabled())) {
			delegateHandler.mouseReleased(event);
		}
	}
}