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

📄 abstractshape.java

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

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.GeneralPath;
import java.awt.Composite;
import java.awt.AlphaComposite;

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

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

import com.jfimagine.utils.commonutil.CommonUtil;

import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.PortList;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.Property;
import com.jfimagine.jfgraph.shape.base.Label;
import com.jfimagine.jfgraph.shape.base.JFVersion;

import com.jfimagine.jfgraph.shape.decorate.Arrow;
import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.FillFormat;
import com.jfimagine.jfgraph.shape.decorate.FontFormat;
import com.jfimagine.jfgraph.shape.decorate.TextRender;

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

 
 
 /**
 * Abstract Shape class. All shapes should extends/instances this class.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public abstract class AbstractShape extends AbstractObject{

   /**
    *   A XML string tag represents the label of a shape.
    */
   public  static final String	 XML_LABEL	="label";

   /**
    *   A XML string tag represents if show or hide label.
    */
   public  static final String	 XML_SHOWLABEL	="showLabel";

   /**  A XML string tag represents if disable or enable scaling*/
   public  static final String	 XML_DISABLESCALING	="disableScaling";  

   /**  A XML string tag represents if disable or enable modifying properties*/
   public  static final String	 XML_DISABLEMODIFYINGPROPERTIES	="disableModifyingProperties";  

   /**  A XML string tag represents if disable or enable motions*/
   public  static final String	 XML_DISABLEMOTION	="disableMotion";  

   /**  A XML string tag represents if this shape is invisible*/
   public  static final String	 XML_INVISIBLE		="invisible";  

   /**  A XML string tag represents the transparency of this shape*/
   public  static final String	 XML_TRANSPARENCY	="transparency";  


   /**
    *   Port List, all shape should has a port list.
    *   <p>A port is a shape used to connect other shapes' ports.
    *   <p>A line/curve shape should has only two ports, one for start point and another for end point.
    *   <p>A rectangular shape should has four or more ports, e.g. middle of top, middle of left, middle of right and middle of bottom.
    *
    */ 	
   protected PortList	m_portList=new PortList();

   /**
    *   Node List, all shape should has a node list.
    *   <p>A node is a point on the outline of a shape.
    *   <p>Nodes for line shape are used to construct a polyline shape.
    *   <p>A rectangular shape should has only two nodes, left-top point and right-bottom point.
    *
    */ 	
   protected NodeList	m_nodeList=new NodeList();

   /**
    *   A bounds node list is a four nodes list of the bounds,
    *   it's an virtual node list for showing, but not the actual
    *   node list for this shape.
    *   Anyone who want to use this node list must call initBoundsNodeList 
    *   first.
    *
    */ 	
   protected NodeList	m_boundsNodeList=new NodeList();


   /**
    *   Property List, all shape should has a property list.
    *   <p>A property is customized and defined by client.
    *   <p>Each shape should has one or more proproties.
    *
    */ 	
   protected PropertyList	m_propertyList=new PropertyList();

   /**
    *   An internal label property for any shape.
    *
    */ 	
   protected Label	m_label	=new Label();

   /**
    *   A font format for current shape and the internal label above.
    *
    */ 	
   protected FontFormat  m_fontFormat	=new FontFormat();

   /**
    *   Access times, an access times variable is used to record
    *   how many times this object has been accessed. 
    *
    */ 	
   private int		m_accessTimes	=0;

   /**
    *   disable scaling
    */
   private boolean  m_disableScaling		=false;

   /**
    *   disable modifying properties
    */
   private boolean  m_disableModifyingProperties=false;

   /**
    *   disable motion
    */
   private boolean  m_disableMotion		=false;

   /**
    *   if this shape is invisible
    */
   private boolean  m_invisible			=false;


   /**
    *   Transparency of this shape. 0% ~ 100%
    */
   private int m_transparency		=0;
  


   /**
    *   Get the arrow format of current object.
    * 
    *   @return  The arrow format.
    *
    */ 	
   public Arrow getArrow(){
   	return null;
   }

   /**
    *   Set the arrow format of current object.
    *
    *   @param arrow  A new arrow format object.
    *
    */ 	
   public void setArrow(Arrow arrow){
   }

   /**
    *   Get the line format of current object.
    * 
    *   @return  The line format.
    *
    */ 	
   public LineFormat getLineFormat(){return null;}

   /**
    *   Set the line format of current object.
    *
    *   @param lineFormat A new line format.
    *
    */ 	
   public void setLineFormat(LineFormat lineFormat){}

   /**
    *   Get the fill format of current object.
    * 
    *   @return  The fill format.
    *
    */ 	
   public FillFormat getFillFormat(){return null;}

   /**
    *   Set the fill format of current object.
    *
    *   @param fillFormat A new fill format.
    *
    */ 	
   public void setFillFormat(FillFormat fillFormat){}
   

   /**
    *   Get the internal label of current object.
    * 
    *   @return  The internal label.
    *
    */ 	
   public Label getLabel(){
   	return m_label;
   }

   /**
    *   Set the internal label of current object.
    *
    *   @param aLabel A new internal label.
    *
    */ 	
   public void setLabel(Label aLabel){  
   	m_label.setValue(aLabel);
   	initLabel();
   }
   
   
   /**
    *   Set the intial position of this internal label.
    *
    */ 	
   protected void initLabel(){
   	m_label.setParent(this);   	
   	Rect 	rect=getBounds();
   	m_label.setLabelPoint(rect.getCenter());
   	m_label.setZoomScale(getZoomScale());
   	m_fontFormat.setZoomScale(getZoomScale());
   }

   /**
    *   get if is disabled scaling
    */
   public boolean  isDisableScaling(){
   	return m_disableScaling;
   }
   
   /**
    *   set disable scaling
    */
   public void  setDisableScaling(boolean disable){
   	m_disableScaling	=disable;
   }

   /**
    *   get if is disabled modifying properties
    */
   public boolean  isDisableModifyingProperties(){
   	return m_disableModifyingProperties;
   }

   /**
    *   set disable modifying properties
    */
   public void  setDisableModifyingProperties(boolean disable){
   	m_disableModifyingProperties	=disable;
   }


   /**
    *   get if is disabled motion
    */
   public boolean  isDisableMotion(){
   	return m_disableMotion;
   }

   /**
    *   set disable motion
    */
   public void  setDisableMotion(boolean disable){
   	m_disableMotion	=disable;
   }


   /**
    *   get if this shape is invisible
    */
   public boolean  isInvisible(){
   	return m_invisible;
   }

   /**
    *   set invisible of this shape.
    */
   public void  setInvisible(boolean invisible){
   	m_invisible	=invisible;
   }


   /**
    *   get transparency of this shape.
    */
   public int getTransparency(){
   	return m_transparency;
   }

   /**
    *   set transparency of this shape.
    */
   public void  setTransparency(int transparency){  
   	if (transparency<0)
   		transparency	=0;
   	if (transparency>100)
   		transparency	=100;
   		
   	m_transparency	=transparency;
   }


   /**A temporary original composite for methods below*/
   private Composite m_originalComposite;

   protected static Composite getTransparencyComposite(int transparency){
		float alpha	=(float) ((100- transparency) * 1.0f / 100);
		return AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
   }	
   
   /**
    *   Set the transparency composite of current graphics context.
    *
    *   @param g The graphics context to set a new composite mode
    */ 	
   public void setTransparencyComposite(Graphics g){
	if (m_transparency>0){
        	Graphics2D g2	=(Graphics2D)g;
    		m_originalComposite = g2.getComposite();

		g2.setComposite(getTransparencyComposite(m_transparency));
	}
   }

   /**
    *   Restore the transparency composite of current graphics context.
    *
    *   @param g The graphics context to resotre to original composite mode
    */ 	
   public void restoreTransparencyComposite(Graphics g){
   	if (m_transparency>0){
   		Graphics2D g2	=(Graphics2D)g;
		if (m_originalComposite!=null)
			g2.setComposite(m_originalComposite);		   	
	}
	m_originalComposite	=null;
   }



  /** An original center before this shape be adjusted or rotated*/         
  private JFPoint m_originalCenter; 
 
   /**
    *   Start move internal label of this shape.
    */  	
  protected void startMoveLabel(){
  	Rect	rect	=getBounds();
  	m_originalCenter	=rect.getCenter();
  };
   /**
    *   Finish move internal label of this shape.
    */  	
  protected void finishMoveLabel(){
  	Rect	rect		=getBounds();
  	JFPoint newCenter	=rect.getCenter();
   	m_label.moveBy(newCenter.getX()-m_originalCenter.getX(),newCenter.getY()-m_originalCenter.getY());   	
  };

   
   /**
    *   Get the font format of current object.
    * 
    *   @return  The font format.
    *
    */ 	
   public FontFormat getFontFormat(){
   	return m_fontFormat;
   } 

   /**
    *   Set the font format of current object.
    *
    *   @param fontFormat A new font format.
    *
    */ 	
   public void setFontFormat(FontFormat fontFormat){
   	m_fontFormat.setValue(fontFormat); 
   	m_label.setFont(fontFormat.getFont());
   }
   
   
   /**
    *  Get the access times of this object.
    *  @return The access times. 
    *  
    */
   public int	getAccessTimes(){
   	return m_accessTimes;
   }

   /**
    *  Increase one time of the access times.
    *  @return The new access times.
    *  
    */
   public int incAccessTimes(){
   	m_accessTimes++;
   	return m_accessTimes;
   }

   /**
    *  Clear the access times of this object.
    *  
    */
   public void clearAccessTimes(){
   	m_accessTimes	=0;
   }


    /** set zoom scale
     *  @param zoomScale A new zoom scale.
     */	
    public void setZoomScale(double zoomScale){
    	super.setZoomScale(zoomScale);
	
	m_portList.setZoomScale(zoomScale);
	m_nodeList.setZoomScale(zoomScale);	
   	m_fontFormat.setZoomScale(zoomScale);
    	//set label's zoom scale.    
        m_label.setZoomScale(zoomScale);
        m_label.setFont(m_fontFormat.getFont());
    }

   /**
    *   Set the id of current object,object id starts at 0.
    *
    *   @param objectId  The object id.
    *
    */ 	
   public void setObjectId(int objectId){
   	super.setObjectId(objectId);
        
        m_portList.setParentId(objectId);
   } 

   /**
    *   Set all nodes/ports/labels' parent, this method will be called
    *   after a clone method because of a clone method will not clone their parent.
    */ 	
   public void setParent(){
   	m_portList.setParent(this);
   	m_nodeList.setParent(this);
   	m_label.setParent(this);
   }

   
   /**
    *   Get the number of all the nodes of a AbstractShape 
    *   a node is a point which is on AbstractShape and used to adjust/transform the AbstractShape
    * 
    *   @return  The number of nodes.
    *
    */ 	
   public int getNodeCount(){
   	return 	m_nodeList.size();
   }

   /**
    *   Get a node list of this shape. A shape should always has a node list.
    * 
    *   @return  The node list.
    *
    */ 	
   public ObjectList getNodeList(){
   	return m_nodeList;
   }

   /**
    *   Set a node list of this shape. 
    * 
    *   @param nodeList  A new node list.
    *
    */ 	
   public void setNodeList(ObjectList nodeList){
   	//this feature will be overrode by sub classes
   	//here it will do nothing.
   }

   /**
    *   Get a specified node by index<br>
    *
    *   @param index  An index of node
    * 
    *   @return  The node object.
    *

⌨️ 快捷键说明

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