Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: JTable/ListSelectionModel right-click issues



On Tuesday, February 24, 2004, at 02:32 PM, Tim Boudreau wrote:

Well, not the only way...an ugly expedient hack I've used a
couple times (where we have to support multiple look and
feels and, e.g., GTK's delegates are usually non-subclassable):

private boolean inSetUI = false;
public void setUI(nnnUI ui) {
inSetUI = true;
try {
super.setUI(ui);
} finally {
inSetUI = false;
}
}

public void addMouseListener (MouseListener ml) {
if (!inSetUI) {
super.addMouseListener(ml);
}
}

I'm not saying this is a great idea or something you should do
without knowing the consequences, but if the mouse behavior
for your component is well-defined *on all platforms*, it will
solve the problem.

I wrote a little test code to try this out, and after some work I
came up with some code that will allow me to tail-patch the mouse
listener installed by the UI Peer. I extended JTable, because that
is what we use, similar code should work for JList and JTree too,
although the listener class names will differ (put a println into
addMouseListener to see what you need to use). I use the class name
because JTable installs two different mouse listeners from under
setUI. Class MouseListenerSelectProxy is a mouse listener that
filters events for the UI Peer's listener. I set mine up to discard
the event if isPopupTrigger returns true, and to change the modifier
from CMD to CTRL on a CMD-Click (Aqua discontiguous multiple-select)
when using the Metal PLAF, otherwise it simply forwards all events
to the original listener. Watch the constructor on the specialized
component, as addMouseListener is called from the base class
constructor before its own constructor is called. This code depends
on the JVM initializing the proxyMouseListener reference to null
before calling any constructors.

public class MyJTable extends JTable {

private final static String METAL_MOUSE_LISTENER = "javax.swing.plaf.basic.BasicTableUI$MouseInputHandler";
private final static String AQUA_MOUSE_LISTENER = "apple.laf.AquaTableUI$MouseInputHandler";

// DO NOT INITIALIZE proxyMouseListener!!!!!
// It is used in addMouseListener BEFORE the constuctor is run
// (called from the base class constructor). If it is set here
// or in the constructor, it will clobber the value set by the
// call from the base class constructor.
private MouseListenerSelectProxy proxyMouseListener;

public synchronized void addMouseListener (MouseListener ml) {
String mlClass = ml.getClass().getName();
if ((proxyMouseListener == null) && (mlClass.equals(METAL_MOUSE_LISTENER) || mlClass.equals(AQUA_MOUSE_LISTENER))) {
proxyMouseListener = new MouseListenerSelectProxy(ml);
super.addMouseListener (proxyMouseListener);
}
else super.addMouseListener (ml);
}
public synchronized void removeMouseListener (MouseListener ml) {
if ((proxyMouseListener != null) && (ml == proxyMouseListener.getPeer())) {
super.removeMouseListener (proxyMouseListener);
proxyMouseListener = null;
}
else super.removeMouseListener (ml);
}
}
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Do not post admin requests to the list. They will be ignored.


References: 
 >Re: JTable/ListSelectionModel right-click issues (From: Tim Boudreau <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.