---
title: "GEF画布的滚动条位移计算"
author: "popjxc"
date: "2010-03-04T14:16:32Z"
tags: ["Eclipse", "JBPM", "JavaScript", "面试"]
categories: ["编程语言"]
slug: "gef"
draft: false
lastmod: "2010-03-04T14:17:45Z"
id: 607766
url: "https://popjxc.iteye.com/blog/607766"
---

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

![](http://dl.iteye.com/upload/attachment/212072/ba6968c2-f0b3-3b42-bb64-05d90f9b5604.jpg)  
 

编辑框的弹出位置并没有落在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);

	}

}
```

 

最终效果图：

![](http://dl.iteye.com/upload/attachment/212074/8dba1683-2977-3829-84a9-d143c77c4e58.jpg)  
 

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