/*=============================================== 文件二: 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; } } } |