001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: aschultz $'
006 * '$Date: 2010-12-23 19:01:04 +0000 (Thu, 23 Dec 2010) $' 
007 * '$Revision: 26600 $'
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 */
029
030package org.kepler.gui.popups;
031
032import java.awt.Component;
033
034import javax.swing.JPopupMenu;
035import javax.swing.tree.TreePath;
036
037import org.apache.commons.logging.Log;
038import org.apache.commons.logging.LogFactory;
039import org.kepler.objectmanager.library.LibItem;
040import org.kepler.objectmanager.library.LibraryManager;
041
042import ptolemy.actor.gui.PtolemyFrame;
043import ptolemy.kernel.ComponentEntity;
044import ptolemy.kernel.util.NamedObj;
045
046/**
047 * A class for organizing popup menus in the Library. By extending this class
048 * you can add actions to the different types of items that show up in the
049 * library. See examples such as OntologyComponentPopup. See the AnnotatedPTree
050 * method "maybeShowPopup" for where to add the subclass in order for it to show
051 * up.
052 * 
053 * @author Aaron Schultz
054 * 
055 */
056public class LibraryPopup extends JPopupMenu {
057
058        private static final long serialVersionUID = -5556895608334936467L;
059        private static final Log log = LogFactory.getLog(LibraryPopup.class
060                        .getName());
061        private static final boolean isDebugging = log.isDebugEnabled();
062        
063        private TreePath _selectionPath;
064        private PtolemyFrame _parentFrame;
065        private int _liid;
066
067        public LibraryPopup() {
068                _selectionPath = null;
069                _parentFrame = null;
070        }
071
072        /**
073         * @param path
074         * @param comp
075         */
076        public LibraryPopup(TreePath path, Component comp) {
077
078                while (comp != null && !(comp instanceof PtolemyFrame)) {
079                        comp = comp.getParent();
080                }
081
082                if (comp == null) {
083                        System.out.println("Cannot find the PtolemyFrame.");
084                        return;
085                }
086
087                setParentFrame((PtolemyFrame) comp);
088                setSelectionPath(path);
089                
090                try {
091                        Object obj = getSelectionPath().getLastPathComponent();
092                        
093                        if (obj instanceof ComponentEntity) {
094                                int liid = LibraryManager.getLiidFor((ComponentEntity) obj);
095                                if (isDebugging) log.debug(liid);
096                                if (liid != -1) {
097                                        setLiid(liid);
098                                } else {
099                                        throw new Exception("LIID not found for "
100                                                        + ((NamedObj) obj).getName() + " "
101                                                        + obj.getClass().getName());
102                                }
103                        } else {
104                                throw new Exception("Object must be a NamedObj");
105                        }
106
107                } catch (Exception e) {
108                        e.printStackTrace();
109                }
110        }
111
112        /**
113         * Override this method with the good stuff.
114         */
115        public void initialize() {
116        }
117
118        public LibraryPopup(TreePath path, PtolemyFrame parentFrame) {
119                _selectionPath = path;
120                _parentFrame = parentFrame;
121        }
122
123        public LibItem getInfo() {
124                LibItem li = LibraryManager.getInstance().getTreeItemIndexInformation(
125                                getLiid());
126                return li;
127        }
128
129        public int getLiid() {
130                return _liid;
131        }
132
133        private void setLiid(int liid) {
134                _liid = liid;
135        }
136
137        public TreePath getSelectionPath() {
138                return _selectionPath;
139        }
140
141        public void setSelectionPath(TreePath selectionPath) {
142                _selectionPath = selectionPath;
143        }
144
145        public PtolemyFrame getParentFrame() {
146                return _parentFrame;
147        }
148
149        public void setParentFrame(PtolemyFrame parentFrame) {
150                _parentFrame = parentFrame;
151        }
152
153}