KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > impl > ClipboardConvertor


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.netbeans.modules.refactoring.impl;
20
21 import java.awt.datatransfer.Transferable JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import javax.swing.Action JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import org.netbeans.modules.refactoring.api.ui.RefactoringActionsFactory;
27 import org.netbeans.modules.refactoring.spi.impl.UIUtilities;
28 import org.openide.nodes.Node;
29 import org.openide.nodes.NodeTransfer;
30 import org.openide.util.Lookup;
31 import org.openide.util.NbBundle;
32 import org.openide.util.datatransfer.ExClipboard.Convertor;
33 import org.openide.util.datatransfer.ExTransferable;
34 import org.openide.util.datatransfer.PasteType;
35 import org.openide.util.lookup.AbstractLookup;
36 import org.openide.util.lookup.InstanceContent;
37
38 /**
39  * @author Jan Becicka
40  */

41
42 public class ClipboardConvertor implements Convertor {
43     
44     public Transferable JavaDoc convert(Transferable JavaDoc t) {
45         Node[] nodes = NodeTransfer.nodes(t, NodeTransfer.CLIPBOARD_CUT);
46         
47         if (nodes!=null && nodes.length>0) {
48             //try to do move refactoring
49
InstanceContent ic = new InstanceContent();
50             for (Node n:nodes) {
51                 ic.add((n));
52             }
53             Hashtable JavaDoc d = new Hashtable JavaDoc();
54             ic.add(d);
55             Lookup l = new AbstractLookup(ic);
56             Action JavaDoc move = RefactoringActionsFactory.moveAction().createContextAwareInstance(l);
57             if (move.isEnabled()) {
58                 ExTransferable tr = ExTransferable.create(t);
59                 tr.put(NodeTransfer.createPaste(new RefactoringPaste(t, ic, move, d)));
60                 return tr;
61             }
62         }
63         nodes = NodeTransfer.nodes(t, NodeTransfer.CLIPBOARD_COPY);
64         if (nodes!=null && nodes.length>0) {
65             //try to do copy refactoring
66
InstanceContent ic = new InstanceContent();
67             for (Node n:nodes) {
68                 ic.add((n));
69             }
70             Hashtable JavaDoc d = new Hashtable JavaDoc();
71             ic.add(d);
72             Lookup l = new AbstractLookup(ic);
73             Action JavaDoc copy = RefactoringActionsFactory.copyAction().createContextAwareInstance(l);
74             if (copy.isEnabled()) {
75                 ExTransferable tr = ExTransferable.create(t);
76                 tr.put(NodeTransfer.createPaste(new RefactoringPaste(t, ic, copy, d)));
77                 return tr;
78             }
79         }
80         return t;
81     }
82     
83     private class RefactoringPaste implements NodeTransfer.Paste {
84         
85         private Transferable JavaDoc delegate;
86         private InstanceContent ic;
87         private Action JavaDoc refactor;
88         private Hashtable JavaDoc d;
89         RefactoringPaste(Transferable JavaDoc t, InstanceContent ic, Action JavaDoc refactor, Hashtable JavaDoc d) {
90             delegate = t;
91             this.ic = ic;
92             this.refactor = refactor;
93             this.d=d;
94         }
95         
96         public PasteType[] types(Node target) {
97             RefactoringPasteType refactoringPaste = new RefactoringPasteType(delegate, target);
98             if (refactoringPaste.canHandle())
99                 return new PasteType[] {refactoringPaste};
100             return new PasteType[0];
101         }
102         
103         private class RefactoringPasteType extends PasteType {
104             RefactoringPasteType(Transferable JavaDoc orig, Node target) {
105                 d.clear();
106                 d.put("target", target); //NOI18N
107
d.put("transferable", orig); //NOI18N
108
}
109             
110             public boolean canHandle() {
111                 if (refactor==null)
112                     return false;
113                 return refactor.isEnabled();
114             }
115             public Transferable JavaDoc paste() throws IOException JavaDoc {
116                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
117                     public void run() {
118                         if (refactor!=null) {
119                             refactor.actionPerformed(null);
120                         }
121                     };
122                 });
123                 return null;
124             }
125             public String JavaDoc getName() {
126                 return NbBundle.getMessage(UIUtilities.class,"Actions/Refactoring") + " " + refactor.getValue(Action.NAME);
127             }
128         }
129     }
130 }
131
Popular Tags