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--可以簡單的理解成一個命令,可以關(guān)聯(lián)到菜單,工具條,以及按鈕
          Contribution--在JFace里面,一個Action可以對應(yīng)多個GUI對象,這些對象就是所謂的Contribution Item. 有兩個主要的Contribution類:ContributionItem和ContributionManager,它們都是抽象類,靠其子類來實現(xiàn)事件的處理。繼承關(guān)系見下圖
          ContributionItem--引發(fā)事件的單獨GUI組件
          ContributionManager--產(chǎn)生包含ContributionItems的對象

          ActionContributionItem--最重要,在ApplicationWindow中創(chuàng)建和實施,來將一個action連接至此GUI,它雖沒有設(shè)定好的外觀,但是依賴于你使用的fill()方法,卻可以幫助一個按鈕、菜單欄和工具欄的成形

          另一個與Contribution協(xié)作的方法是通過ContributionManager,它的子類類似于ContributionItem的container。其中MenuManager將ContributionItems組合在窗口最高層菜單, ToolBarManager則將這些對象放在僅在菜單之下的toolbar中。

          • 創(chuàng)建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領(lǐng)域內(nèi)的“Ctrl+T”確保了當(dāng)用戶在同時按下Ctrl鍵和T鍵時該動作就會被激發(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)建了一個Ch4_StatusAction的實例

          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中。因為這里的參數(shù)是Composite對象,所以根據(jù)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); //關(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

          兩個途徑來將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)點擊的鍵盤塊捷方式


          Listener methods for the Action class

          雖然JFace使用action代替了SWT的listener/event機(jī)制,但是Actiono類仍然可以和listener協(xié)作來處理特定需求的事件。
          IPropertyChangeListener接口關(guān)注客戶自定義的PropertyChangeEvents,當(dāng)所給的對象按照你所給的方式變成另一個對象時,此事件被觸發(fā)。

          Miscellaneous methods of the Action class

          posted on 2006-03-24 17:02 JOO 閱讀(803) 評論(1)  編輯  收藏 所屬分類: SWT & JFace IN ACTION

          FeedBack:
          # re: 4.2 Event processing in JFace
          2007-01-25 09:45 | cai niao
          great  回復(fù)  更多評論
            
          Hit the target!

          <2006年3月>
          2627281234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          常用鏈接

          留言簿(2)

          隨筆分類(23)

          隨筆檔案(22)

          文章檔案(1)

          相冊

          Neighbor

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 太和县| 芷江| 鄱阳县| 库尔勒市| 留坝县| 红安县| 长治市| 江华| 乌拉特后旗| 澳门| 乡宁县| 台湾省| 江阴市| 长岛县| 平南县| 云浮市| 舞阳县| 获嘉县| 万年县| 丹东市| 吉木乃县| 永寿县| 昭平县| 洛阳市| 连山| 洪湖市| 涞水县| 汨罗市| 曲沃县| 莒南县| 扶风县| 阿尔山市| 南丹县| 虹口区| 涪陵区| 申扎县| 澳门| 湛江市| 三江| 敖汉旗| 宝应县|