KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > diff > DiffActionTooltipWindow


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.diff;
20
21 import org.netbeans.api.diff.Difference;
22
23 import javax.swing.*;
24 import java.awt.*;
25 import java.awt.event.AWTEventListener JavaDoc;
26 import java.awt.event.MouseEvent JavaDoc;
27
28 /**
29  * @author Maros Sandor
30  */

31 class DiffActionTooltipWindow implements AWTEventListener JavaDoc {
32
33     private static final int SCREEN_BORDER = 20;
34     
35     private JWindow actionsWindow;
36     private JWindow contentWindow;
37
38     private final DiffSidebar master;
39     private final Difference diff;
40
41     public DiffActionTooltipWindow(DiffSidebar master, Difference diff) {
42         this.master = master;
43         this.diff = diff;
44         Window w = SwingUtilities.windowForComponent(master.getTextComponent());
45         actionsWindow = new JWindow(w);
46         if (diff.getType() != Difference.ADD) {
47             contentWindow = new JWindow(w);
48         }
49     }
50
51     DiffSidebar getMaster() {
52         return master;
53     }
54
55     public void show(Point location) {
56         DiffTooltipActionsPanel tp = new DiffTooltipActionsPanel(this, diff);
57         actionsWindow.add(tp);
58         actionsWindow.pack();
59         actionsWindow.setLocation(location);
60
61         if (contentWindow != null) {
62             DiffTooltipContentPanel cp = new DiffTooltipContentPanel(master.getTextComponent(), master.getMimeType(), diff);
63             contentWindow.add(cp);
64             contentWindow.pack();
65             Dimension dim = contentWindow.getSize();
66             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
67
68             if (location.y + actionsWindow.getHeight() + dim.height + SCREEN_BORDER > screenSize.height) {
69                 dim.height = screenSize.height - (location.y + actionsWindow.getHeight() + SCREEN_BORDER);
70             }
71             if (location.x + dim.width + SCREEN_BORDER > screenSize.width) {
72                 dim.width = screenSize.width - (location.x + SCREEN_BORDER);
73             }
74             contentWindow.setSize(dim);
75
76             contentWindow.setLocation(location.x, location.y + actionsWindow.getHeight() - 1); // slight visual adjustment
77
contentWindow.setVisible(true);
78         }
79
80         actionsWindow.setVisible(true);
81         
82         Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_EVENT_MASK);
83     }
84
85     public void eventDispatched(AWTEvent event) {
86         if (event.getID() == MouseEvent.MOUSE_PRESSED) {
87             onClick(event);
88 /*
89         } else if (event.getID() == KeyEvent.KEY_PRESSED) {
90             if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_ESCAPE) {
91                 shutdown();
92             }
93 */

94         }
95     }
96
97     private void onClick(AWTEvent event) {
98         Component component = (Component) event.getSource();
99         Window w = SwingUtilities.windowForComponent(component);
100         if (w != actionsWindow && (contentWindow == null || w != contentWindow)) shutdown();
101     }
102
103     void shutdown() {
104         Toolkit.getDefaultToolkit().removeAWTEventListener(this);
105         actionsWindow.dispose();
106         if (contentWindow != null) contentWindow.dispose();
107     }
108 }
109
Popular Tags