001/* KIELER Bend Points with Manhattan Geometry Link. 002 003 Copyright (c) 1998-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.vergil.actor; 029 030import java.awt.geom.GeneralPath; 031import java.awt.geom.Point2D; 032import java.util.List; 033 034import diva.canvas.Site; 035import ptolemy.kernel.Relation; 036import ptolemy.vergil.actor.LayoutHint.LayoutHintItem; 037import ptolemy.vergil.kernel.Link; 038 039/////////////////////////////////////////////////////////////////// 040////KielerLayoutConnector 041 042/** 043 * An extension to LinkManhattanRouter supporting bend points for routing links 044 * provided by the corresponding relation. These bend points can be set by any 045 * mechanism, e.g. the KIELER dataflow layout. The latter is currently used by 046 * the automatic layout mechanism that simply adds such bend point layout hints 047 * to relations. 048 * 049 * @author Christian Motika 050 * @see ptolemy.vergil.modal.KielerLayoutArcConnector 051 * @since Ptolemy II 10.0 052 * @Pt.ProposedRating Red (cmot) 053 */ 054 055public class KielerLayoutConnector extends LinkManhattanConnector { 056 057 /////////////////////////////////////////////////////////////////// 058 //// public methods //// 059 060 /** 061 * Construct a new connector with the given tail and head for the specified 062 * link. The head and tail sites may be representative sites for multiport, 063 * in which case they are not necessarily the ones returned by getHeadSite() 064 * or getTailSite(). Those methods will return new sites as needed to ensure 065 * that each each connection is to its own site. 066 * 067 * @param tail The tail site. 068 * @param head The head site. 069 * @param link The link. 070 */ 071 public KielerLayoutConnector(Site tail, Site head, Link link) { 072 super(tail, head, link); 073 } 074 075 /** 076 * Tell the connector to route itself between the current positions of the 077 * head and tail sites. If bend points are available, draw the line with 078 * these instead. Delete bend point information if modification detected 079 * (i.e., movement of one or the other end of a link). 080 */ 081 @Override 082 public void route() { 083 // Parse the bend points if existing. 084 List<Point2D> bendPointList = null; 085 Object object = this.getUserObject(); 086 Link link = null; 087 Relation relation = null; 088 LayoutHintItem layoutHintItem = null; 089 boolean considerBendPoints = false; 090 if (object instanceof Link) { 091 link = (Link) object; 092 relation = link.getRelation(); 093 // relation may be null if a new link is currently dragged from some port 094 if (relation != null) { 095 LayoutHint layoutHint = (LayoutHint) relation 096 .getAttribute("_layoutHint"); 097 if (layoutHint != null) { 098 layoutHintItem = layoutHint 099 .getLayoutHintItem(link.getHead(), link.getTail()); 100 if (layoutHintItem != null) { 101 // Bend points are always considered while a layout operation is 102 // in progress to keep them from being removed. This is not quite 103 // thread-safe, but should work since no more than one MomlChangeRequest 104 // is executed at a given time, and those are the only things that 105 // could trigger a problem with this code. 106 considerBendPoints = _layoutInProgress 107 || layoutHintItem.revalidate(); 108 if (considerBendPoints) { 109 bendPointList = layoutHintItem.getBendPointList(); 110 } else { 111 layoutHint.removeLayoutHintItem(layoutHintItem); 112 } 113 } 114 } 115 } 116 } 117 118 if (considerBendPoints) { 119 repaint(); 120 121 GeneralPath path = new GeneralPath(); 122 123 // we need the "real" start and end points, i.e. the anchor points on the sites 124 Point2D[] startEnd = KielerLayoutUtil.getHeadTailPoints(this, 125 bendPointList); 126 double startX = startEnd[0].getX(); 127 double startY = startEnd[0].getY(); 128 double previousX = startX; 129 double previousY = startY; 130 double endX = startEnd[1].getX(); 131 double endY = startEnd[1].getY(); 132 133 // Start drawing the line. 134 // Under Java 1.5, we only have moveTo(float, float). 135 path.moveTo((float) startX, (float) startY); 136 137 // Add the start point and end point to the bendPointList in 138 // order to get the curveTo-effect working. 139 bendPointList.add(0, new Point2D.Double(startX, startY)); 140 bendPointList.add(new Point2D.Double(endX, endY)); 141 142 for (int i = 1; i <= bendPointList.size() - 1; i++) { 143 int i1 = i; 144 int i0 = i - 1; 145 // Coverity points out that i0 can never be less than 0. 146 //if (i0 < 0) { 147 // i0 = 0; 148 //} 149 if (i0 > bendPointList.size() - 1) { 150 i0 = bendPointList.size() - 1; 151 } 152 153 // Consider triplets of coordinates. 154 double x0 = previousX; 155 double y0 = previousY; 156 double x1 = bendPointList.get(i0).getX(); 157 double y1 = bendPointList.get(i0).getY(); 158 159 double x2 = bendPointList.get(i1).getX(); 160 double y2 = bendPointList.get(i1).getY(); 161 162 // Calculate midpoints. 163 x2 = (x1 + x2) / 2; 164 y2 = (y1 + y2) / 2; 165 166 // First make sure that the radius is not 167 // bigger than half one of the arms of the triplets 168 double d0 = Math 169 .sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); 170 double d1 = Math 171 .sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); 172 double r = Math.min(_bendRadius, d0); 173 r = Math.min(r, d1); 174 175 // The degenerate case of a direct line. 176 if (d0 == 0.0 || d1 == 0.0) { 177 path.lineTo((float) x1, (float) y1); 178 } else { 179 // Next calculate the intermediate points 180 // that define the bend. 181 double intX0 = x1 + r / d0 * (x0 - x1); 182 double intY0 = y1 + r / d0 * (y0 - y1); 183 double intX1 = x1 + r / d1 * (x2 - x1); 184 double intY1 = y1 + r / d1 * (y2 - y1); 185 186 // Next draw the line from the previous 187 // coord to the intermediate coord, and 188 // curve around the corner. 189 path.lineTo((float) intX0, (float) intY0); 190 path.curveTo((float) x1, (float) y1, (float) x1, (float) y1, 191 (float) intX1, (float) intY1); 192 previousX = x2; 193 previousY = y2; 194 } 195 } 196 197 // Finally close the last segment with a line. 198 // Under Java 1.5, we only have moveTo(float, float). 199 path.lineTo((float) endX, (float) endY); 200 201 // Now set the shape. 202 setShape(path); 203 204 // Move the label. 205 int count = bendPointList.size(); 206 // Pick a location for the label in the middle of the connector. 207 Point2D point1 = bendPointList.get(count / 2 - 1); 208 Point2D point2 = bendPointList.get(count / 2); 209 _labelLocation = new Point2D.Double( 210 (point1.getX() + point2.getX()) / 2, 211 (point1.getY() + point2.getY()) / 2); 212 repositionLabel(); 213 214 repaint(); 215 } else { 216 // In this case we have no bend point annotations available so 217 // we use the normal draw functionality. 218 super.route(); 219 } 220 } 221 222 /** 223 * Notifies layout connections that a layout is in progress, which stops them 224 * from deciding to remove layout hints from relations. Without this mechanism, 225 * it can happen that layout hints get removed seemingly at random. This is 226 * caused by layout connectors thinking that one actor in a relation is moved 227 * during the application of the layout results. This in turn triggers the 228 * corresponding layout hint to be viewed as being invalid, and consequently to 229 * be removed. 230 * <p> 231 * A call to this method with the parameter value {@code true} must always be 232 * followed by a call with the parameter value {@code false}.</p> 233 * <p> 234 * <b>Note:</b> This mechanism is not thread-safe! However, since the problem 235 * only occurs while a layout result is being applied through a 236 * {@code MoMLChangeRequest} (of which only one is ever being executed at a 237 * given time), this shouldn't be a problem.</p> 238 * 239 * @param inProgress {@code true} if a layout result is currently being applied. 240 */ 241 public static void setLayoutInProgress(boolean inProgress) { 242 _layoutInProgress = inProgress; 243 } 244 245 /////////////////////////////////////////////////////////////////// 246 //// private variables //// 247 248 /** 249 * The radius for filleting the corners of the connector. 250 */ 251 private double _bendRadius = 10; 252 253 /** 254 * Whether automatic layout is currently in progress. If so, no layout hints 255 * are removed. 256 */ 257 private static boolean _layoutInProgress = false; 258 259}