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

📄 objectlist.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (!sendToBack){
                index   =objCount-objList.size();
        }

        it          =objList.iterator();
        while (it!=null && it.hasNext()){
                if (index<0 || index>=objCount)
                        break;
                        
                Object obj      =it.next();
                try{
                        m_objectList.set(index,obj);
                        index++;
               }catch(Exception e){
               }
        }
  }
  

   /**
    *   Send a sub-collection of this object list to back.
    * 
    *   @param objeList a sub collection of this object list.
    */ 	
  public void sendToBack(List objList){ 
        if (objList==null || objList.size()==0)
                return;
                
        /**
         *  A send to back action is actually move all the partly objects to the begining index
         *  of this list.
         */
        moveCollection(objList,true);
  }

   /**
    *   Send a sub-collection of this object list to front.
    * 
    *   @param objeList a sub collection of this object list.
    */ 	
  public void bringToFront(List objList){
        if (objList==null || objList.size()==0)
                return;
                
        /**
         *  A bring to front action is actually move all the partly objects to the end index
         *  of this list.
         */
        moveCollection(objList,false);
  }

   /**
    *   Move one object within this list to a specified index position.
    * 
    *   @param objectId Id of an obect.
    *   @param index   New position to move to.
    */ 	
  public void moveObject(int objectId, int index){

	int originIndex	=getIndexByObjectId(objectId);

	if (originIndex==index)
		return;
	
	try{
		AbstractObject obj	=getByIndex(originIndex);
		if (originIndex>index){
			//shift all objects between originIndex-1 and index  towards originInex
			for (int i=originIndex; i>index; i--){
			     m_objectList.set(i,m_objectList.get(i-1));
			}
		}else{
			//shift all objects between originIndex+1 and index  towards originInex
			for (int i=originIndex; i<index; i++){
			     m_objectList.set(i,m_objectList.get(i+1));
			}
		}

		//replace object at index.
		m_objectList.set(index,obj);
	}catch(Exception e){
	}
	
  }

  
   /**
    *   Clear all objects in list.
    *
    */ 	
  public void clear(){
  	try{
  		m_objectList.clear();
  	}catch(Exception e){
	}
  }

   /**
    *   Get the number of AbstractObject in list.
    * 
    *   @return The number of AbstractObject.
    *
    */ 	
  public int size(){
  	return m_objectList.size();
  }

   /**
    *   Get the bounds of this object list.
    *   Objects that are not AbstractShape would not be cauclated in result.
    *   @return The bounds rectangle.
    *
    */ 	
   public Rect getBounds(){
	
   	double minx=10000,miny=10000,maxx=0,maxy=0;
   	Object obj;
   	Iterator it=getList().iterator();
   	while (it!=null && it.hasNext()){
   		obj	=it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;

   			Rect bounds	=(shapeObj).getBounds();
   			minx		=Math.min(minx,bounds.getX());
   			miny		=Math.min(miny,bounds.getY());
   			maxx		=Math.max(maxx,bounds.getX()+bounds.getWidth());
   			maxy		=Math.max(maxy,bounds.getY()+bounds.getHeight());
   		}
   	}  	
   
       if (minx>maxx)	minx=maxx;
       if (miny>maxy)   miny=maxy;
       return new Rect(minx,miny,maxx-minx,maxy-miny);
   }


 
   /**
    *   Convert this list to String 
    * 
    *   @return  An string represents the content of current list
    *
    */ 	
   public String toString(){
   	StringBuffer buf=new StringBuffer();
   	buf.append(super.toString());
   	
   	AbstractObject obj=null;
   	if (m_objectList!=null && m_objectList.size()>0){
   		Iterator it=m_objectList.iterator();
   		while (it!=null && it.hasNext()){
   			obj	=(AbstractObject) it.next();
   			buf.append("\n<Object>\n");
   			buf.append(obj.toString());
   		}
   	}
   	return buf.toString();
   }

   /**
    *   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 ObjectList();
  }
  
   /**
    *   Creates a new object of the same class and with the same contents as this object.
    * 
    *   @return  A clone of this instance.
    *
    */ 	
  public Object clone() throws CloneNotSupportedException{
  	try{
  		ObjectList objList =(ObjectList)super.clone(); 
  		ArrayList	newList =new ArrayList();
  		AbstractObject	obj	=null;

  		if (m_objectList!=null && m_objectList.size()>0){
   			Iterator it=m_objectList.iterator();
   			while (it!=null && it.hasNext()){

   				obj	=(AbstractObject) it.next();
   				Object newObj	=obj.clone();
   				newList.add(newObj);
   			}
  		}
  		
		objList.m_objectList	=newList; 

  		return objList;
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Node.
    *
    */ 	
  public int hashCode(){
  	return super.hashCode() ^ m_objectList.hashCode();
  }


   /**
    *   Determines whether or not two objects are equal. 
    * 
    * 	@param obj  an object to be compared with this object 
    * 
    *   @return true if the object to be compared is an instance of Node and has the same values; false otherwise.
    *
    */ 	
  public boolean equals(Object obj){
      if (!super.equals(obj))
      		return false;	
      if (obj == this)
             	return true;
      if (!(obj instanceof ObjectList))
            	return false;
      
      ObjectList  objList= (ObjectList)obj;
      return objList.m_objectList.equals(m_objectList);
  }
   
   /**
    *   Append necessary xml child for current element,
    *   this method will be called internally by toDOM.
    * 
    *   @param  element A XML element to append child xml nodes
    *   @param version A file version notification so this object can obey the rules to save data.
    *
    */ 	
  protected void appendChildToDOM(Element element,JFVersion version){
 	      if (element==null)
  			return;
		
	      super.appendChildToDOM(element,version);
	       		
	      int listSize=0;
	      if (m_objectList!=null){
	      		listSize	=m_objectList.size();
	      }	
	
	      element.addChild(new Element(XML_OBJECTCOUNT, listSize));
  	      if (listSize>0){
			
			AbstractObject	obj=null;		     	      		
			Iterator it=m_objectList.iterator();
			while (it!=null && it.hasNext()){
				obj	=(AbstractObject)it.next();
				obj.toDOM(element,version);
    			}
  	      }
  }

   /**
    *   Extract needed xml child from current element,
    *   this method will be called internally by fromDOM.
    * 
    *   @param  element An element used to extract needed xml child
    *   @param version A file version notification so this object can obey the rules to fetch data.
    *
    */ 	
  protected void extractChildFromDOM(Element element,JFVersion version){
        

     if (element==null)
      	return;

     super.extractChildFromDOM(element,version);

     /** make a new object list*/	
     m_objectList	=new ArrayList();

     try{	
	
	List	l=element.getChildren();

	if (l!=null && l.size()>0){
		Iterator it =l.iterator();
		Element node=null;
        	String xmlTag="";
		while (it!=null && it.hasNext()){
			node	=(Element)it.next();
      			xmlTag	=node.getTag();
     		
      			if (!xmlTag.equals(AbstractObject.XML_OBJECTID) &&
	      		    !xmlTag.equals(AbstractObject.XML_OBJECTTYPE) &&
	      		    !xmlTag.equals(XML_OBJECTCOUNT)){
	 			
	 			
	 			AbstractObject obj =m_factory.createObj(xmlTag);
	 			if (obj!=null){
         				obj.fromDOM(node,version);
         				m_objectList.add(obj);
         			}
         			
         		}
		}
	}


     }catch(Exception e){
     		m_logger.error("extractChildFromDOM: "+e);
     }

  }

   /**
    *   Save this node to a binary stream 
    * 
    *   @param stream An binary output stream
    *
    *   @param version A file version notification so this object can obey the rules to save data.
    *   @exception  java.io.IOException
    *
    */ 	
  public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
	
	super.saveToStream(stream,version);	

	int count=m_objectList.size();
	stream.writeInt(count);       		/** object number in the list */

	if (count>0){	    
		/** store each object in the list into stream */
		AbstractObject	obj=null;
		Iterator it	= m_objectList.iterator();
		while (it!=null && it.hasNext()){
			obj	=(AbstractObject)it.next();
			obj.saveToStream(stream,version);
		}
	}
  }


   /**
    *   Load node data from a binary stream <br>
    * 
    *   @param stream An binary input stream
    *
    *   @param skipHead Skip head 'TYPE' check, an shape object should always 
    *   has its own shape-type stored, if this shape-type has already been readed,
    *   this loadFromStream should/could not read the type anymore.
    *
    *   @param version A file version notification so this object can obey the rules to fetch data.
    *   @exception  java.io.IOException
    *
    */ 	
  public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{

	    try{	

	    	clear();

	    }catch(Exception e){
	    	throw new IOException(e.getMessage());
	    }
	
	    super.loadFromStream(stream, skipHead,version);
	    
  	    int count		=stream.readInt();
  	    AbstractObject	obj=null;

  	    while (count>0){
  	    	obj	=m_factory.loadObjFromStream(stream,version);
  	    	if (obj!=null){
  	    		m_objectList.add(obj);
  	    	}
  	    	count--;
  	    }
  }



   /**
    *   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 the ports in list.
    * 
    *   @param shapeList  A shapeList used to pick out their ports for ports' attached list
    *
    */ 	
  public void attachRealPort(){
  
  	
  	ObjectList list	=new ObjectList();
  	Iterator it	=m_objectList.iterator();
  	while (it!=null && it.hasNext()){
  		AbstractShape shape	=(AbstractShape)it.next();
  		list.add(shape);
  		shape.fetchSubObjects(list);
	}
  	
  	attachRealPort(list);
  }


   /**
    *   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 the ports in this list.
    * 
    *   @param shapeList  A shapeList used to pick out their ports for ports' attached list
    *
    */ 	
  public void attachRealPort(ObjectList shapeList){
  	Iterator it	=m_objectList.iterator();
  	while (it!=null && it.hasNext()){
  		AbstractObject obj	=(AbstractObject)it.next();
  		if (obj instanceof AbstractShape){
  			AbstractShape shape	=(AbstractShape)obj;
  			shape.attachRealPort(shapeList);
  			//PortList portList	=(PortList)shape.getPortList();
  			//portList.attachRealPort(shapeList);
  		}
	}
  }


    /** set zoom scale
     *  @param zoomScale A new zoom scale.
     */	
    public void setZoomScale(double zoomScale){
    	super.setZoomScale(zoomScale);
   	
   	Iterator it =m_objectList.iterator();
   	while (it!=null && it.hasNext()){
   		AbstractObject obj	=(AbstractObject)it.next();
   		obj.setZoomScale(zoomScale);
	}
    }


 	
 }

⌨️ 快捷键说明

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