001/* The node controller for states.
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.kernel;
029
030import java.awt.event.ActionEvent;
031import java.util.List;
032
033import diva.graph.GraphController;
034import ptolemy.data.BooleanToken;
035import ptolemy.data.expr.Parameter;
036import ptolemy.kernel.util.ChangeRequest;
037import ptolemy.kernel.util.NamedObj;
038import ptolemy.moml.MoMLChangeRequest;
039import ptolemy.util.MessageHandler;
040import ptolemy.vergil.icon.EditorIcon;
041import ptolemy.vergil.toolbox.FigureAction;
042
043///////////////////////////////////////////////////////////////////
044//// AttributeWithIconController
045
046/**
047 This class provides interaction with nodes that represent that can have
048 custom icons. It adds context menu items to edit the custom icons.
049
050 @author Edward A. Lee
051 @version $Id$
052 @since Ptolemy II 10.0
053 @Pt.ProposedRating Red (eal)
054 @Pt.AcceptedRating Red (johnr)
055 */
056public class AttributeWithIconController extends AttributeController {
057
058    /** Create a controller associated with the specified graph
059     *  controller.
060     *  @param controller The associated graph controller.
061     */
062    public AttributeWithIconController(GraphController controller) {
063        this(controller, FULL);
064    }
065
066    /** Create a controller associated with the specified graph
067     *  controller.
068     *  @param controller The associated graph controller.
069     *  @param access The access level.
070     */
071    public AttributeWithIconController(GraphController controller,
072            Access access) {
073        super(controller, access);
074        _appearanceMenuActionFactory.addAction(_editIconAction);
075        _appearanceMenuActionFactory.addAction(_removeIconAction);
076    }
077
078    ///////////////////////////////////////////////////////////////////
079    ////                         protected variables               ////
080
081    /** The edit custom icon action. */
082    protected EditIconAction _editIconAction = new EditIconAction();
083
084    /** The remove custom icon action. */
085    protected RemoveIconAction _removeIconAction = new RemoveIconAction();
086
087    ///////////////////////////////////////////////////////////////////
088    ////                         inner classes                     ////
089
090    /** Customize the icon of the attribute.
091     */
092    @SuppressWarnings("serial")
093    private class EditIconAction extends FigureAction {
094
095        /** Create an action to edit an icon. */
096        public EditIconAction() {
097            super("Edit Custom Icon");
098        }
099
100        ///////////////////////////////////////////////////////////////////
101        ////                         public methods                    ////
102
103        /** Process the edit icon command.
104         *  @param e The event.
105         */
106        @Override
107        public void actionPerformed(ActionEvent e) {
108            if (_configuration == null) {
109                MessageHandler.error(
110                        "Cannot edit icon without a " + "configuration.");
111                return;
112            }
113
114            // Determine which entity was selected for the action.
115            super.actionPerformed(e);
116
117            final NamedObj object = getTarget();
118
119            // Do this as a change request since it may add a new icon.
120            ChangeRequest request = new ChangeRequest(this,
121                    "Edit Custom Icon") {
122                @Override
123                protected void _execute() throws Exception {
124                    EditorIcon icon = null;
125                    List<EditorIcon> iconList = object
126                            .attributeList(EditorIcon.class);
127                    for (EditorIcon oldIcon : iconList) {
128                        if (oldIcon.getClass().equals(EditorIcon.class)) {
129                            icon = oldIcon;
130                            break;
131                        }
132                    }
133
134                    if (icon == null) {
135                        icon = new EditorIcon(object, "_icon");
136                        Parameter hideName = (Parameter) object
137                                .getAttribute("_hideName");
138                        if (((BooleanToken) hideName.getToken())
139                                .booleanValue()) {
140                            hideName.setToken(BooleanToken.FALSE);
141                        }
142                    }
143
144                    _configuration.openModel(icon);
145                }
146            };
147
148            object.requestChange(request);
149        }
150    }
151
152    /** Action to remove a custom icon.
153     */
154    @SuppressWarnings("serial")
155    private static class RemoveIconAction extends FigureAction {
156
157        /** Create an action to remove the custom icon. */
158        public RemoveIconAction() {
159            super("Remove Custom Icon");
160        }
161
162        ///////////////////////////////////////////////////////////////////
163        ////                         public methods                    ////
164
165        /** Process the remove icon command.
166         *  @param e The event.
167         */
168        @Override
169        public void actionPerformed(ActionEvent e) {
170            super.actionPerformed(e);
171
172            NamedObj object = getTarget();
173            EditorIcon icon = null;
174            List<EditorIcon> iconList = object.attributeList(EditorIcon.class);
175            for (EditorIcon oldIcon : iconList) {
176                if (oldIcon.getClass().equals(EditorIcon.class)) {
177                    icon = oldIcon;
178                    break;
179                }
180            }
181
182            if (icon != null) {
183                String moml = "<deleteProperty name=\"" + icon.getName()
184                        + "\"/>";
185                MoMLChangeRequest request = new MoMLChangeRequest(this, object,
186                        moml);
187                object.requestChange(request);
188            }
189        }
190    }
191}