001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.versioning.diff;
042:
043: import org.netbeans.api.diff.Difference;
044:
045: import javax.swing.*;
046: import java.awt.*;
047: import java.awt.event.AWTEventListener;
048: import java.awt.event.MouseEvent;
049:
050: /**
051: * @author Maros Sandor
052: */
053: class DiffActionTooltipWindow implements AWTEventListener {
054:
055: private static final int SCREEN_BORDER = 20;
056:
057: private JWindow actionsWindow;
058: private JWindow contentWindow;
059:
060: private final DiffSidebar master;
061: private final Difference diff;
062:
063: public DiffActionTooltipWindow(DiffSidebar master, Difference diff) {
064: this .master = master;
065: this .diff = diff;
066: Window w = SwingUtilities.windowForComponent(master
067: .getTextComponent());
068: actionsWindow = new JWindow(w);
069: if (diff.getType() != Difference.ADD) {
070: contentWindow = new JWindow(w);
071: }
072: }
073:
074: DiffSidebar getMaster() {
075: return master;
076: }
077:
078: public void show(Point location) {
079: DiffTooltipActionsPanel tp = new DiffTooltipActionsPanel(this ,
080: diff);
081: actionsWindow.add(tp);
082: actionsWindow.pack();
083: actionsWindow.setLocation(location);
084:
085: if (contentWindow != null) {
086: DiffTooltipContentPanel cp = new DiffTooltipContentPanel(
087: master.getTextComponent(), master.getMimeType(),
088: diff);
089: contentWindow.add(cp);
090: contentWindow.pack();
091: Dimension dim = contentWindow.getSize();
092:
093: Rectangle screenBounds = null;
094: GraphicsEnvironment ge = GraphicsEnvironment
095: .getLocalGraphicsEnvironment();
096: GraphicsDevice[] gds = ge.getScreenDevices();
097: for (GraphicsDevice device : gds) {
098: GraphicsConfiguration gc = device
099: .getDefaultConfiguration();
100: screenBounds = gc.getBounds();
101: if (screenBounds.contains(location))
102: break;
103: }
104:
105: if (location.y + dim.height + SCREEN_BORDER > screenBounds.y
106: + screenBounds.height) {
107: dim.height = (screenBounds.y + screenBounds.height)
108: - (location.y + SCREEN_BORDER);
109: }
110: if (location.x + dim.width + SCREEN_BORDER > screenBounds.x
111: + screenBounds.width) {
112: dim.width = (screenBounds.x + screenBounds.width)
113: - (location.x + SCREEN_BORDER);
114: }
115:
116: contentWindow.setSize(dim);
117:
118: contentWindow.setLocation(location.x, location.y
119: + actionsWindow.getHeight() - 1); // slight visual adjustment
120: contentWindow.setVisible(true);
121: }
122:
123: actionsWindow.setVisible(true);
124:
125: Toolkit.getDefaultToolkit().addAWTEventListener(this ,
126: AWTEvent.MOUSE_EVENT_MASK);
127: }
128:
129: public void eventDispatched(AWTEvent event) {
130: if (event.getID() == MouseEvent.MOUSE_PRESSED) {
131: onClick(event);
132: /*
133: } else if (event.getID() == KeyEvent.KEY_PRESSED) {
134: if (((KeyEvent) event).getKeyCode() == KeyEvent.VK_ESCAPE) {
135: shutdown();
136: }
137: */
138: }
139: }
140:
141: private void onClick(AWTEvent event) {
142: Component component = (Component) event.getSource();
143: Window w = SwingUtilities.windowForComponent(component);
144: if (w != actionsWindow
145: && (contentWindow == null || w != contentWindow))
146: shutdown();
147: }
148:
149: void shutdown() {
150: Toolkit.getDefaultToolkit().removeAWTEventListener(this);
151: actionsWindow.dispose();
152: if (contentWindow != null)
153: contentWindow.dispose();
154: }
155: }
|