001/* A menu item factory that opens a dialog for adding ports.
002
003 Copyright (c) 1999-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.Component;
031import java.awt.Frame;
032import java.awt.event.ActionEvent;
033
034import javax.swing.AbstractAction;
035import javax.swing.Action;
036import javax.swing.JMenuItem;
037
038import diva.gui.toolbox.JContextMenu;
039import ptolemy.actor.gui.Configuration;
040import ptolemy.actor.gui.DialogTableau;
041import ptolemy.actor.gui.PortConfigurerDialog;
042import ptolemy.actor.gui.TableauFrame;
043import ptolemy.kernel.Entity;
044import ptolemy.kernel.util.NamedObj;
045import ptolemy.vergil.toolbox.MenuItemFactory;
046import ptolemy.vergil.unit.UnitConstraintsDialog;
047
048///////////////////////////////////////////////////////////////////
049//// PortDialogFactory
050
051/**
052 A factory that creates a dialog to configure, add, or remove ports
053 from objects.
054
055 @deprecated Use PortDialogAction.
056 @author Edward A. Lee and Steve Neuendorffer
057 @version $Id$
058 @since Ptolemy II 2.0
059 @Pt.ProposedRating Red (eal)
060 @Pt.AcceptedRating Red (johnr)
061 */
062@Deprecated
063public class PortDialogFactory implements MenuItemFactory {
064    ///////////////////////////////////////////////////////////////////
065    ////                         public methods                    ////
066
067    /**
068     * Add an item to the given context menu that will open a dialog to add or
069     * remove ports from an object.
070     *
071     * @param menu The context menu.
072     * @param object The object whose ports are being manipulated.
073     * @return The JMenuItem or null if the object argument is not an
074     * Entity.
075     */
076    @Override
077    public JMenuItem create(final JContextMenu menu, NamedObj object) {
078        JMenuItem retv = null;
079
080        // Removed this method since it was never used. EAL
081        // final NamedObj target = _getItemTargetFromMenuTarget(object);
082        final NamedObj target = object;
083
084        // ensure that we actually have a target, and that it's an Entity.
085        if (!(target instanceof Entity)) {
086            return null;
087        }
088
089        // Create a dialog for configuring the object.
090        // First, identify the top parent frame.
091        // Normally, this is a Frame, but just in case, we check.
092        // If it isn't a Frame, then the edit parameters dialog
093        // will not have the appropriate parent, and will disappear
094        // when put in the background.
095        // Note, this uses the "new" way of doing dialogs.
096        @SuppressWarnings("serial")
097        Action configPortsAction = new AbstractAction(_configPorts) {
098            @Override
099            public void actionPerformed(ActionEvent e) {
100                Component parent = menu.getInvoker();
101
102                while (parent.getParent() != null) {
103                    parent = parent.getParent();
104                }
105
106                if (parent instanceof Frame) {
107                    DialogTableau dialogTableau = DialogTableau.createDialog(
108                            (Frame) parent, _configuration,
109                            ((TableauFrame) parent).getEffigy(),
110                            PortConfigurerDialog.class, (Entity) target);
111
112                    if (dialogTableau != null) {
113                        dialogTableau.show();
114                    }
115                }
116            }
117        };
118
119        retv = menu.add(configPortsAction, _configPorts);
120
121        @SuppressWarnings("serial")
122        Action configUnitsAction = new AbstractAction(_configUnits) {
123            @Override
124            public void actionPerformed(ActionEvent e) {
125                Component parent = menu.getInvoker();
126
127                while (parent.getParent() != null) {
128                    parent = parent.getParent();
129                }
130
131                if (parent instanceof Frame) {
132                    DialogTableau dialogTableau = DialogTableau.createDialog(
133                            (Frame) parent, _configuration,
134                            ((TableauFrame) parent).getEffigy(),
135                            UnitConstraintsDialog.class, (Entity) target);
136
137                    if (dialogTableau != null) {
138                        dialogTableau.show();
139                    }
140                }
141            }
142        };
143
144        retv = menu.add(configUnitsAction, _configUnits);
145
146        return retv;
147    }
148
149    /**
150     * Set the configuration for use by the help screen.
151     *
152     * @param configuration
153     *            The configuration.
154     */
155    public void setConfiguration(Configuration configuration) {
156        _configuration = configuration;
157    }
158
159    ///////////////////////////////////////////////////////////////////
160    ////                         private variables                 ////
161
162    /** The configuration. */
163    private static String _configPorts = "Configure Ports";
164
165    private static String _configUnits = "Configure Units";
166
167    private Configuration _configuration;
168}