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是抽象類。
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 閱讀(802)
評論(1) 編輯 收藏 所屬分類:
SWT & JFace IN ACTION