shapesxylayouteditpolicy.java

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

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

import java.util.ArrayList;
import java.util.List;

import net.sf.freenote.commands.ShapeCreateCommand;
import net.sf.freenote.commands.ShapeSetConstraintCommand;
import net.sf.freenote.mindmap.model.BranchShape;
import net.sf.freenote.model.CircleShape;
import net.sf.freenote.model.ImageFileShape;
import net.sf.freenote.model.Shape;
import net.sf.freenote.model.ShapesContainer;

import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ResizableEditPolicy;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.handles.ResizableHandleKit;
import org.eclipse.gef.handles.ResizeHandle;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.CreateRequest;

/**
 * EditPolicy for the Figure used by this edit part.
 * Children of XYLayoutEditPolicy can be used in Figures with XYLayout.
 * @author Elias Volanakis
 */
public class ShapesXYLayoutEditPolicy extends XYLayoutEditPolicy {
	
	/* (non-Javadoc)
	 * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(ChangeBoundsRequest, EditPart, Object)
	 */
	protected Command createChangeConstraintCommand(ChangeBoundsRequest request,
			EditPart child, Object constraint) {
		if (child instanceof ShapeEditPart && constraint instanceof Rectangle) {
			// return a command that can move and/or resize a Shape
			return new ShapeSetConstraintCommand(
					(Shape) child.getModel(), request, (Rectangle) constraint);
		}
		return super.createChangeConstraintCommand(request, child, constraint);
	}
	
	/* (non-Javadoc)
	 * @see ConstrainedLayoutEditPolicy#createChangeConstraintCommand(EditPart, Object)
	 */
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		// not used in this example
		return null;
	}
	
	@Override
	protected Command createAddCommand(EditPart child, Object constraint) {
		if (child instanceof ShapeEditPart && constraint instanceof Rectangle) 
			return new ShapeCreateCommand((Shape)child.getModel(),(ShapesContainer)getHost().getModel(),(Rectangle)constraint);
		return super.createAddCommand(child, constraint);
	}

	/* (non-Javadoc)
	 * @see LayoutEditPolicy#getCreateCommand(CreateRequest)
	 */
	protected Command getCreateCommand(CreateRequest request) {
		Object childClass = request.getNewObjectType();
		//分支主题只允许添加到中心主题或分支主题上
		if(BranchShape.class ==  childClass)
			return null;
		if(Shape.class.isAssignableFrom((Class<?>) childClass)){
			// return a command that can add a Shape to a ShapesDiagram 
			return new ShapeCreateCommand((Shape)request.getNewObject(), 
					(ShapesContainer)getHost().getModel(), (Rectangle)getConstraintFor(request));
		}
		return null;
	}

	@Override
	protected EditPolicy createChildEditPolicy(EditPart child) {
		if(child.getModel() instanceof CircleShape || child.getModel() instanceof ImageFileShape){
			return new ResizableEditPolicy(){
				@Override
				protected List createSelectionHandles() {
					List list=new ArrayList();
					GraphicalEditPart part=(GraphicalEditPart)getHost();
					ResizableHandleKit.addMoveHandle(part, list);
					list.add(createHandle(part, PositionConstants.SOUTH_EAST));
					list.add(createHandle(part, PositionConstants.SOUTH_WEST));
					list.add(createHandle(part, PositionConstants.NORTH_WEST));
					list.add(createHandle(part, PositionConstants.NORTH_EAST));						
					return list;
				}

				private Object createHandle(GraphicalEditPart part, int direction) {
					return new ResizeHandle(part,direction);
				}
				
			};
		}
		return super.createChildEditPolicy(child);
	}
	
}

⌨️ 快捷键说明

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