shapescontainereditpolicy.java

来自「mywork是rcp开发的很好的例子」· Java 代码 · 共 78 行

JAVA
78
字号
package net.sf.freenote.parts;

import java.util.List;

import net.sf.freenote.model.Shape;
import net.sf.freenote.model.ShapesContainer;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CompoundCommand;
import org.eclipse.gef.editpolicies.ContainerEditPolicy;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.gef.requests.GroupRequest;

/**
 * 对容器中移动图形,无父图形的操作
 * @author levin
 * @since 2008-2-5 下午05:02:32
 */
public class ShapesContainerEditPolicy extends ContainerEditPolicy {
	@Override
	protected Command getAddCommand(GroupRequest request) {
		return super.getAddCommand(request);
	}

	@Override
	protected Command getCreateCommand(CreateRequest request) {
		return null;
	}

	@Override
	protected Command getOrphanChildrenCommand(GroupRequest request) {
		List parts = request.getEditParts();
		CompoundCommand result = new CompoundCommand("Boundary");
		for (int i = 0; i < parts.size(); i++) {
			OrphanChildCommand orphan = new OrphanChildCommand();
			orphan.setChild((Shape) ((EditPart) parts.get(i)).getModel());
			orphan.setParent((ShapesContainer) getHost().getModel());
			orphan.setLabel("Boundary");
			result.add(orphan);
		}
		return result.unwrap();
	}
	class OrphanChildCommand extends Command {

		private Point oldLocation;
		private ShapesContainer diagram;
		private Shape child;

		public OrphanChildCommand() {
			super("Boundary");
		}

		public void execute() {
			oldLocation = child.getLocation();
			diagram.removeChild(child);
		}

		public void redo() {
			diagram.removeChild(child);
		}

		public void setChild(Shape child) {
			this.child = child;
		}

		public void setParent(ShapesContainer parent) {
			diagram = parent;
		}

		public void undo() {
			child.setLocation(oldLocation);
			diagram.addChild(child);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?