001/* An attribute shown as an arrow. 002 003 Copyright (c) 2008-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.kernel.attributes; 029 030import java.awt.Shape; 031import java.awt.geom.AffineTransform; 032 033import diva.util.java2d.Polygon2D; 034import ptolemy.data.DoubleToken; 035import ptolemy.data.expr.Parameter; 036import ptolemy.data.type.BaseType; 037import ptolemy.kernel.util.Attribute; 038import ptolemy.kernel.util.IllegalActionException; 039import ptolemy.kernel.util.NameDuplicationException; 040import ptolemy.kernel.util.NamedObj; 041import ptolemy.kernel.util.Workspace; 042 043/////////////////////////////////////////////////////////////////// 044//// ArrowAttribute 045 046/** 047 An attribute shown as an arrow. The length and width of the arrow head can be 048 set with the <i>arrowLength</i> and <i>arrowWidth</i> parameters. 049 050 @author Thomas Huining Feng 051 @version $Id$ 052 @since Ptolemy II 8.0 053 @Pt.ProposedRating Red (tfeng) 054 @Pt.AcceptedRating Red (tfeng) 055 */ 056public class ArrowAttribute extends LineAttribute { 057 058 /** Construct an attribute with the given name contained by the 059 * specified container. The container argument must not be null, or a 060 * NullPointerException will be thrown. This attribute will use the 061 * workspace of the container for synchronization and version counts. 062 * If the name argument is null, then the name is set to the empty 063 * string. Increment the version of the workspace. 064 * @param container The container. 065 * @param name The name of this attribute. 066 * @exception IllegalActionException If the attribute is not of an 067 * acceptable class for the container, or if the name contains a period. 068 * @exception NameDuplicationException If the name coincides with 069 * an attribute already in the container. 070 */ 071 public ArrowAttribute(NamedObj container, String name) 072 throws IllegalActionException, NameDuplicationException { 073 super(container, name); 074 075 arrowLength = new Parameter(this, "arrowLength"); 076 arrowLength.setTypeEquals(BaseType.DOUBLE); 077 arrowLength.setExpression("16.0"); 078 079 arrowWidth = new Parameter(this, "arrowWidth"); 080 arrowWidth.setTypeEquals(BaseType.DOUBLE); 081 arrowWidth.setExpression("10.0"); 082 083 _icon.setFillColor(lineColor.asColor()); 084 } 085 086 /////////////////////////////////////////////////////////////////// 087 //// parameters //// 088 089 /** Length of the arrow head. Default to 16.0 */ 090 public Parameter arrowLength; 091 092 /** Width of the arrow head. Default to 10.0. */ 093 public Parameter arrowWidth; 094 095 /////////////////////////////////////////////////////////////////// 096 //// public methods //// 097 098 /** React to a changes in the attributes by changing the icon. 099 * @param attribute The attribute that changed. 100 * @exception IllegalActionException If the change is not acceptable 101 * to this container (should not be thrown). 102 */ 103 @Override 104 public void attributeChanged(Attribute attribute) 105 throws IllegalActionException { 106 if (attribute == x || attribute == y || attribute == arrowLength 107 || attribute == arrowWidth || attribute == lineColor) { 108 double xValue = ((DoubleToken) x.getToken()).doubleValue(); 109 double yValue = ((DoubleToken) y.getToken()).doubleValue(); 110 double arrowLengthValue = ((DoubleToken) arrowLength.getToken()) 111 .doubleValue(); 112 double arrowWidthValue = ((DoubleToken) arrowWidth.getToken()) 113 .doubleValue(); 114 _icon.setShape(_createArrow(xValue, yValue, arrowLengthValue, 115 arrowWidthValue)); 116 _icon.setLineColor(lineColor.asColor()); 117 _icon.setFillColor(lineColor.asColor()); 118 } else { 119 super.attributeChanged(attribute); 120 } 121 } 122 123 /** Clone the object into the specified workspace. The new object is 124 * <i>not</i> added to the directory of that workspace (you must do this 125 * yourself if you want it there). 126 * The result is an object with no container. 127 * @param workspace The workspace for the cloned object. 128 * @exception CloneNotSupportedException Not thrown in this base class 129 * @return The new Attribute. 130 */ 131 @Override 132 public Object clone(Workspace workspace) throws CloneNotSupportedException { 133 ArrowAttribute newObject = (ArrowAttribute) super.clone(workspace); 134 135 // The cloned icon ends up referring to the clonee's shape. 136 // We need to fix that here. 137 try { 138 newObject._icon.attributeChanged(x); 139 } catch (IllegalActionException e) { 140 // Should not occur. 141 throw new CloneNotSupportedException(e.getMessage()); 142 } 143 return newObject; 144 } 145 146 /////////////////////////////////////////////////////////////////// 147 //// protected methods //// 148 149 /** Return a default arrow. 150 * @return An arrow. 151 */ 152 @Override 153 protected Shape _getDefaultShape() { 154 return _createArrow(40.0, 40.0, 16.0, 10.0); 155 } 156 157 /////////////////////////////////////////////////////////////////// 158 //// private methods //// 159 160 /** Create an arrow with the given x, y, arrow length and arrow width 161 * values. 162 * @param x The x extent. 163 * @param y The y extent. 164 * @param arrowLength Length of the arrow head. 165 * @param arrowWidth Width of the arrow head. 166 * @return An arrow with the given x, y, arrow length and arrow width 167 * values. 168 */ 169 private static Shape _createArrow(double x, double y, double arrowLength, 170 double arrowWidth) { 171 double halfWidth = arrowWidth / 2.0; 172 Polygon2D polygon = new Polygon2D.Double(); 173 polygon.moveTo(0.0, halfWidth); 174 polygon.lineTo(arrowLength + 3.0, 0.0); 175 polygon.lineTo(arrowLength, halfWidth); 176 polygon.lineTo(Math.sqrt(x * x + y * y), halfWidth); 177 polygon.lineTo(arrowLength, halfWidth); 178 polygon.lineTo(arrowLength + 3.0, arrowWidth); 179 polygon.closePath(); 180 181 AffineTransform transform = AffineTransform 182 .getRotateInstance(Math.atan2(y, x)); 183 polygon.transform(transform); 184 return polygon; 185 } 186}