1 19 package org.netbeans.modules.refactoring.impl; 20 21 import java.awt.datatransfer.Transferable ; 22 import java.io.IOException ; 23 import java.util.Hashtable ; 24 import javax.swing.Action ; 25 import javax.swing.SwingUtilities ; 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 41 42 public class ClipboardConvertor implements Convertor { 43 44 public Transferable convert(Transferable t) { 45 Node[] nodes = NodeTransfer.nodes(t, NodeTransfer.CLIPBOARD_CUT); 46 47 if (nodes!=null && nodes.length>0) { 48 InstanceContent ic = new InstanceContent(); 50 for (Node n:nodes) { 51 ic.add((n)); 52 } 53 Hashtable d = new Hashtable (); 54 ic.add(d); 55 Lookup l = new AbstractLookup(ic); 56 Action 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 InstanceContent ic = new InstanceContent(); 67 for (Node n:nodes) { 68 ic.add((n)); 69 } 70 Hashtable d = new Hashtable (); 71 ic.add(d); 72 Lookup l = new AbstractLookup(ic); 73 Action 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 delegate; 86 private InstanceContent ic; 87 private Action refactor; 88 private Hashtable d; 89 RefactoringPaste(Transferable t, InstanceContent ic, Action refactor, Hashtable 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 orig, Node target) { 105 d.clear(); 106 d.put("target", target); d.put("transferable", orig); } 109 110 public boolean canHandle() { 111 if (refactor==null) 112 return false; 113 return refactor.isEnabled(); 114 } 115 public Transferable paste() throws IOException { 116 SwingUtilities.invokeLater(new Runnable () { 117 public void run() { 118 if (refactor!=null) { 119 refactor.actionPerformed(null); 120 } 121 }; 122 }); 123 return null; 124 } 125 public String getName() { 126 return NbBundle.getMessage(UIUtilities.class,"Actions/Refactoring") + " " + refactor.getValue(Action.NAME); 127 } 128 } 129 } 130 } 131 | Popular Tags |