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

📄 portlist.java

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


import java.awt.Graphics;
import java.awt.Color;

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

import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.Port;
 
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Rect;
 
 /**
 * PortList class. A port list is used to manipulate port issues.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class PortList extends ObjectList{

   /**
    *   A XML string tag represents a  Node List
    */
   public  static final String	 XML_PORTLIST		="PortList";

   /**
    *   Constructor for PortList
    */
   public PortList(){
   	setObjectType(ShapeConst.OBJECTTYPE_PORTLIST);
   	setXMLTag(XML_PORTLIST);
   }	


   /**
    *   Add a new port to port list.
    * 
    *   @param port A new port.
    *   @return the port to be added.
    *
    */ 	
   public Port  addPort(Port port){
   	if (port==null) return null;
   	
   	port.setObjectId(newObjectId());
   	m_objectList.add(port);
   	
   	return port;
   }


   /**
    *   Get the index of a port within the list.
    * 
    *   @param port A port to be found.
    *
    *   @return  The index.
    *
    */ 	
  public int getIndex(Port port){
        if (port==null)  return -1;
        try{
                return getIndexByObjectId(port.getObjectId());
        }catch(Exception e){
                return -1;
        }
  }


   /**
    *   Creates a new AbstractObject of the same class and with the same contents as this object.
    *   This method implements the method defined in AbstractObject.
    * 
    *   @return  A clone of this class.
    *
    */ 	
  protected AbstractObject cloneMe() throws CloneNotSupportedException{
  	return new PortList();
  }


   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *
    */ 	
   public void  draw(Graphics g){
   	if (g==null)
   		return;

   	Iterator it	=m_objectList.iterator();
	while (it!=null && it.hasNext()){
		Port port	=(Port)it.next();
		port.draw(g,false);
	}
   }


   /**
    * When move a node, we have three important nodes here.
    */ 	
   private  JFPoint m_moveNode	=new JFPoint();
   private  JFPoint m_refNode1	=new JFPoint();
   private  JFPoint m_refNode2	=new JFPoint();
   private  JFPoint m_refNode3	=new JFPoint();
   private  int     m_moveNodeType	=0;


   /**
    *  Get the moveNode point.
    *  @return the moveNode point
    */ 	
   public JFPoint getMoveNode(){
   	return m_moveNode;
   }
   /**
    *  Get the refNode1 point.
    *  @return the refNode1 point
    */ 	
   public JFPoint getRefNode1(){
   	return m_refNode1;
   }
   /**
    *  Get the refNode2 point.
    *  @return the refNode2 point
    */ 	
   public JFPoint getRefNode2(){
   	return m_refNode2;
   }
   /**
    *  Get the refNode3 point.
    *  @return the refNode3 point
    */ 	
   public JFPoint getRefNode3(){
   	return m_refNode3;
   }

   /**
    *  Get type of the moving node.
    *  @return the type of the moving node.
    */ 	
   public int getMoveNodeType(){
   	return m_moveNodeType;
   }

   /**
    *   When start move node, all of its relational ports will be moved concurrently,
    *   so we 'register' a start moving node event here.
    * 
    *   @param  moveNode Currently moving node.
    *   @param  refNode1,refNode2,refNode3 Three relational nodes according to the moving node.
    *   @param  nodeType The type of the moving node(defined in JFPointNode).
    *
    */ 	
   public void startMoveNode(JFPoint moveNode,JFPoint refNode1, JFPoint refNode2,JFPoint refNode3,int nodeType){
   	m_moveNodeType	=nodeType;
   	
   	if (moveNode==null)
   		m_moveNode.setValue(GeomConst.LARGE_VALUE,GeomConst.LARGE_VALUE);
   	else
   		m_moveNode.setValue(moveNode);

   	if (refNode1==null)
   		m_refNode1.setValue(GeomConst.LARGE_VALUE,GeomConst.LARGE_VALUE);
   	else
   		m_refNode1.setValue(refNode1);

   	if (refNode2==null)
   		m_refNode2.setValue(GeomConst.LARGE_VALUE,GeomConst.LARGE_VALUE);
   	else
   		m_refNode2.setValue(refNode2);

   	if (refNode3==null)
   		m_refNode3.setValue(GeomConst.LARGE_VALUE,GeomConst.LARGE_VALUE);
   	else
   		m_refNode3.setValue(refNode3);
   }

   /**
    *   When finish move node, all of its relational ports will be moved concurrently,
    * 
    *   @param  newMoveNode Currently moving node.
    *   @param  newRefNode1,newRefNode2,newRefNode3 Three relational nodes according to the moving node.
    *
    */ 	
   public void finishMoveNode(JFPoint newMoveNode,JFPoint newRefNode1, JFPoint newRefNode2,JFPoint newRefNode3){

   	Iterator it	=m_objectList.iterator();
	   	
	while (it!=null && it.hasNext()){
		Port port	=(Port)it.next();
		JFPoint firstPoint	=port.getFirstPoint();
		JFPoint secondPoint	=port.getSecondPoint();
		
		boolean changed		=false;

		
		if (firstPoint.equals(m_moveNode,true) && newMoveNode!=null){
			firstPoint.setValue(newMoveNode);
			changed		=true;
		
		}else if(firstPoint.equals(m_refNode1,true) && newRefNode1!=null){
			firstPoint.setValue(newRefNode1);
			changed		=true;

		}else if(firstPoint.equals(m_refNode2,true) && newRefNode2!=null){
			firstPoint.setValue(newRefNode2);
			changed		=true;
		}else if(firstPoint.equals(m_refNode3,true) && newRefNode3!=null){
			firstPoint.setValue(newRefNode3);
			changed		=true;
		}

		if (secondPoint.equals(m_moveNode,true) && newMoveNode!=null){
			secondPoint.setValue(newMoveNode);
			changed		=true;
		
		}else if(secondPoint.equals(m_refNode1,true) && newRefNode1!=null){
			secondPoint.setValue(newRefNode1);
			changed		=true;

		}else if(secondPoint.equals(m_refNode2,true) && newRefNode2!=null){
			secondPoint.setValue(newRefNode2);
			changed		=true;

		}else if(secondPoint.equals(m_refNode3,true) && newRefNode3!=null){
			secondPoint.setValue(newRefNode3);
			changed		=true;
		}
		
		if (changed)
			port.finishMoving();
	}
	   	
   }

   /**
    *   Move ports that between the two end points, to two new end points position.
    *   @param startPoint,endPoint Assume ports that between these two end points.
    *   @param newStartPoint,newEndPoint New end points position.
    */
  public void movePort(JFPoint startPoint, JFPoint endPoint, JFPoint newStartPoint, JFPoint newEndPoint){

	if (startPoint==null || endPoint==null || newStartPoint==null || newEndPoint==null)
		return;


   	Iterator it	=m_objectList.iterator();
	   	
	while (it!=null && it.hasNext()){
		Port port	=(Port)it.next();
		JFPoint firstPoint	=port.getFirstPoint();
		JFPoint secondPoint	=port.getSecondPoint();

		
		if (firstPoint.equals(startPoint,true) && secondPoint.equals(endPoint,true) ||
		    firstPoint.equals(endPoint,true) && secondPoint.equals(startPoint,true)){
		    	boolean changed	=false;
		    	
			if (firstPoint.equals(startPoint,true)){
				firstPoint.setValue(newStartPoint);
				changed		=true;
		
			}else if(firstPoint.equals(endPoint,true)){
				firstPoint.setValue(newEndPoint);
				changed		=true;
			}

			if (secondPoint.equals(startPoint,true)){
				secondPoint.setValue(newStartPoint);
				changed		=true;
		
			}else if(secondPoint.equals(endPoint,true)){
				secondPoint.setValue(newEndPoint);
				changed		=true;
			}
			
			if (changed)
				port.finishMoving();
		}
		
	}
  }
 

   /**
    *   Divide end points pair of all ports by a middle point of end points.
    *   @param startPoint,endPoint Assume ports that between these two end points.
    *   @param midPoint A middle point to divide ports' end points.
    */
  public void dividePort(JFPoint startPoint, JFPoint endPoint, JFPoint midPoint){

	if (startPoint==null || endPoint==null || midPoint==null)
		return;

   	Iterator it	=m_objectList.iterator();
	   	
	while (it!=null && it.hasNext()){
		Port port	=(Port)it.next();
		JFPoint firstPoint	=port.getFirstPoint();
		JFPoint secondPoint	=port.getSecondPoint();
		JFPoint portPoint	=port.getPortPoint();

	  
		if (firstPoint.equals(startPoint,true) && secondPoint.equals(endPoint,true) ||
		    firstPoint.equals(endPoint,true) && secondPoint.equals(startPoint,true)){
		    	double len1		=firstPoint.distance(portPoint);
		    	double len2		=secondPoint.distance(portPoint);
		    	if (len1==0){
		    		secondPoint.setValue(midPoint);
		    		port.setPortPoint(firstPoint);

		    	
		    	}else if (len2==0){
		    		firstPoint.setValue(midPoint);
		    		port.setPortPoint(secondPoint);

		    	

⌨️ 快捷键说明

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