jfline.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 1,258 行 · 第 1/3 页
JAVA
1,258 行
}
}
}
}
//four node of polyline.
JFPointNode firstNode =m_polyLine.getNode(0);
JFPointNode secondNode =m_polyLine.getNextNode(0,false);
int nodeCount =m_polyLine.getNodeCount();
JFPointNode lastFirstNode =m_polyLine.getNode(nodeCount-1);
JFPointNode lastSecondNode =m_polyLine.getNextNode(nodeCount-1,true);
//now, all of the new ports position have been decided,
//but consider all of the node currently moved may no longer exist,
//so we started to check and correct the new value of each port.
for (int i=m_portList.size()-1; i>=0; i--){
try{
port =(Port)m_portList.getByIndex(i);
}catch(Exception e){
m_logger.error("finishMovePolyLineNode:"+e);
break;
}
firstPoint =port.getFirstPoint();
secondPoint =port.getSecondPoint();
portPoint =port.getPortPoint();
if (port.getPortType()==Port.PORTTYPE_DEFAULT){
//only two default port for a polyline.
JFPointNode node1,node2;
//port index=0: default start port, port index=1: default end port.
if (i==0){
//the start port.
node1 =firstNode;
node2 =secondNode;
}else{
//the end port.
node1 =lastFirstNode;
node2 =lastSecondNode;
}
port.setFirstPoint(node1);
port.setSecondPoint(node2);
port.setPortPoint(node1);
}else{
LineSeg line =m_polyLine.pickLine(portPoint.getX(),portPoint.getY(),GeomConst.PICK_OFFSET,firstPoint,secondPoint);
if (line==null){
//if this port is no longer on the poly line, we remove it silently here.
m_portList.removePort(port);
}else {
JFPoint newPortPoint =line.uprightFoot(portPoint.getX(),portPoint.getY());
JFPoint pnt1=line.getPoint1();
JFPoint pnt2=line.getPoint2();
if (!newPortPoint.middleOf(pnt1,pnt2)){
if(newPortPoint.distance(pnt1)<newPortPoint.distance(pnt2))
newPortPoint.setValue(pnt1);
else
newPortPoint.setValue(pnt2);
}
port.setFirstPoint(line.getPoint1());
port.setSecondPoint(line.getPoint2());
port.setPortPoint(newPortPoint);
}
}
}
}
/**
* finish move/adjust a node of current object.
*
* @param node Currently moving node.
*
* @param x, y New position of this node.
*
* @param g current drawing canvas.
*
*/
public void finishMoveNode(Node node, double x, double y,Graphics g){
if (node!=null){
finishMoveLabel();
moveNode(node,x,y,g);
int index =m_nodeList.getIndexByObjectId(node.getObjectId());
if (index<0) return;
m_polyLine.finishDrawing();
initNodes();
finishMovePolyLineNode(new JFPoint(x,y));
m_portList.setZoomScale(getZoomScale());
if (g!=null)
draw(g,false);
}
}
/**
* Move/adjust a node of current object.
*
* @param node Currently moving node.
*
* @param x, y new position of this node.
*
* @param g current drawing canvas.
*
*/
public void moveNode(Node node, double x, double y,Graphics g){
if (node!=null){
double zoom =getZoomScale();
int index =m_nodeList.getIndexByObjectId(node.getObjectId());
if (index<0) return;
JFPointNode movingNode =m_polyLine.getNode(index);
movingNode.setValue(x,y,JFPointNode.NODETYPE_END);
if (g!=null){
JFPointNode priorNode =m_polyLine.getNextNode(index,true);
JFPointNode nextNode =m_polyLine.getNextNode(index,false);
float x1=0,y1=0,x2=0,y2=0;
GeneralPath line= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
if (priorNode!=null){
x1 =(float)(priorNode.getX() * zoom);
y1 =(float)(priorNode.getY() * zoom);
x2 =(float)(movingNode.getX() * zoom);
y2 =(float)(movingNode.getY() * zoom);
line.moveTo(x1,y1);
line.lineTo(x2,y2);
}
if (nextNode!=null){
x1 =(float)(movingNode.getX() * zoom);
y1 =(float)(movingNode.getY() * zoom);
x2 =(float)(nextNode.getX() * zoom);
y2 =(float)(nextNode.getY() * zoom);
line.moveTo(x1,y1);
line.lineTo(x2,y2);
}
((Graphics2D)g).draw(line);
}
node.setXOffset(x);
node.setYOffset(y);
}
}
/**
* Move/adjust a port of current object.
* A move port event will always occured by relative objects' moving event.
*
* @param port Currently moving port.
* @param x, y new position of this port.
* @return True if successfully moved, false otherwise.
*
*/
public boolean movePort(Port port, double x, double y){
if (port==null || port.getParentId()!=getObjectId())
return false;
//not allowed to move customized ports.
if (port.getPortType()==Port.PORTTYPE_CUSTOM)
return false;
int portIndex =m_portList.getIndex(port);
PolyNode node;
PolyNode bakNode; //baknode is the opposite node of node above.
JFPoint bakNodePos=new JFPoint();
try{
if (portIndex==0){
//the start node.
node =(PolyNode)m_nodeList.getByIndex(0);
bakNode =(PolyNode)m_nodeList.getByIndex(m_nodeList.size()-1);
bakNodePos.setValue(bakNode.getNodePoint());
}else if (portIndex==1){
//the end node.
node =(PolyNode)m_nodeList.getByIndex(m_nodeList.size()-1);
bakNode =(PolyNode)m_nodeList.getByIndex(0);
bakNodePos.setValue(bakNode.getNodePoint());
}else{
return false;
}
}catch(Exception e){
return false;
}
//move the port relative nodes will move both nodes and ports.
startMoveNode(node);
moveNode(node,x,y,null);
finishMoveNode(node,x,y,null);
//if the bak node(opposite node of the node above) was moved, here to move back it.
if (!bakNode.getNodePoint().equals(bakNodePos,true)){
startMoveNode(bakNode);
moveNode(bakNode,bakNodePos.getX(),bakNodePos.getY(),null);
finishMoveNode(bakNode,bakNodePos.getX(),bakNodePos.getY(),null);
}
return true;
}
/**
* Finish drawing object.
*
*/
public boolean finishDrawing(){
m_polyLine.finishDrawing();
initNodes();
initPorts();
initLabel();
return 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());
buf.append("\n<arrowFormat>");
buf.append(m_arrow.toString());
buf.append("\n<lineFormat>");
buf.append(m_lineFormat.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 JFLine();
}
/**
* 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;
}
JFLine line =(JFLine) obj;
line.m_polyLine =(PolyLine)m_polyLine.clone();
line.initNodes();
line.m_arrow.setValue(m_arrow);
line.m_lineFormat.setValue(m_lineFormat);
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_arrow.hashCode()^
m_lineFormat.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 JFLine))
return false;
JFLine line= (JFLine)obj;
return (line.m_arrow.equals(m_arrow))&&
(line.m_lineFormat.equals(m_lineFormat));
}
/**
* 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);
m_arrow.toDOM(element,version);
m_lineFormat.toDOM(element,version);
}
/**
* 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);
m_arrow.fromDOM(element.getChild(m_arrow.getXMLTag()),version);
m_lineFormat.fromDOM(element.getChild(m_lineFormat.getXMLTag()),version);
recallNodes();
initNodes();
}
/**
* 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);
m_arrow.saveToStream(stream,version);
m_lineFormat.saveToStream(stream,version);
}
/**
* 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_arrow.loadFromStream(stream,false,version);
m_lineFormat.loadFromStream(stream,false,version);//don't skip head here
recallNodes();
initNodes();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?