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.netbeans.modules.refactoring.impl;
042:
043: import java.awt.datatransfer.Transferable;
044: import java.io.IOException;
045: import javax.swing.Action;
046: import javax.swing.SwingUtilities;
047: import org.netbeans.modules.refactoring.api.ui.ExplorerContext;
048: import org.netbeans.modules.refactoring.api.ui.RefactoringActionsFactory;
049: import org.netbeans.modules.refactoring.spi.impl.Util;
050: import org.openide.nodes.Node;
051: import org.openide.nodes.NodeTransfer;
052: import org.openide.util.Lookup;
053: import org.openide.util.NbBundle;
054: import org.openide.util.datatransfer.ExClipboard.Convertor;
055: import org.openide.util.datatransfer.ExTransferable;
056: import org.openide.util.datatransfer.PasteType;
057: import org.openide.util.lookup.AbstractLookup;
058: import org.openide.util.lookup.InstanceContent;
059:
060: /**
061: * @author Jan Becicka
062: */
063:
064: public class ClipboardConvertor implements Convertor {
065:
066: public Transferable convert(Transferable t) {
067: Node[] nodes = NodeTransfer
068: .nodes(t, NodeTransfer.CLIPBOARD_CUT);
069:
070: if (nodes != null && nodes.length > 0) {
071: //try to do move refactoring
072: InstanceContent ic = new InstanceContent();
073: for (Node n : nodes) {
074: ic.add((n));
075: }
076: ExplorerContext td = new ExplorerContext();
077: ic.add(td);
078: Lookup l = new AbstractLookup(ic);
079: Action move = RefactoringActionsFactory.moveAction()
080: .createContextAwareInstance(l);
081: if (move.isEnabled()) {
082: ExTransferable tr = ExTransferable.create(t);
083: tr.put(NodeTransfer.createPaste(new RefactoringPaste(t,
084: ic, move, td)));
085: return tr;
086: }
087: }
088: nodes = NodeTransfer.nodes(t, NodeTransfer.CLIPBOARD_COPY);
089: if (nodes != null && nodes.length > 0) {
090: //try to do copy refactoring
091: InstanceContent ic = new InstanceContent();
092: for (Node n : nodes) {
093: ic.add((n));
094: }
095: ExplorerContext td = new ExplorerContext();
096: ic.add(td);
097: Lookup l = new AbstractLookup(ic);
098: Action copy = RefactoringActionsFactory.copyAction()
099: .createContextAwareInstance(l);
100: if (copy.isEnabled()) {
101: ExTransferable tr = ExTransferable.create(t);
102: tr.put(NodeTransfer.createPaste(new RefactoringPaste(t,
103: ic, copy, td)));
104: return tr;
105: }
106: }
107: return t;
108: }
109:
110: private class RefactoringPaste implements NodeTransfer.Paste {
111:
112: private Transferable delegate;
113: private InstanceContent ic;
114: private Action refactor;
115: private ExplorerContext d;
116:
117: RefactoringPaste(Transferable t, InstanceContent ic,
118: Action refactor, ExplorerContext d) {
119: delegate = t;
120: this .ic = ic;
121: this .refactor = refactor;
122: this .d = d;
123: }
124:
125: public PasteType[] types(Node target) {
126: RefactoringPasteType refactoringPaste = new RefactoringPasteType(
127: delegate, target);
128: if (refactoringPaste.canHandle())
129: return new PasteType[] { refactoringPaste };
130: return new PasteType[0];
131: }
132:
133: private class RefactoringPasteType extends PasteType {
134: RefactoringPasteType(Transferable orig, Node target) {
135: d.setTargetNode(target);
136: d.setTransferable(orig);
137: }
138:
139: public boolean canHandle() {
140: if (refactor == null)
141: return false;
142: return refactor.isEnabled();
143: }
144:
145: public Transferable paste() throws IOException {
146: SwingUtilities.invokeLater(new Runnable() {
147: public void run() {
148: if (refactor != null) {
149: refactor.actionPerformed(null);
150: }
151: };
152: });
153: return null;
154: }
155:
156: public String getName() {
157: return NbBundle.getMessage(Util.class,
158: "Actions/Refactoring")
159: + " " + refactor.getValue(Action.NAME);
160: }
161: }
162: }
163: }
|