如何有条件的设置Action
程序员文章站
2022-06-01 19:33:11
...
最近做的一个PrettyPro的plugin有这样一个需求:用户在Editor中的右键菜单中有Format和Format Selection两个Action,根据用户是否选择了Editor中的内容而决定其中那个是enable的,哪个是disable的。本来以为实现这个功能要动态的添加action而不是在plugin.xml中声明能做到的,但是苦于找不到动态添加action的方法,就回头去啃plugin.xml的dtd,终于让我找到了~
注意加重斜体的地方,这就是声明当选中1或多个("+")和不选中("!")时是否要enable这个action。我一开始还以为这个东西只能在outline view或browser view中有具体选择项(如:文件)时才能用,没想到在editor里面判断用户是否选中内容的时候也可以用,真是有趣~
<!----> 1 <extension point="org.eclipse.ui.popupMenus">
2 <viewerContribution id="com.qad.progress.prettypro.ui.editor.PopupMenuContribution" targetID="#TextEditorContext" >
3 <menu label="%editorPopupMenuLabel" path="additions" id="com.qad.progress.prettypro.ui.menu.FormatSubPopupMenu">
4 <separator name="%editorPopupMenuSeparatorName"/>
5 </menu>
6 <action
7 label = "%editorPopupMenuAction1Label"
8 icon = "%editorPopupMenuAction1Icon"
9 class = "com.qad.progress.prettypro.ui.actions.FormatSelectionAction"
10 tooltip = "%editorPopupMenuAction1ToolTip"
11 menubarPath = "com.qad.progress.prettypro.ui.menu.FormatSubPopupMenu/%editorPopupMenuSeparatorName"
12 id = "com.qad.progress.prettypro.ui.actions.editorPopupMenu.FormatSelectionAction"
13 enablesFor="+">
14 </action>
15 <action
16 label="%editorPopupMenuAction2Label"
17 icon="%editorPopupMenuAction2Icon"
18 class="com.qad.progress.prettypro.ui.actions.FormatAction"
19 tooltip="%editorPopupMenuAction2ToolTip"
20 menubarPath="com.qad.progress.prettypro.ui.menu.FormatSubPopupMenu/%editorPopupMenuSeparatorName"
21 id="com.qad.progress.prettypro.ui.actions.editorPopupMenu.FormatAction"
22 enablesFor="!">
23 </action>
24 </viewerContribution>
25 </extension>
2 <viewerContribution id="com.qad.progress.prettypro.ui.editor.PopupMenuContribution" targetID="#TextEditorContext" >
3 <menu label="%editorPopupMenuLabel" path="additions" id="com.qad.progress.prettypro.ui.menu.FormatSubPopupMenu">
4 <separator name="%editorPopupMenuSeparatorName"/>
5 </menu>
6 <action
7 label = "%editorPopupMenuAction1Label"
8 icon = "%editorPopupMenuAction1Icon"
9 class = "com.qad.progress.prettypro.ui.actions.FormatSelectionAction"
10 tooltip = "%editorPopupMenuAction1ToolTip"
11 menubarPath = "com.qad.progress.prettypro.ui.menu.FormatSubPopupMenu/%editorPopupMenuSeparatorName"
12 id = "com.qad.progress.prettypro.ui.actions.editorPopupMenu.FormatSelectionAction"
13 enablesFor="+">
14 </action>
15 <action
16 label="%editorPopupMenuAction2Label"
17 icon="%editorPopupMenuAction2Icon"
18 class="com.qad.progress.prettypro.ui.actions.FormatAction"
19 tooltip="%editorPopupMenuAction2ToolTip"
20 menubarPath="com.qad.progress.prettypro.ui.menu.FormatSubPopupMenu/%editorPopupMenuSeparatorName"
21 id="com.qad.progress.prettypro.ui.actions.editorPopupMenu.FormatAction"
22 enablesFor="!">
23 </action>
24 </viewerContribution>
25 </extension>
注意加重斜体的地方,这就是声明当选中1或多个("+")和不选中("!")时是否要enable这个action。我一开始还以为这个东西只能在outline view或browser view中有具体选择项(如:文件)时才能用,没想到在editor里面判断用户是否选中内容的时候也可以用,真是有趣~
BTW:注意我的两个action action的顺序是Format selection和Format entire file,但是在显示的时候却是反的,不知道是为什么...
上一篇: SugarCRM为什么这么慢?