分享
 
 
 

用JBuilder实现类似VisualStudio的宏扩展功能

王朝other·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

对于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"))

{

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有