KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > ItemNode


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
21 package org.netbeans.modules.palette;
22
23 import java.awt.datatransfer.Transferable JavaDoc;
24 import java.io.IOException JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import org.netbeans.spi.palette.PaletteController;
27 import org.netbeans.spi.palette.PaletteActions;
28 import org.netbeans.spi.palette.DragAndDropHandler;
29 import org.openide.nodes.*;
30 import org.openide.util.HelpCtx;
31 import org.openide.util.Lookup;
32 import org.openide.util.datatransfer.ExTransferable;
33 import org.openide.util.datatransfer.PasteType;
34
35 /**
36  * A Node for palette item.
37  *
38  * @author S. Aubrecht
39  */

40 class ItemNode extends FilterNode {
41     
42
43     private Action JavaDoc[] actions;
44
45     public ItemNode( Node originalNode ) {
46         super( originalNode, Children.LEAF );
47     }
48
49     public Action JavaDoc[] getActions(boolean context) {
50         if (actions == null) {
51             Node n = getParentNode();
52             actions = new Action JavaDoc[] {
53                 new Utils.CutItemAction( this ),
54                 new Utils.CopyItemAction( this ),
55                 new Utils.PasteItemAction( n ),
56                 null,
57                 new Utils.RemoveItemAction( this ),
58                 null,
59                 new Utils.SortItemsAction( n ),
60                 null,
61                 new Utils.RefreshPaletteAction()
62             };
63         }
64         PaletteActions customActions = getCustomActions();
65         if( null != customActions ) {
66             return Utils.mergeActions( actions, customActions.getCustomItemActions( getLookup() ) );
67         }
68         return actions;
69     }
70
71     public Transferable JavaDoc clipboardCut() throws java.io.IOException JavaDoc {
72         ExTransferable t = ExTransferable.create( super.clipboardCut() );
73         
74         customizeTransferable( t );
75         t.put( createTransferable() );
76         
77         return t;
78     }
79
80     public Transferable JavaDoc clipboardCopy() throws IOException JavaDoc {
81         ExTransferable t = ExTransferable.create( super.clipboardCopy() );
82         
83         customizeTransferable( t );
84         t.put( createTransferable() );
85         
86         return t;
87     }
88
89     public PasteType getDropType( Transferable JavaDoc t, int action, int index ) {
90         return null;
91     }
92
93     public Transferable JavaDoc drag() throws IOException JavaDoc {
94         ExTransferable t = ExTransferable.create( super.drag() );//NodeTransfer.transferable(this, NodeTransfer.DND_MOVE) );
95

96         customizeTransferable( t );
97         t.put( createTransferable() );
98         
99         return t;
100     }
101     
102     private ExTransferable.Single createTransferable() {
103         final Lookup lkp = getLookup();
104         return new ExTransferable.Single( PaletteController.ITEM_DATA_FLAVOR ) {
105            public Object JavaDoc getData () {
106                return lkp;
107            }
108        };
109     }
110     
111     private void customizeTransferable( ExTransferable t ) {
112         DragAndDropHandler tp = getTransferableProvider();
113         if( null != tp ) {
114             tp.customize( t, getLookup() );
115         }
116     }
117
118     private PaletteActions getCustomActions() {
119         Node category = getParentNode();
120         assert null != category;
121         Node root = category.getParentNode();
122         assert null != root;
123         return (PaletteActions)root.getLookup().lookup( PaletteActions.class );
124     }
125
126     private DragAndDropHandler getTransferableProvider() {
127         Node category = getParentNode();
128         assert null != category;
129         Node root = category.getParentNode();
130         assert null != root;
131         return (DragAndDropHandler)root.getLookup().lookup( DragAndDropHandler.class );
132     }
133     
134     public Action JavaDoc getPreferredAction() {
135
136         PaletteActions customActions = getCustomActions();
137         
138         if( null == customActions )
139             return null;;
140         
141         return customActions.getPreferredAction( getLookup() );
142     }
143
144     public boolean canDestroy() {
145
146         return !Utils.isReadonly( getOriginal() );
147     }
148     
149     Node getOriginalNode() {
150         return getOriginal();
151     }
152
153     public HelpCtx getHelpCtx() {
154         return Utils.getHelpCtx( this, super.getHelpCtx() );
155     }
156 }
157
Popular Tags