最大的網(wǎng)站源碼資源下載站,
豐富的 gui 中的視圖可以以各種方式顯示信息,從而改善用戶體驗。非常自然,ui 視圖之間是相互依賴的,需要進(jìn)行交互。eclipse 簡化了鏈接 ui 視圖的工作,并提供了將視圖鏈接應(yīng)用到非 ui 場景的方式。
public void selectionchanged(iworkbenchpart part, iselection selection);
要使視圖能夠監(jiān)聽選擇改變,視圖必須實現(xiàn) iselectionlistener 接口并必須將自己注冊到工作臺頁面。清單 1 顯示一個例子。public class myview extends viewpart implements iselectionlistener{
public void createpartcontrol(composite parent) {
// add this view as a selection listener to the workbench page
getsite().getpage().addselectionlistener((iselectionlistener) this);
}
// implement the method defined in iselectionlistener, to consume ui selections
public void selectionchanged(iworkbenchpart part, iselection selection) {
//examine selection and act on it!
}
}
getsite().getpage().addselectionlistener("sampleviewid",(iselectionlistener)this);
這種方式可以避免對消費者視圖進(jìn)行多余的回調(diào),如果視圖被注冊為非特定的監(jiān)聽器,就會出現(xiàn)這種情況。 清單 2 中的代碼片段顯示一個視圖的 createpartcontrol() 方法,這個方法創(chuàng)建一個 jface tableviewer 并將它作為選擇提供器添加到工作臺站點。這些代碼使 tableviewer 中的任何 ui 選擇改變能夠傳播到頁面,并最終傳播到對這種事件感興趣的消費者視圖。public void createpartcontrol(composite parent) {
// set up a jface viewer
viewer = new tableviewer(parent, swt.multi | swt.h_scroll | swt.v_scroll);
viewer.setcontentprovider(new viewcontentprovider());
viewer.setlabelprovider(new viewlabelprovider());
viewer.setsorter(new namesorter());
viewer.setinput(getviewsite());
// add the jface viewer as a selection provider to the view site.
getsite().setselectionprovider(viewer);
}
  這個視圖將它創(chuàng)建的 jface tableviewer 注冊為選擇提供器有兩個原因:
  這個視圖打算使用這個 tableviewer 顯示信息,而且用戶將與 tableviewer 進(jìn)行交互。
  tableviewer 實現(xiàn)了選擇提供器接口并能夠向工作臺部分站點傳播選擇事件。
  因為 jface 查看器是選擇提供器,所以在大多數(shù)情況下就不必創(chuàng)建選擇提供器了。視圖只需使用眾多的 jface 查看器之一來顯示信息,并將 jface 查看器注冊為選擇提供器。
  另一種鏈接方式
  某些情況需要另一種視圖鏈接方式:
  信息量可能太大,由于內(nèi)存使用量增加,ui 選擇對象無法有效地容納它。
  視圖可能希望公布其他信息,而不只是公布可視化選擇信息。公布的信息可能是根據(jù)選擇進(jìn)行某些后期處理的結(jié)果。
  視圖可能希望使用來自另一個插件的信息,而這個插件可能根本沒有提供視圖(使用包含的 jface 查看器)。在這種情況下,使用基于 ui 選擇的鏈接是不可能的。
  可以使用 org.eclipse.core.runtime.iadaptable 接口來緩解第一個問題,這個接口使選擇對象能夠在需要時傳播更多信息。第二個和第三個問題需要用手工方式解決,屬性改變監(jiān)聽器模式是合適的解決方案。
  使用 iadaptable 接口
  實現(xiàn) iadaptable 接口的類能夠動態(tài)地返回某些類型的適配器,然后可以使用這些適配器獲取更多信息。如果查看器中的選擇對象實現(xiàn)了 iadaptable 接口,那么根據(jù)它們可以返回的適配器類型,可以有效地獲取更多信息或相關(guān)信息。org.eclipse.core.runtime.iadaptable 接口如下所示。
public void object getadapter(class adapter);
顯然,調(diào)用者應(yīng)該知道它期望選擇返回的適配器接口類型。考慮一個 jface treeviewer,它在一個單層的樹中顯示城市。代表城市的對象是 cityclass 類型的。cityclass 對象應(yīng)該包含關(guān)于此城市的基本信息,并只在需要時返回詳細(xì)信息。在清單 3 中要注意,cityclass 支持的適配器類型使調(diào)用者能夠在需要時獲得更多信息。class cityclass implements iadaptable { 
 private string cityname;
 public cityclass(string name) { 
  this.name = name;
 }
 public string getname() {
  return name;
 }
 public cityclass getparent() {
  return parent;
 }
 public string tostring() {
  return getname();
 } 
 public object getadapter(class key) {
  if (key.getname().equals("itransportationinfo"))
   return cityplugin.getinstance().gettransportadapter();
  else (key.getname().equals("iplacesinfo"))
   return cityplugin.getinstance().getplacesadapter();
  return null;
 }
}
//to add a listener for property changes to this notifier:
public void addpropertychangelistener(ipropertychangelistener listener);
//to remove the given content change listener from this notifier:
public void removepropertychangelistener(ipropertychangelistener listener);
class citypopulationplugin {
 arraylist mylisteners;
 // a public method that allows listener registration
 public void addpropertychangelistener(ipropertychangelistener listener) {
  if(!mylisteners.contains(listener))
   mylisteners.add(listener);
 }
 // a public method that allows listener registration
 public void removepropertychangelistener(ipropertychangelistener listener) {
  mylisteners.remove(listener);
 }
 public citypopulationplugin (){
  // method to start the thread that invokes the population /
  web service once every hour
  // and then notifies the listeners via the propertychange() callback method.
  initwebserviceinvokerthread( mylisteners );
 }
 void initwebserviceinvokerthread(arraylist listeners) {
  // code to invoke web service periodically, and retrieve information
  // post invocation, inform listeners
  for (iterator iter = listeners.iterator(); iter.hasnext();) {
   ipropertychangelistener element = (iproperty/
   changelistener) iter.next();
   element.propertychange(new propertychangeevent(this, /"citiesweatherxml" , null , cityweatherxmlobj));
  }
 }
}
class myview implements ipropertychangelistener {
 public void createpartcontrol() {
  //register with a known plugin that sources population data 
  citypopulationplugin.getinstance().addpropertychangelistener(this);
 }
 public void propertychange(propertychangeevent event) {
  //this view is interested in the population counts of the cities.
  //the population data is being sourced by another 
  plugin in the background.
  if( event.getproperty().equals("citiesweatherxml")) {
   object val = event.getnewvalue();
   // do something with val
  }
 }
}
新聞熱點
疑難解答
圖片精選