jftext.java

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

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


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

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Container;
import java.awt.Insets;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.awt.Composite;
import java.awt.AlphaComposite;

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

import com.jfimagine.jfgraph.shape.decorate.FontFormat;
import com.jfimagine.jfgraph.shape.decorate.TextRender;

import com.jfimagine.jfgraph.shape.rectangle.JFRectangle;
import com.jfimagine.jfgraph.shape.base.Node;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.JFVersion;

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

import com.jfimagine.jfdraw.gui.dialog.TextDialog;

 
 /**
 * JFText class.  JFText is a class to encapsulate text processing.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class JFText extends JFRectangle{

   /**
    *   A XML string tag represents a Text
    */
   public  static final String	 XML_TEXT		="Text";
   
   public  static final String	 XML_TEXTALIGNMENT	="TextAlignment";
   
   public static final int TEXTALIGNMENT_LEFT		=0;
   public static final int TEXTALIGNMENT_MIDDLE		=1;
   public static final int TEXTALIGNMENT_RIGHT		=2;
   
   /**
    *   A multi lines text render.
    */
   private	TextRender	m_textRender=new TextRender();
   
   
   private int m_textAlignment				=TEXTALIGNMENT_LEFT;

   
   /**
    *   Constructor for Text
    */
   public JFText(){
   	setObjectType(ShapeConst.SHAPETYPE_TEXT);
   	setXMLTag(XML_TEXT); 
  	
   }	

   /**
    *   Constructor for round rectangle.
    *
    *   @param x  X coordiate.
    *
    *   @param y  Y coordiate.
    *
    *   @param w  Width of this round rectangle.
    *
    *   @param h  Height of this round rectangle.
    *
    *   @param text The string text.
    *
    *   @param showOutline True if need to show outline rectangle.
    *
    */ 	
   public JFText(double x, double y, double w, double h, String text, boolean showOutline){
   	setObjectType(ShapeConst.SHAPETYPE_TEXT);
   	setXMLTag(XML_TEXT);

	addNode(x,y);
	addNode(x+w,y+h);

	//set text 
	setText(text);

	//hide line outline	
	if (!showOutline){
		getLineFormat().setLineWidth(0);
	}
		   	
   	finishDrawing();
   }
   

   /**
    *   Get the text content of current text. 
    *
    *   @return  The text of current text 'box'.
    *
    */ 	
   public String getText(){
   	return getLabel().getText();
   }

   /**
    *   Set the text content of current text. 
    *
    *   @param  text The text content of current text 'box'.
    *
    *   @return  No return.
    *
    */ 	
   public void setText(String text){
   	getLabel().setText(text);
   }


   /**
    *   Get current text alignment
    *
    *   @return  current text alignment type
    *
    */ 	
   public int getTextAlignment(){
   	return m_textAlignment;
   }
   
   /**
    *   Set current text alignment
    *
    *   @param  A new text alignment type.
    *
    *   @return  No return.
    *
    */ 	
   public void setTextAlignment(int textAlignment){
   	m_textAlignment	=textAlignment;
   }

   	
   /**
    *   Draw label of current shape.
    * 
    *   @param g A graphic canvas.
    *
    */  	
  public void drawLabel(Graphics g){
  	//this method will be invalid here.
  }

   /**
    *   Draw a label  picked state of current shape.
    * 
    *   @param g A graphic canvas.
    *
    */  	
  public void drawLabelPicked(Graphics g){
  	//this method will be invalid here.
  }

   /**
    *   Set the intial position of this internal label.
    *
    */ 	
   protected void initLabel(){
   	super.initLabel();
   	m_label.setMovable(false);
   }
  

   /**
    *   Draw current object on graphic canvas.
    * 
    *   @param  g  A graphic canvas.	
    *   @param  isXorMode If is in xor mode now.
    *
    */ 	
   public void  draw(Graphics g,boolean isXorMode){
   	if (g==null)
   		return;

   	//if user hide this shape, we'll draw an 'invisible' bounds here.	
   	if (isInvisible()){
   		drawInvisibleBounds(g,isXorMode);
   		return;
   	}	

	super.draw(g,isXorMode);


   	if (!isXorMode && m_label.getVisible() && getText().length()>0){

  		double zoom	=getZoomScale();

		//draw rotated image
		Graphics2D g2 = (Graphics2D)g;  

		Composite originalComposite =g2.getComposite();
		int transparency	=m_fontFormat.getTransparency();
		if (transparency>0){
			g2.setComposite(getTransparencyComposite(transparency));
		}
		
		
		g2.setFont(m_label.getZoomedFont());
		g2.setColor(m_fontFormat.getFontColor());
			   		
     		double angle	=getRotateAngle();
  	
  		//get original rectangle
     		Rect rect	=getOriginalRect();
		rect.setValue(rect.getX() * zoom, rect.getY() * zoom, rect.getWidth() * zoom, rect.getHeight() * zoom);
	     	
	     	//System.out.println("draw text.."+(new java.util.Date()));
     		if (angle==0){
			m_textRender.drawMultiLines(g2,rect,getText(),m_fontFormat,m_textAlignment);
		}else{
	     			
     			AffineTransform origXform = g2.getTransform();
     			AffineTransform newXform = (AffineTransform)(origXform.clone());
     		
     			//center of rotation is center of the panel
     			JFPoint	center	=rect.getCenter();
     			int xRot = (int)center.getX();
     			int yRot = (int)center.getY();
     			newXform.rotate(angle, xRot, yRot);
     			g2.setTransform(newXform);
     		
			m_textRender.drawMultiLines(g2,rect,getText(),m_fontFormat,m_textAlignment);
        		
     			g2.setTransform(origXform);
		}

		if (transparency>0){
			g2.setComposite(originalComposite);
		}

	}

   }



   /**
    *   Move/adjust a node of current object.
    * 
    *   @param  node Currently moving node.
    *
    *   @param  x, y Moving offsets.
    *
    *   @param g current drawing canvas.
    *
    */ 	
   public void  moveNode(Node node, double x, double y,Graphics g){
   	m_label.setVisible(false);
   	super.moveNode(node,x,y,g);
   	m_label.setVisible(true);
   }	

   /**
    *   Move current object by an x and y offset.
    * 
    *   @param  x, y Moving offsets.
    *
    */ 	
   public void  moveBy(double x, double y){
   	m_label.setVisible(false);
   	super.moveBy(x,y);
   	m_label.setVisible(true);
   }

  
   /**
    *   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());
   	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 JFText();
  }
  
  
   /**
    *   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;
  		}
  		
  		JFText text =(JFText) obj;     
  		text.m_textAlignment	=m_textAlignment;
  		return text;
  		
	}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();
  }


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

      JFText  text= (JFText)obj;
      
      return    text.m_textAlignment == m_textAlignment;
  }


   /**
    *   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);
  	
    	element.addChild(new Element(XML_TEXTALIGNMENT,m_textAlignment));
  }


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

      	if (version!=null && !version.lowerVersionOf(JFVersion.VERSIONID_1801)){
        	m_textAlignment			=Element.getIntValue(element.getChild(XML_TEXTALIGNMENT));
        }
      
  }

 
   /**
    *   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_textAlignment);
  }

   /**
    *   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);  
	    
   	   if (version!=null && !version.lowerVersionOf(JFVersion.VERSIONID_1801)){
   	   	m_textAlignment	=stream.readInt();
   	   }
	    
  }



 }

⌨️ 快捷键说明

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