Sunday Mar 17, 2013

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;

@ActionID(
        category = "Edit",
        id = "org.demo.feature.CopyDOMToClipboardAction")
@ActionRegistration(
        lazy = true,
        displayName = "#CTL_COPYNODES")
@ActionReference(
        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();
        }
    }

    @Override
    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 = 
                    node.getLookup().lookup(SourceElementHandle.class);
            final FileObject file = sourceElementHandle.getFileObject();
            Source source = Source.create(file);
            try {
                ParserManager.parse(Collections.singleton(source), new UserTask() {
                    @Override
                    public void run(ResultIterator resultIterator) throws Exception {
                        Parser.Result result = resultIterator.getParserResult();
                        org.netbeans.modules.html.editor.lib.api.elements.Node resolved =
                                sourceElementHandle.resolve(result);
                        String text = resolved.image().toString();
                        sb.append(text).append("\n");
                    }
                });
            } catch (ParseException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        setClipboardContents(sb.toString());
    }
    
    private void setClipboardContents(String content) {
        if (clipboard != null) {
            if (content == null) {
                clipboard.setContents(null, null);
            } else {
                clipboard.setContents(new StringSelection(content), 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
« March 2013 »
SunMonTueWedThuFriSat
     
10
11
12
13
14
15
18
23
24
27
28
      
Today