Paste a Multiline Literal with Line Wrap On

The new line wrap (in Tools/Options/Editor/Formatting) in NetBeans IDE 7 is great but not so great when you paste a multiline literal:

Better would be if pasting a multiline literal would immediately generate this, i.e., below line wrap is switched on (hence the line wrap arrows at the end of the long lines), while the paste is aware of the multiline nature of the text in the clipboard:

Here's how:

@ActionID(category = "Edit",
id = "org.netbeans.multinline.PasteMultilineAction")
@ActionRegistration(displayName = "#CTL_PasteMultilineAction")
@ActionReferences({
    @ActionReference(path = "Menu/Edit", position = 1350),
    @ActionReference(path = "Editors/text/x-java/Popup", position = 4050),
    @ActionReference(path = "Shortcuts", name = "DA-V")
})
@Messages("CTL_PasteMultilineAction=Paste Mu&ltiline")
public final class PasteMultilineAction implements ActionListener {

    private final EditorCookie context;

    public PasteMultilineAction(EditorCookie context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        String result = null;
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable contents = clipboard.getContents(null);
        boolean hasTransferableText = (contents != null)
                && contents.isDataFlavorSupported(DataFlavor.stringFlavor);
        if (hasTransferableText) {
            try {
                result = (String) contents.getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException ex) {
                System.out.println(ex);
            } catch (IOException ex) {
                System.out.println(ex);
            }
        }
        result = result.replace("\\","\\\\");
        result = result.replace("\"","\\\"");
        result = result.replace("\n","\\n\" +\n\t\"");
        try {
            StyledDocument doc = context.openDocument();
            JTextComponent jtc = EditorRegistry.lastFocusedComponent();
            int dot = jtc.getCaret().getDot();
            doc.insertString(dot, result, null);
        } catch (BadLocationException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
 
} 

Also handy for SQL scripts. Here's one:

CREATE VIEW METRIC_STATS (ID, MONTH, TEMP_C, RAIN_C) AS
SELECT ID,
MONTH,
(TEMP_F - 32) * 5 /9,
RAIN_I * 0.3937
FROM STATS;

Compare this standard paste result, in a "line wrap on" scenario:

...to this one, via my action above:

Above, you now have a nice place to start working with your query, instead of a whole bunch of error marks in the editor that you need to deal with before integrating the query into your code.

Comments:

You asked about more complex netbeans examples. Well; the code above is nice and all but for a newer user to netbeans... where does it go?

Posted by guest on August 19, 2011 at 01:22 AM PDT #

In a NetBeans module: http://platform.netbeans.org/tutorials/nbm-google.html

Posted by Geertjan on August 19, 2011 at 01:24 AM PDT #

It seems simple enough when you give us the code just to use it. Anyway thanks for the time spent for this, it is helpful to me.

Posted by asigurare on August 19, 2011 at 01:27 AM PDT #

Does this have anything to do with line wrap? I think the same would apply without line wrap.

Anyway this should really be a bug report for the Java editor, I think - pasting a string containing newlines should insert "+" escapes when the context of the paste can be determined to be a string literal according to the lexer.

Posted by Jesse Glick on August 19, 2011 at 01:58 AM PDT #

OK, created a new issue: http://netbeans.org/bugzilla/show_bug.cgi?id=201130

Posted by Geertjan on August 19, 2011 at 02:47 AM PDT #

great - voted

Posted by guest on March 07, 2012 at 06:51 AM PST #

can't enable it :( Line Wrap is On ...but nothing is happening :(

Posted by guest on March 25, 2013 at 06:08 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
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
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today