1 19 package org.netbeans.modules.versioning.system.cvss.ui.history; 20 21 import javax.swing.*; 22 import java.awt.*; 23 import java.awt.event.MouseEvent ; 24 import java.awt.event.AWTEventListener ; 25 import java.awt.event.KeyEvent ; 26 27 32 class TooltipWindow extends JWindow implements AWTEventListener { 33 34 private static final int SCREEN_BORDER = 20; 35 36 private final JComponent content; 37 38 public TooltipWindow(Window parent, JComponent content) { 39 super(parent); 40 this.content = content; 41 } 42 43 public void show(Point location) { 44 this.add(content); 45 this.pack(); 46 Dimension dim = this.getSize(); 47 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 48 49 if (location.y + dim.height + SCREEN_BORDER > screenSize.height) { 50 dim.height = screenSize.height - (location.y + SCREEN_BORDER); 51 } 52 if (location.x + dim.width + SCREEN_BORDER > screenSize.width) { 53 dim.width = screenSize.width - (location.x + SCREEN_BORDER); 54 } 55 this.setSize(dim); 56 this.setLocation(location.x, location.y - 1); this.setVisible(true); 58 59 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.KEY_EVENT_MASK); 60 } 61 62 public void eventDispatched(AWTEvent event) { 63 if (event.getID() == MouseEvent.MOUSE_PRESSED) { 64 onClick(event); 65 } else if (event.getID() == KeyEvent.KEY_PRESSED) { 66 if (((KeyEvent ) event).getKeyCode() == KeyEvent.VK_ESCAPE) { 67 shutdown(); 68 } 69 } 70 } 71 72 private void onClick(AWTEvent event) { 73 Component component = (Component) event.getSource(); 74 Window w = SwingUtilities.windowForComponent(component); 75 if (w != this) shutdown(); 76 } 77 78 void shutdown() { 79 Toolkit.getDefaultToolkit().removeAWTEventListener(this); 80 dispose(); 81 } 82 } 83 | Popular Tags |