jfvhlabelline.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 329 行

JAVA
329
字号
/**
 *    $Id:JFVHLabelLine.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfgraph.shape.line;

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

import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.BasicStroke;
import java.awt.geom.AffineTransform;

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

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

import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.JFVersion;
import com.jfimagine.jfgraph.shape.base.Node;

import com.jfimagine.jfgraph.shape.decorate.LineFormat;
import com.jfimagine.jfgraph.shape.decorate.Arrow;

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Angle;
import com.jfimagine.jfgraph.geom.JFVector;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfgraph.geom.LabelLine;
import com.jfimagine.jfgraph.geom.GeomConst;
 
 
 /**
 * JFVHLabelLine class.  
 * A JFVHLabelLine class used to represent a enforced vertical or horizontal label line.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.3.2 $
 */  
 public class JFVHLabelLine extends JFLabelLine{

   /**
    *   A XML string tag represents a vertical or horizontal label Line
    */
   public  static final String	 XML_VHLABELLINE	="VHLabelLine";

   /**  A XML string tag represents the line type is vertical or horizontal. */
   public  static final String   XML_LINETYPE		="lineType";

	
   /** A horizontal label line */		
   public static final int LINETYPE_HORIZONTAL		=0;
   /** An vertical label line */		
   public static final int LINETYPE_VERTICAL		=1;

   /**
    *   label line type.
    */ 
   private int m_lineType				=LINETYPE_HORIZONTAL;

   
   /**
    *   Constructor for label Line
    */
   public JFVHLabelLine(){
   	setObjectType(ShapeConst.SHAPETYPE_HLABELLINE);
   	setXMLTag(XML_VHLABELLINE);
   }	

   /**
    *   Constructor for vertical or horizontal label Line
    *   @param x1,y1 start points for label line
    *   @param x2,y2 end points for label line
    *   @param lineType line type,LINETYPE_HORIZONTAL or LINETYPE_VERTICAL
    */
   public JFVHLabelLine(double x1, double y1,double x2, double y2, int lineType){
   	setObjectType(ShapeConst.SHAPETYPE_HLABELLINE);
   	setXMLTag(XML_VHLABELLINE);
   	
   	m_lineType	=lineType;
   	if (m_lineType==LINETYPE_HORIZONTAL){
   		y2	=y1;
	}else{
		x2	=x1;
	}
   	
   	addNode(x1,y1);
   	addNode(x2,y2);
   	finishDrawing();
   }


   /**
    *   Get the line type, horizontal or vertical.
    * 
    *   @return  line type.
    *
    */ 	
   public int getLineType(){
   	return m_lineType;
   }

   /**
    *   Set current label line type.
    *
    *   @param lineType A new line type.
    *
    */ 	
   public void setLineType(int lineType){
   	m_lineType	=lineType;
   }


   /**
    * When moving a node, in some cases, e.g. ctrl key or shift key pressed to force preserving 
    * the form of a shape, or force equilateral shapes, we need to recalculate the actual moving
    * node's position.
    *
    *  @param movePos Desire moving position of a node.
    *  @param moveCase Move case of a node, normal, shift key pressed or ctrl key pressed
    *  @return The actual moving position of a node.
    *
    */	  
   public JFPoint getMoveNodePos(Node node,JFPoint movePos,int moveCase){
   	if (node==null || movePos==null)
   		return null;

	//here will will force the label line as a horizontal or vertical label line according to the line type.   	

	int index	=m_nodeList.getIndexByObjectId(node.getObjectId());
	//only change two end points.
	if (index<0 || index>1) return movePos;

	JFPoint start	=m_labelLine.getPoint1();
	JFPoint end	=m_labelLine.getPoint2();
	JFPoint baseNode;			
	if (index==0)
		baseNode	=end;
	else
		baseNode	=start;
			
   	//offset between current moving position and baseNode
   	double x	=movePos.getX() - baseNode.getX();
   	double y	=movePos.getY() - baseNode.getY();
   	if (m_lineType==LINETYPE_HORIZONTAL){
   		movePos.setY(baseNode.getY());
   	}else{
   		movePos.setX(baseNode.getX());
   	}

   	return movePos;			
   }
   
   

   /**
    *   Convert this object to String <br>
    * 
    *   @return  An string represents the content of the object
    *
    */ 	
   public String toString(){
   	StringBuffer buf=new StringBuffer();
	
	buf.append(super.toString());
	buf.append("lineType=");	buf.append(m_lineType);

   	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 JFVHLabelLine();
  }
  
  
   /**
    *   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;
  		}
  		
  		JFVHLabelLine line =(JFVHLabelLine) obj;
  		line.m_lineType		=getLineType();
  		
  		return line;
  		
	}catch(Exception e){
		throw new CloneNotSupportedException(e.getMessage());
	}
  }


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


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

      JFVHLabelLine  line= (JFVHLabelLine)obj;
      
      return  	(line.m_lineType==m_lineType);
  }


   /**
    *   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);
	
	//line type
    	element.addChild(new Element(XML_LINETYPE,m_lineType));
  }


   /**
    *   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);
	
	int m_lineType	=Element.getIntValue(element.getChild(XML_LINETYPE));
  }

 
   /**
    *   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);
       
        stream.writeInt(m_lineType);
  }


   /**
    *   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);
	
	m_lineType	=stream.readInt();
  }


 }

⌨️ 快捷键说明

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