package com.dwave.util;
/*
* VPopupMenu
*
* Copyright (C) 2000 Cris Sinnott
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import javax.swing.*;
import java.awt.*;
/**
This component extends JPopupMenu and adds a method to display the menu inside the screen,
even if the mouse pointer is near the edge of the screen.
Class created by Cris Sinnott
Source at : http://www.egroups.com/list/advanced-java/md1875700976.html
*/
public class VPopupMenu extends JPopupMenu implements java.io.Serializable {
/**Displays the popUpMenu at a specified position*/
public void show(Component invoker, int x, int y){
Point p = getPopupMenuOrigin(invoker, x, y);
super.show(invoker, p.x, p.y);
}
/**Figures out the sizes needed to calculate the menu position*/
protected Point getPopupMenuOrigin(Component invoker, int x, int y){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension pmSize = this.getSize();
// For the first time the menu is popped up
// the size has not yet been initialised
if(pmSize.width==0){
pmSize=this.getPreferredSize();
}
Point absp = new Point(x,y);
SwingUtilities.convertPointToScreen(absp, invoker);
int aleft = absp.x+pmSize.width;
int abottom = absp.y+pmSize.height;
if(aleft > screenSize.width)
x -= aleft - screenSize.width;
if(abottom > screenSize.height)
y -= abottom - screenSize.height;
return new Point(x,y);
}
}