Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

Copy Nodes in Live DOM View to Clipboard

$
0
0
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);
            }
        }
    }

}

Viewing all articles
Browse latest Browse all 19780

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>