001/* The node controller for icons of attributes
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.Color;
031
032import javax.swing.Action;
033
034import diva.canvas.Figure;
035import diva.graph.GraphController;
036import diva.graph.JGraph;
037import diva.gui.GUIUtilities;
038import ptolemy.actor.gui.ColorAttribute;
039import ptolemy.actor.gui.Configuration;
040import ptolemy.kernel.util.IllegalActionException;
041import ptolemy.kernel.util.NamedObj;
042import ptolemy.vergil.basic.BasicGraphController;
043import ptolemy.vergil.basic.CustomizeDocumentationAction;
044import ptolemy.vergil.basic.GetDocumentationAction;
045import ptolemy.vergil.basic.IconController;
046import ptolemy.vergil.basic.RemoveCustomDocumentationAction;
047import ptolemy.vergil.toolbox.MenuActionFactory;
048import ptolemy.vergil.toolbox.MoveAction;
049
050///////////////////////////////////////////////////////////////////
051//// AttributeController
052
053/**
054 This class provides interaction with nodes that represent Ptolemy II
055 attributes.  It provides a double click binding and context menu
056 entry to edit the parameters of the node ("Configure") and a
057 command to get documentation.
058 It can have one of two access levels, FULL or PARTIAL.
059 If the access level is FULL, the the context menu also
060 contains a command to rename the node.
061
062 @author Steve Neuendorffer and Edward A. Lee
063 @version $Id$
064 @since Ptolemy II 2.0
065 @Pt.ProposedRating Red (eal)
066 @Pt.AcceptedRating Red (johnr)
067 */
068public class AttributeController extends IconController {
069    /** Create an attribute controller associated with the specified graph
070     *  controller.  The attribute controller is given full access.
071     *  @param controller The associated graph controller.
072     */
073    public AttributeController(GraphController controller) {
074        this(controller, FULL);
075    }
076
077    /** Create an attribute controller associated with the specified graph
078     *  controller.
079     *  @param controller The associated graph controller.
080     *  @param access The access level.
081     */
082    public AttributeController(GraphController controller, Access access) {
083        super(controller);
084
085        if (access == FULL) {
086            // Add to the context menu, configure submenu.
087            _renameAction = new RenameDialogAction("Rename");
088            _configureMenuFactory.addAction(_renameAction, "Customize");
089
090            Action[] actions = { _getDocumentationAction,
091                    new CustomizeDocumentationAction(),
092                    new RemoveCustomDocumentationAction() };
093            _menuFactory.addMenuItemFactory(
094                    new MenuActionFactory(actions, "Documentation"));
095
096            // Note that we also have "Send to Back" and "Bring to Front" in
097            // vergil/basic/BasicGraphFrame.java
098            // Derived classes like IOPortController override _moveToFirstDescription()
099            Action[] appearanceActions = {
100                    new MoveAction(_moveToFirstDescription(),
101                            MoveAction.TO_FIRST),
102                    new MoveAction(_moveToLastDescription(),
103                            MoveAction.TO_LAST) };
104            _appearanceMenuActionFactory = new MenuActionFactory(
105                    appearanceActions, "Appearance");
106            _menuFactory.addMenuItemFactory(_appearanceMenuActionFactory);
107
108            _listenToAction = new ListenToAction(
109                    (BasicGraphController) getController(),
110                    _getComponentType());
111            _menuFactory
112                    .addMenuItemFactory(new MenuActionFactory(_listenToAction));
113            _listenToAction.setConfiguration(_configuration);
114
115        }
116    }
117
118    ///////////////////////////////////////////////////////////////////
119    ////                         public methods                    ////
120
121    /** Add hot keys to the actions in the given JGraph.
122     *   It would be better that this method was added higher in the hierarchy. Now
123     *   most controllers
124     *  @param jgraph The JGraph to which hot keys are to be added.
125     */
126    @Override
127    public void addHotKeys(JGraph jgraph) {
128        super.addHotKeys(jgraph);
129        GUIUtilities.addHotKey(jgraph, _renameAction);
130    }
131
132    /** Render the value of the _highlightColor parameter.
133     *  @param namedObj  The NamedObj that contains the _highlightColor
134     *  parameter.
135     *  @param figure the Diva figure that is rendered.
136     */
137    public static void renderHighlight(NamedObj namedObj, Figure figure) {
138        // New way to specify a highlight color.
139        try {
140            ColorAttribute highlightAttribute = (ColorAttribute) namedObj
141                    .getAttribute("_highlightColor", ColorAttribute.class);
142            if (highlightAttribute != null
143                    && !highlightAttribute.getExpression().trim().equals("")) {
144                Color color = highlightAttribute.asColor();
145                AnimationRenderer animationRenderer = new AnimationRenderer(
146                        color);
147                animationRenderer.renderSelected(figure);
148            }
149        } catch (IllegalActionException e) {
150            // Ignore.
151        }
152
153    }
154
155    /** Render the value of the _highlightColor parameter.
156     *  @param namedObj  The NamedObj that contains the _highlightColor
157     *  parameter.
158     *  @param figure the Diva figure that is rendered.
159     */
160    public static void renderDecoratorHighlight(NamedObj namedObj,
161            Figure figure) {
162        // New way to specify a highlight color.
163        try {
164            ColorAttribute highlightAttribute = (ColorAttribute) namedObj
165                    .getAttribute("_decoratorHighlightColor",
166                            ColorAttribute.class);
167            if (highlightAttribute != null
168                    && !highlightAttribute.getExpression().trim().equals("")) {
169                Color color = highlightAttribute.asColor();
170                AnimationRenderer decoratorRenderer = new AnimationRenderer(
171                        color, 0.2f);
172                decoratorRenderer.renderSelected(figure);
173            }
174        } catch (IllegalActionException e) {
175            // Ignore.
176        }
177
178    }
179
180    /** Set the configuration.  This is used in derived classes to
181     *  to open files (such as documentation).  The configuration is
182     *  is important because it keeps track of which files are already
183     *  open and ensures that there is only one editor operating on the
184     *  file at any one time.
185     *  @param configuration The configuration.
186     */
187    @Override
188    public void setConfiguration(Configuration configuration) {
189        super.setConfiguration(configuration);
190        _getDocumentationAction.setConfiguration(configuration);
191        _listenToAction.setConfiguration(_configuration);
192    }
193
194    ///////////////////////////////////////////////////////////////////
195    ////                         protected methods                 ////
196
197    /**
198     * Get the class label of the component.
199     * @return the class label of the component.
200     */
201    protected String _getComponentType() {
202        return "Attribute";
203    }
204
205    /** The text used in the MoveAction.TO_FIRST action menu choice.
206     *  Derived classes like IOPortController change this.
207     *  @return the text.
208     */
209    protected String _moveToFirstDescription() {
210        return "Send to Back";
211    }
212
213    /** The text used in the MoveAction.TO_LAST action menu choice.
214     *  Derived classes like IOPortController change this.
215     *  @return the text.
216     */
217    protected String _moveToLastDescription() {
218        return "Bring to Front";
219    }
220
221    ///////////////////////////////////////////////////////////////////
222    ////                     public members                        ////
223
224    /** Indicator to give full access to the attribute. */
225    public static final Access FULL = new Access();
226
227    /** Indicator to give partial access to the attribute. */
228    public static final Access PARTIAL = new Access();
229
230    ///////////////////////////////////////////////////////////////////
231    ////                     protected members                     ////
232
233    /** The appearance menu factory. */
234    protected MenuActionFactory _appearanceMenuActionFactory;
235
236    /** Action to listen to debug messages. */
237    protected ListenToAction _listenToAction;
238
239    /** Action to launch rename dialog. */
240    protected RenameDialogAction _renameAction;
241
242    ///////////////////////////////////////////////////////////////////
243    ////                     private members                       ////
244
245    /** The "get documentation" action. */
246    private GetDocumentationAction _getDocumentationAction = new GetDocumentationAction();
247
248    ///////////////////////////////////////////////////////////////////
249    ////                         inner classes                     ////
250
251    /** A static enumerator for constructor arguments. */
252    protected static class Access {
253    }
254
255}