001/* The node controller for objects that offer a configure command.
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.basic;
029
030import java.util.Iterator;
031import java.util.List;
032
033import diva.canvas.event.MouseFilter;
034import diva.canvas.interactor.ActionInteractor;
035import diva.canvas.interactor.SelectionModel;
036import diva.graph.GraphController;
037import diva.graph.NodeInteractor;
038import diva.gui.toolbox.MenuCreator;
039import ptolemy.actor.gui.Configuration;
040import ptolemy.vergil.toolbox.ConfigureAction;
041import ptolemy.vergil.toolbox.MenuActionFactory;
042import ptolemy.vergil.toolbox.PtolemyMenuFactory;
043
044///////////////////////////////////////////////////////////////////
045//// ParameterizedNodeController
046
047/**
048 This class provides interaction with nodes that represent Ptolemy II
049 components with parameters.  It provides a context menu item labeled
050 "Configure" for editing those parameters, and binds double click
051 to invoke the dialog that edits those parameters.
052
053 @author Steve Neuendorffer and Edward A. Lee
054 @version $Id$
055 @since Ptolemy II 2.0
056 @Pt.ProposedRating Red (eal)
057 @Pt.AcceptedRating Red (johnr)
058 */
059public class ParameterizedNodeController extends NamedObjController {
060    /** Create an attribute controller associated with the specified graph
061     *  controller.
062     *  @param controller The associated graph controller.
063     */
064    public ParameterizedNodeController(GraphController controller) {
065        super(controller);
066
067        // Add a menu creator.
068        _menuCreator = new MenuCreator(null);
069        _menuCreator.setMouseFilter(new PopupMouseFilter());
070
071        // FIXME: Why doesn't getNodeInteractor() return a NodeInteractor?
072        NodeInteractor interactor = (NodeInteractor) getNodeInteractor();
073        interactor.addInteractor(_menuCreator);
074
075        // The contents of the menu is determined by the associated
076        // menu factory, which is a protected member of this class.
077        // Derived classes can add menu items to it.
078        _menuFactory = new PtolemyMenuFactory(controller);
079
080        List configsList = Configuration.configurations();
081
082        Configuration config = null;
083        for (Iterator it = configsList.iterator(); it.hasNext();) {
084            config = (Configuration) it.next();
085            if (config != null) {
086                break;
087            }
088        }
089
090        //If a MenuFactory has been defined in the configuration, use this
091        //one; otherwise, use the default Ptolemy one:
092        if (config != null && _contextMenuFactoryCreator == null) {
093            _contextMenuFactoryCreator = (ContextMenuFactoryCreator) config
094                    .getAttribute("contextMenuFactory");
095        }
096        if (_contextMenuFactoryCreator != null) {
097            try {
098                _menuFactory = (PtolemyMenuFactory) _contextMenuFactoryCreator
099                        .createContextMenuFactory(controller);
100            } catch (Exception ex) {
101                //do nothing - will default to ptii right-click menus
102                System.out.println(
103                        "Unable to use the alternative right-click menu "
104                                + "handler that was specified in the "
105                                + "configuration; defaulting to ptii handler. "
106                                + "Exception was: " + ex);
107            }
108
109        }
110
111        // If the above has failed in any way, _menuFactory will still be null,
112        // in which case we should default to ptii context menus
113        if (_menuFactory == null) {
114            _menuFactory = new PtolemyMenuFactory(controller);
115        }
116
117        // In this base class, there is only one configure command, so
118        // there won't be a submenu. Subclasses convert this to a submenu.
119        _configureMenuFactory = new MenuActionFactory(_configureAction);
120        _menuFactory.addMenuItemFactory(_configureMenuFactory);
121        _menuCreator.setMenuFactory(_menuFactory);
122
123        // Add a double click interactor.
124        ActionInteractor doubleClickInteractor = new ActionInteractor(
125                _configureAction);
126        doubleClickInteractor.setConsuming(false);
127        doubleClickInteractor.setMouseFilter(new MouseFilter(1, 0, 0, 2));
128
129        interactor.addInteractor(doubleClickInteractor);
130
131        // NOTE: This dance is so that the
132        // doubleClickInteractor gets the events before the drag interactor.
133        interactor.setDragInteractor(interactor.getDragInteractor());
134
135        // Set the selection model to allow this to be independently selected.
136        SelectionModel sm = controller.getSelectionModel();
137        interactor.setSelectionModel(sm);
138    }
139
140    /** Return the configuration menu factory.
141     *
142     *  @return The configuration menu factory.
143     */
144    public MenuActionFactory getConfigureMenuFactory() {
145        return _configureMenuFactory;
146    }
147
148    ///////////////////////////////////////////////////////////////////
149    ////                     protected members                     ////
150
151    /** The configure action, which handles edit parameters requests. */
152    protected static ConfigureAction _configureAction = new ConfigureAction(
153            "Configure");
154
155    /** The submenu for configure actions. */
156    protected MenuActionFactory _configureMenuFactory;
157
158    /** The menu creator. */
159    protected MenuCreator _menuCreator;
160
161    /** The factory belonging to the menu creator. */
162    protected PtolemyMenuFactory _menuFactory;
163
164    /** A configurable object that allows a different MenuFactory
165     * to be specified instead of the default ptII one.
166     * The MenuFactory constructs the right-click context menus.
167     */
168    private static ContextMenuFactoryCreator _contextMenuFactoryCreator;
169}