001/* Action to edit a custom icon. 002 003 Copyright (c) 2003-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.toolbox; 029 030import java.awt.event.ActionEvent; 031import java.util.Iterator; 032import java.util.List; 033 034import ptolemy.actor.gui.Configuration; 035import ptolemy.kernel.util.ChangeRequest; 036import ptolemy.kernel.util.NamedObj; 037import ptolemy.util.MessageHandler; 038import ptolemy.vergil.icon.EditorIcon; 039import ptolemy.vergil.icon.XMLIcon; 040 041/////////////////////////////////////////////////////////////////// 042//// EditIconAction 043 044/** Action to edit a custom icon. 045 @author Edward A. Lee 046 @version $Id$ 047 @since Ptolemy II 4.0 048 @Pt.ProposedRating Red (eal) 049 @Pt.AcceptedRating Red (johnr) 050 */ 051@SuppressWarnings("serial") 052public class EditIconAction extends FigureAction { 053 054 /** Create an action to edit an icon. */ 055 public EditIconAction() { 056 super("Edit Custom Icon"); 057 058 // For some inexplicable reason, the I key doesn't work here. 059 // putValue(GUIUtilities.ACCELERATOR_KEY, 060 // KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK)); 061 } 062 063 /////////////////////////////////////////////////////////////////// 064 //// public methods //// 065 066 /** Process the edit icon command. 067 * @param e The event. 068 */ 069 @Override 070 public void actionPerformed(ActionEvent e) { 071 if (_configuration == null) { 072 MessageHandler.error("Cannot edit icon without a configuration."); 073 return; 074 } 075 076 // Determine which entity was selected for the action. 077 super.actionPerformed(e); 078 079 final NamedObj object = getTarget(); 080 081 // Do this as a change request since it may add a new icon. 082 // FIXME: This is not undoable. 083 ChangeRequest request = new ChangeRequest(this, "Edit Custom Icon") { 084 @Override 085 protected void _execute() throws Exception { 086 EditorIcon icon = null; 087 List iconList = object.attributeList(EditorIcon.class); 088 089 if (iconList.size() > 0) { 090 // Get the last icon. 091 icon = (EditorIcon) iconList.get(iconList.size() - 1); 092 } 093 094 if (icon == null) { 095 icon = new EditorIcon(object, "_icon"); 096 } else if (icon.getDerivedLevel() < Integer.MAX_VALUE 097 || !icon.isPersistent()) { 098 099 // The icon is either implied by the class 100 // or is not persistent (XMLIcon will not be 101 // persistent as it is derived from the 102 // _iconDescription parameter). 103 104 // There is an icon currently that is not custom. 105 // Without trashing the _iconDescription, we can remove 106 // this icon and replace it. 107 icon.setContainer(null); 108 icon = new EditorIcon(object, "_icon"); 109 110 // Propagate this to derived objects, being 111 // careful to not trash their custom icons 112 // if they have them. However, there is a trickiness. 113 // They may not have a custom icon, but rather have 114 // an instance of XMLIcon. We have to remove that 115 // first. 116 Iterator derivedObjects = object.getDerivedList() 117 .iterator(); 118 119 while (derivedObjects.hasNext()) { 120 NamedObj derived = (NamedObj) derivedObjects.next(); 121 122 // See whether it has an icon. 123 EditorIcon derivedIcon = null; 124 List derivedIconList = derived 125 .attributeList(EditorIcon.class); 126 127 if (derivedIconList.size() > 0) { 128 // Get the last icon. 129 derivedIcon = (EditorIcon) derivedIconList 130 .get(derivedIconList.size() - 1); 131 } 132 133 if (derivedIcon instanceof XMLIcon) { 134 // There is an icon currently that is not custom. 135 // Without trashing the _iconDescription, we can remove 136 // this icon and replace it. 137 derivedIcon.setContainer(null); 138 } 139 } 140 141 // Now it is safe to propagate. 142 icon.propagateExistence(); 143 } 144 145 _configuration.openModel(icon); 146 } 147 }; 148 149 object.requestChange(request); 150 } 151 152 /** Specify the configuration. This has to be called with a 153 * non-null argument for this action to work. 154 * @param configuration The configuration. 155 */ 156 public void setConfiguration(Configuration configuration) { 157 _configuration = configuration; 158 } 159 160 /////////////////////////////////////////////////////////////////// 161 //// private members //// 162 // The configuration. 163 private Configuration _configuration; 164}