KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > toolbars > ActionsTree


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
20 package org.netbeans.core.windows.view.ui.toolbars;
21
22
23 import java.awt.Cursor JavaDoc;
24 import java.awt.datatransfer.Transferable JavaDoc;
25 import java.awt.dnd.*;
26 import java.io.IOException JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.util.logging.Logger JavaDoc;
29 import javax.swing.JTree JavaDoc;
30 import javax.swing.tree.*;
31 import org.openide.explorer.view.*;
32 import org.openide.nodes.Node;
33
34 /**
35  * A tree displaying a hierarchy of Node in a similar fashion as the TreeView does
36  * except for unnecessary popmenus and drag'n'drop implementation.
37  *
38  * @author Stanislav Aubrecht
39  */

40 public class ActionsTree extends JTree JavaDoc implements DragGestureListener, DragSourceListener {
41     
42     private boolean firstTimeExpand = true;
43     
44     private Cursor JavaDoc dragMoveCursor = DragSource.DefaultMoveDrop;
45     private Cursor JavaDoc dragNoDropCursor = DragSource.DefaultMoveNoDrop;
46     
47     /** Creates a new instance of ActionsTree */
48     public ActionsTree( Node root ) {
49         super( new NodeTreeModel( root ) );
50         setRootVisible( false );
51         getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
52         setCellRenderer( new NodeRenderer() );
53         setShowsRootHandles( true );
54         expandAll();
55         DragSource.getDefaultDragSource().createDefaultDragGestureRecognizer( this, DnDConstants.ACTION_MOVE, this );
56     }
57     
58     private void expandAll() {
59         int i = 0;
60         int j /*, k = tree.getRowCount()*/;
61
62         do {
63             do {
64                 j = getRowCount();
65                 expandRow(i);
66             } while (j != getRowCount());
67
68             i++;
69         } while (i < getRowCount());
70     }
71
72     public void dragGestureRecognized(DragGestureEvent dge) {
73         TreePath path = getPathForLocation( dge.getDragOrigin().x, dge.getDragOrigin().y );
74         if( null != path ) {
75             Object JavaDoc obj = path.getLastPathComponent();
76             if( getModel().isLeaf( obj ) ) {
77                 try {
78                     Node node = Visualizer.findNode( obj );
79                     Transferable JavaDoc t = node.drag();
80                     dge.getDragSource().addDragSourceListener( this );
81                     dge.startDrag( dragNoDropCursor, t );
82                 } catch( IOException JavaDoc e ) {
83                     Logger.getLogger(ActionsTree.class.getName()).log(Level.WARNING, null, e);
84                 }
85             }
86         }
87     }
88
89     public void dragExit(java.awt.dnd.DragSourceEvent JavaDoc dse) {
90         dse.getDragSourceContext().setCursor( dragNoDropCursor );
91     }
92
93     public void dropActionChanged(java.awt.dnd.DragSourceDragEvent JavaDoc dsde) {
94     }
95
96     public void dragOver(java.awt.dnd.DragSourceDragEvent JavaDoc e) {
97         DragSourceContext context = e.getDragSourceContext();
98         int action = e.getDropAction();
99         if ((action & DnDConstants.ACTION_MOVE) != 0) {
100             context.setCursor( dragMoveCursor );
101         } else {
102             context.setCursor( dragNoDropCursor );
103         }
104     }
105
106     public void dragEnter(java.awt.dnd.DragSourceDragEvent JavaDoc dsde) {
107         dragOver( dsde );
108     }
109
110     public void dragDropEnd(java.awt.dnd.DragSourceDropEvent JavaDoc dsde) {
111     }
112 }
113
Popular Tags