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.module.provenance;
031
032import java.util.List;
033
034import javax.swing.JButton;
035import javax.swing.JToolBar;
036
037import org.kepler.build.modules.Module;
038import org.kepler.configuration.ConfigurationManager;
039import org.kepler.configuration.ConfigurationProperty;
040import org.kepler.gui.KeplerGraphFrame;
041import org.kepler.gui.KeplerGraphFrameUpdater;
042import org.kepler.module.ModuleInitializer;
043import org.kepler.moml.filter.TopProvenanceRecorderFilter;
044import org.kepler.provenance.ProvenanceRecorder;
045import org.kepler.provenance.gui.DisabledGlassPane;
046import org.kepler.provenance.gui.ProvenanceConfigureAction;
047import org.kepler.tagging.Tagging;
048import org.kepler.util.WorkflowRun;
049
050import ptolemy.kernel.util.NamedObj;
051import ptolemy.moml.filter.BackwardCompatibility;
052import ptolemy.util.MessageHandler;
053
054
055/**
056 * Perform initialization for the provenance module. This class adds itself as a
057 * updater to KeplerGraphFrame so that can add the configure provenance button
058 * to the toolbar.
059 * 
060 * @author Daniel Crawl
061 * @version $Id: Initialize.java 33636 2015-08-24 22:49:48Z crawl $
062 * 
063 */
064
065public class Initialize implements KeplerGraphFrameUpdater, ModuleInitializer {
066        /** Compares this object with the specified object for order. */
067        @Override
068    public int compareTo(KeplerGraphFrameUpdater o) {
069                // always return less than
070                return -1;
071        }
072        
073        @Override
074    public boolean equals(Object o) {
075            return this == o;
076        }
077
078        /** Perform any module-specific initializations. */
079        @Override
080    public void initializeModule() {
081                Tagging.registerAtomicallyTaggableClass(WorkflowRun.class);
082                // add ourself as an updater so we can add a button
083                KeplerGraphFrame.addUpdater(this);
084                
085                // the recorder filter adds the recorder to workflows
086                BackwardCompatibility.addFilter(new TopProvenanceRecorderFilter());
087                
088        ConfigurationManager cm = ConfigurationManager.getInstance();
089        
090        Module provenanceModule = ConfigurationManager.getModule("provenance");
091        Module coreModule = ConfigurationManager.getModule("core");
092        
093        ConfigurationProperty provenanceModuleRootProperty =
094                cm.getProperty(provenanceModule);
095        ConfigurationProperty coreRootProperty = cm.getProperty(coreModule);
096        
097        ConfigurationProperty kehfProvenance =
098                provenanceModuleRootProperty.getProperty("karEntryHandlerFactory");
099        ConfigurationProperty kehfCore = coreRootProperty.getProperty("karEntryHandlerFactory");
100        
101        List<ConfigurationProperty> handlers = kehfProvenance.getProperties("karHandler");
102
103        for (ConfigurationProperty handler : handlers) {
104            try {
105                List<ConfigurationProperty> l = kehfCore.getProperties();
106                boolean add = true;
107                for (int i = 0; i < l.size(); i++) {
108                    ConfigurationProperty cp = l.get(i);
109                    String name = cp.getProperty("name").getValue();
110                    if (name.equals(handler.getProperty("name").getValue())) {
111                        add = false;
112                    }
113                }
114
115                if (add) {
116                    kehfCore.addProperty(handler);
117                }
118            } catch (Exception e) {
119                e.printStackTrace();
120            }
121        }
122        }
123
124        /** Update the components. */
125        @Override
126    public void updateFrameComponents(KeplerGraphFrame.Components components) {
127                ProvenanceConfigureAction action = new ProvenanceConfigureAction(
128                                components.getFrame());
129                JToolBar toolbar = components.getToolBar();
130                JButton button = diva.gui.GUIUtilities
131                                .addToolBarButton(toolbar, action);
132                button.setToolTipText("Provenance recorder");
133
134                // tell the action about its button so it can change the icon
135                action.setButton(button);
136        }
137        
138        @Override
139    public void dispose(KeplerGraphFrame frame) {
140                                
141                DisabledGlassPane glassPane = new DisabledGlassPane();
142                frame.setGlassPane(glassPane);
143                glassPane.activate("Please wait...");
144                //force paint
145                frame.paintComponents(frame.getGraphics());
146                
147                NamedObj model = frame.getModel();
148                // clean up any provenance recorders contained in this model
149                List<?> recorderList = model.attributeList(ProvenanceRecorder.class);
150                for(Object recorder : recorderList)
151                {
152                        try {
153                                ((ProvenanceRecorder)recorder).setContainer(null);
154                        } catch (Exception e) {
155                                MessageHandler.error("Error disposing provenance recorder.", e);
156                        }
157                }
158        }
159}