对于visual studio的宏,大家应该很熟悉了,这是一篇关于jbuilder实现类似visual studio的宏扩展功能,我们就通过对一段代码是否注释掉作为简单例子。大家可以实现自己的一些扩展,算是抛玉引砖了。
支持环境: jbuilder 4.0 - jbuilder 7.0
使用jbuilder编译时需要在 project ---> project properties ---> required libaries中加上jbuilder下的 open tool sdk,编译成功后将生成的class打包成一个javacommentswitch.jar文件,其中包含这样一个文件meta-inf/manifest.mf,该文件内容如下:
manifest-version: 1.0
opentools-ui: javacommentswitch
不需要什么详细的讲解,代码如下:
/*===============================================
文件一: ideactions.java
===============================================*/
/**
*
title: jbuilder ide toolbox
*
description: http://www.vchelp.net/itbookreview/view_user.asp?user_name=ghost
*
copyright: copyright (c) 2002 ghost studio. all rights reserved.
*
company: ghost studio
* @author 阿鬼 [mornlee@21cn.com]
* @version 1.0
*/
import com.borland.primetime.actions.actiongroup;
import com.borland.primetime.editor.editorcontextactionprovider;
import com.borland.primetime.editor.editorpane;
import javax.swing.action;
public class ideactions
{
private static final string string_actiongroupname = "jbuilder ide toolbox"; // activegroup's name
public static final editorcontextactionprovider contextmenu_actionprovider = new editorcontextactionprovider()
{
public action getcontextaction(editorpane target)
{
actiongroup actiongroup = new actiongroup();
actiongroup actionsubgroup = new actiongroup(string_actiongroupname);
actionsubgroup.add(javacommentswitch.action_generatejavacommentswitch);
// 此处可以增加更多的功能
// ......
actiongroup.add(actionsubgroup);
return actiongroup;
}
public int getpriority()
{
return 0;
}
};
public ideactions()
{
}
static class ction
implements editorcontextactionprovider
{
public action getcontextaction(editorpane target)
{
actiongroup actiongroup = new actiongroup();
actiongroup actionsubgroup = new actiongroup(string_actiongroupname);
actionsubgroup.add(javacommentswitch.action_generatejavacommentswitch);
actiongroup.add(actionsubgroup);
return actiongroup;
}
public int getpriority()
{
return 0;
}
ction()
{
}
}
}
/*===============================================
文件二: javacommentswitch.java
===============================================*/
/**
*
title: jbuilder ide toolbox
*
description: http://www.vchelp.net/itbookreview/view_user.asp?user_name=ghost
*
copyright: copyright (c) 2002 ghost studio. all rights reserved.
*
company: ghost studio
* @author 阿鬼 [mornlee@21cn.com]
* @version 1.0
*/
import com.borland.jbuilder.jot.*;
import com.borland.jbuilder.node.jbproject;
import com.borland.jbuilder.node.javafilenode;
import com.borland.primetime.editor.*;
import com.borland.primetime.ide.browser;
import com.borland.primetime.node.filenode;
import com.borland.primetime.node.node;
import com.borland.primetime.vfs.url;
import java.awt.event.actionevent;
import java.beans.propertychangeevent;
import java.beans.propertychangelistener;
import java.io.file;
import java.io.printstream;
import javax.swing.*;
import javax.swing.text.jtextcomponent;
import javax.swing.text.keymap;
public class javacommentswitch extends editoraction
implements propertychangelistener
{
private static final string versionstring = "0.1";
public static editoraction action_createjavacommentswitch = new javacommentswitch("javacommentswitch");
protected static string str_long_description = "comment/uncomment for selection."; // 状态条显示
protected static string str_short_description = "comment/uncomment for selection.";
public static editoraction action_generatejavacommentswitch = new javacommentswitch("javacommentswitch", true);
protected static string menu_str_long_description = "comment/uncomment for selection";
protected static string menu_str_short_description = "comment/uncomment";
static browser browser = null;
private editorpane target;
private string errormessagetext;
private boolean ismenuaction;
public javacommentswitch(string name)
{
super(name);
target = null;
errormessagetext = "";
ismenuaction = false;
}
public javacommentswitch(string name, boolean ismenuaction)
{
super(name);
target = null;
errormessagetext = "";
this.ismenuaction = ismenuaction;
}
public static void initopentool(byte majorversion, byte minorversion)
{
if(majorversion < 4) // 支持jbuilder 4.0以上
return;
editormanager.registercontextactionprovider(ideactions.contextmenu_actionprovider);
action_generatejavacommentswitch.putvalue("longdescription", menu_str_long_description);
action_generatejavacommentswitch.putvalue("shortdescription", menu_str_short_description);
editoractions.addbindableeditoraction(action_createjavacommentswitch);
action_createjavacommentswitch.putvalue("longdescription", str_long_description);
action_createjavacommentswitch.putvalue("shortdescription", str_short_description);
keymap keymap = editormanager.getkeymap(); // 支持快捷键 alt + f10
if(keymap != null)
{
keystroke stroke[] = keymap.getkeystrokesforaction(action_createjavacommentswitch);
if(stroke == null)
keymap.addactionforkeystroke(keystroke.getkeystroke(java.awt.event.f10,
java.awt.event.alt_mask ), action_createjavacommentswitch);
}
editormanager.addpropertychangelistener((propertychangelistener)action_createjavacommentswitch);
}
public void actionperformed(actionevent evt)
{
target = geteditortarget(evt);
node thenode = getnode();
if(thenode == null)
return;
jbproject proj = (jbproject)thenode.getproject();
jotclass theclass = getclass(thenode);
if(ismenuaction)
if(theclass != null)
runjavacommentswitch();
else
joptionpane.showmessagedialog(null,
"current editor target is not a java class.",
"error creating javadoc", 0);
}
/**
* @description
*
* @param
* @return
* @exception
*/
private boolean runjavacommentswitch()
{
string selection = target.getselectedtext();
string newselection = "";
// 去掉注释
if(selection.trim().startswith("/*") && selection.trim().endswith("*/"))
{
stringbuffer sb = new stringbuffer(selection);
newselection = sb.substring(2, selection.length() - 2);
}
// 添加注释
else
{
newselection = string.valueof(string.valueof((new stringbuffer("/*")).append(selection).append("*/")));
}
target.replaceselection(newselection);
return true;
}
public void propertychange(propertychangeevent evt)
{
string propertyname = evt.getpropertyname();
if(propertyname.equals("keymap"))
{
闽公网安备 35060202000074号