alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.examples.scripts;
import java.awt.Toolkit;
import javax.swing.JEditorPane;
import javax.swing.text.*;
import org.openide.cookies.EditorCookie;
import org.openide.nodes.Node;
import org.openide.util.*;
import org.openide.util.actions.CookieAction;

/** An example action for the NetBeans IDE that works on open text editor windows.
* To customize, change performAction(Document,Caret); getName; and if you wish,
* iconResource() too.
* To install, you might:
* - get the API Support module, make a module manifest (see bottom of file),
*   make a JAR with this class and supplied manifest, hit Execute on JAR to
*   install as "Tools" action; now available e.g. in Editor context menu
* @author Jesse Glick
* @version 0.1; just an example
*/
public class ExampleTextAction extends CookieAction {

    /** Do something with an open document.
    * This is the part you should write according to your needs.
    * Please use javax.swing.text.* API.
    * @param d a document open in the editor pane
    * @param c the caret giving cursor position, selection, etc.
    */
    protected void performAction (Document d, Caret c) throws BadLocationException {
        int dot = c.getDot ();
        int mark = c.getMark ();
        if (dot == mark) {
            // No selection: insert [[]] at point and move cursor to center
            d.insertString (dot, "[[]]", null);
            c.setDot (dot + 2);
        } else {
            // Selection: wrap it in [[...]] and clear selection
            int start = Math.min (dot, mark);
            int end = Math.max (dot, mark);
            d.insertString (end, "]]", null);
            d.insertString (start, "[[", null);
            c.setDot (end + 4);
        }
    }

    public String getName () {
        return "Example Action";
    }

    /* Implement if you want an icon (e.g. for non-context menu item/toolbar button):
    protected String iconResource () {
      return "resource/path/to/icon.gif";
    }
    */

    // --- BOILERPLATE BELOW THIS POINT ---
    protected Class[] cookieClasses () {
        return new Class[] { EditorCookie.class };
    }
    protected int mode () {
        return MODE_EXACTLY_ONE;
    }
    protected boolean enable (Node[] nodes) {
        if (! super.enable (nodes)) return false;
        EditorCookie ed = (EditorCookie) nodes[0].getCookie (EditorCookie.class);
        return ed.getOpenedPanes () != null;
    }
    protected void performAction (Node[] nodes) {
        EditorCookie ed = (EditorCookie) nodes[0].getCookie (EditorCookie.class);
        JEditorPane pane = ed.getOpenedPanes ()[0];
        try {
            performAction (pane.getDocument (), pane.getCaret ());
        } catch (BadLocationException ble) {
            Toolkit.getDefaultToolkit ().beep ();
            // TopManager.getDefault ().notifyException (ble);
        }
    }
    public HelpCtx getHelpCtx () {
        return HelpCtx.DEFAULT_HELP;
    }
}

/*
Example manifest to install this as a module for inclusion in Tools... menus:
--------------------%<---------------------- example-action.mf
Manifest-Version: 1.0
OpenIDE-Module: org.netbeans.examples.scripts.example_action
OpenIDE-Module-Name: Example Action Module

Name: org/netbeans/examples/scripts/ExampleTextAction.class
OpenIDE-Module-Class: Action

 --------------------%<----------------------
*/
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.