国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

在定制Eclipse SWT組件中實(shí)現(xiàn)MVC

2024-07-21 02:14:51
字體:
供稿:網(wǎng)友
  eclipse swt(標(biāo)準(zhǔn)部件工具包)提供了豐富的 api 集來實(shí)現(xiàn)定制部件(widget)。在這篇文章中,作者簡要介紹了 mvc(模型-視圖-控制器)架構(gòu),以結(jié)構(gòu)化查看器的形式解釋了 mvc 的當(dāng)前實(shí)現(xiàn),并介紹了一種使用定制 swt 部件的實(shí)現(xiàn)。

  什么是 mvc?

  mvc 架構(gòu)(或設(shè)計(jì)模式)是圖形用戶界面(gui)的設(shè)計(jì)樣式,由三部分構(gòu)成:模型、視圖和控制器。mvc 把表示層從數(shù)據(jù)解耦出來,也把表示從數(shù)據(jù)的操作解耦出來。

  實(shí)現(xiàn) mvc 架構(gòu)與其他類型的應(yīng)用程序有所不同。主要的區(qū)別來自如何放置和實(shí)現(xiàn)業(yè)務(wù)邏輯或查看呈現(xiàn)邏輯。與典型的 web 應(yīng)用程序不同,在這類程序中,程序員必須設(shè)計(jì)和實(shí)現(xiàn)所有 mvc 組件,而 eclipse 提供的 api 可以替您做大部分控制或呈現(xiàn)工作。所以,不能嚴(yán)格地把 eclipse 的 mvc 實(shí)現(xiàn)與 web 或其他應(yīng)用程序類型的 mvc 進(jìn)行比較。

  eclipse jface

  eclipse jface 用內(nèi)容提供者和標(biāo)簽提供者實(shí)現(xiàn) mvc 架構(gòu)。jface api 包裝了標(biāo)準(zhǔn)(并非不重要的)部件,例如表和樹,并實(shí)現(xiàn)了結(jié)構(gòu)化內(nèi)容提供者和標(biāo)簽提供者。可以根據(jù)部件類型實(shí)現(xiàn)不同的內(nèi)容提供者。面向列表的查看器會(huì)實(shí)現(xiàn)結(jié)構(gòu)化查看器,而內(nèi)容則以結(jié)構(gòu)化(列表的)方式映射到部件條目上。

  基類叫做 viewer,它是結(jié)構(gòu)化查看器的一個(gè)擴(kuò)展。查看器充當(dāng)部件容器。內(nèi)容提供者以結(jié)構(gòu)化的方式得到數(shù)據(jù);類似地,標(biāo)簽提供者獲得對(duì)應(yīng)的標(biāo)簽。jface 查看器實(shí)現(xiàn)檢索該數(shù)據(jù),設(shè)置對(duì)應(yīng)的關(guān)聯(lián),并用數(shù)據(jù)集更新用戶界面(ui)組件。它還執(zhí)行選擇、過濾和排序。

  如何實(shí)現(xiàn) jface

  eclipse view 和 viewer 負(fù)責(zé)執(zhí)行大部分 jface 控制功能。viewer 或者說 mvc 的視圖部分,也充當(dāng)部件容器;這是表示組件。

  eclipse view 實(shí)例化 viewer、內(nèi)容提供者和標(biāo)簽提供者,并充當(dāng)模型,容納值對(duì)象,并在 viewer 中把它們?cè)O(shè)置為 inputelement。

  要?jiǎng)?chuàng)建 view,請(qǐng)用 createpartcontrol() 方法實(shí)例化 viewer。清單 1 實(shí)例化一個(gè)默認(rèn)的樹查看器;您也可以定制樹,并用樹對(duì)象作為參數(shù),用構(gòu)造函數(shù)實(shí)例化樹查看器。

  清單 1. exampleview 的 createpartcontrol 方法

  public class exampleview extends viewpart   { ... public void createpartcontrol(composite parent)   { // define a grid layout   gridlayout layout = new gridlayout();   layout.numcolumns = 1;   layout.marginheight = 0;   layout.marginwidth = 0; l  ayout.horizontalspacing = 0;   layout.verticalspacing = 1;   parent.setlayout(layout);   // create widgets createactionbar(parent);   createtree(parent);   // add context menu and listeners  viewer.adddoubleclicklistener(this); viewer.addselectionchangedlistener(openaction);   // register viewer so actions respond to selection getsite().setselectionprovider(viewer);   hookcontextmenu();   }  private void createtree(composite parent)   {   viewer = new treeviewer(parent, swt.single | swt.h_scroll | swt.v_scroll | swt.border);  viewer.setcontentprovider(new exampleviewcontentprovider()); viewer.setlabelprovider  (new exampleviewlabelprovider());   viewer.setsorter(new viewersorter());   viewer.setinput(modelmanager.getexamplemodel());   viewer.getcontrol().setlayoutdata(new griddata(griddata.fill_both));   } ... }   

  在另一個(gè)獨(dú)立類中實(shí)現(xiàn) contentprovider,它是一個(gè)對(duì)象,用適合查看器類型的接口向視圖提供數(shù)據(jù)。例如,您可以實(shí)現(xiàn) istructuredcontentprovider 或 itreecontentprovider 查看器。

  請(qǐng)?jiān)?contentprovider 代碼中實(shí)現(xiàn)以下一個(gè)方法,把 contentprovider 與 viewer 相關(guān)聯(lián):
  • getelements(object parent)
  • getchildren(object element)

  注意:jface 框架將調(diào)用這些方法。

  清單 2. 創(chuàng)建定制的 contentprovider

    public class exampleviewcontentprovide implements itreecontentprovide {  

  mvc 架構(gòu)通常包含多個(gè)視圖和一個(gè)數(shù)據(jù)源。目前在 eclipse 平臺(tái)上,只能把一個(gè)視圖與一個(gè)模型相關(guān)聯(lián)。但是,也可以創(chuàng)建多個(gè)視圖,用適配器視圖訪問同一數(shù)據(jù)。只要把 inputchanged() 方法包含在 contentprovider 類中即可。只要 viewer 有新的輸入集,就會(huì)使用 inputchanged() 方法通知 contentprovider。inputchanged() 方法接受 viewer 作為輸入?yún)?shù),所以多個(gè)視圖可以使用一個(gè) contentprovider。

  清單 3. 將 inputchanged 方法用于不同的查看器

  /** * register content provider with model. */   public void inputchanged(viewer viewer, object oldinput, object newinput)   {   if (newinput != null)      {   this.viewer = viewer;  this.model = (exampledelegate)newinput; this.model.addmodellistener(this);   }   }  
  與 eclipse swt 結(jié)合使用 mvc

  在多數(shù)常見 gui 應(yīng)用程序中,創(chuàng)建布局來顯示請(qǐng)求的數(shù)據(jù),或完成表單(例如用戶界面)來添加或修改數(shù)據(jù)。圖 1 的示例應(yīng)用程序演示了如何在定制表單中,用只讀和可編寫模式顯示來自 xml 存儲(chǔ)的數(shù)據(jù)。它還解釋了每個(gè)組件相對(duì)于 mvc 架構(gòu)的角色。

  圖 1. 示例應(yīng)用程序

圖 2 顯示了應(yīng)用程序的類圖,有助于更好地理解整體架構(gòu)。

  圖 2. 示例應(yīng)用程序的類圖



  創(chuàng)建控件

  exampleview 充當(dāng)整個(gè)應(yīng)用程序的容器。它將在 createpartcontrol 方法中初始化應(yīng)用程序。

  清單 4. createpartcontrol 方法初始化布局

  public void createpartcontrol(composite parent) {  exampleeditlayout _layout = new       exampleeditlayout(parent,swt.none,fieldmode.read,new exampleviewcontentprovider());  }

  創(chuàng)建表單和布局

  基本布局類定義了不同的表單應(yīng)用程序使用的全局方法和聲明。有些充當(dāng)回調(diào)機(jī)制的容器事件,也注冊(cè)到了這里。

  清單 5. 布局的 createcontrol 方法

  public void createcontrols(int style) {  griddata    griddata;  text                textfld, subjectfld;  control            tolabel, cclabel, bcclabel;  control            fromdatetime;  control            control;  button durationtext;  button submit;  gridlayout layout = new gridlayout(2, false);  layout.marginwidth = 0;  layout.marginheight = 4;  setlayout(layout);  //label  griddata = new griddata(griddata.horizontal_align_fill  | griddata.vertical_align_center);  griddata.horizontalindent = 10;  labelfactory.create(this,     messages.getstring("exampleeditlayout.title"), griddata); //$non-nls-1$  griddata = new griddata(griddata.horizontal_align_fill  | griddata.vertical_align_center);  griddata.horizontalindent = 40;  labelfactory.create(this, "", griddata);  //text  griddata = new griddata(griddata.horizontal_align_fill  | griddata.vertical_align_center);  griddata.horizontalindent = 10;  control = labelfactory.create(this,     messages.getstring("exampleeditlayout.email"), griddata); //$non-nls-1$  griddata = new griddata(griddata.horizontal_align_beginning  | griddata.vertical_align_center);  griddata.horizontalindent = 10;  control = textfactory.create(this,    swt.border | swt.v_scroll | swt.wrap, griddata, fieldmode.edit); //$non-nls-1$  addfield(new textfield(control, exampleviewcontentprovider.first_index));  //combo  griddata = new griddata(griddata.horizontal_align_fill  | griddata.vertical_align_center);  griddata.horizontalindent = 10;  labelfactory.create(this,     messages.getstring("exampleeditlayout.group"), griddata); //$non-nls-1$  griddata = new griddata(griddata.horizontal_align_beginning  | griddata.vertical_align_center);  griddata.horizontalindent = 40;  control = combofactory.create(this,     fieldmode.edit, false, griddata); //$non-nls-1$  addfield(new combofield(control,  exampleviewcontentprovider.second_index));  ...}  
  創(chuàng)建字段(視圖)

  field 是一個(gè)抽象類,它定義了包含各種用戶界面控件的方法,還有全局地識(shí)別這些控件的相關(guān) id。每個(gè)用戶界面控件都是 field 的子類,并向內(nèi)容提供者提供了讀寫能力。清單 6 用工廠模式,在 layout 類中創(chuàng)建了 field。

  清單 6. 用 field 類創(chuàng)建文本對(duì)象

  public class textfield extends field {      /**      * @param control      * @param id      */      public textfield(control control, int id) {          super(control, id);      }      /* based on the id of the widget, values retrieved from        * the content provider are set.       */      public  void readfromcontent(iexamplecontentprovider content) {          string newtext = (string )content.getelement(getid());          if (newtext != null)              ((text )_control).settext(newtext);      }      /* based on the id of the widget, values retrieved from widget are        * sent back to the content provider.       */      public void writetocontent(iexamplecontentprovider content) {          string newtext = ((text )_control).gettext();          content.setelement(getid(), newtext);      }  }

  簡化內(nèi)容提供者(模型)

  exampleviewcontentprovider 充當(dāng)模型偵聽器,后者擴(kuò)展自 istructuredcontentprovider。它是 eclipse api 的簡單實(shí)現(xiàn),提供了用于檢索數(shù)據(jù)的回調(diào)。每個(gè)請(qǐng)求數(shù)據(jù)的條目都基于視圖創(chuàng)建時(shí)在布局中為條目定義的惟一 id。

  方法調(diào)用會(huì)返回與每個(gè)定義的全局 id 關(guān)聯(lián)的數(shù)據(jù)。在 清單 7 所示的內(nèi)容提供者中,可以使用適配器從 xml 文件或數(shù)據(jù)庫檢索數(shù)據(jù)。

  清單 7. 在定制的 contentprovider 中實(shí)現(xiàn)方法

  public object getelement(int iindex) {          switch (iindex) {          case first_index: return "[email protected]";          case second_index : return new integer(1);          case fourth_index : return new boolean(true);          case third_index: return new boolean(false);          case fifth_index: return new boolean(false);          }          return null;      }  

  創(chuàng)建了控件并初始化布局之后,表單會(huì)用控件 id 要求內(nèi)容提供者用數(shù)據(jù)填充表單控件。

  清單 8. 初始化布局并填充控件的表單

  public form (composite parent, int style, fieldmode mode, exampleviewcontentprovider content) {              super(parent, style);              _content = content;              _style = style;              setmode(mode);              init(style);      }         private void init(int style) {              createcontrols(style);          controlscreated();      }  protected void controlscreated() {              readfromcontent();      }  

  結(jié)束語

  web 應(yīng)用程序是 mvc 架構(gòu)樣式的早期實(shí)現(xiàn)者。但是,隨著像 eclipse 這樣的簡單而強(qiáng)大的開發(fā)平臺(tái)的到來,程序員可以輕易地用更短的時(shí)間和最小的復(fù)雜程度,開發(fā)出更豐富的用戶界面。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 无锡市| 桐梓县| 沾益县| 焦作市| 孟津县| 瓦房店市| 贡觉县| 法库县| 清原| 外汇| 徐水县| 资讯 | 定远县| 包头市| 台中市| 武穴市| 汉川市| 望城县| 无极县| 堆龙德庆县| 固原市| 长寿区| 丰城市| 尼木县| 河北省| 冀州市| 鄂州市| 毕节市| 三亚市| 河北区| 昭通市| 万宁市| 唐河县| 萨嘎县| 石门县| 长乐市| 昌宁县| 武宣县| 秦安县| 怀柔区| 北川|