001/*
002 * Copyright (c) 2007-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2010-11-10 01:36:37 +0000 (Wed, 10 Nov 2010) $' 
007 * '$Revision: 26278 $'
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.event.ActionEvent;
033import java.util.List;
034
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.kepler.moml.NamedObjId;
038import org.kepler.moml.NamedObjIdReferralList;
039import org.kepler.objectmanager.ActorMetadata;
040import org.kepler.objectmanager.ObjectManager;
041import org.kepler.objectmanager.cache.ActorCacheObject;
042import org.kepler.objectmanager.cache.CacheManager;
043import org.kepler.objectmanager.lsid.KeplerLSID;
044
045import ptolemy.actor.gui.TableauFrame;
046import ptolemy.kernel.util.NamedObj;
047import ptolemy.moml.MoMLChangeRequest;
048import ptolemy.vergil.basic.KeplerDocumentationAttribute;
049import ptolemy.vergil.basic.RemoveCustomDocumentationAction;
050import ptolemy.vergil.toolbox.FigureAction;
051
052/**
053 * This action removes KeplerDocumentationAttribute, and DocAttribute (by
054 * calling RemoveCustomDocumentationAction) from a NamedObj and then attempts to
055 * reset the KeplerDocumentationAttribute to its "original" state using the
056 * first LSID in the NamedObj's referral list. The "original LSID" is used to
057 * get the ActorMetadata on the associated ActorCacheObject; this will obviously
058 * not work if there's no ActorCacheObject for the original LSID.
059 * 
060 * The NamedObj is gotten via FigureAction.getTarget() unless a specific LSID
061 * has been set with setLSID (currently unused).
062 */
063public class RemoveCustomKeplerDocumentationAction extends FigureAction {
064
065        private static final Log log = LogFactory
066                        .getLog(RemoveCustomKeplerDocumentationAction.class.getName());
067        private static final boolean isDebugging = log.isDebugEnabled();
068
069        private TableauFrame parent = null;
070        private KeplerLSID _lsid = null;
071
072        public RemoveCustomKeplerDocumentationAction(TableauFrame parent) {
073                super("Remove Custom Kepler Documentation");
074                if (parent == null) {
075                        IllegalArgumentException iae = new IllegalArgumentException(
076                                        "RemoveCustomKeplerDocumentationAction constructor received NULL argument for TableauFrame");
077                        iae.fillInStackTrace();
078                        throw iae;
079                }
080                this.parent = parent;
081        }
082
083        public void setLSID(KeplerLSID lsid) {
084                _lsid = lsid;
085        }
086
087        public KeplerLSID getLSID() {
088                return _lsid;
089        }
090
091        /**
092         * Invoked when an action occurs.
093         * 
094         *@param e
095         *            ActionEvent
096         */
097        public void actionPerformed(ActionEvent e) {
098                super.actionPerformed(e);
099                NamedObj target = null;
100                KeplerLSID lsidToView = getLSID();
101
102                try {
103
104                        if (lsidToView != null) {
105                                target = ObjectManager.getInstance().getObjectRevision(
106                                                lsidToView);
107                        } else {
108                                target = getTarget();
109                        }
110
111                        if (target != null) {
112
113                                // remove the KeplerDocumentationAttribute if it exists
114                                List keplerDocAttributeList = target
115                                                .attributeList(KeplerDocumentationAttribute.class);
116
117                                if (keplerDocAttributeList.size() != 0) {
118                                        String moml = "<deleteProperty name=\""
119                                                        + ((KeplerDocumentationAttribute) keplerDocAttributeList
120                                                                        .get(0)).getName() + "\"/>";
121                                        MoMLChangeRequest request = new MoMLChangeRequest(this,
122                                                        target, moml);
123                                        target.requestChange(request);
124                                }
125
126                                // now remove the DocAttribute
127                                RemoveCustomDocumentationAction rcda = new RemoveCustomDocumentationAction();
128                                rcda.actionPerformed(e);
129
130                                // now attempt to reset KeplerDocumentationAttribute to original
131                                // documentation
132                                KeplerLSID lsid = NamedObjId.getIdFor(target);
133                                NamedObjIdReferralList norl = NamedObjId
134                                                .getIDListAttributeFor(target);
135                                KeplerLSID origLSID = lsid;
136                                if (norl.getReferrals().size() > 0) {
137                                        origLSID = norl.getReferrals().get(
138                                                        norl.getReferrals().size() - 1);
139                                }
140                                if (isDebugging) {
141                                        log.debug("Going to try to reset " + target.getName()
142                                                        + " to original documentation,"
143                                                        + "the documentation for " + origLSID.toString());
144                                }
145                                CacheManager cacheMan = CacheManager.getInstance();
146                                ActorCacheObject aco = (ActorCacheObject) cacheMan
147                                                .getObject(origLSID);
148                                if (aco == null) {
149                                        if (isDebugging) {
150                                                log.warn("Couldn't find ActorCacheObject for "
151                                                                + origLSID + ". Can't reset documentation.");
152                                        }
153                                        return;
154                                }
155                                ActorMetadata am = aco.getMetadata();
156                                if (am == null) {
157                                        if (isDebugging) {
158                                                log.warn("No ActorMetadata for ActorCacheObject:"
159                                                                + origLSID + ". Can't reset documentation.");
160                                        }
161                                        return;
162                                }
163
164                                KeplerDocumentationAttribute kda = am
165                                                .getDocumentationAttribute();
166                                KeplerDocumentationAttribute keplerDocumentation = new KeplerDocumentationAttribute(
167                                                target, "KeplerDocumentation");
168                                keplerDocumentation.createInstanceFromExisting(kda);
169                                keplerDocumentation.setContainer(target);
170                        }
171
172                } catch (Exception ee) {
173                        ee.printStackTrace();
174                }
175        }
176
177}