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

📄 port.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
   	double distance1	=distance * m_percentPos;
   	if (distance1<1)
   		m_portPoint.setValue(m_firstPoint);
   	else
   	if (distance1>distance-1)//1 pixel difference
   		m_portPoint.setValue(m_secondPoint);
   	else
   		m_portPoint	=m_firstPoint.nearPoint(m_secondPoint,distance1);
   }

   
   /**
    *   Set the percent position of this port.
    *   @param percentPos  A new percent position.
    */ 	
   public void setPercentPos(double percentPos){
   	m_percentPos	=percentPos;
	calcPortPosition();
   }   

   	

   /**
    *   Constructor for Port
    */
   public Port(){
   	setObjectType(ShapeConst.SHAPETYPE_PORT);
   	setXMLTag(XML_PORT);
   }	

   /**
    *   Constructor for Port.
    * 
    *   @param parent parent object of this port.
    *   @param portType The type of a port.
    *   @param firstPoint The first reference end point of a port.
    *   @param secondPoint The second reference end point of a port.
    *   @param portPoint The port point of a port.
    *
    */ 	
   public Port(AbstractObject parent,int portType, JFPoint firstPoint, JFPoint secondPoint, JFPoint portPoint){

   	setObjectType(ShapeConst.SHAPETYPE_PORT);
   	setXMLTag(XML_PORT);
   	
   	setPortType(portType);
   	setParentId(parent.getObjectId());
   	setFirstPoint(firstPoint);
   	setSecondPoint(secondPoint);
   	setPortPoint(portPoint);
   	setParent(parent);
   }



   /**
    *   Get attached port list. <br>
    *
    *   @return  The list of ports attached to current port.
    *
    */ 	
   public List getAttachedList(){
   	return   m_attachedList;
   }
 
   /**
    *   Set attached port list. <br>
    *
    *   @param attachedList  The list of ports attached to current port.
    *
    */ 	
   public void setAttachedList(List attachedList){
   	m_attachedList	=new ArrayList(attachedList);
   }

   /**
    *   Get attached port list str. <br>
    *
    *   @return  The string of the ports list attached to current port.
    *
    */ 	
   public String getAttachedListStr(){
   	return m_attachedListStr;
   }

   /**
    *   Set attached port list str. <br>
    *
    *   @param attachedListStr  The string of the ports list attached to current port.
    *
    */ 	
   public void setAttachedListStr(String attachedListStr){
   	m_attachedListStr	=attachedListStr;
   }

   /**
    *   Get the quantity of  attached ports.
    *
    *   @return  The quantity.
    *
    */ 	
   public int getAttachedSize(){
   	return   m_attachedList.size();
   }
   

   
   /**
    *   Attach this port to a specified port of other shape.
    *   @param port A new port of other shape.
    *
    */ 	
   public boolean attachPort(Port port){

        if (port==null)
                return false;

        int index       =getAttachedPortIndex(port);

        if (index>=0)
                return true;
     
        //if (m_portPoint.distance(port.getPortPoint())<=GeomConst.PICK_OFFSET){
        if (m_portPoint.distance(port.getPortPoint())<=GeomConst.NODE_SIZE/2 + GeomConst.PICK_OFFSET){

                m_attachedList.add(port);
                port.attachPort(this);
		collectAttachedList();

                return true;
        }
        return false;
   }


   /**
    *   Detach this port to a specified port of other shape.
    *   @param port A port of other shape.
    *
    */ 	
   public boolean detachPort(Port port){
        if (port==null)
                return false;

        int index       =getAttachedPortIndex(port);
        if (index<0)
                return false;
       
        try{
                m_attachedList.remove(index);

                port.detachPort(this);

		collectAttachedList();
        	
                return true;
        }catch(Exception e){
                return false;
        }
        
   }

   /**
    *   Detach all relational ports of this port.
    */ 	
   public void detachAll(){
  	
   	try{
   		for (int i=m_attachedList.size()-1; i>=0; i--){
        		Port	port	=(Port)m_attachedList.get(i);
        		port.detachPort(this);
        	}
        	m_attachedList.clear();
        	collectAttachedList();
	}catch(Exception e){
		m_logger.error("detach all: "+e);
	}
   }


   /**
    *   move relational ports.
    *   @param movedList An object list that has already processed, so don't change them further.
    *   @param isMovePorts Move ports or move ports' parent objects.True move ports, false otherwise.
    */ 	
   public void  moveRelationalPorts(ObjectList movedList,boolean isMovePorts){


   	AbstractShape	parentShape	=(AbstractShape)getParent();

   	for (int i=m_attachedList.size()-1; i>=0; i--){
   		try{

   			//pick an attached port.
                	Port port       =(Port)m_attachedList.get(i); 
                	
                      
                	AbstractShape thisShape    =(AbstractShape)port.getParent();

                        if (movedList.contains(thisShape) || !thisShape.isOpenShape())
                                continue;


			/* under following conditions, the relational ports must be moved,
			 * otherwise break them.
			 * 1. parent shape is a closed shape, so it's relational ports
			 *    must be open shapes ports,e.g. curve,arc,line
			 * 2. this shape(attached port's parent) is a open shape,so no wonder
			 *    its ports can be moved
			 * 3. parent shape and this shape are all open shapes.
			 */
			 
                	if (!parentShape.isOpenShape() || thisShape.isOpenShape()){

				if (isMovePorts){
                			thisShape.movePort(port,getXOffset(),getYOffset()); 
                		}else{
                			if (thisShape.incAccessTimes()>3) 
			        		movedList.add(thisShape);

			        	thisShape.moveRelationalPorts(movedList);
			        }
			}                		
			
                }catch(Exception e){
                	m_logger.error("moveRelationalPorts: "+e);
                	break;
                }
        }
      
   }     


   /**
    *   unbound broken ports.
    *
    */ 	
   public void unboundBrokenPorts(){

   	for (int i=m_attachedList.size()-1; i>=0; i--){
   		try{
   			//pick an attached port.
                	Port port       =(Port)m_attachedList.get(i);
			
			//if no position changed, simply continue.
                	double dist	=m_portPoint.distance(port.getPortPoint());
                	if (dist>GeomConst.PICK_OFFSET){
				detachPort(port);
			}
                }catch(Exception e){
                	m_logger.error("unboundBrokenPorts: "+e);
                	break;
                }
        }        
   }     


   /**
    *   Get the index of an attached port in the attached port list..
    *
    *   @param port An attached port.
    *
    *   @return  The index. return -1 if false.
    *
    */ 	
   public int getAttachedPortIndex(Port port){
        if (port==null)
                return -1;
        else
                return getAttachedPortIndex(port.getParentId(),port.getObjectId());
   }
   
   
   /**
    *   Get the index of an attached port in the attached port list..
    *
    *   @param parentId The parent id of a new port.
    *   @param objectId The object id of a new port.
    *
    *   @return  The index. return -1 if false.
    *
    */ 	
   public int getAttachedPortIndex(int parentId, int objectId){
        for (int i=0; i<m_attachedList.size(); i++){
                try{
   		        Port  port=(Port)m_attachedList.get(i);
   		        if (port!=null && port.getParentId()==parentId && port.getObjectId()==objectId)
   			        return i;
                }catch(Exception e){
                        break;
                }
	}
	return -1;
   }


   /**
    *   Get a attached port of current port by index.
    *
    *   @param index   An index in the list.
    *
    *   @return  A port found.
    *
    */ 	
   public Port getAttachedPortByIndex(int index){
   	if (index >= 0 && index < m_attachedList.size()){
   		return   (Port)m_attachedList.get(index);
   	}
   	else{
		return   null; 
   	}
   }


   /**
    *   Collect all the port in the attachedList to a string,
    *   actually the method should generate a new parentId-index pair list string,
    *   each pair seperated by PAIR_SEPARATOR, and pair itself seperated by KEY_SEPARATOR
    *
    *   @return  No return.
    *
    */ 	
   public void collectAttachedList(){
   	Port port		=null;

	m_attachedListStr	="";
	if (m_attachedList!=null && m_attachedList.size()>0){
		Iterator it  =	m_attachedList.iterator();
		while (it!=null && it.hasNext()){
			Object o=it.next();
			if (o!=null && o instanceof Port){
				port	=(Port)o;
				m_attachedListStr	=m_attachedListStr + 
							port.getParentId() + KEY_SEPARATOR + port.getObjectId() + 
							PAIR_SEPARATOR;
			}
		}
	}
   }

   /**
    *   A loadFromStream method should only load an parentId-objectId list of ports attached,
    *   So here we use an attachRealPort to ACTUALLY attach some ports to this port
    * 
    *   @param shapeList  A shapeList used to pick out their ports for this port's attached list
    *
    */ 	
  public void attachRealPort(ObjectList shapeList){
  	if (m_attachedList==null){
  		m_attachedList		=new ArrayList();
  	}
  	m_attachedList.clear();
  	
  	String attachedListStr	=m_attachedListStr;

	/** pick each pair and find each port from shape list*/
 	StringTokenizer st=new StringTokenizer(attachedListStr,PAIR_SEPARATOR);
   	while (st!=null && st.hasMoreTokens()){
   	    	String pair  	= st.nextToken();
   	    	if (pair==null) break;

 	    	
   	  	int pos		=pair.indexOf(KEY_SEPARATOR);

   	  	if (pos>=0){
			/** parentId is used for picking up an object using its objectId in an objectList.*/
   	  		int parentId  	=CommonUtil.s2i(pair.substring(0,pos));
			/** objectId is used for picking up a node of an object*/
   	  		int objectId	=CommonUtil.s2i(pair.substring(pos+1));

			Object o	=shapeList.getByObjectId(parentId);
			if (o==null){
				o	=shapeList.getFromGroupsByObjectId(parentId,false);
			}

			if (o!=null && o instanceof  AbstractShape){
				AbstractShape shp	=(AbstractShape)o;
				Port port	=shp.getPortByObjectId(objectId);
				if (port!=null){
					m_attachedList.add(port);
					port.attachPort(this);
				}
			}
   	  	}
   	    	
   	}

	//to avoid change in port.attachPort method.
  	m_attachedListStr  =attachedListStr;
  }



   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param  isXorMode If is in xor mode now.
    *
    */ 	
   public void  draw(Graphics g,boolean isXorMode){
   	if (g==null)
   		return;

  	double zoom	=getZoomScale();

	//we draw an 'X' icon at the position of a port. 
        GeneralPath line= new GeneralPath();//GeneralPath.WIND_EVEN_ODD); 
        double x  =m_portPoint.getX() * zoom;
        double y  =m_portPoint.getY() * zoom;

	float x0    	=(float)x;

⌨️ 快捷键说明

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