The Goal
Keep walking……
posts - 23,  comments - 1,  trackbacks - 0

ActionContributionItem--combines the function of a GUI widget and its attached listener class.
Action--处理事件
与SWT的listener/event模式很类似,但是其class更抽象,更易于使用,scope更窄。

  • actions and contributions

Action--可以简单的理解成一个命令,可以关联到菜单,工具条,以及按钮
Contribution--在JFace里面,一个Action可以对应多个GUI对象,这些对象就是所谓的Contribution Item. 有两个主要的Contribution类:ContributionItem和ContributionManager,它们都是抽象类,靠其子类来实现事件的处理。继承关系见下图
ContributionItem--引发事件的单独GUI组件
ContributionManager--产生包含ContributionItems的对象

ActionContributionItem--最重要,在ApplicationWindow中创建和实施,来将一个action连接至此GUI,它虽没有设定好的外观,但是依赖于你使用的fill()方法,却可以帮助一个按钮、菜单栏和工具栏的成形

另一个与Contribution协作的方法是通过ContributionManager,它的子类类似于ContributionItem的container。其中MenuManager将ContributionItems组合在窗口最高层菜单, ToolBarManager则将这些对象放在仅在菜单之下的toolbar中。

  • 创建Action类

Action是抽象类。

package com.swtjface.Ch4;
import org.eclipse.jface.action.*;
import org.eclipse.jface.resource.*;
public class Ch4_StatusAction extends Action
{
StatusLineManager statman;
short triggercount = 0;
public Ch4_StatusAction(StatusLineManager sm)
{
super("&Trigger@Ctrl+T",
AS_PUSH_BUTTON);//在T字母之前的&符号意味着这个字母将作为该动作的快捷键。而在TEXT领域内的“Ctrl+T”确保了当用户在同时按下Ctrl键和T键时该动作就会被激发。
statman = sm;
setToolTipText("Trigger the Action");
setImageDescriptor(ImageDescriptor.createFromFile
(this.getClass(),"eclipse.gif"));
}
public void run() //每次当Ch4_StatusAction被生成,run()方法就被调用
{
triggercount++;
statman.setMessage("The status action has fired. Count: " +
triggercount);
}

  • Implementing contributions in an ApplicationWindow

package com.swtjface.Ch4;

import org.eclipse.swt.*;

import org.eclipse.swt.widgets.*;

import org.eclipse.jface.window.*;

import org.eclipse.jface.action.*;

public class Ch4_Contributions extends ApplicationWindow {

StatusLineManager slm = new StatusLineManager();

Ch4_StatusAction status_action = new Ch4_StatusAction(slm); //StatusLineManager的对象作参数,创建了一个Ch4_StatusAction的实例

ActionContributionItem aci = new ActionContributionItem(status_action); //用Ch4_StatusAction的对象作参数,创建了ActionContributionItem对象

public Ch4_Contributions() {

super(null); // 创建了 ApplicationWindow对象

addStatusLine();

addMenuBar();

addToolBar(SWT.FLAT | SWT.WRAP); //在窗口上添加了status line, menu, toolbar

}

protected Control createContents(Composite parent) {

getShell().setText("Action/Contribution Example");

parent.setSize(290,150); //设置了窗口的title和size

aci.fill(parent); // 将ActionContributionItem放在GUI中。因为这里的参数是Composite对象,所以根据Action的STYLE属性来确定。此处是Button,因为Ch4_StatusAction 的STYLE属性是AS_PUSH_BUTTON;

return parent;

}

public static void main(String[] args) {

Ch4_Contributions swin = new Ch4_Contributions();

swin.setBlockOnOpen(true);

swin.open();

Display.getCurrent().dispose();

}

protected MenuManager createMenuManager() {

MenuManager main_menu = new MenuManager(null);

MenuManager action_menu = new MenuManager("Menu");

main_menu.add(action_menu);

action_menu.add(status_action); //关联status_action.created and added to the menu in the form of a menu item

return main_menu;

}

protected ToolBarManager createToolBarManager(int style) {

ToolBarManager tool_bar_manager = new ToolBarManager(style);

tool_bar_manager.add(status_action); //关联status_action。created and added to the toolbar as a toolbar item.

return tool_bar_manager;

}

protected StatusLineManager createStatusLineManager() {

return slm;

}

}

  • Interfacing with contributions

两个途径来将ActionContributionItem添加到GUI:
1. 通过ContributionManager子类的add()方法。
(1)可接受Action对象的参数,从而间接的将ContributionItem和ContributionManager关联。可多次执行
(2)可直接接受ActionContributionItem对象的参数。只可执行一次
2.通过ActionContributionItem类的fill()方法。根据其参数的不同,所先是的组件也不同,具体见下表:

  • Exploring the Action class

Important methods of the Action class


Property methods for the Action class

DESCRIPTION--written to a status line to provide additional help.

Style methods for the Action class

如果ENABLED是FALSE,则变灰。CHECKED主要用于radio和checkbox

Accelerator key / keyboard methods for the Action class

Accelerator keys--鼠标点击的键盘块捷方式


Listener methods for the Action class

虽然JFace使用action代替了SWT的listener/event机制,但是Actiono类仍然可以和listener协作来处理特定需求的事件。
IPropertyChangeListener接口关注客户自定义的PropertyChangeEvents,当所给的对象按照你所给的方式变成另一个对象时,此事件被触发。

Miscellaneous methods of the Action class

posted on 2006-03-24 17:02 JOO 阅读(784) 评论(1)  编辑  收藏 所属分类: SWT & JFace IN ACTION

FeedBack:
# re: 4.2 Event processing in JFace
2007-01-25 09:45 | cai niao
great  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
Hit the target!

<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(2)

随笔分类(23)

随笔档案(22)

文章档案(1)

相册

Neighbor

搜索

  •  

最新评论

阅读排行榜

评论排行榜