001/*
002 * Copyright (c) 2012 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:44:14 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33630 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029package org.kepler.gui;
030
031import java.awt.Toolkit;
032import java.awt.event.ActionEvent;
033import java.util.concurrent.atomic.AtomicBoolean;
034
035import javax.swing.KeyStroke;
036
037import org.kepler.gui.frame.TabbedKeplerGraphFrame;
038
039import diva.gui.GUIUtilities;
040import ptolemy.actor.CompositeActor;
041import ptolemy.actor.gui.TableauFrame;
042import ptolemy.kernel.util.NamedObj;
043import ptolemy.vergil.basic.LookInsideAction;
044
045/** An action to open a composite actor in a tab.
046 * 
047 *  @author Daniel Crawl
048 *  @version $Id
049 */
050public class TabbedLookInsideAction extends LookInsideAction {
051
052    /** Create a new TabbedLookInsideAction object with the given
053     *  frame as it's container.
054     *  @param menuActionLabel The label of the menu action to be displayed in
055     *   the GUI context menus.
056     */
057    public TabbedLookInsideAction(TableauFrame parent) {
058        this("Open Actor in Tab");
059        _parent = parent;
060        putValue(GUIUtilities.ACCELERATOR_KEY, KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T,
061                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
062    }
063    
064    /** Create a new TabbedLookInsideAction object with the given
065     *  string as its menu action label.
066     *  @param menuActionLabel The label of the menu action to be displayed in
067     *   the GUI context menus.
068     */
069    public TabbedLookInsideAction(String menuActionLabel) {
070        super(menuActionLabel);
071        putValue(GUIUtilities.ACCELERATOR_KEY, KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T,
072                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
073    }
074
075    /** Execute the look inside action command received from the event sent from
076     *  the user interface.
077     *  @param event The event received to execute the look inside action.
078     */
079    @Override
080    public void actionPerformed(ActionEvent event) {
081
082        //System.out.println("actionPerformed in TabbedLookInsideActionAction");
083        
084        _actionIsRunning.set(true);
085
086        if(_parent != null) {
087            setConfiguration(_parent.getConfiguration());
088        }
089                
090        // call the parent class to make sure effigy and tableau exist for the
091        // actor
092        super.actionPerformed(event);
093        
094        // see if we're opening a composite actor
095        // XXX what about atomic actors?
096        NamedObj object = getTarget();
097        //System.out.println("target is : " + object.getFullName());
098        if(object instanceof CompositeActor) {
099            if(_parent instanceof TabbedKeplerGraphFrame) {
100                ((TabbedKeplerGraphFrame)_parent).setSelectedTab(object);
101            } else if(_parent == null) {
102                KeplerGraphFrame frame = ModelToFrameManager.getInstance().getFrame(object);
103                if(frame instanceof TabbedKeplerGraphFrame) {
104                    ((TabbedKeplerGraphFrame)frame).setSelectedTab(object);
105                }
106            }
107        }
108        
109        _actionIsRunning.set(false);
110    }
111    
112    /** Returns true if the action is being performed. */
113    public static boolean actionIsRunning() {
114        return _actionIsRunning.get();
115    }
116    
117    /** Get the target to be opened. */
118    @Override
119    public NamedObj getTarget() {
120        if(_target == null) {
121            return super.getTarget();
122        }
123        return _target;
124    }
125    
126    /** Set the target to be opened. */
127    public void setTarget(NamedObj namedObj) {
128        _target = namedObj;
129    }
130
131    /** True when the action is being performed. */
132    private static AtomicBoolean _actionIsRunning = new AtomicBoolean(false);
133    
134    /** The containing frame. */
135    private TableauFrame _parent;
136    
137    /** The target to be opened. */
138    private NamedObj _target;
139}