EMF学习--CompoundCommand

1、新建插件工程graph,建立ecore文件。
EMF学习--CompoundCommand

2、由ecore生成对应的genmodel文件,用来生成Model代码,edit和editor两个插件。

3、修改edit插件的EdgeItemProvider类,使Edge的source属性可以和Node的outgoing属性之间实现双向引用。
覆写createSetCommand方法,当feature为GraphPackage.Literals.EDGE__SOURCE时,触发SetSrcEdgeCompoundCommand这样一个组合命令。
@Override
	protected Command createSetCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) {
		if (feature == GraphPackage.Literals.EDGE__SOURCE) {
			return new SetSrcEdgeCompoundCommand(domain, owner, feature, value);
		}
		return super.createSetCommand(domain, owner, feature, value);
	}

	private class SetSrcEdgeCompoundCommand extends CompoundCommand {

		private EditingDomain domain;
		private EObject owner;
		private Object value;

		public SetSrcEdgeCompoundCommand(EditingDomain domain, EObject owner, EStructuralFeature feature, Object value) {
			this.domain = domain;
			this.owner = owner;
			this.value = value;

			append(new SetCommand(domain, owner, feature, value));
		}

		@Override
		public void execute() {
			Node node = (Node) value;
			if (node != null) {
				Edge outgoingEdge = node.getOutgoing();
				if (outgoingEdge != null) {// 关联一个存在其他关联的对象
					appendAndExecute(new SetCommand(domain, node, GraphPackage.Literals.NODE__OUTGOING, null));
					appendAndExecute(new SetCommand(domain, outgoingEdge, GraphPackage.Literals.EDGE__SOURCE, null));
				}
			}

			Edge edge = (Edge) owner;
			Node sourceNode = edge.getSource();
			if (sourceNode == null) {// add
				appendAndExecute(new SetCommand(domain, node, GraphPackage.Literals.NODE__OUTGOING, edge));
			} else if (node == null) { // remove
				appendAndExecute(new SetCommand(domain, sourceNode, GraphPackage.Literals.NODE__OUTGOING, null));
			} else { // edit
				appendAndExecute(new SetCommand(domain, sourceNode, GraphPackage.Literals.NODE__OUTGOING, null));
				appendAndExecute(new SetCommand(domain, node, GraphPackage.Literals.NODE__OUTGOING, edge));
			}

			super.execute();
		}

	}

你可能感兴趣的:(emf)