GEF中文教程 自学笔记 8

目标:1. 使用ZoomManager来执行图形的缩放
2. 对图形进行对齐(Alignment)操作
3. 图形修改后进行dirty check(提示保存文档)

 

图形缩放
提供缩放能力的方法就是设置根图形的RootEditPart为ScalableRootEditPart。一般在Editor的configureGraphicalViewer()方法中设置RootEditPart。
ScalableRootEditPart提供了一个ZoomManager类用来管理图形的缩放操作。
通过该ZoomManager,我们可以创建出实现缩放的Actions(ZoomInActionZoomOutAction)。

一个问题:为什么ZoomInAction的注册工作放在configureGraphicalViewer()方法中而不是前面注册DirectEditAction时用到的createActions()方法?
因为createActions()方法是Editor的init()函数调用的,而configureGraphicalViewer()方法是Editor的createPartControl()方法调用的,创建EditorPart时init()函数先于createPartControl()函数被调用。由于我们创建ZoomInAction需要用到ScalableRootEditPart提供的ZoomManager,所以只能容忍这个不统一的地方了。

还可以用ZoomComboContributionItem实现缩放比例组合框。
值得注意的是ZoomComboContributionItem会在part激活时设置它所需要的ZoomManager,通过(ZoomManager) part.getAdapter(ZoomManager.class)这样的手段。所以所有需要使用该组合框的视图或编辑器都需要实现getAdapter方法,以返回正确的ZoomManager实例。
public Object getAdapter(Class type) {
    if (type == ZoomManager.class) {
        return ((ScalableRootEditPart) getGraphicalViewer().getRootEditPart()).getZoomManager();
    }
    return super.getAdapter(type);
}

图形对齐
GEF的图形对齐非常简单,直接使用AlignmentAction类即可。
protected void createActions() {
    super.createActions();
    ActionRegistry registry = getActionRegistry();
    IAction action = new AlignmentAction((IWorkbenchPart) this, PositionConstants.LEFT);
    registry.registerAction(action);
    getSelectionActions().add(action.getId());
    ......}
通过把action加入selection actions list使得该action可以响应选择改变(selection change)事件从而更新自己的状态。如果不加这句代码,左对齐工具按钮永远都是灰色的

Dirty Check:
由于所有的操作都是由CommandStack管理的,所以通过CommandStack的isDirty()方法就可以得知Editor是否有发生修改。

此外再让DiagramEditor重载以下两个方法
public void doSave(IProgressMonitor monitor) {
    getCommandStack().markSaveLocation();
}
public void commandStackChanged(EventObject event) {
    firePropertyChange(PROP_DIRTY);
    super.commandStackChanged(event);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值