国产一区二区精品久久_蜜桃狠狠狠狠狠狠狠狠狠_午夜视频精品_激情都市一区二区

當(dāng)前位置:首頁 > 網(wǎng)站舊欄目 > 學(xué)習(xí)園地 > 設(shè)計(jì)軟件教程 > 如何使EMF模型對應(yīng)的編輯控件能處理Undo&Redo操作

如何使EMF模型對應(yīng)的編輯控件能處理Undo&Redo操作
2010-01-14 22:57:07  作者:  來源:

在EMF自動(dòng)生成的editor中,只提供了TreeViewer作為編輯控件,自動(dòng)生成的代碼能讓TreeViewer處理Redo&Undo操作,如果還需要對EMF對應(yīng)的Attribute使用Text, Combo等Widget進(jìn)行編輯的話,那么如何將這些Widget的編輯也能實(shí)現(xiàn)Undo&Redo操作呢?

首先我們來分析一下,對于TreeViewer是如何實(shí)現(xiàn)Redo&Undo操作的,在生成的editor代碼中有類似這樣的一句(這個(gè)是我改寫后的,實(shí)際生成的可能有所區(qū)別):為了只針對Redo&Undo對UI進(jìn)行更新,還需要對RedoAction和UndoAction做進(jìn)一步處理:
其做法就是要讓editor知道當(dāng)前的Action,然后在修改UI的時(shí)候,取得該Action看是否是我們指定的Action(RedoAction&UndoAction).

java 代碼
 
  1. viewer.setContentProvider(new AdapterFactoryContentProvider(   
  2.                 getAdapterFactory()));  

AdapterFactoryContentProvider實(shí)現(xiàn)了INotifyChangedListener接口,該接口就是用來處理在EMF模型發(fā)生變動(dòng)時(shí),如何更新綁定的UI控件:

java 代碼
 
  1. public void notifyChanged(Notification notification)   
  2.   {   
  3.     if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed())   
  4.     {   
  5.       // If the notification is an IViewerNotification, it specifies how ViewerRefresh should behave.  Otherwise fall   
  6.       // back to NotifyChangedToViewerRefresh, which determines how to refresh the viewer directly from the model   
  7.       // notification.   
  8.       //   
  9.       if (notification instanceof IViewerNotification)   
  10.       {   
  11.         if (viewerRefresh == null)   
  12.         {   
  13.           viewerRefresh = new ViewerRefresh(viewer);   
  14.         }   
  15.   
  16.         if (viewerRefresh.addNotification((IViewerNotification)notification))   
  17.         {   
  18.           viewer.getControl().getDisplay().asyncExec(viewerRefresh);   
  19.         }   
  20.       }   
  21.       else  
  22.       {   
  23.         NotifyChangedToViewerRefresh.handleNotifyChanged(   
  24.           viewer,   
  25.           notification.getNotifier(),   
  26.           notification.getEventType(),   
  27.           notification.getFeature(),   
  28.           notification.getOldValue(),   
  29.           notification.getNewValue(),   
  30.           notification.getPosition());   
  31.       }   
  32.     }   
  33.   }  

再看代碼:

java 代碼
 
  1. public AdapterFactoryContentProvider(AdapterFactory adapterFactory)   
  2.   {   
  3.     this.adapterFactory = adapterFactory;   
  4.   
  5.     if (adapterFactory instanceof IChangeNotifier)   
  6.     {   
  7.       ((IChangeNotifier)adapterFactory).addListener(this);   
  8.     }   
  9.   }  

這里關(guān)鍵的一句就是:

java 代碼
 
  1. ((IChangeNotifier)adapterFactory).addListener(this);  

它將更新UI的通知操作與adapterFactory關(guān)聯(lián)起來,然后在EMF模型發(fā)生變動(dòng)的時(shí)候,會(huì)從adapterFactory來找到所有注冊的INotifyChangedListener
因此做法就相當(dāng)簡單了,即將EMF模型對應(yīng)的編輯控件實(shí)現(xiàn)INotifyChangedListener接口,然后拿到adapterFactory,并把該控件注冊進(jìn)去.
下面我的一個(gè)實(shí)現(xiàn):
AbstractDoradoSection包含了EMF模型當(dāng)前節(jié)點(diǎn)屬性的對應(yīng)編輯控件集合,讓其實(shí)現(xiàn)INotifyChangedListener接口

并在構(gòu)造函數(shù)中注冊到adapterFactory中:

java 代碼
 
  1. public AbstractDoradoSection(IAdaptable adaptable, Object input,   
  2.             Composite parent, String title, int style) {   
  3.         this.adaptable = adaptable;   
  4.         this.input = input;   
  5.         this.title = title;   
  6.         this.style = ExpandableComposite.TITLE_BAR | style;   
  7.   
  8.         AdapterFactory adapterFactory = (AdapterFactory) adaptable   
  9.                 .getAdapter(AdapterFactory.class);   
  10.         ((IChangeNotifier) adapterFactory).addListener(this);   
  11.   
  12.         initialize(parent);   
  13.     }  
Redo&Undo更新UI處理:
java 代碼
 
  1. /**  
  2.  * 當(dāng)emf模型發(fā)生變化之后,更新ui,主要針對redo和undo處理  
  3.  *   
  4.  * @see org.eclipse.emf.edit.provider.INotifyChangedListener#notifyChanged(org.eclipse.emf.common.notify.Notification)  
  5.  */  
  6. public void notifyChanged(final Notification notification) {   
  7.     IActionProvider actionProvider = (IActionProvider) adaptable   
  8.             .getAdapter(IActionProvider.class);   
  9.     if (actionProvider.getAction() instanceof NotifierAction) {   
  10.         Object feature = notification.getFeature();   
  11.         if (feature instanceof EAttribute && rows != null) {   
  12.             for (Iterator iter = rows.iterator(); iter.hasNext();) {   
  13.                 final AttributeRow row = (AttributeRow) iter.next();   
  14.                 if (row.getAttribute() == feature) {   
  15.                     row.setTextContent(notification.getNewStringValue(), true);   
  16.                     break;   
  17.                 }   
  18.             }   
  19.         }   
  20.     }   

安徽新華電腦學(xué)校專業(yè)職業(yè)規(guī)劃師為你提供更多幫助【在線咨詢