KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.nodes.Node;
22
23 import java.awt.Point JavaDoc;
24 import java.awt.dnd.*;
25
26 import javax.swing.JList JavaDoc;
27
28
29 /**
30 *
31 * @author Dafe Simonek, Jiri Rechtacek
32 */

33 class ListViewDragSupport extends ExplorerDragSupport {
34     // Attributes
35

36     /** Holds selected indices - it's here only
37     * as a workaround for sun's bug */

38
39     /*int[] oldSelection;
40     int[] curSelection;*/

41
42     // Associations
43

44     /** The view that manages viewing the data in a tree. */
45     protected ListView view;
46
47     /** The tree which we are supporting (our client) */
48     protected JList JavaDoc list;
49
50     // Operations
51

52     /** Creates new TreeViewDragSupport, initializes gesture */
53     public ListViewDragSupport(ListView view, JList JavaDoc list) {
54         this.comp = list;
55         this.view = view;
56         this.list = list;
57     }
58
59     int getAllowedDropActions() {
60         return view.getAllowedDropActions();
61     }
62
63     protected int getAllowedDragActions() {
64         return view.getAllowedDragActions();
65     }
66
67     /** Initiating the drag */
68     public void dragGestureRecognized(DragGestureEvent dge) {
69         super.dragGestureRecognized(dge);
70     }
71
72     /** Utility method. Returns either selected nodes in the list
73     * (if cursor hotspot is above some selected node) or the node
74     * the cursor points to.
75     * @return Node array or null if position of the cursor points
76     * to no node.
77     */

78     Node[] obtainNodes(DragGestureEvent dge) {
79         Point JavaDoc dragOrigin = dge.getDragOrigin();
80         int index = list.locationToIndex(dge.getDragOrigin());
81         Object JavaDoc obj = list.getModel().getElementAt(index);
82
83         if (obj instanceof VisualizerNode) {
84             obj = ((VisualizerNode) obj).node;
85         }
86
87         // check conditions
88
if ((index < 0)) {
89             return null;
90         }
91
92         if (!(obj instanceof Node)) {
93             return null;
94         }
95
96         Node[] result = null;
97
98         if (list.isSelectedIndex(index)) {
99             // cursor is above selection, so return all selected indices
100
Object JavaDoc[] selected = list.getSelectedValues();
101             result = new Node[selected.length];
102
103             for (int i = 0; i < selected.length; i++) {
104                 if (selected[i] instanceof VisualizerNode) {
105                     result[i] = ((VisualizerNode) selected[i]).node;
106                 } else {
107                     if (!(selected[i] instanceof Node)) {
108                         return null;
109                     }
110
111                     result[i] = (Node) selected[i];
112                 }
113             }
114         } else {
115             // return only the node the cursor is above
116
result = new Node[] { (Node) obj };
117         }
118
119         return result;
120     }
121 }
122  // end of ListViewDragSupport
123
Popular Tags