熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

用JBuilder實現類似Visual Studio的宏擴展功能

2022-06-13   來源: JSP教程 

  對於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))
  {
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19468.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.