2008-04-25

GEF显示网格和吸附功能

关键字: gef grid snap
GraphicalEditor中,需要申明要开启网格和吸附功能.
protected void configureGraphicalViewer() {
       ...
       viewer.setProperty(SnapToGrid.PROPERTY_GRID_ENABLED,Boolean.TRUE); // 网格功能可用
       viewer.setProperty(SnapToGrid.PROPERTY_GRID_VISIBLE,Boolean.TRUE); // 网格可视
       viewer.setProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED, Boolean.TRUE); // 自动吸附
}


EditPart中
	/**
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class adapter) {
		if (adapter == SnapToHelper.class) {
			List snapStrategies = new ArrayList();
			Boolean val = (Boolean)getViewer().getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY);
			if (val != null && val.booleanValue())
				snapStrategies.add(new SnapToGuides(this));
			val = (Boolean)getViewer().getProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED);
			if (val != null && val.booleanValue())
				snapStrategies.add(new SnapToGeometry(this));
			val = (Boolean)getViewer().getProperty(SnapToGrid.PROPERTY_GRID_ENABLED);
			if (val != null && val.booleanValue())
				snapStrategies.add(new SnapToGrid(this));
			
			if (snapStrategies.size() == 0)
				return null;
			if (snapStrategies.size() == 1)
				return snapStrategies.get(0);

			SnapToHelper ss[] = new SnapToHelper[snapStrategies.size()];
			for (int i = 0; i < snapStrategies.size(); i++)
				ss[i] = (SnapToHelper)snapStrategies.get(i);
			return new CompoundSnapToHelper(ss);
		}
		return super.getAdapter(adapter);
	}


好了, 这样就可以了
  • A1dfc29e-8b47-3ae7-9aa8-ad5bfef46b19-thumb
  • 描述: gef显示网格和开启吸附的效果图
  • 大小: 14.1 KB
评论
发表评论

您还没有登录,请登录后发表评论

lggege
搜索本博客
我的相册
49edb3d3-94ce-3f1d-b3b2-d2dd99204513-thumb
IndexCardGenerator
共 13 张
最近加入圈子
存档
最新评论