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: Contetxtual Menu changes selection



I tried to search for the link to this post, but was unable to find one, so here is the code that I use to circumvent this problem. Note that it is not my code, and solutions were given to fix this from Sun's, Apple's and a developer's standpoint, which was very cool, but has largely been ignored by the first two (whether or not that's because they were never told, I do not know). So I'm just going to post the developer solution.

Since I am on neither team :-) I used a workaround that looked like
this. Inside any JTable that I am using I override updateUI() like this:


  public void updateUI() {
    super.updateUI();
    ForwardingTableUIMouseListener.fixMouseListeners(this);
  }

And then add a class ForwardingTableUIMouseListener to delegate as
appropriate and filter out the popup triggers. This class looks like
this:

/**
 * Copyright (C) 2002 Smartspread Ltd.
 * All rights reserved.
 *
 * Created: 15-Oct-02 14:23:19
 */

package com.smartspread.smartspread.impl;

import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.util.EventListener;
import javax.swing.JTable;
import javax.swing.plaf.basic.BasicTableUI;

/**
* This class exists to deal with a defect that has fallen between the Apple/Sun
* cracks for MacOS Java with selection changes on Alt-click mouse clicks.
* Currently Alt-clicks don't get ignored and so selection
* changes when they occur. Until this gets fixed by Sun or Apple we add a new
* ignore for the MouseHandler from BasicTableUI, and delegate everything else.
*
* There is no defect report in the Sun Bug database as they don't allow you to
* submit Mac defects. There is no defect report for Mac as I can't work out
* how to post one. Sorry for the lack of references.
*/
public class ForwardingTableUIMouseListener implements MouseListener {
private MouseListener _delegate;


public static void fixMouseListeners(JTable table) {
// in general there will only be one, but removing and then readding all is
// more correct, and in the case where there is only one isn't a big
// efficiency hit
EventListener [] listeners = table.getListeners(MouseListener.class);
// this remove may seem unnecessary but keeps all MouseListeners in the same
// order
for (int i = 0; i < listeners.length; i++) {
table.removeMouseListener((MouseListener)listeners[i]);
}
// now add them back on, wrapping any as necessary
for (int i = 0; i < listeners.length; i++) {
MouseListener listener = (MouseListener)listeners[i];
if(listener instanceof BasicTableUI.MouseInputHandler) {
table.addMouseListener(new ForwardingTableUIMouseListener(listener));
} else {
table.addMouseListener(listener);
}
}
}


  private ForwardingTableUIMouseListener(MouseListener delegate) {
    _delegate = delegate;
  }

  protected boolean shouldIgnore(MouseEvent e) {
    return e.isPopupTrigger();
  }

  public void mouseClicked(MouseEvent e) {
    if(!shouldIgnore(e)) {
      _delegate.mouseClicked(e);
    }
  }

  public void mousePressed(MouseEvent e) {
    if(!shouldIgnore(e)) {
      _delegate.mousePressed(e);
    }
  }

  public void mouseReleased(MouseEvent e) {
    if(!shouldIgnore(e)) {
      _delegate.mouseReleased(e);
    }
  }

  public void mouseEntered(MouseEvent e) {
    _delegate.mouseEntered(e);
  }

  public void mouseExited(MouseEvent e) {
    _delegate.mouseExited(e);
  }
}

This has worked perfectly for me, and will allow you to bring up the contextual menu on a control click in a JTable (do the same type of thing for JTree too) that has a multiple selection (without changing it).


Hope this helps,

J
--
Julian Wood <email@hidden>

Programmer/Analyst
University of Calgary

http://commons.ucalgary.ca

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Contetxtual Menu changes selection (From: Werner Randelshofer <email@hidden>)
 >Re: Contetxtual Menu changes selection (From: Paul Taylor <email@hidden>)
 >Re: Contetxtual Menu changes selection (From: Paul Taylor <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.