转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> JAVA开发 >> 正文
用JBuilder实现类似Visual Studio的宏扩展功能         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:644 更新时间:2009/4/22 23:27:40
  对于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"))
{
Keymap keymap = EditorManager.getKeymap();
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);
}
}
}

protected Node getNode()
{
Browser browser = Browser.getActiveBrowser();
Node node = browser.getActiveNode();
if((node instanceof JavaFileNode) && (node.getProject() instanceof JBProject))
return node;
else
return null;
}

protected JotClass getClass(Node node)
{
if((node instanceof JavaFileNode) && (node.getProject() instanceof JBProject))
{
JBProject project = (JBProject)node.getProject();
String filename = ((JavaFileNode)node).getUrl().getFile();
JotPackages pkg = project.getJotPackages();
Url url = new Url(new File(filename));
JotFile jfile = pkg.getFile(url);
JotClass jclasses[] = jfile.getClasses();
if(jclasses.length > 1)
{
System.err.println("More than one class found in source file");
errorMessageText = "More than one class found in source file";
return null;
}
if(jclasses.length == 0)
{
System.err.println("No classes found in source file");
errorMessageText = "No classes found in source file";
return null;
} else
{
return jclasses[0];
}
} else
{
return null;
}
}
}


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台