服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

eclipse开发经验:文本替换和操作界面

快速eclipse插件开发经验:

1.取得对当前选中文本内容,并转换。

1)定义action实现ieditoractiondelegate接口。

2)取得当前编辑器(geteditor())。

platformui.getworkbench().getactiveworkbenchwindow().getactivepage().getactiveeditor();

3)取得选中文本。

(itextselection) geteditor().geteditorsite().getselectionprovider().getselection();

4)替换选中文本。

abstracttexteditor editor = (abstracttexteditor) geteditor();

editor.getdocumentprovider().getdocument(editor.geteditorinput());

document.replace(offset, length, replacetext);

5)选中替换后的文本。

itextselection tsnew = new textselection(document, offset, length);

geteditor().geteditorsite().getselectionprovider().setselection(tsnew);

2.菜单分组,图标,快捷键,工具条的实现。

1)菜单分组。如下分成4组,组之间有分隔线。

<menu label="xxxxtools(&amp;t)" id="xxxxtoolsmenu">
<separator name="xxxxtoolsgrouptxt"></separator>
<separator name="xxxxtoolsgroupsql"></separator>
<separator name="xxxxtoolsgroupcode"></separator>
<separator name="xxxxtoolsgrouprefrence"></separator>
</menu>

2)分配action到组。设置图标。和工具条。

<action label="to lower case(&amp;l)"
       class="xxxxtools.actions.text.tolowercaseaction"
       icon="icons/lower.gif"
       menubarpath="xxxxtoolsmenu/xxxxtoolsgrouptxt"
       toolbarpath="xxxxtoolsgrouptxt"
       id="xxxxtools.actions.text.tolowercaseaction"
       definitionid="xxxxtools.actions.text.tolowercaseaction">
       <selection class="org.eclipse.jface.text.itextselection" />
</action>

3)设置快捷键。

<extension point="org.eclipse.ui.commands">     
       <command name="to lower case"
                     id="xxxxtools.actions.text.tolowercaseaction" /> 
</extension>
<extension point="org.eclipse.ui.bindings">        
       <key sequence="ctrl+shift+u"
                     contextid="org.eclipse.ui.texteditorscope"
                     commandid="xxxxtools.actions.text.tolowercaseaction"
                     schemeid="org.eclipse.ui.defaultacceleratorconfiguration" />
</extension>

扫描关注微信公众号