用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);
- }
- }
最终效果图: