GEF画布的滚动条位移计算

   用GEF做流程图编辑器时,当流程图比较复杂,单个屏幕无法完全显示的时候就会出现滚动条。当有滚动条的时候,一些图形控件的位置计算就会出现相应的偏差。很明显的一个例子就是直接在图形上进行编辑时出现的那个输入框,gef里叫做NodeDirectEditCell。
如下图所示:



 

编辑框的弹出位置并没有落在testNode上,它的偏移距离恰好是滚动条的滚动位移。这在JavaScript里是很好解决的,在gef里也很容易,只是网上资料繁多一直都没有找到。当初看JBPM可视化插件源码的时候,想看看它是如何找到这个偏移量的,可是它并没有解决这个问题,不知道新的版本中有没有解决。没办法,只有自己一点一点找了。
这里关键的是要实现org.eclipse.gef.tools.CellEditorLocator这个借口,在relocate方法里计算出真实位置。废话少说直接上代码:

Java代码  收藏代码
  1. public class NodeCellEditorLocator implements CellEditorLocator {  
  2.     private NodeFigure nodeFigure;  
  3.   
  4.     /** 
  5.      * Creates a new ActivityCellEditorLocator for the given Label 
  6.      *  
  7.      * @param nodeFigure 
  8.      *            the Label 
  9.      */  
  10.     public NodeCellEditorLocator(NodeFigure nodeFigure) {  
  11.         this.nodeFigure = nodeFigure;   //得到当前编辑的figure  
  12.     }  
  13.   
  14.     /** 
  15.      * @see CellEditorLocator#relocate(org.eclipse.jface.viewers.CellEditor) 
  16.      */  
  17.     public void relocate(CellEditor celleditor) {  
  18.         Text text = (Text) celleditor.getControl();  
  19.         int scrollWidth = 0;//东西偏移量  
  20.         int scrollHeight = 0;//南北偏移量  
  21.   
  22.         FigureCanvas canvas = (FigureCanvas) text.getParent(); // 得到滚动区域的画布  
  23.         scrollWidth = canvas.getViewport().getHorizontalRangeModel().getValue();  
  24.         scrollHeight = canvas.getViewport().getVerticalRangeModel().getValue();  
  25.   
  26.         Point pref = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);  
  27.         Rectangle rect = this.nodeFigure.getTextBounds();   //得到覆盖的文本label  
  28.         text.setBounds(rect.x - 1 - scrollWidth, rect.y - 1 - scrollHeight,  
  29.                 pref.x + 1, pref.y + 1);  
  30.   
  31.     }  
  32.   
  33. }  
 

最终效果图:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值