Skip to main content

Geertjan Wants To See The Code

Posted by gsporar on November 16, 2006 at 2:59 PM PST

For the growing community of people who write NetBeans plugin modules, Geertjan needs no introduction. He writes tons of tutorials, book chapters, and blog entries on the topic. He recently wrote a blog entry that showed how to quickly and easily extend the JSP editor in the NetBeans IDE.

One of the commenters mentioned a problem with Geertjan's code where Undo/Redo did not work correctly. The stated solution was to use BaseDocument.atomicLock(). That sounded familiar to me. Back in April I wrote a small plugin module to extend the HTML editor; details on the functionality are here. I had based my code on the Line Tools module from the prolific Sandip Chitale. And it made use of BaseDocument.atomicLock(), so I left a comment on Geertjan's blog entry.

He responded to my comment and said he wanted to see the code, so I have copied it below.

 23     protected void performAction(Node[] activatedNodes) {
24        
25         // get the string from the system clipboard
26         String url = null;
27         Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
28         try {
29             url = cb.getData(DataFlavor.stringFlavor).toString();
30         } catch (IOException ex) {
31             ex.printStackTrace();
32         } catch (UnsupportedFlavorException ex) {
33             ex.printStackTrace();
34         }
35        
36         EditorCookie ec = (EditorCookie) activatedNodes[0].getCookie(EditorCookie.class);
37         if (ec != null) {
38             JEditorPane[] panes = ec.getOpenedPanes();
39             if (panes != null) {
40                 TopComponent activetc = TopComponent.getRegistry().getActivated();
41                 for (int i = 0; i < panes.length; i++) {
42                     if (activetc.isAncestorOf(panes[i])) {
43                         insertLink(panes[i], url);
44                     }
45                 }
46             }
47         }
48     }
49    
50     private void insertLink(JTextComponent textComponent, String url) {
51         if (textComponent.isEditable()) {
52             Document doc = textComponent.getDocument();
53             if (doc instanceof BaseDocument) {
54                 ((BaseDocument)doc).atomicLock();
55             }
56             try {
57                 // if there is a selection then we want the
58                 // anchor tag inserted before it.
59                 Caret caret = textComponent.getCaret();
60                 boolean selection = false;
61                 boolean backwardSelection = false;
62                 int start = textComponent.getCaretPosition();
63                 int end = start;
64                 if (caret.isSelectionVisible()) {
65                     int selStart = caret.getDot();
66                     int selEnd = caret.getMark();
67                     start = Math.min(selStart, selEnd);
68                     end =   Math.max(selStart, selEnd) - 1;
69                     selection = true;
70                     backwardSelection = (selStart >= selEnd);
71                 }
72                 try {
73                     // insert the anchor tag
74                     url = " + url + "\">";
75                     doc.insertString(start, url, null);
76                    
77                     // and then insert the closing tag
78                     doc.insertString(end + url.length()+1, "
"
, null);
79                 } catch (BadLocationException ex) {
80                     ex.printStackTrace();
81                 }
82                
83                
84             } finally {
85                 if (doc instanceof BaseDocument) {
86                     ((BaseDocument)doc).atomicUnlock();
87                 }
88             }
89         } else {
90             beep();
91         }
92     }