nodesetconstraintcommand.java

来自「对eclipse gef进行封装,可以生成图形化编辑器」· Java 代码 · 共 214 行

JAVA
214
字号
/*******************************************************************************
 * $Header: /cvsroot/EOS6/work_dir/niegy/com.primeton.studio.gef.ui/src/com/primeton/studio/gef/ui/commands/NodeSetConstraintCommand.java,v 1.8 2006/12/30 02:45:04 niegy Exp $
 * $Revision: 1.8 $
 * $Date: 2006/12/30 02:45:04 $
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2006 Primeton Technologies, Ltd.
 * All rights reserved. 
 * 
 * Created on 2006-9-28
 *******************************************************************************/

package com.primeton.studio.gef.ui.commands;

import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;

import com.primeton.studio.gef.core.Node;
import com.primeton.studio.gef.core.GroupNode;
import com.primeton.studio.gef.core.Location;
import com.primeton.studio.gef.core.NodeElement;
import com.primeton.studio.gef.core.NodeElementLabel;
import com.primeton.studio.gef.core.Size;
import com.primeton.studio.gef.core.TNodeType;
/**
 * TODO此处填写 class 信息
 *
 * @author niegy (mailto:niegy@primeton.com)
 */
/*
 * 修改历史
 * $Log: NodeSetConstraintCommand.java,v $
 * Revision 1.8  2006/12/30 02:45:04  niegy
 * 重构代码
 *
 * Revision 1.7  2006/12/29 09:06:18  niegy
 * 重构代码
 *
 * Revision 1.6  2006/12/16 09:04:42  niegy
 * 重构代码
 *
 * Revision 1.5  2006/12/12 08:26:39  niegy
 * 重构代码,实现一般图元的编辑框
 *
 * Revision 1.4  2006/12/08 07:04:32  niegy
 * 重构代码
 *
 * Revision 1.3  2006/11/22 07:07:38  niegy
 * 增加table
 *
 * Revision 1.2  2006/11/18 12:13:53  niegy
 * 增加容器模型
 *
 * Revision 1.1  2006/11/17 03:15:13  niegy
 * create
 * 
 */
public class NodeSetConstraintCommand extends Command {
	/** Stores the new size and location. */
	private final Rectangle newBounds;
	/** Stores the old size and location. */
	private Rectangle oldBounds;
	/** A request to move/resize an edit part. */
	private final ChangeBoundsRequest request;
	
	/** Shape to manipulate. */
	private final NodeElement nodeElement;
	
	private final int widthSize = 30;
	
	private final int heightSize = 30;
	/**
	 * Create a command that can resize and/or move a node. 
	 * @param nodeElement	the node to manipulate
	 * @param req		the move and resize request
	 * @param newBounds the new size and location
	 * @throws IllegalArgumentException if any of the parameters is null
	 */
	public NodeSetConstraintCommand(NodeElement shape, ChangeBoundsRequest req, 
			Rectangle newBounds) {
		if (shape == null || req == null || newBounds == null) {
			throw new IllegalArgumentException();
		}
		this.nodeElement = shape;
		this.request = req;
		this.newBounds = newBounds.getCopy();
		setLabel("move / resize");
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		Object type = request.getType();
		// make sure the Request is of a type we support:
		return (RequestConstants.REQ_MOVE.equals(type)
				|| RequestConstants.REQ_MOVE_CHILDREN.equals(type) 
				|| RequestConstants.REQ_RESIZE.equals(type)
				|| RequestConstants.REQ_RESIZE_CHILDREN.equals(type)
				|| RequestConstants.REQ_ALIGN_CHILDREN.equals(type));
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		if(nodeElement instanceof NodeElementLabel)
			return;
		oldBounds = new Rectangle(
		        nodeElement.getLocation().getX(),
				nodeElement.getLocation().getY(), 
				nodeElement.getSize().getWidth(), 
				nodeElement.getSize().getHeight());
		redo();
	}
	
	private void moveGroupChild(boolean isredo){
		if(nodeElement instanceof GroupNode){
			int x = newBounds.x - oldBounds.x;
			int y = newBounds.y - oldBounds.y;
			if(!isredo){
				 x = oldBounds.x - newBounds.x;
				 y = oldBounds.y - newBounds.y;
			}
			List children = ((GroupNode)nodeElement).getChidren();
	        for (Iterator iter = children.iterator(); iter.hasNext();) {
	        	String id = (String) iter.next();
	        	NodeElement element = findNodeforId(id);
				if(element != null && element.isGrouped()){
					Location loc  = element.getLocation();
				    loc.setX(loc.getX()+x);
				    loc.setY(loc.getY()+y);
				    element.setLocation(loc);
				    if(isredo)
				    	moveNodeLabel(element,newBounds.x,newBounds.y);
				    else
				    	moveNodeLabel(element,oldBounds.x,oldBounds.y);
				}	  
			}			
		}
	}
	
    private NodeElement findNodeforId(String id){
    	Node parent = (Node)nodeElement.eContainer();
    	for (Iterator iter = parent.getNodes().iterator(); iter.hasNext();) {
    		NodeElement element = (NodeElement) iter.next();
			if(id.equals(element.getId()))
				return element;
		}
    	return null;
    }
    
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		if(nodeElement instanceof NodeElementLabel)
			return;
		moveGroupChild(true);
		Location loc  = nodeElement.getLocation();
	    loc.setX(newBounds.x);
	    loc.setY(newBounds.y);
	    nodeElement.setLocation(loc);
	    
	    if (nodeElement.getNodeType()!=TNodeType.TABLE_LITERAL && !nodeElement.isCollapsed()){
			Size size  = nodeElement.getSize();
		    size.setWidth((newBounds.width>widthSize)?newBounds.width:widthSize);
		    size.setHeight((newBounds.height>heightSize)?newBounds.height:heightSize);
		   
		    nodeElement.setSize(size);
	    }
	    if(nodeElement.getNodeType()==TNodeType.TABLE_LITERAL){
	    	Size size  = nodeElement.getSize();
	    	if(newBounds.width!=-1)
	    		size.setWidth((newBounds.width>widthSize)?newBounds.width:widthSize);		   
		    nodeElement.setSize(size);
	    }
	    moveNodeLabel(nodeElement,newBounds.x,newBounds.y);
	}
	
	private void moveNodeLabel(NodeElement commannode,int x,int y){
		if(commannode.getNodeLabel() != null){
			Location labelLoc = commannode.getNodeLabel().getLocation();
			labelLoc.setX(x);
			labelLoc.setY(y);
			commannode.getNodeLabel().setLocation(labelLoc);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		if(nodeElement instanceof NodeElementLabel)
			return;
		moveGroupChild(false);
		Location loc  = nodeElement.getLocation();
		Size size  = nodeElement.getSize();
	    loc.setX(oldBounds.x);
	    loc.setY(oldBounds.y);
	    size.setWidth(oldBounds.width);
	    size.setHeight(oldBounds.height);
	    nodeElement.setLocation(loc);
	    nodeElement.setSize(size);
	    moveNodeLabel(nodeElement,oldBounds.x,oldBounds.y);
	}
}

⌨️ 快捷键说明

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