001/*
002 * Copyright (c) 2009-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2011-04-12 21:47:26 +0000 (Tue, 12 Apr 2011) $' 
007 * '$Revision: 27503 $'
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.reporting;
031
032import java.awt.event.WindowAdapter;
033import java.awt.event.WindowEvent;
034import java.util.List;
035import java.util.Vector;
036
037import javax.swing.BoxLayout;
038import javax.swing.JComboBox;
039import javax.swing.JComponent;
040import javax.swing.JLabel;
041import javax.swing.JPanel;
042import javax.swing.JToolBar;
043
044import org.apache.commons.logging.Log;
045import org.apache.commons.logging.LogFactory;
046import org.kepler.build.modules.Module;
047import org.kepler.configuration.ConfigurationManager;
048import org.kepler.configuration.ConfigurationProperty;
049import org.kepler.gui.KeplerGraphFrame;
050import org.kepler.gui.KeplerGraphFrameUpdater;
051import org.kepler.gui.ViewManager;
052import org.kepler.module.ModuleInitializer;
053import org.kepler.moml.NamedObjId;
054import org.kepler.moml.filter.TopReportingListenerFilter;
055import org.kepler.objectmanager.lsid.KeplerLSID;
056import org.kepler.reporting.ReportingListener;
057import org.kepler.workflow.WorkflowManager;
058
059import ptolemy.actor.gui.PtolemyFrame;
060import ptolemy.actor.gui.TableauFrame;
061import ptolemy.kernel.util.NamedObj;
062import ptolemy.moml.filter.BackwardCompatibility;
063
064/** Perform initialization for the reporting module. 
065 * This class acts as a listening for workflow execution 
066 * and adds reporting data to provenance when workflow run completes
067 *
068 * @author Ben Leinfelder
069 * @version $Id: Initialize.java 27503 2011-04-12 21:47:26Z crawl $
070 *
071 */
072
073public class Initialize implements KeplerGraphFrameUpdater, ModuleInitializer
074{
075        private static final Log log = LogFactory.getLog(Initialize.class);
076        private static final String VIEW_MENU_LABEL = "View:";
077
078    /** Compares this object with the specified object for order. */
079    public int compareTo(KeplerGraphFrameUpdater o)
080    {
081        /// always return greater than provenance
082        ///if (o instanceof org.kepler.module.provenance.Initialize) {
083        ///     return 1;
084        ///}
085        ///return 0;
086        
087        // we currently like view menu always on far-right
088        // setting this high:
089        return 3;
090    }
091
092    /** Perform any module-specific initializations. */
093    public void initializeModule()
094    {           
095        // the reporting filter adds the reporting listener to workflows
096        BackwardCompatibility.addFilter(new TopReportingListenerFilter());
097
098        // add ourself as an updater so we are called
099        KeplerGraphFrame.addUpdater(this);
100        
101        ConfigurationManager cm = ConfigurationManager.getInstance();
102        
103        Module reportingModule = ConfigurationManager.getModule("reporting");
104        Module coreModule = ConfigurationManager.getModule("core");
105        
106        ConfigurationProperty reportingRootProperty = cm.getProperty(reportingModule);
107        ConfigurationProperty coreRootProperty = cm.getProperty(coreModule);
108        
109        ConfigurationProperty kehfReporting = reportingRootProperty.getProperty("karEntryHandlerFactory");
110        ConfigurationProperty kehfCore = coreRootProperty.getProperty("karEntryHandlerFactory");
111        
112        List<ConfigurationProperty> handlers = kehfReporting.getProperties("karHandler");
113
114                for (ConfigurationProperty handler : handlers) {
115                        try {
116                                // System.out.println("checking if core prop " +
117                                // kehfCore.getName() +
118                                // " contains " + handler.getProperty("name").getValue());
119
120                                List<ConfigurationProperty> l = kehfCore.getProperties();
121                                boolean add = true;
122                                for (int i = 0; i < l.size(); i++) {
123                                        ConfigurationProperty cp = l.get(i);
124                                        String name = cp.getProperty("name").getValue();
125                                        if (name.equals(handler.getProperty("name").getValue())) {
126                                                add = false;
127                                        }
128                                }
129
130                                if (add) {
131                                        kehfCore.addProperty(handler);
132                                }
133                        } catch (Exception e) {
134                                e.printStackTrace();
135                        }
136                }
137        
138        
139        ConfigurationProperty commonProperty = cm.getProperty(ConfigurationManager.getModule("common"));
140        ConfigurationProperty reportingProperty = cm.getProperty(ConfigurationManager.getModule("reporting"));
141        ConfigurationProperty ecogridProperty = cm.getProperty(ConfigurationManager.getModule("ecogrid"));
142        ConfigurationProperty guiProperty = cm.getProperty(ConfigurationManager.getModule("gui"));
143          
144        //we need to override the common tab pane properties with the ones from reporting
145        ConfigurationProperty commonViewPaneTabPanesProp = commonProperty.getProperty("viewPaneTabPanes");
146        ConfigurationProperty reportingViewPaneTabPanesProp = reportingProperty.getProperty("viewPaneTabPanes");
147        commonProperty.overrideProperty(commonViewPaneTabPanesProp, reportingViewPaneTabPanesProp, true);
148        
149        ConfigurationProperty commonCanvasViewPaneLocationProp = commonProperty.getProperty("canvasViewPaneLocation");
150        ConfigurationProperty reportingCanvasViewPaneLocationProp = reportingProperty.getProperty("canvasViewPaneLocation");
151        commonProperty.overrideProperty(commonCanvasViewPaneLocationProp, reportingCanvasViewPaneLocationProp, true);
152        
153        ///override the servicesList using reporting configuration if exists
154        ConfigurationProperty ecogridServicesProp = ecogridProperty.getProperty("servicesList");
155        ConfigurationProperty reportingServicesProp = reportingProperty.getProperty("servicesList");
156        if (reportingServicesProp != null){
157                ecogridProperty.overrideProperty(ecogridServicesProp, reportingServicesProp, true);
158        }
159        
160        //override the viewPaneFactory
161        ConfigurationProperty guiViewPaneFactory = guiProperty.getProperty("viewPaneFactory");
162        ConfigurationProperty reportingViewPaneFactory = reportingProperty.getProperty("viewPaneFactory");
163        guiProperty.overrideProperty(guiViewPaneFactory, reportingViewPaneFactory, true);
164        
165        //override the tabPaneFactory
166        ConfigurationProperty guiTabPaneFactory = guiProperty.getProperty("tabPaneFactory");
167        ConfigurationProperty reportingTabPaneFactory = reportingProperty.getProperty("tabPaneFactory");
168        guiProperty.overrideProperty(guiTabPaneFactory, reportingTabPaneFactory, true);
169        
170        System.out.println("common tabpane configuration overridden by reporting");
171        
172        
173    }
174
175    /** Update the components. */
176    public void updateFrameComponents(KeplerGraphFrame.Components components)
177    {
178        final NamedObj model = components.getFrame().getModel().toplevel();
179        
180        // add the reporting listener
181        ReportingListener.addReportingListener(model, components.getFrame().getEffigy());
182
183        //add a close listener
184        final KeplerLSID workflowLSID = NamedObjId.getIdFor(model);
185        final KeplerGraphFrame frame = components.getFrame();
186        frame.addWindowListener(new WindowAdapter() {
187                public void windowClosed(WindowEvent e) {
188                        //is this the toplevel model?
189                        if (frame.getModel() == model) {
190                                //check the other open frames for this model
191                                Vector<TableauFrame> openFrames = KeplerGraphFrame.getOpenFrames();
192                                for (TableauFrame openFrame: openFrames) {
193                                         if (openFrame instanceof PtolemyFrame) {
194                                                 NamedObj openModel = ((PtolemyFrame)openFrame).getModel().toplevel();
195                                                 KeplerLSID openLSID = NamedObjId.getIdFor(openModel);
196                                                 if (openLSID.toStringWithoutRevision().equals(workflowLSID.toStringWithoutRevision())) {
197                                                         return;
198                                                 }
199                                         }
200                                }
201                                //clear the report layout in memory if we got here
202                                log.warn("removing workflow/layout entry for LSID: " + workflowLSID);
203                                WorkflowManager.getInstance().removeWorkflow(workflowLSID);
204                        }
205                }
206        });
207        
208                // Add the extra view menu to the main Kepler toolbar
209                JToolBar toolbar = components.getToolBar();
210                JPanel ddHolder = new JPanel();
211                ddHolder.setLayout(new BoxLayout(ddHolder, BoxLayout.X_AXIS));
212                ViewManager vman = ViewManager.getInstance();
213                JComponent vsel = vman.getViewSelector(frame);
214                
215                // but don't add the Report Designer to view drop down
216                // if we're not at top level of workflow (e.g. composite)
217                // see http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5100
218        ConfigurationManager cm = ConfigurationManager.getInstance();
219        ConfigurationProperty reportingProperty = cm.getProperty(ConfigurationManager.getModule("reporting"));
220        String reportDesignerName = reportingProperty.getProperty("reportDesignerName").getValue();
221        
222        if (frame.getModel() != model) {
223                if (vsel instanceof JComboBox){
224                        JComboBox jcb = (JComboBox)vsel;
225                        int itemCount = jcb.getItemCount();
226                        for(int i=0; i<itemCount; i++){
227                                Object item = jcb.getItemAt(i);
228                                if (item != null && item.toString() != null &&
229                                                item.toString().equals(reportDesignerName)){
230                                        jcb.removeItemAt(i);
231                                }
232                        }
233                        vman.setViewComboBox(frame, jcb);
234                        //reset vsel so if now only 1 item, we'll get the blank JLabel back
235                        vsel = vman.getViewSelector(frame);
236                }
237        }
238        //only add the View Menu Label if more than 1 item in dropdown
239                if (vsel instanceof JComboBox){
240                        JComboBox jcb = (JComboBox)vsel;
241                        int itemCount = jcb.getItemCount();
242                        if (itemCount > 1){
243                                ddHolder.add(new JLabel(VIEW_MENU_LABEL));
244                        }
245                }
246                ddHolder.add(vsel);
247                toolbar.add(ddHolder);
248
249    }
250    
251    
252        public void dispose(KeplerGraphFrame frame) {
253        }
254    
255}