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

📄 objectlist.java

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


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

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import com.jfimagine.jfgraph.geom.Rect;

import com.jfimagine.jfdom.Document;
import com.jfimagine.jfdom.Element;

import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ObjectFactory;
import com.jfimagine.jfgraph.shape.base.JFVersion;

import com.jfimagine.jfgraph.shape.union.JFGroup;
 
import com.jfimagine.utils.log.*; 
 /**
 * ObjectList class. A major type of object list class used in CAD.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.0.0 $
 */  
 public class ObjectList extends AbstractObject{


   /**
    *   A XML string tag represents the number of objects in this list.
    */
   public  static final String   XML_OBJECTCOUNT	="objectCount";

   /**
    *   A XML string tag represents a  ObjectList
    */
   public  static final String	 XML_OBJECTLIST		="ObjectList";

   /**
    *   ObjectFactory instance for creating objects.
    */
  private static ObjectFactory m_factory= ObjectFactory.getInstance();

   /**
    *   A java.util.List used to store all AbstractObjects.
    */
  protected List m_objectList = new ArrayList();

  /**an internal log utility*/
  private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
   
   /**
    *   Constructor for ObjectList
    */
   public ObjectList(){
   	setObjectType(ShapeConst.OBJECTTYPE_OBJECTLIST);
   	setXMLTag(XML_OBJECTLIST);
   }	

  /**
    *   Get a java.util.List object encapsulates AbstractObjects
    * 
    *   @return A java.util.List.
    *
    */ 	
  public List getList(){
  	return m_objectList;
  }

  /**
    *   Set a new object list.
    * 
    *   @param  A new java.util.List.
    *
    */ 	
  public void setList(List objList){
  	m_objectList    =new ArrayList(objList);
  }


   /**
    *   Get an object by an index
    * 
    *   @param index  An index.
    *
    *   @return  An AbstractObject
    *
    */ 	
  public AbstractObject getByIndex(int index){
	if (index>=0 && index< m_objectList.size()){
		return (AbstractObject)m_objectList.get(index);
	}  	
	
	return null;
  }
  
   /**
    *   Get an object by an object id
    * 
    *   @param objectId  An object id.
    *
    *   @return  An AbstractObject
    *
    */ 	
  public AbstractObject getByObjectId(int objectId){

	AbstractObject obj=null;  	
	Iterator it	=m_objectList.iterator();
	while (it!=null && it.hasNext()){
		obj	=(AbstractObject)it.next();
		if (obj.getObjectId()==objectId){
			return	obj;
		}
	}
	
	return null;
  }

   /**
    *   Get an object by an object name
    * 
    *   @param name An object name.
    *
    *   @return  An AbstractObject
    *
    */ 	
  public AbstractObject getByName(String name){
	if (name==null || name.equals(""))
		return null;
		
	AbstractObject obj=null;
	Iterator it	=m_objectList.iterator();
	while (it!=null && it.hasNext()){
		obj	=(AbstractObject)it.next();
		if (name.equals(obj.getName())){
			return	obj;
		}
	}
	
	return null;
  }



   /**
    *   Get an object from groups by an object id
    * 
    *   @param objectId  An object id.
    *   @param returnGroup True if return the group, false return the object itself.
    *
    *   @return  An AbstractObject
    *
    */ 	
  public AbstractObject getFromGroupsByObjectId(int objectId,boolean returnGroup){
  
	//the object may be inside some groups.
	Iterator it	=m_objectList.iterator();
	while (it!=null && it.hasNext()){
		AbstractObject aObj  =(AbstractObject)it.next();
		if (aObj instanceof JFGroup){
			JFGroup g	=(JFGroup)aObj;
			Object obj	=g.getList().getByObjectId(objectId);
			if (obj==null){
				//recursively look up objects within the object list of a group.
				obj	=g.getList().getFromGroupsByObjectId(objectId,returnGroup);
			}
				
			if (obj!=null){
				if (returnGroup)
					return g;
				else
					return (AbstractObject)obj;				
			}
		}
	}
	
	return null;
  }



   /** set if show or hide the design info of objects in this list
    */
   public void setShowDesign(boolean showDesign){
   	AbstractObject	obj;
   	Iterator it	=m_objectList.iterator();
   	while (it!=null && it.hasNext()){
   		obj	=(AbstractObject)it.next();
   		obj.setShowDesign(showDesign);
   	}
   }



   /**
    *   If this objectList has already contains such an object.
    * 
    *   @param obj  A new AbstractObject.
    *
    *   @return If obj is already inside the object list, then return true,otherwise false.
    *
    */ 	
  public boolean contains(AbstractObject obj){
  	if (obj==null)
  		return false;
  		
  	AbstractObject oldObj;
  	try{
  		oldObj =getByObjectId(obj.getObjectId());
  	}catch(Exception e){
  		oldObj =null;
  	}
  	return oldObj!=null && oldObj.equals(obj);
  }
  
   /**
    *   Get the index of an object by an object id.
    * 
    *   @param objectId  An object id.
    *
    *   @return  The index.
    *
    */ 	
  public int getIndexByObjectId(int objectId){

	for (int i=0; i<m_objectList.size(); i++){
		try{
		        Object obj      =m_objectList.get(i);

		        if (obj==null){
		                continue;
		        }else{
			        AbstractObject absObj	=(AbstractObject)obj;

			        if (absObj.getObjectId()==objectId)
				        return i;
                        }				       
		}catch(Exception e){
			break;
		}
	}
	
	return -1;
  }


   /**
    *   Generate a new object id. An object id is always unique one in this list and starts at 1.
    *   Anyone who want to make a new Abstractobject, should need a new object id.
    *   This method should also be used by <code>add</code> method.
    *
    *   @return  A new object id.
    *
    */ 	
  public int newObjectId(){
  	int maxId=0;

	AbstractObject obj=null;  	
	/** Here we used an iterator to find out the max id that already used.*/
	Iterator it	=m_objectList.iterator();
	while (it!=null && it.hasNext()){
		obj	=(AbstractObject)it.next();
		int objectId	=obj.getMaxObjectId();
		if (objectId>maxId)
			maxId	=objectId;
	}
	
	return ++maxId;  	
  }

   /**
    *   Add a new object at the end of the list.
    * 
    *   @param obj  A new AbstractObject.
    *
    *   @return The index the object located in the list.
    *
    */ 	
  public int add(AbstractObject obj){
  	if (obj==null){
  		return -1;
  	}else if (obj.getObjectId()==0){
  		obj.setObjectId(newObjectId());
  	}
  	
  	try{
  		m_objectList.add(obj);
  	}catch(Exception e){
  	}
  	
  	return m_objectList.size()-1;
  }


   /**
    *   Add a new object list at the end of the list. We don't 
    *   care about the object id here.
    * 
    *   @param list  A new AbstractObject list.
    *
    */ 	
  public void add(List list){
  	if (list==null || list.size()==0)
  		return;
  	
  	Iterator it	=list.iterator();
  	while (it!=null && it.hasNext()){
  		Object obj	=it.next();
  		if (obj instanceof AbstractObject){
  			m_objectList.add(obj);
  		}
  	}
  }

 
   /**
    *   Remove an object by index
    * 
    *   @param index A index.
    *
    */ 	
  public void removeByIndex(int index){
   	if (m_objectList!=null && index>=0 && index <m_objectList.size()){
   		m_objectList.remove(index);
   	}
  }


   /**
    *   Remove an object by object id.
    * 
    *   @param objectId An object id.
    *
    */ 	
  public void removeByObjectId(int objectId){
  	AbstractObject obj = getByObjectId(objectId);
  	if (obj!=null){
  		m_objectList.remove(obj);
  	}
  }

   /**
    *   Remove a sub-collection of this list.
    * 
    *   @param objList A sub collection.
    *
    */ 	
  public void removeAll(List objList){
  	if (objList==null || objList.size()==0)
  		return;

	Iterator it	=objList.iterator();
	while (it!=null && it.hasNext()){
		try{
			AbstractObject obj	=(AbstractObject)it.next();

			if (obj instanceof AbstractShape){
				AbstractShape shape	=(AbstractShape)getByObjectId(obj.getObjectId());
				if (shape!=null){
					PortList portList	=(PortList)shape.getPortList();
					portList.detachAll();
				}
			}
			removeByObjectId(obj.getObjectId());
		}catch(Exception e){ 
			m_logger.error("removeAll: "+e);
		}
	}  		
  }

  
   /**
    *   Move a sub collection in this list to back or front.
    * 
    *   @param objeList a sub collection of this object list. 
    *   @param sendToBack True if want to send to back, false bring to front.
    */ 	
  private void moveCollection(List objList,boolean sendToBack){
        if (objList==null || objList.size()==0)
                return;

       
        //set partly of this collection to null                
        Iterator it     =objList.iterator();
        while (it!=null && it.hasNext()){
                AbstractObject obj      =(AbstractObject)it.next();

                int index               =getIndexByObjectId(obj.getObjectId());
                if (index>=0){
                        try{
                               m_objectList.set(index,null);
                        }catch(Exception e){
                        }
                }
        }
        
        //adjust the list. move not null objcts to front or back.
        int lastNotNull =0;
        int objCount    =m_objectList.size();
        if (sendToBack){
                lastNotNull=objCount;
                for (int i=objCount-1; i>=0; i--){
                     try{
                        Object obj     =m_objectList.get(i);
                        if (obj!=null){
                                lastNotNull--;
                                if (i<lastNotNull){
                                        m_objectList.set(lastNotNull,obj);                                
                                        m_objectList.set(i,null);
                                }
                        }
                     }catch(Exception e){
                     }
                }
        }else{
                lastNotNull=-1;
                for (int i=0; i<objCount; i++){
                     try{
                        Object obj     =m_objectList.get(i);
                        if (obj!=null){
                                lastNotNull++;
                                if (i>lastNotNull){
                                        m_objectList.set(lastNotNull,obj);                                
                                        m_objectList.set(i,null);
                                }
                        }
                     }catch(Exception e){
                     }
                }
        }

        //insert the partly collection to front to back
        objCount    =m_objectList.size();
        int index=0;

⌨️ 快捷键说明

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