⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jfoperationmovenode.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:JFOperationMoveNode.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.shape.action;


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

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.ObjectList;

import com.jfimagine.jfgraph.shape.union.JFPage;
import com.jfimagine.jfgraph.shape.union.JFLayer;

import com.jfimagine.utils.log.*;

/**
 *  Class JFOperationMoveNode is used to record a move node operation.
 */
public class JFOperationMoveNode extends JFOperation{

	/**an internal log utility*/
	private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
	
	/** 
	 *  > For JFLine,JFRegularLine and JFPolygon, when one of their nodes is moved,
	 *  the number of all nodes maybe automatically extend or shrink,
	 *  so for such objects, we use a BACKUP_ALL feature to backup whole
	 *  the node list and port list.
	 *
	 *  > For other objects that have specific unchangeable number of nodes, 
	 *   we will use a BACKUP_NODEONLY feature to backup the moving node only.
	 */
	private final int BACKUP_NODEONLY	=1;
	private final int BACKUP_ALL		=2;
	
	/** backup type for distinguish different objects*/
	private int m_backupType	=BACKUP_NODEONLY;

	//******************************************
	//**  m_backupType == BACKUP_NODEONLY   ****
	//******************************************
	
	/** nodeId backuped when m_backupType is BACKUP_NODEONLY*/
	private int m_nodeId  =0;

	/** A port that may attach to this object*/
	private int m_attachPortId  		=0;
	private int m_attachPortParentId  	=0;

	/** original position of the node when m_backupType is BACKUP_NODEONLY*/
	private double m_x1  =0;
	private double m_y1  =0;

	/** final position of the node when m_backupType is BACKUP_NODEONLY*/
	private double m_x2  =0;
	private double m_y2  =0;
	
	//******************************************
	//**  m_backupType == BACKUP_ALL        ****
	//******************************************
					
	/** source node list when m_backupType is BACKUP_ALL*/
	private ObjectList m_srcNodeList =null;
	/** dest node list backuped when m_backupType is BACKUP_ALL*/
	private ObjectList m_destNodeList =null;

	/** source port list backuped when m_backupType is BACKUP_ALL*/
	private ObjectList m_srcPortList =null;
	/** dest port list backuped when m_backupType is BACKUP_ALL*/
	private ObjectList m_destPortList =null;
	
	/**
	 *   Constructor
	 */
	public JFOperationMoveNode(){
	}
	
	/**
	 *  set start moving node parameters for JFLine,JFRegularLine and Polygon.
	 *  @param page A page that used to do opeation.
	 *  @param obj An object that being moved node.
	 *  @param lastOperation A last operation that is necessary as a reference for a MoveNode operation.
	 */
	public void setStartMoveNode(JFPage page,Object obj, JFOperation lastOperation){
        	m_page	=page;
		setActionId(JFOperation.OPER_MOVENODE);
		if (!(obj instanceof AbstractShape)) return;
		
		AbstractShape aObj	=(AbstractShape)obj;
		setObjectId(aObj.getObjectId());
		
		//backup all the nodes and ports
		try{	
		    	m_backupType	=BACKUP_ALL;
		    	boolean sourceAssigned	=false;
		    	
		    	//try to make a simple reference of last node list and port list to avoid cloning.
		    	if (lastOperation!=null && lastOperation instanceof JFOperationMoveNode){
		    		JFOperationMoveNode op	=(JFOperationMoveNode)lastOperation;
		    		if (op.getObjectId()==aObj.getObjectId() && op.m_backupType==this.m_backupType &&
		    		    op.m_destNodeList!=null && op.m_destPortList!=null){
		    				//where we simply make a reference to last operation's dest nodelist and port list.
		    				sourceAssigned	=true;
		    				this.m_srcNodeList	=op.m_destNodeList;
		    				this.m_srcPortList	=op.m_destPortList;
		    		}
		    	}
			
			//if source node list and port list are not assigned, 
			//we need to make a clone of the node list and port list.			
			if (!sourceAssigned){
		    		this.m_srcNodeList	=(ObjectList)aObj.getNodeList().clone();
		    		this.m_srcPortList	=(ObjectList)aObj.getPortList().clone();
		    	}
		}catch(Exception e){
		}
	}


	/**
	 *  set finish moving node parameters for JFLine,JFRegularLine and Polygon.
	 *  @param page A page that used to do opeation.
	 *  @param obj An object that being moved node.
	 *  @param port A port that may attach to this object
	 */
	public void setFinishMoveNode(Object obj,Port port){

		if (!(obj instanceof AbstractShape)) return;
		
		AbstractShape aObj	=(AbstractShape)obj;
		
		//backup all the nodes and ports
		try{	
		    	this.m_destNodeList	=(ObjectList)aObj.getNodeList().clone();
		    	this.m_destPortList	=(ObjectList)aObj.getPortList().clone();
		    	if (port!=null){
		    		this.m_attachPortId		=port.getObjectId();
				this.m_attachPortParentId  	=port.getParentId();
			
			}
		}catch(Exception e){
		}
	}

	/**
	 *  set move node parameters for shapes except JFLine,JFRegularLine and Polygon.
	 *  @param page A page that used to do opeation.
	 *  @param obj An object that being moved node.
	 *  @param node A node to be moved.
	 *  @param x1,y1 The original position of the node.
	 *  @param x2,y2 The final position of the node.
	 */
	public void setMoveNode(JFPage page,Object obj, Node node,double x1, double y1, double x2, double y2){
        	m_page	=page;
		setActionId(JFOperation.OPER_NEW);
		if (!(obj instanceof AbstractShape)) return;
		
		AbstractShape aObj	=(AbstractShape)obj;
		setObjectId(aObj.getObjectId());
		m_nodeId	=node.getObjectId();
		
		m_backupType	=BACKUP_NODEONLY;
		m_x1		=x1;
		m_y1		=y1;
		m_x2		=x2;
		m_y2		=y2;
	}



	/** undo this operation
	 *  @return new objects selected
	 */
	public List undo(){
		AbstractShape  aShape	=(AbstractShape)getObject();

		switch (m_backupType){
			case BACKUP_ALL:

				//detach the port attached.				
				if (m_attachPortParentId>0 && m_attachPortId>0){


					try{
						//get the attached port and its parent.
						JFLayer	layer			=m_page.getCurrentLayer();

						AbstractShape attachShape	=(AbstractShape)layer.getShapeList().getByObjectId(m_attachPortParentId);

						Port attachPort			=(Port)attachShape.getPortList().getByObjectId(m_attachPortId);

						//try to find the attach port of this object.
						Port thisPort			=aShape.portIntersects(attachPort.getPortPoint(),null);

						if (thisPort!=null){
							attachPort.detachPort(thisPort);
						}
					}catch(Exception e){
						m_logger.error("undo-BACKUP_ALL:"+e);
					}
				}


				aShape.setNodeList(m_srcNodeList);
				//aShape.setPortList(m_srcPortList);
				aShape.setPortList(m_srcPortList,m_page.getCurrentLayer().getShapeList());
				
				break;					
			case BACKUP_NODEONLY:
				try{

					Node node	=(Node)aShape.getNodeList().getByObjectId(m_nodeId);

					aShape.startMoveNode(node);
					aShape.moveNode(node,m_x1,m_y1,null);
					aShape.finishMoveNode(node,m_x1,m_y1,null);

				}catch(Exception e){
					m_logger.error("undo-BACKUP_NODEONLY:"+e);
				}
				break;					
		}
	
		moveRelationalPorts();
		return getObjectList();
	}


	/** redo this operation
	 *  @return new objects selected
	 */
	public List redo(){
		AbstractShape  aShape	=(AbstractShape)getObject();
		
		switch (m_backupType){
			case BACKUP_ALL:
				aShape.setNodeList(m_destNodeList);
				aShape.setPortList(m_destPortList,m_page.getCurrentLayer().getShapeList());

				//attach the port found.
				if (m_attachPortParentId>0 && m_attachPortId>0){
					try{
						//get the attached port and its parent.
						JFLayer	layer			=m_page.getCurrentLayer();
						AbstractShape attachShape	=(AbstractShape)layer.getShapeList().getByObjectId(m_attachPortParentId);
						Port attachPort			=(Port)attachShape.getPortList().getByObjectId(m_attachPortId);
						//try to find the attach port of this object.
						Port thisPort			=aShape.portIntersects(attachPort.getPortPoint(),null);
						if (thisPort!=null){
							attachPort.attachPort(thisPort);
						}
					}catch(Exception e){
						m_logger.error("redo-BACKUP_ALL:"+e);
					}
				}

				break;					

			case BACKUP_NODEONLY:
				try{
					Node node	=(Node)aShape.getNodeList().getByObjectId(m_nodeId);
					aShape.startMoveNode(node);
					aShape.moveNode(node,m_x2,m_y2,null);
					aShape.finishMoveNode(node,m_x2,m_y2,null);
				}catch(Exception e){
					m_logger.error("redo-BACK_NODEONLY:"+e);
				}
				break;					
		}
	
		moveRelationalPorts();
		return getObjectList();
	}

}

⌨️ 快捷键说明

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