/*
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.
*
*
* 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.
*
*
*
* To use, call JTable.setUI() 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);
}
}
}