001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-08-24 22:49:48 +0000 (Mon, 24 Aug 2015) $' 
007 * '$Revision: 33636 $'
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.provenance.gui;
031
032import java.awt.event.ActionEvent;
033import java.net.URL;
034
035import javax.swing.ImageIcon;
036import javax.swing.JButton;
037
038import org.kepler.gui.KeplerGraphFrame;
039import org.kepler.provenance.ProvenanceEnabledListener;
040import org.kepler.provenance.ProvenanceRecorder;
041import org.kepler.tagging.gui.TagBarUI;
042
043import diva.gui.GUIUtilities;
044import ptolemy.actor.CompositeActor;
045import ptolemy.actor.gui.Effigy;
046import ptolemy.kernel.util.NamedObj;
047import ptolemy.util.MessageHandler;
048import ptolemy.vergil.toolbox.FigureAction;
049
050/** An action that toggles the default provenance recorder in the top
051 *  level workflow container.
052 *
053 *  @author Daniel Crawl
054 *  @version $Id: ProvenanceConfigureAction.java 33636 2015-08-24 22:49:48Z crawl $
055 */
056public class ProvenanceConfigureAction extends FigureAction implements ProvenanceEnabledListener
057{
058    public ProvenanceConfigureAction(KeplerGraphFrame owner)
059    {
060        super("Configure Provenance");
061
062        _button = null;
063
064        // see if the top-level provenance recorder is present so we can
065        // set the appropriate icon.
066
067        // get the top-level composite actor
068        _top = owner.getModel().toplevel();
069        
070        _effigy = owner.getEffigy();
071
072        _enabled = turnOnProvenanceRecorder();
073        
074        String iconStr = OFF_ICON_STR;
075        if(_enabled)
076        {
077            iconStr = ON_ICON_STR;
078            
079            TagBarUI.addTagEventListener(_recorder, owner);
080        }
081
082        // configure the button icons
083        GUIUtilities.addIcons(this, new String[][] {
084            { iconStr, GUIUtilities.LARGE_ICON },
085            { OVER_ICON_STR, GUIUtilities.ROLLOVER_ICON },
086            { OVER_ICON_STR, GUIUtilities.ROLLOVER_SELECTED_ICON },
087            { OVER_ICON_STR, GUIUtilities.SELECTED_ICON } });
088    }
089
090    ///////////////////////////////////////////////////////////////////
091    ////                         public methods                    ////
092
093    public void actionPerformed(ActionEvent event)
094    {
095        super.actionPerformed(event);
096
097        String iconStr = OFF_ICON_STR;
098        
099        // see if provenance is turned on
100        if(_enabled)
101        {
102            try
103            {
104                // turn provenance off
105                _recorder.recordingType.setToken(ProvenanceRecorder.DEFAULT_REC_TYPE_IGNORE);
106                // set the recording type to be persistent so that 
107                // provenance is off when reloading the model
108               // _recorder.recordingType.setPersistent(true);
109                _enabled = false;
110                //_effigy.setModified(true);
111            }
112            catch(Throwable t)
113            {
114                MessageHandler.error("Error disabling provenance.", t);
115                iconStr = ON_ICON_STR;
116            }
117        }
118        else if(!ProvenanceRecorder.containsSupportedDirector((CompositeActor)_top))
119        {
120            MessageHandler.error("Cannot record provenance for director " +
121                ((CompositeActor)_top).getDirector().getName());
122        }
123        else if(turnOnProvenanceRecorder())
124        {
125            iconStr = ON_ICON_STR;
126        }
127
128        URL img = getClass().getResource(iconStr);
129        ImageIcon icon = new ImageIcon(img);
130        _button.setIcon(icon);
131    }
132
133    /** This method is called when provenance is enabled or disabled.
134     *  @param enabled True if provenance is enabled.
135     */
136        public void toggle(boolean enabled)
137        {               
138                // see if provenance was turned off, but button is in on state.
139                if(!enabled && _enabled)
140                {
141                URL img = getClass().getResource(OFF_ICON_STR);
142                ImageIcon icon = new ImageIcon(img);
143                _button.setIcon(icon);
144                _enabled = false;
145                }
146        }
147
148    /** Set the button. */
149    public void setButton(JButton button)
150    {
151        _button = button;
152    }
153
154    ///////////////////////////////////////////////////////////////////
155    ////                         public fields                     ////
156
157    /** Icon when top-level recorder is present. */
158    public static final String ON_ICON_STR =
159        "/org/kepler/provenance/gui/img/prov_on.gif";
160
161    /** Icon when top-level recorder is not present. */
162    public static final String OFF_ICON_STR =
163        "/org/kepler/provenance/gui/img/prov_off.gif";
164
165    /** Icon when cursor over button. */
166    public static final String OVER_ICON_STR =
167        "/org/kepler/provenance/gui/img/prov_ov.gif";
168
169    ///////////////////////////////////////////////////////////////////
170    ////                         private methods                   ////
171
172    /** Add the default provenance recorder if it does not exist, and
173     *  turn on provenance recording unless the director is not supported,
174     *  or the default recording type is set to ignore.
175     *  @return True if the recorder was successfully added and the recording
176     *  type was not set to ignore.
177     */
178    private boolean turnOnProvenanceRecorder()
179    {
180        try
181        {
182            _enabled = ProvenanceRecorder.addProvenanceRecorder((CompositeActor)_top, this, _effigy);
183            if(_recorder == null)
184            {
185                _recorder = ProvenanceRecorder.getDefaultProvenanceRecorder(_top);
186            }
187        }
188        catch(Exception e)
189        {
190            MessageHandler.error("ERROR: Unable to create and configure " +
191                "default provenance recorder.", e);
192        }
193
194        return _enabled;
195    }
196
197    ///////////////////////////////////////////////////////////////////
198    ////                         private fields                    ////
199
200    /** The button for this action. */
201    private JButton _button;
202
203    /** True if TOP provenance recorder is turned on. */
204    private boolean _enabled;
205
206    /** Effigy of top level. */
207    private Effigy _effigy;
208   
209    /** Top-level provenance recorder, if found. */
210    private ProvenanceRecorder _recorder;
211
212    /** Top-level NamedObj. */
213    private NamedObj _top;
214}