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:
042: package org.netbeans.modules.palette;
043:
044: import java.awt.datatransfer.Transferable;
045: import java.io.IOException;
046: import javax.swing.Action;
047: import org.netbeans.spi.palette.PaletteController;
048: import org.netbeans.spi.palette.PaletteActions;
049: import org.netbeans.spi.palette.DragAndDropHandler;
050: import org.openide.nodes.*;
051: import org.openide.util.HelpCtx;
052: import org.openide.util.Lookup;
053: import org.openide.util.datatransfer.ExTransferable;
054: import org.openide.util.datatransfer.PasteType;
055:
056: /**
057: * A Node for palette item.
058: *
059: * @author S. Aubrecht
060: */
061: class ItemNode extends FilterNode {
062:
063: private Action[] actions;
064:
065: public ItemNode(Node originalNode) {
066: super (originalNode, Children.LEAF);
067: }
068:
069: public Action[] getActions(boolean context) {
070: if (actions == null) {
071: Node n = getParentNode();
072: actions = new Action[] { new Utils.CutItemAction(this ),
073: new Utils.CopyItemAction(this ),
074: new Utils.PasteItemAction(n), null,
075: new Utils.RemoveItemAction(this ), null,
076: new Utils.SortItemsAction(n), null,
077: new Utils.RefreshPaletteAction() };
078: }
079: PaletteActions customActions = getCustomActions();
080: if (null != customActions) {
081: return Utils.mergeActions(actions, customActions
082: .getCustomItemActions(getLookup()));
083: }
084: return actions;
085: }
086:
087: public Transferable clipboardCut() throws java.io.IOException {
088: ExTransferable t = ExTransferable.create(super .clipboardCut());
089:
090: customizeTransferable(t);
091: t.put(createTransferable());
092:
093: return t;
094: }
095:
096: public Transferable clipboardCopy() throws IOException {
097: ExTransferable t = ExTransferable.create(super .clipboardCopy());
098:
099: customizeTransferable(t);
100: t.put(createTransferable());
101:
102: return t;
103: }
104:
105: public PasteType getDropType(Transferable t, int action, int index) {
106: return null;
107: }
108:
109: public Transferable drag() throws IOException {
110: ExTransferable t = ExTransferable.create(super .drag());//NodeTransfer.transferable(this, NodeTransfer.DND_MOVE) );
111:
112: customizeTransferable(t);
113: t.put(createTransferable());
114:
115: return t;
116: }
117:
118: private ExTransferable.Single createTransferable() {
119: final Lookup lkp = getLookup();
120: return new ExTransferable.Single(
121: PaletteController.ITEM_DATA_FLAVOR) {
122: public Object getData() {
123: return lkp;
124: }
125: };
126: }
127:
128: private void customizeTransferable(ExTransferable t) {
129: DragAndDropHandler tp = getTransferableProvider();
130: if (null != tp) {
131: tp.customize(t, getLookup());
132: }
133: }
134:
135: private PaletteActions getCustomActions() {
136: Node category = getParentNode();
137: assert null != category;
138: Node root = category.getParentNode();
139: assert null != root;
140: return (PaletteActions) root.getLookup().lookup(
141: PaletteActions.class);
142: }
143:
144: private DragAndDropHandler getTransferableProvider() {
145: Node category = getParentNode();
146: assert null != category;
147: Node root = category.getParentNode();
148: assert null != root;
149: return (DragAndDropHandler) root.getLookup().lookup(
150: DragAndDropHandler.class);
151: }
152:
153: public Action getPreferredAction() {
154:
155: PaletteActions customActions = getCustomActions();
156:
157: if (null == customActions)
158: return null;
159: ;
160:
161: return customActions.getPreferredAction(getLookup());
162: }
163:
164: public boolean canDestroy() {
165:
166: return !Utils.isReadonly(getOriginal());
167: }
168:
169: Node getOriginalNode() {
170: return getOriginal();
171: }
172:
173: public HelpCtx getHelpCtx() {
174: return Utils.getHelpCtx(this, super.getHelpCtx());
175: }
176: }
|