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

📄 jflibelem.java

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

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

import java.awt.Color;
import java.awt.Graphics;  
import java.awt.Graphics2D; 
import java.awt.BasicStroke;
 

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.AbstractShape;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.ObjectFactory;
import com.jfimagine.jfgraph.shape.base.JFVersion;
                                  
import com.jfimagine.jfgraph.shape.rectangle.JFImage;

import com.jfimagine.jfgraph.shape.union.JFGroup;      
import com.jfimagine.jfgraph.transfer.JFClipboard;

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.GeomConst;


 /**
 * JFLibElem class is used to store and recover a customized graph library element.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.1.0 $
 */  
 public class JFLibElem extends AbstractObject{

   /**
    *   Library element.
    */
   private AbstractObject  m_elem	=null;

   /**
    *   title of this element
    */
   private String   m_title		="element";

   /**
    *   description of this element
    */
   private String   m_description	="";


   /**
    *   A draw region for furthur usage.
    */
   private Rect m_drawRegion		=new Rect();
   
   	
   /**
    *   get element
    *   @return The element AbstractObject.
    */
   public AbstractObject getElement(){
   	return m_elem;
   }


   /**
    *   Set element
    *   @param obj A new element.
    */
   public void setElement(AbstractObject obj){
   	try{

   		m_elem	=(AbstractObject)obj.clone();
   		JFClipboard.finishLoading(m_elem,1);
   	}catch(Exception e){
	}
   }

   /**
    *   Set element
    *   @param objList An object list.
    */
   public void setElement(ObjectList objList){
   	if (objList==null || objList.size()==0)
   		m_elem=null;
   	else
   		setElement(objList.getList());
   }

   /**
    *   Set element
    *   @param objList An object list collection.
    */
   public void setElement(List objList){
   	if (objList==null || objList.size()==0){
   		m_elem=null;
   		return;
   	}

	List l		=new ArrayList();   	
   	Iterator it	=objList.iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=it.next();
   		if (objList.size()==1){
   			setElement((AbstractObject)obj);
   			return;
   		}else{
   			if (obj!=null && obj instanceof AbstractObject){
   				AbstractObject aObj	=(AbstractObject)obj;
   				try{
   					l.add((AbstractObject)aObj.clone());
   				}catch(Exception e){
   				}
   			}
   		}
	}
   	JFGroup	 g	=new JFGroup();
   	g.setList(l);
   	JFClipboard.finishLoading(g,1);
	m_elem	=g;
   }
  
   

   /**
    *   get object id of element
    *   @return the max object id.
    */
   public int getElementId(){
   	if (m_elem==null)
   		return 0;
   	else
   		return m_elem.getMaxObjectId();
   }


   /**
    *   Get title of this element.
    */
   public String getTitle(){
   	return m_title;
   }

   /**
    *   Set title of this element.
    */
   public void setTitle(String title){
   	m_title	=title;
   }

   /**
    *   Get description of this element.
    */
   public String getDescription(){
   	return m_description;
   }

   /**
    *   Set description of this element.
    */
   public void setDescription(String description){
   	m_description	=description;
   }


   /**
    *   Constructor for Layer
    */
   public JFLibElem(){ 
   	setObjectType(ShapeConst.OBJECTTYPE_LIBELEM);
   }	
   
   /**
    *  A shape will has its own position/offset anyway,
    *  we remove its position here.
    */
   private void clearObjectOffset(AbstractObject obj){
   	if (obj==null)
   		return;
   		
   	AbstractShape shape	=(AbstractShape)obj;
   	Rect	bounds=shape.getBounds();	
   	shape.moveBy(-1 * bounds.getX(), -1 * bounds.getY());
   }

	
   //the margin of each element graph.	
   private static final int MARGIN	=5;   
   
   /** drawing object*/
   private AbstractShape m_drawingObj	=null;

   /** prepare drawing object from library element **/	   
   private void prepareDrawingObject(){
   	try{   
   		if (m_elem==null)
   			return;
   				
               	if (m_drawingObj!=null){
			clearObjectOffset(m_drawingObj);
               		return;
               	}
		
		//in template mode.               	
   		if (m_elem instanceof JFPage){ 
   			JFPage page	=(JFPage)m_elem;

   			JFLayer layer	=page.getCurrentLayer();

   			if (layer!=null){
   				JFGroup g	=new JFGroup();
   				g.setObjectId(1);
   				ObjectList list	=(ObjectList)layer.getShapeList().clone();
   				JFClipboard.finishLoading(list,2);
   				g.setList(list.getList());
   				m_drawingObj	=g;
				clearObjectOffset(m_drawingObj);
   			}
   		//in library mode:
   		}else{
   			m_drawingObj	=(AbstractShape)m_elem;
			clearObjectOffset(m_drawingObj);
   		}

   	}catch(Exception e){
   	}
   }
  
   /**
    *  Prepare drawing zoomScale,xoffset and yoffset of current element.
    *  @param destRect Destination rectangle to map to.
    */
   private void prepareDrawing(Rect destRect){
   	if (m_elem==null)
   		return; 
	
	//prepare drawing object from element.
	prepareDrawingObject();
        
        //get bounds of drawing list.
   	Rect	srcRect=m_drawingObj.getBounds();
   	
   	double srcWidth		=srcRect.getWidth();
   	double srcHeight	=srcRect.getHeight();
   	double destWidth	=destRect.getWidth() - MARGIN * 2;
   	double destHeight	=destRect.getHeight() - MARGIN * 2;	
   	
   	if (srcWidth<1) srcWidth=1;
   	if (srcHeight<1) srcHeight=1;
   	if (destWidth<1) destWidth=1;
   	if (destHeight<1) destHeight=1;
  
   		
   	double widthScale	=destWidth/srcWidth;
   	double heightScale	=destHeight/srcHeight;

 	//minimum scale in mapping.
	double scale		=1;	
	if (widthScale<1 || heightScale<1){
		scale	=Math.min(widthScale,heightScale);
	}

	m_drawingObj.setZoomScale(scale);
 

	//get the offset the shape will be drawn to,related to dest rect.
	//i.e. centralize the drawing shape on dest rect.
	srcWidth	*=scale;
	srcHeight	*=scale;
	
	double x	=(destWidth - srcWidth)/2 + MARGIN;
	double y	=(destHeight - srcHeight)/2 + MARGIN;
	if (x<0) x=0;
	if (y<0) y=0; 
	
	//assume the position of currrent element under specified zoom scale.
	x	=(destRect.getX()+x)/scale;
	y	=(destRect.getY()+y)/scale;

	m_drawingObj.moveBy(x,y);
   }	


   /**
    *   Draw current element on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param  drawRegion A retangular region to draw current element.
    *
    */ 	
   public void  draw(Graphics g,Rect drawRegion){

   	if (g==null || m_elem==null || !(m_elem instanceof AbstractObject))
		return;

	m_drawRegion.setValue(drawRegion);

	//prepare drawing scale,xoffset and yoffset.
	prepareDrawing(drawRegion);

	//hide ports, bounds of JFGroup, etc.
	m_drawingObj.setShowDesign(false);
	//drawing
	m_drawingObj.draw(g,false);
   }


   /**
    *   draw a picked rectangular sign around current library element.
    * 
    *   @param  g A graphics context used to pick one element.
    *
    */ 	
   public void  drawPicked(Graphics g){
   	//value of m_drawRegion will be assigned in early draw(Graphics,Rect) method calling.
   	if (m_drawRegion.getWidth()<1 || m_drawRegion.getHeight()<1)
   		return;
   		
   		
   	Graphics2D g2	=(Graphics2D)g;
   	g2.setStroke(new BasicStroke(2));
   	g2.setColor(new Color(51,204,255)); //light blue
   	g2.drawRect((int)m_drawRegion.getX(),
   		    (int)m_drawRegion.getY(),
   		    (int)(m_drawRegion.getWidth()-1),
   		    (int)(m_drawRegion.getHeight()-1)
   		    );   
   }	  
   
   /**
    *   if this element intersects(contains)the mouse point.
    *   @return True if this element contains the mouse point, false otherwise.
    *
    */ 	
   public boolean intersects(JFPoint pnt){
   	return m_drawRegion.contains(pnt);
   }


   /**
    *   Convert this object to String <br>
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
   	StringBuffer buf=new StringBuffer();
	
	buf.append("\ntitle=");		buf.append(m_title); buf.append(";");
	buf.append("description=");	buf.append(m_description); buf.append(";");
   	buf.append("\n<element>\n");
   	if (m_elem!=null)
   		buf.append(m_elem.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 JFLibElem();
  }
  
   /**
    *   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{
  		Object obj =super.clone();
  		if (obj==null){
  			return null;
  		}
  		
  		JFLibElem libElem =(JFLibElem) obj;
  		if (m_elem!=null)
			libElem.m_elem	=(AbstractObject)m_elem.clone();
		libElem.m_title		=m_title;
		libElem.m_description	=m_description;
		libElem.m_drawRegion.setValue(m_drawRegion);
  		
  		return libElem;
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


   /**
    *   Returns the hashcode for this Object.
    * 
    *   @return hash code for this Point2D.
    *
    */ 	
  public int hashCode(){
	
	int code	=super.hashCode();
	if (m_elem!=null)
		code	^=m_elem.hashCode();
	
	code	=code ^ m_title.hashCode() ^ m_description.hashCode();
	
	return code;
  }


   /**
    *   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 Port 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 JFLibElem))
            return false;

      JFLibElem  libElem= (JFLibElem)obj;
	
      if (!m_title.equals(libElem.m_title) || !m_description.equals(libElem.m_description))
      	return false;
      
      if (libElem.m_elem==null && m_elem==null)
      	return true;
      
      if (libElem.m_elem==null || m_elem==null)
      	return false;
      			
      return  	libElem.m_elem.equals(m_elem);
  }


   /**
    *   Save this object 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);

	if (m_elem==null)//write true if element is not null, false if null.
		stream.writeBoolean(false);
	else{
		stream.writeBoolean(true);
		m_elem.saveToStream(stream,version);
	}

	stream.writeUTF(m_title);
	stream.writeUTF(m_description);
  }


   /**
    *   Load object 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{

	super.loadFromStream(stream,skipHead,version);

	boolean hasElem	=stream.readBoolean();

	if (hasElem){

		m_elem	=ObjectFactory.loadObjFromStream(stream,version);
	}

	m_title		=stream.readUTF();

	m_description	=stream.readUTF();

	if (m_elem!=null){
	        JFClipboard.finishLoading(m_elem,1);
	}

  }
  
   /**
    *   for loading an image, we need to call this finish loading image method to completely create a new image.
    * 
    *   @param canvas A component for creating an image object.
    */
  public boolean finishLoadingImages(java.awt.Component canvas){	
  	if (m_elem==null)
  		return false;
  	else
		return JFImage.finishLoadingImages(m_elem,canvas);
  }




 }

⌨️ 快捷键说明

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