ActionContributionItem--combines the function of a GUI widget and its attached listener class.
Action--處理事件
與SWT的listener/event模式很類似,但是其class更抽象,更易于使用,scope更窄。
-
actions and contributions
Action--可以簡單的理解成一個(gè)命令,可以關(guān)聯(lián)到菜單,工具條,以及按鈕
Contribution--在JFace里面,一個(gè)Action可以對應(yīng)多個(gè)GUI對象,這些對象就是所謂的Contribution Item. 有兩個(gè)主要的Contribution類:ContributionItem和ContributionManager,它們都是抽象類,靠其子類來實(shí)現(xiàn)事件的處理。繼承關(guān)系見下圖
ContributionItem--引發(fā)事件的單獨(dú)GUI組件
ContributionManager--產(chǎn)生包含ContributionItems的對象

ActionContributionItem--最重要,在ApplicationWindow中創(chuàng)建和實(shí)施,來將一個(gè)action連接至此GUI,它雖沒有設(shè)定好的外觀,但是依賴于你使用的fill()方法,卻可以幫助一個(gè)按鈕、菜單欄和工具欄的成形
另一個(gè)與Contribution協(xié)作的方法是通過ContributionManager,它的子類類似于ContributionItem的container。其中MenuManager將ContributionItems組合在窗口最高層菜單, ToolBarManager則將這些對象放在僅在菜單之下的toolbar中。
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字母之前的&符號意味著這個(gè)字母將作為該動作的快捷鍵。而在TEXT領(lǐng)域內(nèi)的“Ctrl+T”確保了當(dāng)用戶在同時(shí)按下Ctrl鍵和T鍵時(shí)該動作就會被激發(fā)。
statman = sm;
setToolTipText("Trigger the Action");
setImageDescriptor(ImageDescriptor.createFromFile
(this.getClass(),"eclipse.gif"));
}
public void run() //每次當(dāng)Ch4_StatusAction被生成,run()方法就被調(diào)用
{
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的對象作參數(shù),創(chuàng)建了一個(gè)Ch4_StatusAction的實(shí)例
ActionContributionItem aci = new
ActionContributionItem(status_action); //用Ch4_StatusAction的對象作參數(shù),創(chuàng)建了ActionContributionItem對象
public Ch4_Contributions() {
super(null); //
創(chuàng)建了
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); //設(shè)置了窗口的title和size
aci.fill(parent); //
將ActionContributionItem放在GUI中。因?yàn)檫@里的參數(shù)是Composite對象,所以根據(jù)Action的STYLE屬性來確定。此處是Button,因?yàn)?font color="#0000ff">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); //關(guān)聯(lián)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); //關(guān)聯(lián)status_action。created and added to the toolbar as a toolbar item.
return tool_bar_manager;
}
protected StatusLineManager createStatusLineManager() {
return slm;
}
}
-
Interfacing with contributions
兩個(gè)途徑來將ActionContributionItem添加到GUI:
1. 通過ContributionManager子類的add()方法。
(1)可接受Action對象的參數(shù),從而間接的將ContributionItem和ContributionManager關(guān)聯(lián)。可多次執(zhí)行
(2)可直接接受ActionContributionItem對象的參數(shù)。只可執(zhí)行一次
2.通過ActionContributionItem類的fill()方法。根據(jù)其參數(shù)的不同,所先是的組件也不同,具體見下表:
-
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--鼠標(biāo)點(diǎn)擊的鍵盤塊捷方式
Listener methods for the Action class

雖然JFace使用action代替了SWT的listener/event機(jī)制,但是Actiono類仍然可以和listener協(xié)作來處理特定需求的事件。
IPropertyChangeListener接口關(guān)注客戶自定義的PropertyChangeEvents,當(dāng)所給的對象按照你所給的方式變成另一個(gè)對象時(shí),此事件被觸發(fā)。
Miscellaneous methods of the Action class
posted on 2006-03-24 17:02
JOO 閱讀(797)
評論(1) 編輯 收藏 所屬分類:
SWT & JFace IN ACTION