`

实现GEF RulerComposite

    博客分类:
  • GEF
阅读更多
模型部分:
public class ElementBase extends AbstractModel implements IAdaptable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 布局
	 */
	private Rectangle layout;

	/**
	 * 获得布局
	 * 
	 * @return
	 */
	public Rectangle getLayout() {
		return layout;
	}

	public void setLayout(Rectangle layout) {
		this.layout = layout;
	}

	/**
	 * 垂直向导、水平向导
	 */
	private Guide verticalGuide, horizontalGuide;

	public Guide getVerticalGuide() {

		return verticalGuide;

	}

	/**
	 * 设置导航参数
	 * 
	 * @param verticalGuide
	 */
	public void setVerticalGuide(Guide verticalGuide) {

		this.verticalGuide = verticalGuide;

	}

	public Guide getHorizontalGuide() {

		return horizontalGuide;

	}

	public void setHorizontalGuide(Guide horizontalGuide) {

		this.horizontalGuide = horizontalGuide;

	}

	public Object getAdapter(Class adapter) {
		return null;
	}


/**
 * 向导
 * 
 * @author Ming.He
 * 
 */
public class Guide implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 对于一个向导, 当附件部分被改变时,这个属性通常用来通知监听者
	 */
	public static final String PROPERTY_CHILDREN = "subparts changed";

	/**
	 * 
	 * Property used to notify listeners when the guide is re-positioned
	 */

	public static final String PROPERTY_POSITION = "position changed";

	protected PropertyChangeSupport listeners = new PropertyChangeSupport(this);

	private Map map;

	/**
	 * 定位
	 */
	private int position;

	/**
	 * 是否水平
	 */
	private boolean horizontal;

	/**
	 * 
	 * Empty default constructor
	 */

	public Guide() {

		// empty constructor

	}

	/**
	 * 
	 * Constructor
	 * 
	 * 
	 * 
	 * @param isHorizontal
	 * 
	 *            <code>true</code> if the guide is horizontal (i.e., placed on
	 * 
	 *            a vertical ruler)
	 */

	public Guide(boolean isHorizontal) {

		setHorizontal(isHorizontal);

	}

	/**
	 * 
	 * @see PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
	 */

	public void addPropertyChangeListener(PropertyChangeListener listener) {

		listeners.addPropertyChangeListener(listener);

	}

	/*
	 * 
	 * @TODO:Pratik use PositionConstants here
	 */

	/**
	 * 
	 * Attaches the given part along the given edge to this guide. The
	 * 
	 * LogicSubpart is also updated to reflect this attachment.
	 * 
	 * 
	 * 
	 * @param part
	 * 
	 *            The part that is to be attached to this guide; if the part is
	 * 
	 *            already attached, its alignment is updated
	 * 
	 * @param alignment
	 * 
	 *            -1 is left or top; 0, center; 1, right or bottom
	 */

	public void attachElement(ElementBase element, int alignment) {

		if (getMap().containsKey(element) && getAlignment(element) == alignment)

			return;

		getMap().put(element, alignment);

		Guide parent = isHorizontal() ? element.getHorizontalGuide() : element

		.getVerticalGuide();

		if (parent != null && parent != this) {

			parent.detachElement(element);

		}

		if (isHorizontal()) {

			element.setHorizontalGuide(this);

		} else {

			element.setVerticalGuide(this);

		}

		listeners.firePropertyChange(PROPERTY_CHILDREN, null, element);

	}

	/**
	 * 
	 * Detaches the given part from this guide. The LogicSubpart is also updated
	 * 
	 * to reflect this change.
	 * 
	 * 
	 * 
	 * @param part
	 * 
	 *            the part that is to be detached from this guide
	 */

	public void detachElement(ElementBase element) {

		if (getMap().containsKey(element)) {

			getMap().remove(element);

			if (isHorizontal()) {

				element.setHorizontalGuide(null);

			} else {

				element.setVerticalGuide(null);

			}

			listeners.firePropertyChange(PROPERTY_CHILDREN, null, element);

		}

	}

	/**
	 * 
	 * This methods returns the edge along which the given part is attached to
	 * 
	 * this guide. This information is used by
	 * 
	 * {@link org.eclipse.gef.examples.logicdesigner.edit.LogicXYLayoutEditPolicy
	 * 
	 * LogicXYLayoutEditPolicy} to determine whether to attach or detach a part
	 * 
	 * from a guide during resize operations.
	 * 
	 * 
	 * 
	 * @param part
	 * 
	 *            The part whose alignment has to be found
	 * 
	 * @return an int representing the edge along which the given part is
	 * 
	 *         attached to this guide; 1 is bottom or right; 0, center; -1, top
	 * 
	 *         or left; -2 if the part is not attached to this guide
	 * 
	 * @see org.eclipse.gef.examples.logicdesigner.edit.LogicXYLayoutEditPolicy#createChangeConstraintCommand(ChangeBoundsRequest,
	 *      EditPart, Object)
	 */

	public int getAlignment(ElementBase element) {

		if (getMap().get(element) != null)

			return ((Integer) getMap().get(element)).intValue();

		return -2;

	}

	/**
	 * 
	 * @return The Map containing all the parts attached to this guide, and
	 * 
	 *         their alignments; the keys are LogicSubparts and values are
	 * 
	 *         Integers
	 */

	public Map getMap() {

		if (map == null) {

			map = new Hashtable();

		}

		return map;

	}

	/**
	 * 
	 * @return the set of all the parts attached to this guide; a set is used
	 * 
	 *         because a part can only be attached to a guide along one edge.
	 */

	public Set getParts() {

		return getMap().keySet();

	}

	/**
	 * 
	 * @return the position/location of the guide (in pixels)
	 */

	public int getPosition() {

		return position;

	}

	/**
	 * 
	 * @return <code>true</code> if the guide is horizontal (i.e., placed on a
	 * 
	 *         vertical ruler)
	 */

	public boolean isHorizontal() {

		return horizontal;

	}

	/**
	 * 
	 * @see PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
	 */

	public void removePropertyChangeListener(PropertyChangeListener listener) {

		listeners.removePropertyChangeListener(listener);

	}

	/**
	 * 
	 * Sets the orientation of the guide
	 * 
	 * 
	 * 
	 * @param isHorizontal
	 * 
	 *            <code>true</code> if this guide is to be placed on a vertical
	 * 
	 *            ruler
	 */

	public void setHorizontal(boolean isHorizontal) {

		horizontal = isHorizontal;

	}

	/**
	 * 
	 * Sets the location of the guide
	 * 
	 * 
	 * 
	 * @param offset
	 * 
	 *            The location of the guide (in pixels)
	 */

	public void setPosition(int offset) {

		if (position != offset) {

			int oldValue = position;

			position = offset;

			listeners.firePropertyChange(PROPERTY_POSITION, new Integer(

			oldValue), new Integer(position));

		}

	}

}


/**
 * 标尺
 * 
 * @author Ming.He
 * 
 */
public class Ruler implements Serializable {

	public static final String PROPERTY_CHILDREN = "children changed";

	public static final String PROPERTY_UNIT = "units changed";

	static final long serialVersionUID = 1;

	protected PropertyChangeSupport listeners = new PropertyChangeSupport(this);

	private int unit;

	private boolean horizontal;

	private List<Guide> guides = new ArrayList<Guide>();

	public Ruler(boolean isHorizontal) {

		this(isHorizontal, RulerProvider.UNIT_PIXELS);

	}

	public Ruler(boolean isHorizontal, int unit) {

		horizontal = isHorizontal;

		setUnit(unit);

	}

	public void addGuide(Guide guide) {

		if (!guides.contains(guide)) {

			guide.setHorizontal(!isHorizontal());

			guides.add(guide);

			listeners.firePropertyChange(PROPERTY_CHILDREN, null, guide);

		}

	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {

		listeners.addPropertyChangeListener(listener);

	}

	// the returned list should not be modified

	public List<Guide> getGuides() {

		return guides;

	}

	public int getUnit() {

		return unit;

	}

	public boolean isHidden() {

		return false;

	}

	public boolean isHorizontal() {

		return horizontal;

	}

	public void removeGuide(Guide guide) {

		if (guides.remove(guide)) {

			listeners.firePropertyChange(PROPERTY_CHILDREN, null, guide);

		}

	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {

		listeners.removePropertyChangeListener(listener);

	}

	public void setHidden(boolean isHidden) {

	}

	public void setUnit(int newUnit) {

		if (unit != newUnit) {

			int oldUnit = unit;

			unit = newUnit;

			listeners.firePropertyChange(PROPERTY_UNIT, oldUnit, newUnit);

		}

	}

}


标尺提供者:
/**
 * 编辑器标尺提供者
 * 
 * @author Ming.He
 * 
 */
public class EditorRulerProvider extends RulerProvider {

	/** 标尺 */
	private Ruler ruler;

	/** 标尺监听 */
	private PropertyChangeListener rulerListener = new PropertyChangeListener() {

		public void propertyChange(PropertyChangeEvent evt) {

			if (evt.getPropertyName().equals(Ruler.PROPERTY_CHILDREN)) {

				Guide guide = (Guide) evt.getNewValue();

				if (getGuides().contains(guide)) {

					guide.addPropertyChangeListener(guideListener);

				} else {

					guide.removePropertyChangeListener(guideListener);

				}

				for (int i = 0; i < listeners.size(); i++) {

					((RulerChangeListener) listeners.get(i))

					.notifyGuideReparented(guide);

				}

			} else {

				for (int i = 0; i < listeners.size(); i++) {

					((RulerChangeListener) listeners.get(i))

					.notifyUnitsChanged(ruler.getUnit());

				}

			}

		}

	};

	private PropertyChangeListener guideListener = new PropertyChangeListener() {

		public void propertyChange(PropertyChangeEvent evt) {

			if (evt.getPropertyName().equals(Guide.PROPERTY_CHILDREN)) {

				for (int i = 0; i < listeners.size(); i++) {

					((RulerChangeListener) listeners.get(i))

					.notifyPartAttachmentChanged(evt.getNewValue(), evt

					.getSource());

				}

			} else {

				for (int i = 0; i < listeners.size(); i++) {

					((RulerChangeListener) listeners.get(i))

					.notifyGuideMoved(evt.getSource());

				}

			}

		}

	};

	public EditorRulerProvider(Ruler ruler) {

		this.ruler = ruler;

		this.ruler.addPropertyChangeListener(rulerListener);

		List guides = getGuides();

		for (int i = 0; i < guides.size(); i++) {

			((Guide) guides.get(i))

			.addPropertyChangeListener(guideListener);

		}

	}

	public List getAttachedModelObjects(Object guide) {

		return new ArrayList(((Guide) guide).getParts());

	}

	public Command getCreateGuideCommand(int position) {

		return new CreateGuideCommand(ruler, position);

	}

	public Command getDeleteGuideCommand(Object guide) {

		return new DeleteGuideCommand((Guide) guide, ruler);

	}

	public Command getMoveGuideCommand(Object guide, int pDelta) {

		return new MoveGuideCommand((Guide) guide, pDelta);
	}

	public int[] getGuidePositions() {

		List guides = getGuides();

		int[] result = new int[guides.size()];

		for (int i = 0; i < guides.size(); i++) {

			result[i] = ((Guide) guides.get(i)).getPosition();

		}

		return result;

	}

	public Object getRuler() {

		return ruler;

	}

	public int getUnit() {

		return ruler.getUnit();

	}

	public void setUnit(int newUnit) {

		ruler.setUnit(newUnit);

	}

	public int getGuidePosition(Object guide) {

		return ((Guide) guide).getPosition();

	}

	public List getGuides() {

		return ruler.getGuides();

	}
}


向导操作命令:
/**
 * 创建向导命令
 * 
 * @author Ming.He
 * 
 */
public class CreateGuideCommand extends Command {

	/** 向导 */
	private Guide guide;

	/** 标尺 */
	private Ruler parent;

	/** 位置 */
	private int position;

	public CreateGuideCommand(Ruler parent, int position) {

		super("Create guide");

		this.parent = parent;

		this.position = position;

	}

	public boolean canUndo() {

		return true;

	}

	public void execute() {

		if (guide == null)

			guide = new Guide(!parent.isHorizontal());

		guide.setPosition(position);

		parent.addGuide(guide);

	}

	public void undo() {

		parent.removeGuide(guide);

	}

}


/**
 * 删除向导命令
 * @author Ming.He
 *
 */
public class DeleteGuideCommand extends Command {

	/**
	 * 标尺
	 */
	private Ruler parent;
	/**
	 * 向导
	 */
	private Guide guide;
	/**
	 * 已创建的控制器
	 */
	private Map oldParts;

	public DeleteGuideCommand(Guide guide, Ruler parent) {
		super("Delete guide");
		this.guide = guide;
		this.parent = parent;
	}

	public boolean canUndo() {
		return true;
	}

	public void execute() {
		oldParts = new HashMap(guide.getMap());
		Iterator iter = oldParts.keySet().iterator();
		while (iter.hasNext()) {
			guide.detachElement((ElementBase) iter.next());
		}
		parent.removeGuide(guide);
	}

	public void undo() {
		parent.addGuide(guide);
		Iterator iter = oldParts.keySet().iterator();
		while (iter.hasNext()) {
			ElementBase element = (ElementBase) iter.next();
			guide.attachElement(element, ((Integer) oldParts.get(element)).intValue());
		}
	}
}


/**
 * 移动向导命令
 * @author Ming.He
 *
 */
public class MoveGuideCommand extends Command {

	/**三角定位*/
	private int pDelta;
	/**向导*/
	private Guide guide;

	public MoveGuideCommand(Guide guide, int positionDelta) {
		super("Move guide");
		this.guide = guide;
		pDelta = positionDelta;
	}

	public void execute() {
		guide.setPosition(guide.getPosition() + pDelta);
		Iterator iter = guide.getParts().iterator();
		while (iter.hasNext()) {
			ElementBase element = (ElementBase) iter.next();
			Rectangle layout = element.getLayout().getCopy();
			if (guide.isHorizontal()) {
				layout.y += pDelta;
			} else {
				layout.x += pDelta;
			}
			element.setLayout(layout);
		}
	}

	public void undo() {
		guide.setPosition(guide.getPosition() - pDelta);
		Iterator iter = guide.getParts().iterator();
		while (iter.hasNext()) {
			ElementBase element = (ElementBase) iter.next();
			Rectangle layout = element.getLayout().getCopy();
			if (guide.isHorizontal()) {
				layout.y -= pDelta;
			} else {
				layout.x -= pDelta;
			}
			element.setLayout(layout);
		}
	}

}


最后在编辑器中配置RulerComposite(因为GEF是支持RulerComposite的,所以配置就可以了)
/** 标尺容器 */
	private RulerComposite rulerComp;// Override
	protected void createGraphicalViewer(Composite parent) {
		rulerComp = new RulerComposite(parent, SWT.NONE);

		super.createGraphicalViewer(rulerComp);

		rulerComp.setGraphicalViewer((ScrollingGraphicalViewer) getGraphicalViewer());

	}


/**
	 * 配置标尺
	 */
	private void configureRuler() {

		GraphicalViewer viewer = getGraphicalViewer();

		viewer.setProperty(RulerProvider.PROPERTY_VERTICAL_RULER,

		new EditorRulerProvider(new Ruler(false)));

		viewer.setProperty(RulerProvider.PROPERTY_HORIZONTAL_RULER,

		new EditorRulerProvider(new Ruler(true)));

		viewer.setProperty(RulerProvider.PROPERTY_RULER_VISIBILITY, true);

		IAction action = new ToggleRulerVisibilityAction(getGraphicalViewer());

		getActionRegistry().registerAction(action);

	}


最后在configureGraphicalViewer方法中调用configureRuler即可
  • 大小: 31.8 KB
2
0
分享到:
评论

相关推荐

    gef 转折线的相关方法实现和 GEF的API chm 格式

    在本篇文章中,我们将深入探讨gef转折线的相关方法实现以及GEF的API。 首先,让我们理解一下什么是转折线。在图形编辑中,转折线通常指的是具有多个折点的线条,这些折点可以由用户交互式地调整,以改变线条的形状...

    GEF实现拷贝粘贴

    标题“GEF实现拷贝粘贴”涉及到的是在软件开发中使用Graphical Editing Framework(GEF)进行图形界面编辑时,如何实现复制和粘贴功能的技术。GEF是Eclipse平台下用于构建图形化编辑器的框架,它提供了一套完整的...

    GEF典型实现例子

    标题“GEF典型实现例子”指的是使用Graphical Editing Framework (GEF)的示例应用。GEF是Eclipse平台下的一个开源项目,主要用于构建图形化编辑工具,它提供了丰富的图形用户界面(GUI)组件和框架,帮助开发者创建...

    自己下的GEF资源打包

    "GEF_Tutorial.pdf"很可能是GEF的基础教程,涵盖了GEF的基本概念、架构和使用方法,包括图元的创建、连接线的绘制、交互操作的实现等。用户可以通过阅读这个教程来了解如何在Eclipse环境中搭建GEF项目,并进行基本的...

    GEF中属性页面的实现

    在GEF(Graphical Editing Framework)中,属性视图(Property View)的实现是一个关键功能,它允许用户查看和编辑图形模型的属性。属性视图是数据源与用户界面之间的桥梁,确保图形模型和属性视图之间保持同步更新...

    GEF入门必读 GEF入门系列 GEF-whole-upload

    本资料集旨在为初学者提供一个全面的入门指南,帮助理解GEF的基础概念和实现方法。 GEF的核心功能是提供了一套强大的组件和API,用于创建可交互的、图形式的用户界面。通过使用GEF,开发者可以轻松地创建出具有拖放...

    GEF快速入门教程和EMF教程

    本文将详细介绍如何使用Graphical Editing Framework (GEF)实现一个简单的“Hello World”示例,并结合Eclipse Modeling Framework (EMF)进行扩展。通过此教程,读者能够理解GEF的基本架构以及如何在Eclipse RCP应用...

    GEF入门学习例子

    这个入门学习例子旨在帮助初学者理解GEF的基本用法和核心概念,通过实例展示如何利用GEF实现工具栏、菜单栏、属性栏以及图形的交互功能,如移动、删除、撤销、连线等,并涵盖了大纲视图和鹰眼功能的实现。...

    GEF-ALL-3.7+GEF-ALL-3.8+GEF_Draw2d学习资料

    它的核心功能包括图形对象的创建、拖放操作、连接线的管理、以及各种图形编辑行为的实现。GEF-3.7和GEF-3.8是其不同版本,可能包含不同的特性和改进,例如性能优化、API调整或新功能的添加。 Draw2D则是一个底层的...

    GEF_Tutorial.rar(GEF开发指南)

    **GEF(Graphical Editing Framework)开发指南*...总之,"GEF_Tutorial.rar"是一个宝贵的资源,它将引导你踏入GEF开发的世界,通过实例学习,你将能够熟练地创建出功能丰富的图形编辑器,实现各种复杂的图形用户界面。

    GEF_Demo_Code20170307

    这个项目旨在演示如何利用GEF进行图形界面开发,并且特别关注了通过适配器(Adapter)扩展点来实现属性页配置以及在非Editor的ViewPart中使用GEF。 GEF是Eclipse平台下的一个开源库,专门用于构建可自定义的、图形...

    GEF入门实例代码2《Eclipse插件开发》中实例

    总之,通过这个"GEF入门实例代码2",开发者可以了解到如何在Eclipse中构建一个基于GEF的图形编辑器,这不仅涉及数据模型的设计,还包括视图的绘制、用户交互的处理以及可撤销/重做机制的实现。通过实践,开发者可以...

    GEF锚点鼠标定位

    GEF的核心组件包括模型(Model)、视图(View)、编辑器(Editor)和控制器(Controller)等,这些组件共同协作以实现图形编辑功能。 2. **锚点(Anchor)**: 锚点是连接线在图形元素上的固定点,它决定了连接线...

    Eclipse的GEF学习

    通过将复杂的编辑操作抽象为请求(Request)、角色(Role)、策略(EditPolicy)和命令(Command),GEF提供了一套灵活且强大的机制,让开发者可以专注于业务逻辑,而不必担心图形界面的具体实现细节。 #### 五、GEF在实际...

    GEF-Update-3.7.1.zip

    4. **plugins**:这个目录通常包含了GEF插件的具体实现。每个子目录代表一个Eclipse插件,包含了对应的JAR文件和其他资源。这些插件扩展了Eclipse的平台,提供了图形编辑、图元操作、模型转换等功能。 5. **...

    GEF教程及demo源码GEF_RCP_DEMO.zip

    GEF中的Figure类负责图形绘制,通过重写paint()方法实现自定义图形的绘制,同时支持事件监听和交互。 5. **编辑操作与命令模式** GEF使用命令模式来处理用户的编辑操作,每个编辑操作对应一个Command对象。当用户...

    GEF Example Source Code

    - **模型-视图-控制器(MVC)**:理解GEF如何实现MVC设计模式,以及如何将数据模型与图形视图绑定。 - **事件处理**:学习如何监听和响应用户交互,如点击、拖放、键盘输入等。 - **撤销/重做(Undo/Redo)**:了解...

    GEF 3.10 eclipse 插件

    3. **连接器支持**:GEF 3.10支持自定义的连接器,用于连接图形元素,可以实现复杂的连接关系。 4. **ZOrder管理**:支持图形元素的层次管理,可以调整元素的前后顺序,实现图层效果。 5. **事件处理**:提供了一套...

    GEF简易教程-学习GEF的入门教程

    ### GEF简易教程知识点详解 #### 一、GEF简介与环境配置 **GEF**,全称为**Graphical Editing Framework**,是Eclipse平台上用于创建复杂图形编辑器的框架。它提供了一套完整的工具集,使开发者能够构建具有图形化...

Global site tag (gtag.js) - Google Analytics