对于Visual Studio的宏大家应该很熟悉了这是一篇关于JBuilder实现类似Visual Studio的宏扩展功能我们就通过对一段代码是否注释掉作为简单例子大家可以实现自己的一些扩展算是抛玉引砖了
支持环境 Jbuilder JBuilder
使用JBuilder编译时需要在 Project > Project Properties > Required Libaries中加上Jbuilder下的 Open Tool SDK编译成功后将生成的class打包成一个JavaCommentSwitchjar文件其中包含这样一个文件METAINF\Manifestmf该文件内容如下
ManifestVersion:
OpenToolsUI: JavaCommentSwitch
不需要什么详细的讲解代码如下
/*===============================================
文件一 IDEActionsjava
===============================================*/
/**
*
Title: JBuilder IDE Toolbox
*
Description: ?user_name=ghost
*
Copyright: Copyright (c) Ghost Studio All Rights Reserved
*
Company: Ghost Studio
* @author 阿鬼 []
* @version
*/
import comborlandprimetimeactionsActionGroup;
import comborlandprimetimeeditorEditorContextActionProvider;
import comborlandprimetimeeditorEditorPane;
import javaxswingAction;
public class IDEActions
{
private static final String STRING_ActionGroupName = JBuilder IDE Toolbox; // ActiveGroups Name
public static final EditorContextActionProvider CONTEXTMENU_ActionProvider = new EditorContextActionProvider()
{
public Action getContextAction(EditorPane target)
{
ActionGroup actionGroup = new ActionGroup();
ActionGroup actionSubGroup = new ActionGroup(STRING_ActionGroupName);
actionSubGroupadd(JavaCommentSwitchACTION_GenerateJavaCommentSwitch);
// 此处可以增加更多的功能
//
actionGroupadd(actionSubGroup);
return actionGroup;
}
public int getPriority()
{
return ;
}
};
public IDEActions()
{
}
static class ction
implements EditorContextActionProvider
{
public Action getContextAction(EditorPane target)
{
ActionGroup actionGroup = new ActionGroup();
ActionGroup actionSubGroup = new ActionGroup(STRING_ActionGroupName);
actionSubGroupadd(JavaCommentSwitchACTION_GenerateJavaCommentSwitch);
actionGroupadd(actionSubGroup);
return actionGroup;
}
public int getPriority()
{
return ;
}
ction()
{
}
}
}
/*===============================================
文件二 JavaCommentSwitchjava
===============================================*/
/**
*
Title: JBuilder IDE Toolbox
*
Description: ?user_name=ghost
*
Copyright: Copyright (c) Ghost Studio All Rights Reserved
*
Company: Ghost Studio
* @author 阿鬼 []
* @version
*/
import comborlandjbuilderjot*;
import comborlandjbuildernodeJBProject;
import comborlandjbuildernodeJavaFileNode;
import comborlandprimetimeeditor*;
import comborlandprimetimeideBrowser;
import comborlandprimetimenodeFileNode;
import comborlandprimetimenodeNode;
import comborlandprimetimevfsUrl;
import javaawteventActionEvent;
import javabeansPropertyChangeEvent;
import javabeansPropertyChangeListener;
import javaioFile;
import javaioPrintStream;
import javaxswing*;
import javaxswingtextJTextComponent;
import javaxswingtextKeymap;
public class JavaCommentSwitch extends EditorAction
implements PropertyChangeListener
{
private static final String versionString = ;
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 = ;
thisisMenuAction = isMenuAction;
}
public static void initOpenTool(byte majorVersion byte minorVersion)
{
if(majorVersion < ) // 支持Jbuilder 以上
return;
EditorManagerregisterContextActionProvider(IDEActionsCONTEXTMENU_ActionProvider);
ACTION_GenerateJavaCommentSwitchputValue(LongDescription MENU_STR_LONG_DESCRIPTION);
ACTION_GenerateJavaCommentSwitchputValue(ShortDescription MENU_STR_SHORT_DESCRIPTION);
EditorActionsaddBindableEditorAction(ACTION_CreateJavaCommentSwitch);
ACTION_CreateJavaCommentSwitchputValue(LongDescription STR_LONG_DESCRIPTION);
ACTION_CreateJavaCommentSwitchputValue(ShortDescription STR_SHORT_DESCRIPTION);
Keymap keymap = EditorManagergetKeymap(); // 支持快捷键 ALT + F
if(keymap != null)
{
KeyStroke stroke[] = keymapgetKeyStrokesForAction(ACTION_CreateJavaCommentSwitch);
if(stroke == null)
keymapaddActionForKeyStroke(KeyStrokegetKeyStroke(javaawtEventF
javaawtEventALT_MASK ) ACTION_CreateJavaCommentSwitch);
}
EditorManageraddPropertyChangeListener((PropertyChangeListener)ACTION_CreateJavaCommentSwitch);
}
public void actionPerformed(ActionEvent evt)
{
target = getEditorTarget(evt);
Node theNode = getNode();
if(theNode == null)
return;
JBProject proj = (JBProject)theNodegetProject();
JotClass theClass = getClass(theNode);
if(isMenuAction)
if(theClass != null)
runJavaCommentSwitch();
else
JOptionPaneshowMessageDialog(null
Current Editor target is not a java class
Error creating javadoc );
}
/**
* @description
*
* @param
* @return
* @exception
*/
private boolean runJavaCommentSwitch()
{
String selection = targetgetSelectedText();
String newSelection = ;
// 去掉注释
if(selectiontrim()startsWith(/*) && selectiontrim()endsWith(*/))
{
StringBuffer sb = new StringBuffer(selection);
newSelection = sbsubstring( selectionlength() );
}
// 添加注释
else
{
newSelection = StringvalueOf(StringvalueOf((new StringBuffer(/*))append(selection)append(*/)));
}
targetreplaceSelection(newSelection);
return true;
}
public void propertyChange(PropertyChangeEvent evt)
{
String propertyName = evtgetPropertyName();
if(propertyNameequals(keymap))
{