KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ui > 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.netbeans.modules.palette.ui;
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
32
33 /**
34  * Glass pane which is used for paint of a drop line over <code>JComponent</code>.
35  *
36  * @author Jiri Rechtacek, S. Aubrecht
37  *
38  * @see java.awt.dnd.DropTarget
39  * @see org.openide.explorer.view.TreeViewDropSupport
40  */

41 final class DropGlassPane extends JPanel JavaDoc {
42     static private HashMap JavaDoc<Integer JavaDoc,DropGlassPane> map = new HashMap JavaDoc<Integer JavaDoc,DropGlassPane>();
43     final static private int MIN_X = 0;//5;
44
final static private int MIN_Y = 0;//3;
45
final static private int MIN_WIDTH = 0;//10;
46
final static private int MIN_HEIGTH = 0;//3;
47
static private Component JavaDoc oldPane;
48     static private JComponent JavaDoc originalSource;
49     static private boolean wasVisible;
50     Line2D JavaDoc line = null;
51     Rectangle JavaDoc prevLineRect = 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 = Integer.valueOf(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( JComponent 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         if( !isValid() )
118             return;
119         
120         if( null != prevLineRect
121             && ((null != line
122                 && (!prevLineRect.contains( line.getP1() )
123                     || !prevLineRect.contains( line.getP2() )))
124                 || null == line) ) {
125             
126             repaint( prevLineRect );
127         }
128
129         this.line = line;
130         Rectangle JavaDoc newLineRect = null;
131         if( null != this.line ) {
132             checkLineBounds( this.line );
133             newLineRect = line.getBounds();
134             newLineRect.grow( 5, 5 );
135         }
136         
137         if( null != newLineRect && !newLineRect.equals( prevLineRect ) ) {
138             repaint( newLineRect );
139         }
140         prevLineRect = newLineRect;
141     }
142
143     /** Check the bounds of given line with the bounds of this pane. Optionally
144      * calculate the new bounds in current pane's boundary.
145      * @param line a line for check
146      * @return a line with bounds inside the pane's boundary */

147     private Line2D JavaDoc checkLineBounds(Line2D JavaDoc line) {
148         Rectangle JavaDoc bounds = getBounds();
149         double startPointX;
150         double startPointY;
151         double endPointX;
152         double endPointY;
153         
154         // check start point
155
startPointX = Math.max(line.getX1(), bounds.x + MIN_X);
156         startPointY = Math.max(line.getY1(), bounds.y + MIN_Y);
157
158         // check end point
159
endPointX = Math.min(line.getX2(), (bounds.x + bounds.width));// - MIN_WIDTH);
160
endPointY = Math.min(line.getY2(), (bounds.y + bounds.height) - MIN_HEIGTH);
161
162         // set new bounds
163
line.setLine(startPointX, startPointY, endPointX, endPointY);
164
165         return line;
166     }
167
168     /** Paint drop line on glass pane.
169      * @param Graphics g Obtained graphics */

170     public void paint(Graphics JavaDoc g) {
171         if (line != null) {
172         
173             int x1 = (int) line.getX1();
174             int x2 = (int) line.getX2();
175             int y1 = (int) line.getY1();
176             int y2 = (int)line.getY2 ();
177                     
178             if( y1 == y2 ) {
179                 // LINE
180
g.drawLine(x1 + 2, y1, x2 - 2, y1);
181                 g.drawLine(x1 + 2, y1 + 1, x2 - 2, y1 + 1);
182
183                 // RIGHT
184
g.drawLine(x1, y1 - 2, x1, y1 + 3);
185                 g.drawLine(x1 + 1, y2 - 1, x1 + 1, y1 + 2);
186
187                 // LEFT
188
g.drawLine(x2, y1 - 2, x2, y1 + 3);
189                 g.drawLine(x2 - 1, y1 - 1, x2 - 1, y1 + 2);
190             } else {
191                 // LINE
192
g.drawLine(x1, y1 + 2, x2, y2 - 2);
193                 g.drawLine(x1 + 1, y1 + 2, x2 + 1, y2 - 2);
194
195                 // RIGHT
196
g.drawLine(x1 - 2, y1, x1 + 3, y1);
197                 g.drawLine(x1 - 1, y1 + 1, x1 + 2, y1 + 1);
198
199                 // LEFT
200
g.drawLine(x2 - 2, y2, x2 + 3, y2);
201                 g.drawLine(x2 - 1, y2 - 1, x2 + 2, y2 - 1);
202             }
203         }
204     }
205 }
206
Popular Tags