package pipe.dataLayer; import java.awt.Color; import java.awt.Shape; import java.awt.Point; import java.awt.geom.*; import java.awt.event.*; import java.awt.*; import javax.swing.BorderFactory; /** * Arc - Petri-Net Arc Class * * @see

PNML - Petri-Net XMLSchema (stNet.xsd) * @see

UML - PNML Package

* @version 1.0 * @author James D Bloom */ public class Arc extends PetriNetObject { /** Start X-axis Position */ private Double startPositionX = null; /** Start Y-axis Position */ private Double startPositionY = null; /** End X-axis Position */ private Double endPositionX = null; /** End Y-axis Position */ private Double endPositionY = null; /** Current Marking */ private Integer weighting = null; /** Initial Marking X-axis Offset */ private Double weightingOffsetX = null; /** Initial Marking Y-axis Offset */ private Double weightingOffsetY = null; /** Inscription */ private String inscription = null; /** Inscription X-axis Offset */ private Double inscriptionOffsetX = null; /** Inscription Y-axis Offset */ private Double inscriptionOffsetY = null; /** Arc is of type Line2D.Double */ private Line2D.Double arc = new Line2D.Double(0, 0, 0, 0); private Line2D.Double realarc; private ArrowLabel arrow; /** Members to indicate the different directions that the arc can be drawn in */ public static final int NE = 0; public static final int SE = 1; public static final int SW = 2; public static final int NW = 3; /** Member to indicate which direction this arc was drawn in */ private int quad; /* These fields offset the x and y coordinates accordingly within the component area */ private double startXOffset; private double startYOffset; private double endXOffset; private double endYOffset; /** coordinates for the arrow */ private Double midX; private Double midY; /** for the bounds of the component */ private int _width; private int _height; /** references to the objects this arc connects */ private PlaceTransitionObject source = null; private PlaceTransitionObject target = null; /** * Create Petri-Net Arc object * * @param startPositionXInput Start X-axis Position * @param startPositionYInput Start Y-axis Position * @param endPositionXInput End X-axis Position * @param endPositionYInput End Y-axis Position * @param idInput Arc id * @param sourceInput Arc source * @param targetInput Arc target * @param weightingInput Weighting * @param inscriptionOffsetXInput Inscription X-axis Position * @param inscriptionOffsetYInput Inscription Y-axis Position * @param colorInput Color */ public Arc(double startPositionXInput, double startPositionYInput, double endPositionXInput, double endPositionYInput, String idInput, PlaceTransitionObject sourceInput, PlaceTransitionObject targetInput, int weightingInput, double inscriptionOffsetXInput, double inscriptionOffsetYInput, Color colorInput){ this(startPositionXInput,startPositionYInput,endPositionXInput,endPositionYInput,colorInput); setSource(sourceInput); setTarget(targetInput); weighting = new Integer(weightingInput); inscriptionOffsetX = new Double(inscriptionOffsetXInput); inscriptionOffsetY = new Double(inscriptionOffsetYInput); } /** * Create Petri-Net Arc object * * @param startPositionXInput Start X-axis Position * @param startPositionYInput Start Y-axis Position * @param endPositionXInput End X-axis Position * @param endPositionYInput End Y-axis Position * @param idInput Arc id * @param sourceInput Arc source * @param targetInput Arc target * @param colorInput Color */ public Arc(double startPositionXInput, double startPositionYInput, double endPositionXInput, double endPositionYInput, PlaceTransitionObject sourceInput, PlaceTransitionObject targetInput, String idInput, Color colorInput){ this(startPositionXInput,startPositionYInput,endPositionXInput,endPositionYInput,colorInput); id = idInput; setSource(sourceInput); setTarget(targetInput); } /** * Create Petri-Net Arc object * * @param startPositionXInput Start X-axis Position * @param startPositionYInput Start Y-axis Position * @param endPositionXInput End X-axis Position * @param endPositionYInput End Y-axis Position * @param idInput Arc id * @param colorInput Color */ public Arc(double startPositionXInput, double startPositionYInput, double endPositionXInput, double endPositionYInput, String idInput, Color colorInput){ this(startPositionXInput, startPositionYInput,endPositionXInput,endPositionYInput,colorInput); id = idInput; } /** * Create Petri-Net Arc object * * @param startPositionXInput Start X-axis Position * @param startPositionYInput Start Y-axis Position * @param endPositionXInput End X-axis Position * @param endPositionYInput End Y-axis Position * @param colorInput Color */ public Arc(double startPositionXInput, double startPositionYInput, double endPositionXInput, double endPositionYInput, Color colorInput){ startPositionX = new Double(startPositionXInput); startPositionY = new Double(startPositionYInput); endPositionX = new Double(endPositionXInput); endPositionY = new Double(endPositionYInput); color = colorInput; setLayout(null); setMovable(false); setWeighting(1); // figures out the width and height of the enclosing bounds - this is constant int startx = startPositionX.intValue(); int starty = startPositionY.intValue(); int endx = endPositionX.intValue(); int endy = endPositionY.intValue(); if (startx > endx) _width = startx-endx; else _width = endx-startx; if (starty > endy) _height = starty-endy; else _height = endy-starty; // make sure we can see the arc _width = Math.max(_width,2); _height = Math.max(_height,2); midX = new Double(_width/2); midY = new Double(_height/2); // make some decisions based on the angle of the created arc if ( (startPositionX.doubleValue() <= endPositionX.doubleValue()) && (startPositionY.doubleValue() <= endPositionY.doubleValue()) ) { // the line is going DOWN and RIGHT // System.out.println("down and right, startx" + startx + " starty: " + starty); quad = SE; arc = new Line2D.Double(0,0,(double)_width,(double)_height); doSetBounds(startx,starty); } else if ( (startPositionX.doubleValue() >= endPositionX.doubleValue()) && (startPositionY.doubleValue() <= endPositionY.doubleValue()) ) { // the line is going DOWN and LEFT //System.out.println("down and left"); quad = SW; arc = new Line2D.Double((double)_width,0,0,(double)_height); doSetBounds(startx-_width,endy-_height); } else if ( (startPositionX.doubleValue() <= endPositionX.doubleValue()) && (startPositionY.doubleValue() >= endPositionY.doubleValue()) ) { // the line is going UP and RIGHT //System.out.println("up and right"); quad = NE; arc = new Line2D.Double(0,(double)_height,(double)_width,0); doSetBounds(startx,starty-_height); } else if ( (startPositionX.doubleValue() >= endPositionX.doubleValue()) && (startPositionY.doubleValue() >= endPositionY.doubleValue()) ) { // the line is going UP and LEFT //System.out.println("up and left"); quad = NW; arc = new Line2D.Double((double)_width,(double)_height,0,0); doSetBounds(endx,endy); } } /** Returns quadrant in which the arc is drawn on in the View (M/V/C Design Pattern) * @return Quadrant in which the arc is drawn * */ public int getQuad() { return quad; } /** Sets the bounds of the arc with respect to the enclosing container. * We do this within the arc class as the bounds change according to the arc angle. * @param _startx The x coordinate set in the bounds * @param _starty The y coordinate set in the bounds */ public void doSetBounds(int _startx, int _starty) { setBounds(_startx,_starty,_width,_height); } /** * Create Petri-Net Arc object * */ public Arc(){ } /** * Set id * * @param idInput String value for Arc id; */ public void setId(String idInput) { id = idInput; } /** * Set source * * @param sourceInput PlaceTransitionObject value for Arc source; */ public void setSource(PlaceTransitionObject sourceInput) { source = sourceInput; } /** * Set target * * @param targetInput PlaceTransitionObject value for Arc target; */ public void setTarget(PlaceTransitionObject targetInput) { target = targetInput; } /** * Set X-axis value of start position * * @param startPositionXInput Double value for X-axis of start position */ public void setStartPositionX(double startPositionXInput) { startPositionX = new Double(startPositionXInput); } /** * Set Y-axis value of start position * * @param startPositionYInput Double value for Y-axis of start position */ public void setStartPositionY(double startPositionYInput) { startPositionY = new Double(startPositionYInput); } /** * Set X-axis value of end position * * @param endPositionXInput Double value for X-axis of end position */ public void setEndPositionX(double endPositionXInput) { endPositionX = new Double(endPositionXInput); } /** * Set Y-axis value of end position * * @param endPositionYInput Double value for Y-axis of end position */ public void setEndPositionY(double endPositionYInput) { endPositionY = new Double(endPositionYInput); } /** * Set Weighting * * @param weightingInput String value for Arc weighting; */ public void setWeighting(int weightingInput) { weighting = new Integer(weightingInput); try { if(arrow != null) arrow.getNameLabel().setText(String.valueOf(weighting)); } catch (Exception e) { e.printStackTrace(); } } /** * Set X-axis offset for weighting position * * @param weightingOffsetXInput Double value for weighting X-axis offset */ public void setWeightingOffsetX(double weightingOffsetXInput) { weightingOffsetX = new Double(weightingOffsetXInput); } /** * Set Y-axis offset for weighting position * * @param weightingOffsetYInput Double value for weighting Y-axis offset */ public void setWeightingOffsetY(double weightingOffsetYInput) { weightingOffsetY = new Double(weightingOffsetYInput); } /** * Set inscription * * @param inscriptionInput String value for Arc inscription; */ public void setInscription(String inscriptionInput) { inscription = inscriptionInput; } /** * Set X-axis offset for inscription position * * @param inscriptionOffsetXInput Double value for inscription X-axis offset */ public void setInscriptionOffsetX(double inscriptionOffsetXInput) { inscriptionOffsetX = new Double(inscriptionOffsetXInput); } /** * Set Y-axis offset for inscription position * * @param inscriptionOffsetYInput Double value for inscription Y-axis offset */ public void setInscriptionOffsetY(double inscriptionOffsetYInput) { inscriptionOffsetY = new Double(inscriptionOffsetYInput); } /** * Get id * * @return String value for Arc id; */ public String getId() { if(id != null) { return id; } else { if(source != null && target != null) { return source.getId() + " to " + target.getId(); } else { return ""; } } } /** * Get source returns null if value not yet entered * * @return String value for Arc source; */ public PlaceTransitionObject getSource() { return source; } /** * Get target returns null if value not yet entered * * @return String value for Arc target; */ public PlaceTransitionObject getTarget() { return target; } /** * Get X-axis value of start position * * @return Double value for X-axis of start position */ public double getStartPositionX() { if(startPositionX == null) return 0; return startPositionX.doubleValue(); } /** * Get Y-axis value of start position * * @return Double value for Y-axis of start position */ public double getStartPositionY() { if(startPositionY == null) return 0; return startPositionY.doubleValue(); } /** * Get X-axis value of end position * * @return Double value for X-axis of end position */ public double getEndPositionX() { if(endPositionX == null) return 0; return endPositionX.doubleValue(); } /** * Get Y-axis value of end position * * @return Double value for Y-axis of end position */ public double getEndPositionY() { if(endPositionY == null) return 0; return endPositionY.doubleValue(); } /** * Get Weighting * * @return Integer value for Arc weighting; */ public int getWeighting() { if(weighting == null) return 1; return weighting.intValue(); } /** * Get X-axis offset for weighting position * * @return Double value for weighting X-axis offset */ public double getWeightingOffsetX() { if(weightingOffsetX == null) return 0; return weightingOffsetX.doubleValue(); } /** * Get Y-axis offset for weighting position * * @return Double value for weighting Y-axis offset */ public double getWeightingOffsetY() { if(weightingOffsetY == null) return 0; return weightingOffsetY.doubleValue(); } /** * Get inscription returns null if value not yet entered * * @return String value for Arc inscription; */ public String getInscription() { return inscription; } /** * Get X-axis offset for inscription position * * @return Double value for inscription X-axis offset */ public double getInscriptionOffsetX() { if(inscriptionOffsetX == null) return 0; return inscriptionOffsetX.doubleValue(); } /** * Get Y-axis offset for inscription position * * @return Double value for inscription Y-axis offset */ public double getInscriptionOffsetY() { if(inscriptionOffsetY == null) return 0; return inscriptionOffsetY.doubleValue(); } /** * Get X-axis value of start position * returns null if value not yet entered * * @return Double value for X-axis of start position */ public Double getStartPositionXObject() { return startPositionX; } /** * Get Y-axis value of start position * returns null if value not yet entered * * @return Double value for Y-axis of start position */ public Double getStartPositionYObject() { return startPositionY; } /** * Get X-axis value of end position * returns null if value not yet entered * * @return Double value for X-axis of end position */ public Double getEndPositionXObject() { return endPositionX; } /** * Get Y-axis value of end position * returns null if value not yet entered * * @return Double value for Y-axis of end position */ public Double getEndPositionYObject() { return endPositionY; } /** * Get Weighting returns null if value not yet entered * * * @return Integer value for Arc weighting; */ public Integer getWeightingObject() { return weighting; } /** * Get X-axis offset for weighting position * returns null if value not yet entered * * @return Double value for weighting X-axis offset */ public Double getWeightingOffsetXObject() { return weightingOffsetX; } /** * Get Y-axis offset for weighting position * returns null if value not yet entered * * @return Double value for weighting Y-axis offset */ public Double getWeightingOffsetYObject() { return weightingOffsetY; } /** * Get X-axis offset for inscription position * returns null if value not yet entered * * @return Double value for inscription X-axis offset */ public Double getInscriptionOffsetXObject() { return inscriptionOffsetX; } /** * Get Y-axis offset for inscription position * returns null if value not yet entered * * @return Double value for inscription Y-axis offset */ public Double getInscriptionOffsetYObject() { return inscriptionOffsetY; } /** * Get Shape of PetriNetObject * @return arc */ public Shape getShape(){ return arc; } /** * Modifies end coordinates of arc * @param startX Start X-axis Position * @param startY Start Y-axis Position * @param endX End X-axis Position * @param endY End Y-axis Position */ public void modify(double startX, double startY, double endX, double endY){ startPositionX = new Double(startX); startPositionY = new Double(startY); endPositionX = new Double(endX); endPositionY = new Double(endY); } public void paintComponent(Graphics g) { //System.out.println("painting arc component"); super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setPaint(getColor()); g2.setStroke(new BasicStroke(1.5f)); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.draw(arc); Point start; Point end; } public void mouseDragged(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void updateSize(MouseEvent e) { } public ArrowLabel getArrow() { return arrow; } public void setArrow(ArrowLabel _arrow) { arrow = _arrow; } /** * Returns the depression angle of the arc wrt the y axis of the * coordinate space of the View (M/V/C Design Pattern) * @return Dression Angle of arc. */ public double getArcAngle() { double angle; double divide = (double)_width/(double)_height; angle = Math.atan(divide); angle = Math.toDegrees(angle); return angle; } }