KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > history > TooltipWindow


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
24 import java.awt.event.AWTEventListener JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26
27 /**
28  * Tooltip-like Window.
29  *
30  * @author Maros Sandor
31  */

32 class TooltipWindow extends JWindow implements AWTEventListener JavaDoc {
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); // slight visual adjustment
57
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 JavaDoc) 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