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 org.openide.explorer.ExplorerManager;
044: import org.openide.nodes.Node;
045:
046: import java.awt.Component;
047: import java.awt.dnd.*;
048:
049: import javax.swing.JTree;
050: import javax.swing.SwingUtilities;
051: import javax.swing.tree.*;
052:
053: /** Support for the drag operations in the TreeView.
054: *
055: * @author Dafe Simonek, Jiri Rechtacek
056: */
057: final class TreeViewDragSupport extends ExplorerDragSupport {
058: // Attributes
059: // Associations
060:
061: /** The view that manages viewing the data in a tree. */
062: protected TreeView view;
063:
064: /** The tree which we are supporting (our client) */
065: private JTree tree;
066:
067: /** Cell renderer - PENDING - do we need it? */
068:
069: //protected DnDTreeViewCellRenderer cellRenderer;
070: // Operations
071: /** Creates new TreeViewDragSupport, initializes gesture */
072: public TreeViewDragSupport(TreeView view, JTree tree) {
073: this .view = view;
074: this .comp = tree;
075: this .tree = tree;
076: }
077:
078: public int getAllowedDragActions() {
079: return view.getAllowedDragActions();
080: }
081:
082: int getAllowedDropActions() {
083: return view.getAllowedDropActions();
084: }
085:
086: public void dragGestureRecognized(DragGestureEvent dge) {
087: super .dragGestureRecognized(dge);
088:
089: // notify tree cell editor that DnD operationm is active
090: if (exDnD.isDnDActive()) {
091: TreeCellEditor tce = ((JTree) tree).getCellEditor();
092:
093: if (tce instanceof TreeViewCellEditor) {
094: ((TreeViewCellEditor) tce).setDnDActive(true);
095: }
096: }
097: }
098:
099: public void dragDropEnd(DragSourceDropEvent dsde) {
100: // get the droped nodes
101: Node[] dropedNodes = exDnD.getDraggedNodes();
102: super .dragDropEnd(dsde);
103:
104: // if any original glass pane was stored (the DnD was broken e.g. by Esc)
105: if (DropGlassPane.isOriginalPaneStored()) {
106: // give back the orig glass pane
107: DropGlassPane.putBackOriginal();
108:
109: // DnD is not active
110: exDnD.setDnDActive(false);
111: }
112:
113: // select the droped nodes
114: try {
115: if (dropedNodes != null) {
116: ExplorerManager.Provider panel = (ExplorerManager.Provider) SwingUtilities
117: .getAncestorOfClass(
118: ExplorerManager.Provider.class, view);
119:
120: if (panel != null) {
121: panel.getExplorerManager().setSelectedNodes(
122: dropedNodes);
123: }
124: }
125: } catch (Exception e) {
126: // don't care
127: }
128:
129: // notify tree cell editor that DnD operationm is active
130: // no more
131: TreeCellEditor tce = tree.getCellEditor();
132:
133: if (tce instanceof TreeViewCellEditor) {
134: ((TreeViewCellEditor) tce).setDnDActive(false);
135: }
136: }
137:
138: /** Utility method. Returns either selected nodes in tree
139: * (if cursor hotspot is above some selected node) or the node
140: * the cursor points to.
141: * @return Node array or null if position of the cursor points
142: * to no node.
143: */
144: Node[] obtainNodes(DragGestureEvent dge) {
145: TreePath[] tps = tree.getSelectionPaths();
146:
147: if (tps == null) {
148: return null;
149: }
150:
151: Node[] result = new Node[tps.length];
152:
153: int cnt = 0;
154:
155: for (int i = 0; i < tps.length; i++) {
156: if (tree.getPathBounds(tps[i])
157: .contains(dge.getDragOrigin())) {
158: cnt++;
159: }
160:
161: result[i] = DragDropUtilities.secureFindNode(tps[i]
162: .getLastPathComponent());
163: }
164:
165: // #41954:
166: // if the drag source is not at all in path location, do not return
167: // any nodes
168: return (cnt == 0) ? null : result;
169: }
170: }
171: /* end class TreeViewDragSupport */
|