001/* A menu item factory that opens a dialog for setting breakpoints.
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.debugger;
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.Actor;
040import ptolemy.kernel.ComponentEntity;
041import ptolemy.kernel.Entity;
042import ptolemy.kernel.util.NamedObj;
043import ptolemy.util.MessageHandler;
044import ptolemy.vergil.basic.BasicGraphController;
045import ptolemy.vergil.toolbox.MenuItemFactory;
046
047///////////////////////////////////////////////////////////////////
048//// BreakpointDialogFactory
049
050/**
051 A factory that creates a dialog box to configure breakpoints for the
052 actor selected.
053
054 @see ptolemy.vergil.kernel.PortDialogFactory
055
056 @author Elaine Cheong
057 @version $Id$
058 @since Ptolemy II 2.1
059 @Pt.ProposedRating Red (celaine)
060 @Pt.AcceptedRating Red (celaine)
061 */
062public class BreakpointDialogFactory implements MenuItemFactory {
063    /** Create factory.
064     *  @param graphController The associated graph controller for the
065     *  actor selected.
066     */
067    public BreakpointDialogFactory(BasicGraphController graphController) {
068        super();
069        _graphController = graphController;
070    }
071
072    ///////////////////////////////////////////////////////////////////
073    ////                         public methods                    ////
074
075    /** Add an item to the given context menu that will open a dialog
076     *  to configure breakpoints for an object.
077     *  @param menu The context menu.
078     *  @param object The object whose breakpoints are being modified.
079     *  @return The context menu item.
080     */
081    @Override
082    public JMenuItem create(final JContextMenu menu, NamedObj object) {
083        String name = "Set Breakpoints";
084        final NamedObj target = object;
085
086        // Ensure that if we have a ComponentEntity, it is opaque.
087        // Also ensure that it is an actor and that it has a director.
088        if (!(target instanceof ComponentEntity)
089                || !((ComponentEntity) target).isOpaque()
090                || !(target instanceof Actor)
091                || ((Actor) target).getExecutiveDirector() == null) {
092            return null;
093        }
094
095        @SuppressWarnings("serial")
096        Action action = new AbstractAction(name) {
097            @Override
098            public void actionPerformed(ActionEvent e) {
099                try {
100                    // Create a dialog for configuring the object.  First,
101                    // identify the top parent frame.  Normally, this is a
102                    // Frame, but just in case, we check.  If it isn't a
103                    // Frame, then the set breakpoints dialog will not
104                    // have the appropriate parent, and will disappear
105                    // when put in the background.
106                    Component parent = menu.getInvoker();
107
108                    while (parent.getParent() != null) {
109                        parent = parent.getParent();
110                    }
111
112                    if (parent instanceof Frame) {
113                        new BreakpointConfigurerDialog((Frame) parent,
114                                (Entity) target, _graphController);
115                    } else {
116                        new BreakpointConfigurerDialog(null, (Entity) target,
117                                _graphController);
118                    }
119                } catch (Throwable throwable) {
120                    // If we don't have a SDFDirector, then the error
121                    // message will appear on stderr instead of in a
122                    // dialog unless we catch the error here.
123                    MessageHandler.error(
124                            "Failed to create Breakpoint " + "dialog.",
125                            throwable);
126                }
127            }
128        };
129
130        return menu.add(action, name);
131    }
132
133    ///////////////////////////////////////////////////////////////////
134    ////                         private variables                 ////
135
136    /** The graph controller associated with the actor selected. */
137    private BasicGraphController _graphController;
138}