原创作者: macrochen   阅读:1036次   评论:0条   更新时间:2011-06-01    

在EMF自动生成的editor中,只提供了TreeViewer作为编辑控件,自动生成的代码能让TreeViewer处理Redo&Undo操作,如果还需要对EMF对应的Attribute使用Text, Combo等Widget进行编辑的话,那么如何将这些Widget的编辑也能实现Undo&Redo操作呢?

首先我们来分析一下,对于TreeViewer是如何实现Redo&Undo操作的,在生成的editor代码中有类似这样的一句(这个是我改写后的,实际生成的可能有所区别):为了只针对Redo&Undo对UI进行更新,还需要对RedoAction和UndoAction做进一步处理:
其做法就是要让editor知道当前的Action,然后在修改UI的时候,取得该Action看是否是我们指定的Action(RedoAction&UndoAction).

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

AdapterFactoryContentProvider实现了INotifyChangedListener接口,该接口就是用来处理在EMF模型发生变动时,如何更新绑定的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.   }  

这里关键的一句就是:

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

它将更新UI的通知操作与adapterFactory关联起来,然后在EMF模型发生变动的时候,会从adapterFactory来找到所有注册的INotifyChangedListener
因此做法就相当简单了,即将EMF模型对应的编辑控件实现INotifyChangedListener接口,然后拿到adapterFactory,并把该控件注册进去.
下面我的一个实现:
AbstractDoradoSection包含了EMF模型当前节点属性的对应编辑控件集合,让其实现INotifyChangedListener接口

并在构造函数中注册到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.  * 当emf模型发生变化之后,更新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.     }   
  21. }  
评论 共 0 条 请登录后发表评论

发表评论

您还没有登录,请您登录后再发表评论

文章信息

Global site tag (gtag.js) - Google Analytics