ITeye博客

GEF画布的滚动条位移计算

popjxc · 2010-03-04 14:16 · GEF & Draw2d · 编程语言 · 面试 · Eclipse · JBPM · JavaScript · Markdown

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


 

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

public class NodeCellEditorLocator implements CellEditorLocator {
	private NodeFigure nodeFigure;

	/**
	 * Creates a new ActivityCellEditorLocator for the given Label
	 * 
	 * @param nodeFigure
	 *            the Label
	 */
	public NodeCellEditorLocator(NodeFigure nodeFigure) {
		this.nodeFigure = nodeFigure;	//得到当前编辑的figure
	}

	/**
	 * @see CellEditorLocator#relocate(org.eclipse.jface.viewers.CellEditor)
	 */
	public void relocate(CellEditor celleditor) {
		Text text = (Text) celleditor.getControl();
		int scrollWidth = 0;//东西偏移量
		int scrollHeight = 0;//南北偏移量

		FigureCanvas canvas = (FigureCanvas) text.getParent(); // 得到滚动区域的画布
		scrollWidth = canvas.getViewport().getHorizontalRangeModel().getValue();
		scrollHeight = canvas.getViewport().getVerticalRangeModel().getValue();

		Point pref = text.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		Rectangle rect = this.nodeFigure.getTextBounds();	//得到覆盖的文本label
		text.setBounds(rect.x - 1 - scrollWidth, rect.y - 1 - scrollHeight,
				pref.x + 1, pref.y + 1);

	}

}

 

最终效果图:


 

PS:前几天面试时被问到一个问题:当流程图比较复杂时,一个屏幕显示不下,用什么方法能够得到更好的用户操作?我回答说:可以考虑折叠功能,用户可以把嵌套的节点折叠起来。感觉和面试官预期的答案不是这个,后来想想整个画布的zoom in/zoom out更好一些.

评论(2)

  1. #2 楼 · wangacidlemon · 2012-05-18 23:47

    我在用GEF时出现这种情况,当鼠标选中某个图形时,图形居然往上移动。请问您有什么建议吗?

  2. #1 楼 · AquariusM · 2011-08-22 13:52

    相当不错

    
    int scrollWidth = 0;//东西偏移量  
            int scrollHeight = 0;//南北偏移量  
      
            FigureCanvas canvas = (FigureCanvas) text.getParent(); // 得到滚动区域的画布  
            scrollWidth = canvas.getViewport().getHorizontalRangeModel().getValue();  
            scrollHeight = canvas.getViewport().getVerticalRangeModel().getValue(); 
    

    这段代码帮我解决了问题 Thangk You!

评论功能维护中,当前仅展示历史评论。