Copy Nodes in Live DOM View to Clipboard

The new HTML5 capabilities in NetBeans IDE, a.k.a. Project Easel, can be extended in many different ways. For example, you can add new Actions to the HTML Editor, JavaScript Editor, or CSS Editor.

But that's always been possible. Less well known is that you can also extend the live DOM view with new Actions, via registering Actions in the new Navigation/DOM/Actions folder:

That's a new Action I added for copying the selected DOM Nodes to the clipboard from where, as can be seen in the new Clipboard History viewer, they can be retrieved:

If a single Node is selected, the Action is shown together with the default Actions shown in the DOM Viewer:

The code:

package org.demo.feature;

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 java.util.Collections;
import java.util.List;
import org.netbeans.modules.html.editor.lib.api.SourceElementHandle;
import org.netbeans.modules.parsing.api.ParserManager;
import org.netbeans.modules.parsing.api.ResultIterator;
import org.netbeans.modules.parsing.api.Source;
import org.netbeans.modules.parsing.api.UserTask;
import org.netbeans.modules.parsing.spi.ParseException;
import org.netbeans.modules.parsing.spi.Parser;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.filesystems.FileObject;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.datatransfer.ExClipboard;

        category = "Edit",
        id = "org.demo.feature.CopyDOMToClipboardAction")
        lazy = true,
        displayName = "#CTL_COPYNODES")
        path = "Navigation/DOM/Actions", 
        position = 0)
@Messages("CTL_COPYNODES=Copy DOM to Clipboard")
public final class CopyDOMToClipboardAction implements ActionListener {

    private Clipboard clipboard;
    private final List<Node> context;

    public CopyDOMToClipboardAction(List<Node> nodes) {
        this.context = nodes;
        clipboard = Lookup.getDefault().lookup(ExClipboard.class);
        if (clipboard == null) {
            clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

    public void actionPerformed(ActionEvent e) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < context.size(); i++) {
            Node node = context.get(i);
            final SourceElementHandle sourceElementHandle = 
            final FileObject file = sourceElementHandle.getFileObject();
            Source source = Source.create(file);
            try {
                ParserManager.parse(Collections.singleton(source), new UserTask() {
                    public void run(ResultIterator resultIterator) throws Exception {
                        Parser.Result result = resultIterator.getParserResult();
                        org.netbeans.modules.html.editor.lib.api.elements.Node resolved =
                        String text = resolved.image().toString();
            } catch (ParseException ex) {
    private void setClipboardContents(String content) {
        if (clipboard != null) {
            if (content == null) {
                clipboard.setContents(null, null);
            } else {
                clipboard.setContents(new StringSelection(content), null);


Post a Comment:
  • HTML Syntax: NOT allowed

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.


« February 2015