Thursday Jun 07, 2012

View Clipboard & Copy To Clipboard from NetBeans IDE

Thanks to this code, I can press Ctrl-Alt-V in NetBeans IDE and then view whatever is in the clipboard:

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JOptionPane;
import org.openide.awt.ActionRegistration;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;

@ActionID(
    category = "Tools",
id = "org.demo.ShowClipboardAction")
@ActionRegistration(
    displayName = "#CTL_ShowClipboardAction")
@ActionReferences({
    @ActionReference(path = "Menu/Tools", position = 5),
    @ActionReference(path = "Shortcuts", name = "DA-V")
})
@Messages("CTL_ShowClipboardAction=Show Clipboard")
public final class ShowClipboardAction implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        JOptionPane.showMessageDialog(null, getClipboard(), "Clipboard Content", 1);
    }

    public String getClipboard() {
        String text = null;
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
        try {
            if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                text = (String) t.getTransferData(DataFlavor.stringFlavor);
            }
        } catch (UnsupportedFlavorException e) {
        } catch (IOException e) {
        }
        return text;
    }
    
}

And now I can also press Ctrl-Alt-C, which copies the path of the current file to the clipboard:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.awt.StatusDisplayer;
import org.openide.loaders.DataObject;
import org.openide.util.NbBundle.Messages;

@ActionID(
    category = "Tools",
id = "org.demo.CopyPathToClipboard")
@ActionRegistration(
    displayName = "#CTL_CopyPathToClipboard")
@ActionReferences({
    @ActionReference(path = "Menu/Tools", position = 0),
    @ActionReference(path = "Editors/Popup", position = 10),
    @ActionReference(path = "Shortcuts", name = "DA-C")
})
@Messages("CTL_CopyPathToClipboard=Copy Path to Clipboard")
public final class CopyPathToClipboardAction implements ActionListener {

    private final DataObject context;

    public CopyPathToClipboardAction(DataObject context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String path = context.getPrimaryFile().getPath();
        StatusDisplayer.getDefault().setStatusText(path);
        StringSelection ss = new StringSelection(path);
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        clipboard.setContents(ss, null);
    }

}
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« June 2012 »
SunMonTueWedThuFriSat
     
23
       
Today