📄 port.java
字号:
/**
* $Id:Port.java $
*
* Copyright 2004 ~ 2005 JingFei International Cooperation LTD. All rights reserved. *
*/
package com.jfimagine.jfgraph.shape.base;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.Color;
import java.awt.BasicStroke;
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.ObjectList;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.base.JFVersion;
import com.jfimagine.utils.commonutil.CommonUtil;
import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Angle;
import com.jfimagine.jfgraph.geom.GeomConst;
import com.jfimagine.utils.log.*;
/**
* Port Class. A port is used for connecting other ports of shapes.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class Port extends AbstractObject{
/**
* A XML string tag represents a jfport
*/
public static final String XML_PORT ="Port";
/**
* A XML string tag represents current type of this port
*/
public static final String XML_PORTTYPE ="portType";
/**
* A XML string tag represents the parent id of this port
*/
public static final String XML_PARENTID ="parentId";
/**
* A XML string tag represents the x coordinates of this port
*/
public static final String XML_XOFFSET ="xOffset";
/**
* A XML string tag represents the y coordinates of this port
*/
public static final String XML_YOFFSET ="yOffset";
/**
* A XML string tag represents the x coordinates of first reference point.
*/
public static final String XML_FIRSTPOINTX ="firstPointX";
/**
* A XML string tag represents the y coordinates of first reference point.
*/
public static final String XML_FIRSTPOINTY ="firstPointY";
/**
* A XML string tag represents the x coordinates of second reference point.
*/
public static final String XML_SECONDPOINTX ="secondPointX";
/**
* A XML string tag represents the y coordinates of second reference point.
*/
public static final String XML_SECONDPOINTY ="secondPointY";
/**
* A XML string tag represents the percent position of this port.
*/
public static final String XML_PERCENTPOS ="percentPos";
/**
* A XML string tag represents the ports list attached to this port
*/
public static final String XML_CONNECTEDLIST ="attachedList";
/**
* A default port of a specified shape. A default port will
* always has a default position of a shape.
*/
public static final int PORTTYPE_DEFAULT =0;
/**
* Arbitrary port. An custom port is defined by user and will has arbitrary position on the shape.
* But it will always on the outline of the shape.
*/
public static final int PORTTYPE_CUSTOM =1;
/**
* A char seperating all the parentId-index pairs in the string list
*/
public static final String PAIR_SEPARATOR =";";
/**
* A char seperating each parentId-index pair
*/
public static final String KEY_SEPARATOR =",";
/**an internal log utility*/
private JFLogger m_logger=JFLogManager.getLogger(this.getClass());
/**
* A port type to describe current port is nothing special, IN, OUT or INOUT port
*/
private int m_portType =0;
/**
* Key of a shape that this port attached to
*/
private int m_parentId =0;
/**
* A point for x,y coordinates.
*/
protected JFPoint m_portPoint =new JFPoint();
/**
* A temporary string used to store the parentId-objectId pair list of attached ports
* each pair will be seperated by PAIR_SEPARATOR,
* each parentId-objectId pair itself will be seperated by KEY_SEPARATOR
* this string should be loaded from xml document or stream
*/
private String m_attachedListStr ="";
/**
* An actual list used to store the attached ports list,
* here we don't use a PortList so we can simplify its storage and recovery.
*/
private List m_attachedList =new ArrayList();
/**
* A parent object of this port.
*/
protected AbstractObject m_parent;
/**
* Get the Type of current port.
*
* @return The Port Type.
*
*/
public int getPortType(){
return m_portType;
}
/**
* Set the Type of current port. <br>
*
* @param portType The Type of current port
*
* @return No return.
*
*/
public void setPortType(int portType){
m_portType =portType;
}
/**
* Get the key of parent shape of this port <br>
*
* @return The parent shape's key.
*
*/
public int getParentId(){
return m_parentId;
}
/**
* Set the key of parent shape of this port <br>
*
* @param parentId The parent shape's key.
*
* @return No return.
*
*/
public void setParentId(int parentId){
m_parentId =parentId;
}
/**
* Get parent object of this port.
* @return parent object.
*/
public AbstractObject getParent(){
return m_parent;
}
/**
* Set parent object of this port.
* @param A new parent object.
*/
public void setParent(AbstractObject obj){
m_parent =obj;
if (obj!=null)
m_parentId =obj.getObjectId();
}
/**
* Get the x offset of current port. <br>
* The x offset is the x coordinate position relative to the port which index=0
*
* @return The x offset of current port.
*
*/
public double getXOffset(){
return m_portPoint.getX();
}
/**
* Set the x offset of current port. <br>
* The x offset is the x coordinate position relative to the port which index=0
*
* @param xOffset The x offset of current port.
*
* @return No return.
*
*/
public void setXOffset(double xOffset){
m_portPoint.setX(xOffset);
}
/**
* Get the y offset of current port. <br>
* The y offset is the y coordinate position relative to the port which index=0
*
* @return The y offset of current port.
*
*/
public double getYOffset(){
return m_portPoint.getY();
}
/**
* Set the y offset of current port. <br>
* The y offset is the y coordinate position relative to the port which index=0
*
* @param yOffset The y offset of current port.
*
* @return No return.
*
*/
public void setYOffset(double yOffset){
m_portPoint.setY(yOffset);
}
/**
* Get the port point of current port.
*
* @return The port point.
*
*/
public JFPoint getPortPoint(){
return m_portPoint;
}
/**
* Set the port point of current port.
*
* @param pnt The port point.
*
*/
public void setPortPoint(JFPoint pnt){
if (pnt!=null)
setPortPoint(pnt.getX(),pnt.getY());
}
/**
* Set the port point of current port.
*
* @param x, y The port point.
*
*/
public void setPortPoint(double x, double y){
m_portPoint.setValue(x,y);
//adjust percent pos
double distance =m_firstPoint.distance(m_secondPoint);
double distance1 =m_portPoint.distance(m_firstPoint);
if (distance<1)//1 pixel difference
m_percentPos =1;
else
m_percentPos =distance1/distance;
}
/**
* A port will has two reference points,
* the port will on the middle of these two points, close to first point
* ,and at a specified percent position to first point.
* We use a percent position here, so we can easily SCALE the port while
* user scaling the parent shape.
*
* First point this port second point
* o------------------------x--------------------o
* | percent position(a%) |
*
* The first point or second point is actuall a position of a node of shapes.
* Consider the node will always be changed(especially in polygon, the node will
* frequently be removed or re-created), so we use a POINT for instead here.
*
*/
private JFPoint m_firstPoint =new JFPoint();
private JFPoint m_secondPoint =new JFPoint();
private double m_percentPos =0.5; //50%
/**
* Get the first reference point of this port.
*
* @return The first reference point.
*
*/
public JFPoint getFirstPoint(){
return m_firstPoint;
}
/**
* Set the first reference point of this port.
* @param pnt A new first reference point.
*/
public void setFirstPoint(JFPoint pnt){
setFirstPoint(pnt.getX(),pnt.getY());
}
/**
* Set the first reference point of this port.
* @param x, y New position of the first reference point.
*/
public void setFirstPoint(double x, double y){
m_firstPoint.setValue(x,y);
}
/**
* Get the second reference point of this port.
*
* @return The second reference point.
*
*/
public JFPoint getSecondPoint(){
return m_secondPoint;
}
/**
* Set the second reference point of this port.
* @param pnt A new second reference point.
*/
public void setSecondPoint(JFPoint pnt){
setSecondPoint(pnt.getX(),pnt.getY());
}
/**
* Set the second reference point of this port.
* @param x, y New position of the second reference point.
*/
public void setSecondPoint(double x, double y){
m_secondPoint.setValue(x,y);
}
/**
* Finish moving port. When we have already set a new first point or second point,
* and we want to keep the percent position for this port according these two points,
* we need to call a 'finishMoving' method here.
*/
public void finishMoving(){
calcPortPosition();
}
/**
* Get the percent position of this port.
* @return The percent position.
*
*/
public double getPercentPos(){
return m_percentPos;
}
/**
* Calculate port position by percent pos.
*
*/
private void calcPortPosition(){
//adjust port point.
double distance =m_firstPoint.distance(m_secondPoint);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -