001/* Action to edit a custom icon. 002 003 Copyright (c) 2006-2014 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.toolbox; 029 030import java.awt.event.ActionEvent; 031 032import ptolemy.data.BooleanToken; 033import ptolemy.data.IntToken; 034import ptolemy.data.Token; 035import ptolemy.data.expr.Parameter; 036import ptolemy.kernel.util.Attribute; 037import ptolemy.kernel.util.IllegalActionException; 038import ptolemy.kernel.util.NamedObj; 039import ptolemy.moml.MoMLChangeRequest; 040 041/////////////////////////////////////////////////////////////////// 042//// RotateOrFlipPorts 043 044/** 045 Action to rotate or flip ports. 046 What exactly gets done depends on the constructor arguments. 047 @author Edward A. Lee 048 @version $Id$ 049 @since Ptolemy II 5.2 050 @Pt.ProposedRating Red (eal) 051 @Pt.AcceptedRating Red (johnr) 052 */ 053@SuppressWarnings("serial") 054public class RotateOrFlipPorts extends FigureAction { 055 056 /** Create an action to rotate the ports. 057 * @param direction One of CLOCKWISE, COUNTERCLOCKWISE, FLIP_HORIZONTAL, or FLIP_VERTICAL. 058 * @param label The label to put in the menu. 059 */ 060 public RotateOrFlipPorts(int direction, String label) { 061 super(label); 062 _direction = direction; 063 } 064 065 /////////////////////////////////////////////////////////////////// 066 //// public variables //// 067 068 /** Indicator to rotate clockwise. */ 069 public static final int CLOCKWISE = 0; 070 071 /** Indicator to rotate counterclockwise. */ 072 public static final int COUNTERCLOCKWISE = 1; 073 074 /** Indicator to flip ports horizontally. */ 075 public static final int FLIP_HORIZONTAL = 2; 076 077 /** Indicator to flip ports vertically. */ 078 public static final int FLIP_VERTICAL = 3; 079 080 /////////////////////////////////////////////////////////////////// 081 //// public methods //// 082 083 /** Process the rotate command. 084 * @param event The event. 085 */ 086 @Override 087 public void actionPerformed(ActionEvent event) { 088 // Determine which entity was selected for the action. 089 super.actionPerformed(event); 090 091 final NamedObj object = getTarget(); 092 093 String moml = ""; 094 if (_direction == CLOCKWISE || _direction == COUNTERCLOCKWISE) { 095 int rotation = 90; 096 if (_direction == COUNTERCLOCKWISE) { 097 rotation = -90; 098 } 099 // First determine whether the ports are already rotated. 100 try { 101 Attribute attribute = object.getAttribute("_rotatePorts"); 102 if (attribute instanceof Parameter) { 103 Token token = ((Parameter) attribute).getToken(); 104 if (token instanceof IntToken) { 105 rotation += ((IntToken) token).intValue(); 106 } 107 } 108 } catch (IllegalActionException e) { 109 // Ignore and assume there is no prior rotation. 110 } 111 moml = "<property name=\"_rotatePorts\" class=\"ptolemy.data.expr.Parameter\" value=\"" 112 + rotation + "\"/>"; 113 } else if (_direction == FLIP_VERTICAL) { 114 // First determine whether the ports are already flipped. 115 boolean flipOn = true; 116 try { 117 Attribute attribute = object.getAttribute("_flipPortsVertical"); 118 if (attribute instanceof Parameter) { 119 Token token = ((Parameter) attribute).getToken(); 120 if (token instanceof BooleanToken) { 121 flipOn = !((BooleanToken) token).booleanValue(); 122 } 123 } 124 } catch (IllegalActionException e) { 125 // Ignore and assume there is no prior flip. 126 } 127 moml = "<property name=\"_flipPortsVertical\" class=\"ptolemy.data.expr.Parameter\" value=\"" 128 + flipOn + "\"/>"; 129 } else if (_direction == FLIP_HORIZONTAL) { 130 // First determine whether the ports are already flipped. 131 boolean flipOn = true; 132 try { 133 Attribute attribute = object 134 .getAttribute("_flipPortsHorizontal"); 135 if (attribute instanceof Parameter) { 136 Token token = ((Parameter) attribute).getToken(); 137 if (token instanceof BooleanToken) { 138 flipOn = !((BooleanToken) token).booleanValue(); 139 } 140 } 141 } catch (IllegalActionException e) { 142 // Ignore and assume there is no prior flip. 143 } 144 moml = "<property name=\"_flipPortsHorizontal\" class=\"ptolemy.data.expr.Parameter\" value=\"" 145 + flipOn + "\"/>"; 146 } 147 148 MoMLChangeRequest request = new MoMLChangeRequest(this, object, moml); 149 request.setUndoable(true); 150 object.requestChange(request); 151 } 152 153 /////////////////////////////////////////////////////////////////// 154 //// private members //// 155 156 /** The direction requested in the constructor. */ 157 private int _direction; 158}