001/*
002 * Copyright (c) 2007-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
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;
031
032import java.awt.Component;
033import java.awt.event.ActionEvent;
034
035import javax.swing.JOptionPane;
036import javax.swing.tree.TreePath;
037
038import org.kepler.moml.NamedObjId;
039import org.kepler.objectmanager.ActorMetadata;
040import org.kepler.objectmanager.cache.ActorCacheObject;
041import org.kepler.objectmanager.cache.CacheManager;
042import org.kepler.objectmanager.library.LibraryManager;
043import org.kepler.objectmanager.lsid.KeplerLSID;
044
045import ptolemy.actor.gui.Configuration;
046import ptolemy.actor.gui.PtolemyFrame;
047import ptolemy.kernel.ComponentEntity;
048import ptolemy.kernel.util.NamedObj;
049import ptolemy.vergil.basic.BasicGraphFrame;
050import ptolemy.vergil.toolbox.FigureAction;
051
052/**
053 * This action allows an actor to be removed from the tree Created to display
054 * when actor in library tree is right clicked.
055 * 
056 *@author Chad Berkley
057 *@since 3/13/2008
058 */
059public class RemoveComponentAction extends FigureAction {
060        private final static String LABEL = "Remove Component";
061        private TreePath path;
062        private Component parent;
063        //private NewActorFrame naFrame;
064        //private GetDocumentationAction gda;
065        private PtolemyFrame pFrame = null;
066
067        /**
068         * Constructor
069         * 
070         *@param path
071         *            the TreePath where the actor is being removed.
072         */
073        public RemoveComponentAction(TreePath path, Component parent) {
074                super(LABEL);
075                this.path = path;
076                this.parent = parent;
077        }
078
079        /**
080         * set the config
081         */
082        public void setConfiguration(Configuration config) {
083        }
084
085        /**
086         * Invoked when an action occurs.
087         * 
088         *@param e
089         *            ActionEvent
090         */
091        public void actionPerformed(ActionEvent e) {
092                super.actionPerformed(e);
093                String msg = "This will remove the component from the tree entirely.  "
094                                + "Are you sure you want to do this?";
095                int userInput = JOptionPane.showConfirmDialog(null, msg, "Warning",
096                                JOptionPane.YES_NO_OPTION);
097                if (userInput == JOptionPane.NO_OPTION) {
098                        return;
099                }
100                Component current = parent;
101                while (parent != null && !(parent instanceof BasicGraphFrame)) {
102                        parent = current.getParent();
103                        current = parent;
104                }
105                Component comp = (Component) e.getSource();
106                NamedObj actor = null;
107                ActorMetadata am = null;
108                ComponentEntity ce = (ComponentEntity) path.getLastPathComponent();
109                try {
110                        CacheManager cacheMan = CacheManager.getInstance();
111                        String lsidString = ((NamedObjId) (ce.getAttribute("entityId")))
112                                        .getExpression();
113                        ActorCacheObject aco = (ActorCacheObject) cacheMan
114                                        .getObject(new KeplerLSID(lsidString));
115                        am = aco.getMetadata();
116      GraphicalActorMetadata gam = new GraphicalActorMetadata(am);
117                        actor = gam.getActorAsNamedObj(null);
118                        // System.out.println("removing actor: " + actor.getName());
119
120                        cacheMan.removeObject(aco.getLSID());
121
122                        /*
123                         * Hashtable semTypeHash = am.getSemanticTypeHash(); Enumeration
124                         * keys = semTypeHash.keys(); while(keys.hasMoreElements()) { String
125                         * key = (String)keys.nextElement(); String value =
126                         * (String)semTypeHash.get(key); am.removeSemanticType(key);
127                         * System.out.println("semtype: " + key + " : " + value); }
128                         */
129
130                        LibraryManager library = LibraryManager.getInstance();
131                        library.buildLibrary();
132                        library.refreshJTrees();
133
134                        // System.out.println(actor.getName() + " removed");
135
136                } catch (Exception ee) {
137                        System.out
138                                        .println("Error in getting actor in RemoveComponentAction");
139                }
140        }
141
142        /**
143         * allows you to set the ptolemyFrame that should be used as the parent of
144         * this action.
145         */
146        public void setPtolemyFrame(PtolemyFrame pFrame) {
147                this.pFrame = pFrame;
148        }
149}