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.openide.explorer.view;
042:
043: import java.awt.Color;
044: import java.awt.Component;
045: import java.awt.Graphics;
046: import java.awt.Rectangle;
047: import java.awt.geom.Line2D;
048:
049: import java.util.HashMap;
050:
051: import javax.swing.JComponent;
052: import javax.swing.JPanel;
053: import javax.swing.JTree;
054: import javax.swing.UIManager;
055:
056: /**
057: * Glass pane which is used for paint of a drop line over <code>JComponent</code>.
058: *
059: * @author Jiri Rechtacek
060: *
061: * @see java.awt.dnd.DropTarget
062: * @see org.openide.explorer.view.TreeViewDropSupport
063: */
064: final class DropGlassPane extends JPanel {
065: private static HashMap<Integer, DropGlassPane> map = new HashMap<Integer, DropGlassPane>();
066: final static private int MIN_X = 5;
067: final static private int MIN_Y = 3;
068: final static private int MIN_WIDTH = 10;
069: final static private int MIN_HEIGTH = 3;
070: transient static private Component oldPane;
071: transient static private JTree originalSource;
072: transient static private boolean wasVisible;
073: Line2D line = null;
074:
075: private DropGlassPane() {
076: }
077:
078: /** Check the bounds of given line with the bounds of this pane. Optionally
079: * calculate the new bounds in current pane's boundary.
080: * @param comp
081: * @return */
082: synchronized static public DropGlassPane getDefault(JComponent comp) {
083: Integer id = new Integer(System.identityHashCode(comp));
084:
085: if ((map.get(id)) == null) {
086: DropGlassPane dgp = new DropGlassPane();
087: dgp.setOpaque(false);
088: map.put(id, dgp);
089: }
090:
091: return map.get(id);
092: }
093:
094: /** Stores the original glass pane on given tree.
095: * @param source the active container
096: * @param pane the original glass
097: * @param visible was glass pane visible
098: */
099: static void setOriginalPane(JTree source, Component pane,
100: boolean visible) {
101: // pending, should throw an exception that original is set already
102: oldPane = pane;
103: originalSource = source;
104: wasVisible = visible;
105: }
106:
107: /** Is any original glass pane stored?
108: * @return true if true; false otherwise
109: */
110: static boolean isOriginalPaneStored() {
111: return oldPane != null;
112: }
113:
114: /** Sets the original glass pane to the root pane of stored container.
115: */
116: static void putBackOriginal() {
117: if (oldPane == null) {
118: // pending, should throw an exception
119: return;
120: }
121:
122: originalSource.getRootPane().setGlassPane(oldPane);
123: oldPane.setVisible(wasVisible);
124: oldPane = null;
125: }
126:
127: /** Unset drop line if setVisible to false.
128: * @param boolean aFlag new state */
129: public void setVisible(boolean aFlag) {
130: super .setVisible(aFlag);
131:
132: if (!aFlag) {
133: setDropLine(null);
134: }
135: }
136:
137: /** Set drop line. Given line is used by paint method.
138: * @param line drop line */
139: public void setDropLine(Line2D line) {
140: this .line = line;
141:
142: //repaint ();
143: }
144:
145: /** Check the bounds of given line with the bounds of this pane. Optionally
146: * calculate the new bounds in current pane's boundary.
147: * @param line a line for check
148: * @return a line with bounds inside the pane's boundary */
149: private Line2D checkLineBounds(Line2D line) {
150: Rectangle bounds = getBounds();
151: double startPointX;
152: double startPointY;
153: double endPointX;
154: double endPointY;
155:
156: // check start point
157: startPointX = Math.max(line.getX1(), bounds.x + MIN_X);
158: startPointY = Math.max(line.getY1(), bounds.y + MIN_Y);
159:
160: // check end point
161: endPointX = Math.min(line.getX2(), (bounds.x + bounds.width)
162: - MIN_WIDTH);
163: endPointY = Math.min(line.getY2(), (bounds.y + bounds.height)
164: - MIN_HEIGTH);
165:
166: // set new bounds
167: line.setLine(startPointX, startPointY, endPointX, endPointY);
168:
169: return line;
170: }
171:
172: /** Paint drop line on glass pane.
173: * @param Graphics g Obtained graphics */
174: public void paint(Graphics g) {
175: if (line != null) {
176: Color c = UIManager.getColor("Tree.dropLine");
177: if (c != null) {
178: g.setColor(c);
179: }
180:
181: // check bounds
182: line = checkLineBounds(line);
183:
184: int x1 = (int) line.getX1();
185: int x2 = (int) line.getX2();
186: int y1 = (int) line.getY1();
187:
188: g.setColor(UIManager.getColor("Tree.selectionBackground"));
189:
190: // int y2 = (int)line.getY2 (); actually not used
191: // LINE
192: g.drawLine(x1 + 2, y1, x2 - 2, y1);
193: g.drawLine(x1 + 2, y1 + 1, x2 - 2, y1 + 1);
194:
195: // RIGHT
196: g.drawLine(x1, y1 - 2, x1, y1 + 3);
197: g.drawLine(x1 + 1, y1 - 1, x1 + 1, y1 + 2);
198:
199: // LEFT
200: g.drawLine(x2, y1 - 2, x2, y1 + 3);
201: g.drawLine(x2 - 1, y1 - 1, x2 - 1, y1 + 2);
202: }
203:
204: // help indication of glass pane for debugging
205:
206: /*g.drawLine (0, getBounds ().height / 2, getBounds ().width, getBounds ().height / 2);
207: g.drawLine (0, getBounds ().height / 2+1, getBounds ().width, getBounds ().height / 2+1);
208: g.drawLine (getBounds ().width / 2, 0, getBounds ().width / 2, getBounds ().height);
209: g.drawLine (getBounds ().width / 2+1, 0, getBounds ().width / 2+1, getBounds ().height);
210: */
211: }
212: }
|