KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > explorer > view > DropGlassPane


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.openide.explorer.view;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Graphics JavaDoc;
23 import java.awt.Rectangle JavaDoc;
24 import java.awt.geom.Line2D JavaDoc;
25
26 import java.util.HashMap JavaDoc;
27
28 import javax.swing.JComponent JavaDoc;
29 import javax.swing.JPanel JavaDoc;
30 import javax.swing.JTree JavaDoc;
31 import javax.swing.UIManager JavaDoc;
32
33
34 /**
35  * Glass pane which is used for paint of a drop line over <code>JComponent</code>.
36  *
37  * @author Jiri Rechtacek
38  *
39  * @see java.awt.dnd.DropTarget
40  * @see org.openide.explorer.view.TreeViewDropSupport
41  */

42 final class DropGlassPane extends JPanel JavaDoc {
43     private static HashMap JavaDoc<Integer JavaDoc, DropGlassPane> map = new HashMap JavaDoc<Integer JavaDoc, DropGlassPane>();
44     final static private int MIN_X = 5;
45     final static private int MIN_Y = 3;
46     final static private int MIN_WIDTH = 10;
47     final static private int MIN_HEIGTH = 3;
48     transient static private Component JavaDoc oldPane;
49     transient static private JTree JavaDoc originalSource;
50     transient static private boolean wasVisible;
51     Line2D JavaDoc line = null;
52
53     private DropGlassPane() {
54     }
55
56     /** Check the bounds of given line with the bounds of this pane. Optionally
57      * calculate the new bounds in current pane's boundary.
58      * @param comp
59      * @return */

60     synchronized static public DropGlassPane getDefault(JComponent JavaDoc comp) {
61         Integer JavaDoc id = new Integer JavaDoc(System.identityHashCode(comp));
62
63         if ((map.get(id)) == null) {
64             DropGlassPane dgp = new DropGlassPane();
65             dgp.setOpaque(false);
66             map.put(id, dgp);
67         }
68
69         return map.get(id);
70     }
71
72     /** Stores the original glass pane on given tree.
73      * @param source the active container
74      * @param pane the original glass
75      * @param visible was glass pane visible
76      */

77     static void setOriginalPane(JTree JavaDoc source, Component JavaDoc pane, boolean visible) {
78         // pending, should throw an exception that original is set already
79
oldPane = pane;
80         originalSource = source;
81         wasVisible = visible;
82     }
83
84     /** Is any original glass pane stored?
85      * @return true if true; false otherwise
86      */

87     static boolean isOriginalPaneStored() {
88         return oldPane != null;
89     }
90
91     /** Sets the original glass pane to the root pane of stored container.
92      */

93     static void putBackOriginal() {
94         if (oldPane == null) {
95             // pending, should throw an exception
96
return;
97         }
98
99         originalSource.getRootPane().setGlassPane(oldPane);
100         oldPane.setVisible(wasVisible);
101         oldPane = null;
102     }
103
104     /** Unset drop line if setVisible to false.
105      * @param boolean aFlag new state */

106     public void setVisible(boolean aFlag) {
107         super.setVisible(aFlag);
108
109         if (!aFlag) {
110             setDropLine(null);
111         }
112     }
113
114     /** Set drop line. Given line is used by paint method.
115      * @param line drop line */

116     public void setDropLine(Line2D JavaDoc line) {
117         this.line = line;
118
119         //repaint ();
120
}
121
122     /** Check the bounds of given line with the bounds of this pane. Optionally
123      * calculate the new bounds in current pane's boundary.
124      * @param line a line for check
125      * @return a line with bounds inside the pane's boundary */

126     private Line2D JavaDoc checkLineBounds(Line2D JavaDoc line) {
127         Rectangle JavaDoc bounds = getBounds();
128         double startPointX;
129         double startPointY;
130         double endPointX;
131         double endPointY;
132
133         // check start point
134
startPointX = Math.max(line.getX1(), bounds.x + MIN_X);
135         startPointY = Math.max(line.getY1(), bounds.y + MIN_Y);
136
137         // check end point
138
endPointX = Math.min(line.getX2(), (bounds.x + bounds.width) - MIN_WIDTH);
139         endPointY = Math.min(line.getY2(), (bounds.y + bounds.height) - MIN_HEIGTH);
140
141         // set new bounds
142
line.setLine(startPointX, startPointY, endPointX, endPointY);
143
144         return line;
145     }
146
147     /** Paint drop line on glass pane.
148      * @param Graphics g Obtained graphics */

149     public void paint(Graphics JavaDoc g) {
150         if (line != null) {
151             // check bounds
152
line = checkLineBounds(line);
153
154             int x1 = (int) line.getX1();
155             int x2 = (int) line.getX2();
156             int y1 = (int) line.getY1();
157
158             g.setColor( UIManager.getColor( "Tree.selectionBackground" ) );
159
160             // int y2 = (int)line.getY2 (); actually not used
161
// LINE
162
g.drawLine(x1 + 2, y1, x2 - 2, y1);
163             g.drawLine(x1 + 2, y1 + 1, x2 - 2, y1 + 1);
164
165             // RIGHT
166
g.drawLine(x1, y1 - 2, x1, y1 + 3);
167             g.drawLine(x1 + 1, y1 - 1, x1 + 1, y1 + 2);
168
169             // LEFT
170
g.drawLine(x2, y1 - 2, x2, y1 + 3);
171             g.drawLine(x2 - 1, y1 - 1, x2 - 1, y1 + 2);
172         }
173
174         // help indication of glass pane for debugging
175

176         /*g.drawLine (0, getBounds ().height / 2, getBounds ().width, getBounds ().height / 2);
177         g.drawLine (0, getBounds ().height / 2+1, getBounds ().width, getBounds ().height / 2+1);
178         g.drawLine (getBounds ().width / 2, 0, getBounds ().width / 2, getBounds ().height);
179         g.drawLine (getBounds ().width / 2+1, 0, getBounds ().width / 2+1, getBounds ().height);
180          */

181     }
182 }
183
Popular Tags