001/*
002 * Copyright (c) 2009-2015 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2015-11-02 19:58:24 +0000 (Mon, 02 Nov 2015) $' 
007 * '$Revision: 34195 $'
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.profiling.gui;
030
031import java.awt.BorderLayout;
032import java.awt.Dimension;
033import java.util.concurrent.ExecutionException;
034
035import javax.swing.JCheckBox;
036import javax.swing.SwingWorker;
037
038import org.kepler.gui.state.StateChangeEvent;
039import org.kepler.gui.state.StateChangeMonitor;
040import org.kepler.provenance.ProvenanceRecorder;
041import org.kepler.provenance.Queryable;
042import org.kepler.util.WorkflowRun;
043
044import ptolemy.actor.gui.PtolemyFrame;
045import ptolemy.actor.gui.TableauFrame;
046import ptolemy.kernel.util.NamedObj;
047import ptolemy.util.MessageHandler;
048
049/** A base class for tab panes showing a workflow outline. Subclasses
050 *  must implement _refreshOutline to draw the outline.
051 * 
052 *  @author Daniel Crawl
053 *  @version $Id: WorkflowRunOutlineTabPane.java 34195 2015-11-02 19:58:24Z crawl $
054 */
055public abstract class WorkflowRunOutlineTabPane extends ViewWatchingTabPane {
056
057    @Override
058    public void handleStateChange(StateChangeEvent event) {
059        
060        super.handleStateChange(event);
061        
062        // see if the selected run in the workflow run manager changed
063        if (event.getChangedState().equals(WorkflowRun.WORKFLOWRUN_SELECTED)) {
064            NamedObj namedObj = event.getReference();
065            if (namedObj instanceof WorkflowRun) {
066                _runID = ((WorkflowRun)namedObj).getExecId();
067            }
068        }
069        
070        if(/*_tabIsSelected &&*/ _viewIsSelected) {
071            _refreshOutline();
072        }
073    }
074
075    @Override
076    public void initializeTab() throws Exception {
077        super.initializeTab();
078        setLayout(new BorderLayout());
079        setPreferredSize(new Dimension(300,400));
080        _refreshOutline();
081    }
082
083    /** Clean up resources since panel is being closed. */
084    @Override
085    public void removeNotify() {
086        super.removeNotify();
087        StateChangeMonitor.getInstance().removeStateChangeListener(WorkflowRun.WORKFLOWRUN_SELECTED, this);
088    }
089
090    /** Set the parent frame and add listeners for the workflow
091     *  run manager, view changes, and tab changes.
092     */
093    @Override
094    public void setParentFrame(TableauFrame parent) {
095        super.setParentFrame(parent);
096        StateChangeMonitor.getInstance().addStateChangeListener(WorkflowRun.WORKFLOWRUN_SELECTED, this);
097    }
098
099    /** Set the name of the tab. */
100    public void setTabName(String tabName) {
101        _tabName = tabName;
102    }
103
104    /** Get the workflow from the execution LSID. */
105    protected NamedObj _getWorkflow() {
106        try {
107            ProvenanceRecorder pr = 
108                ProvenanceRecorder.getDefaultProvenanceRecorder(((PtolemyFrame)_frame).getModel().toplevel());
109            if(pr == null) {
110                System.err.println("Error: could not find default provenance recorder.");
111                return null;
112            }
113            Queryable query = pr.getRecording().getQueryable(true);
114            return query.getWorkflowForExecution(_runID);
115        } catch (Exception e) {
116            System.out.println("Exception: " + e.getMessage());
117            return null;
118        }
119    }
120    
121    protected void _refreshOutline() {
122        
123        if(_viewIsSelected /*&& _tabIsSelected*/ && _runID != null) {
124            
125            removeAll();
126            
127            SwingWorker<NamedObj,Void> worker = new SwingWorker<NamedObj,Void>() {
128
129                @Override
130                protected NamedObj doInBackground() throws Exception {
131                    return _getWorkflow();
132                }
133                
134                @Override
135                protected void done() {
136                    NamedObj root = null;
137                    try {
138                        root = get();
139                    } catch (InterruptedException | ExecutionException e) {
140                        MessageHandler.error("Error retrieving workflow.", e);
141                    }
142                    
143                    if(root == null) {
144                        System.err.println("null workflow from _getWorkflow()");
145                    } else {
146                        _finishRefreshOutline(root);
147                    }
148                }                
149            };    
150            
151            worker.execute();
152        }
153    }
154    
155    protected abstract void _finishRefreshOutline(NamedObj root);
156    
157    /** A check box for toggling something in the outline. */
158    protected JCheckBox _toggleCheckbox;
159
160    /** The execution id. */
161    protected Integer _runID;
162}