当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:14:00  来源:本站整理

用JBuilder实现近似Visual Studio的宏扩大功效[Java编程]

赞助商链接



  本文“用JBuilder实现近似Visual Studio的宏扩大功效[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

关于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-INFManifest.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;
}
}
}


  以上是“用JBuilder实现近似Visual Studio的宏扩大功效[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 用JBuilderX开辟WEB操纵程序
  • 用JBuilder 2005实现重构之熟习重构
  • 用JBuilder9制作EXE文件不公开秘籍
  • 用JBuilder高效率开辟Java程序
  • 巧用JBuilderX代码美化工具
  • 用JBuilder实现近似Visual Studio的宏扩大功效
  • 用JBuilder7开辟一个简单的J2EE操纵
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .