001/* 
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: tao $'
006 * '$Date: 2010-07-23 17:04:30 -0700 (Fri, 23 Jul 2010) $' 
007 * '$Revision: 25146 $'
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.workflowscheduler.gui;
030
031import java.util.Vector;
032
033import javax.swing.JTree;
034import javax.swing.tree.DefaultTreeSelectionModel;
035import javax.swing.tree.TreePath;
036
037import org.kepler.gui.ComponentLibraryTab;
038
039/**
040 * A controller controls the relationship between schedule change and presentation(listener) change
041 * @author tao
042 *
043 */
044public class ScheduleChangeController
045{
046  private Vector <ScheduleChangeListenerInterface>listenerList = new Vector<ScheduleChangeListenerInterface>();
047  private ComponentLibraryTab componentTab = null;
048  private boolean firstTime = true;
049  private boolean showWarningMessageOnNoReporting = true;
050  /**
051   * Add a ScheduleChangeListenerInterface into the controller
052   * @param listener
053   */
054  public void addListener(ScheduleChangeListenerInterface listener)
055  {
056    listenerList.add(listener);
057  }
058  
059  /**
060   * Remove a listener from the controller
061   * @param listener
062   */
063  public void removeListener(ScheduleChangeListenerInterface listener)
064  {
065    listenerList.remove(listener);
066  }
067  
068  /**
069   * Handle the event that a new schedule is added
070   * @param schedule  the new schedule
071   * @throws Exception
072   */
073  public void addSchedule(Schedule schedule) throws Exception
074  {
075    for(int i=0; i<listenerList.size(); i++)
076    {
077      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
078      if(listener != null)
079      {
080        listener.addSchedule(schedule);
081      }
082    }
083  }
084  
085  /**
086   * Handle the event that a schedule is removed
087   * @param schedule the schedule will be removed
088   * @throws Exception
089   */
090  public void removeSchedule(Schedule schedule) throws Exception
091  {
092    for(int i=0; i<listenerList.size(); i++)
093    {
094      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
095      if(listener != null)
096      {
097        listener.removeSchedule(schedule);
098      }
099    }
100  }
101  
102  /**
103   * Handle the event that a schedule will be enabled
104   * @param schedule the schedule will be enabled
105   * @throws Exception
106   */
107  public void enableSchedule(Schedule schedule) throws Exception
108  {
109    for(int i=0; i<listenerList.size(); i++)
110    {
111      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
112      if(listener != null)
113      {
114        listener.enableSchedule(schedule);
115      }
116    }
117  }
118  
119  /**
120   * Handle the event that a schedule will be disabled
121   * @param schedule the schedule will be disabled
122   * @throws Exception
123   */
124  public void disableSchedule(Schedule schedule) throws Exception
125  {
126    for(int i=0; i<listenerList.size(); i++)
127    {
128      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
129      if(listener != null)
130      {
131        listener.disableSchedule(schedule);
132      }
133    }
134  }
135  
136  /**
137   * Handle the event that new schedules will replace the old one.
138   * @param newSchedule new schedules
139   * @throws Exception
140   */
141  public void update(Schedule[] newSchedules) throws Exception
142  {
143    for(int i=0; i<listenerList.size(); i++)
144    {
145      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
146      if(listener != null)
147      {
148        listener.update(newSchedules);
149      }
150    }
151  }
152  
153  /**
154   * Call this method when the SearchSchedules worker finishes its job
155   */
156  public void complete()
157  {
158    for(int i=0; i<listenerList.size(); i++)
159    {
160      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
161      if (listener != null &&listener instanceof SearchSchedulesWorkerListenerInterface)
162      {
163        
164        SearchSchedulesWorkerListenerInterface workerListener = 
165                    (SearchSchedulesWorkerListenerInterface)listener;     
166        workerListener.complete();
167      }
168     
169    }
170  }
171  
172  /**
173   * Call this method to start progress bar
174   */
175  public void startProgressBar()
176  {
177    for(int i=0; i<listenerList.size(); i++)
178    {
179      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
180      if (listener != null &&listener instanceof ProgressBarControlInterface)
181      {
182        
183        ProgressBarControlInterface workerListener = 
184                    (ProgressBarControlInterface)listener;     
185        workerListener.start();
186      }
187     
188    }
189  }
190  
191  /**
192   * Call this method to stop progress bar
193   */
194  public void stopProgressBar()
195  {
196    for(int i=0; i<listenerList.size(); i++)
197    {
198      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
199      if (listener != null &&listener instanceof ProgressBarControlInterface)
200      {
201        
202        ProgressBarControlInterface workerListener = 
203                    (ProgressBarControlInterface)listener;     
204        workerListener.stop();
205      }
206     
207    }
208  }
209  
210  /**
211   * Set the component library tab
212   * @param tab
213   */
214  public void setComponentLibraryTab(ComponentLibraryTab tab)
215  {
216    componentTab =tab;
217  }
218  
219  /**
220   *Disable selecting a node from tree
221   */
222  public void disableTreeSelection()
223  {
224    if(componentTab != null)
225    {
226      //System.out.println("component tab is not null");
227      JTree resultTree = componentTab.getResultTree();
228      if(resultTree != null)
229      {
230        //System.out.println("adding non-selectable tree selection model");
231        TreePath path = resultTree.getLeadSelectionPath();
232        resultTree.setSelectionModel(new NonSelectableTreeSelectionModel());
233        resultTree.setSelectionPath(path);
234      }
235    }
236    
237  }
238  
239  /**
240   *Enable selecting a node from tree
241   */
242  public void enableTreeSelection()
243  {
244    if(componentTab != null)
245    {
246      JTree resultTree = componentTab.getResultTree();
247      if(resultTree != null)
248      {
249        //System.out.println("adding default tree selection model");
250        firstTime = true;
251        TreePath path = resultTree.getLeadSelectionPath();
252        NonResponseTreePath nonResponsePath = null;
253        if(path != null)
254        {
255          nonResponsePath = new NonResponseTreePath(path);
256        }
257        resultTree.setSelectionModel(new DefaultTreeSelectionModel());
258        resultTree.setSelectionPath(nonResponsePath);
259      }
260    }
261    
262  }
263  
264  /**
265   * A non-selectable tree selection model but allow first selection
266   */
267  public class NonSelectableTreeSelectionModel extends DefaultTreeSelectionModel
268  {
269    
270    /**
271     * Do nothing on this method
272     */
273    public void setSelectionPath(TreePath path) 
274    {
275      if(firstTime)
276      {
277        //System.out.println("first time to set up the original one");
278        firstTime = false;
279        super.setSelectionPath(path);
280      }
281      /*else
282      {
283
284        System.out.println("NonSelectableTreeSelectionModel  do nothing at setSelectionPath(TreePath path)");
285      }*/
286      
287    }
288   
289  }
290  
291  /**
292   * Call the method when the uploading is done. 
293   * It will put the kar file lsid into a map. Then select the remote copy of the kar file to
294   * scheduler gui.
295   * @param KARFileLSID the lisd of the uploaded kar file
296   * @param KARFilePath the path of the local kar file
297   * @param workflowName the name of the workflow 
298   * @param workflowLSID the lsid of the workflow
299   * @param repositoryName the name of the remote repository
300   */
301  public void completeUploading(String KARFileLSID, String KARFilePath, String workflowName, 
302      String workflowLSID, String repositoryName)
303  {
304    for(int i=0; i<listenerList.size(); i++)
305    {
306      ScheduleChangeListenerInterface listener = listenerList.elementAt(i);
307      if (listener != null &&listener instanceof UploadingKARFileListenerInterface)
308      {
309        
310        UploadingKARFileListenerInterface workerListener = 
311                    (UploadingKARFileListenerInterface)listener;     
312        workerListener.completeUploading(KARFileLSID, KARFilePath, workflowName, 
313                                                       workflowLSID, repositoryName);
314      }
315     
316    }
317  }
318  
319  /**
320   * If the warning message will be shown if there is no reporting design on the workflow.
321   * @return true if the warning message will be shown. false otherwise.
322   */
323  public boolean showWarningMessageOnNoReporting()
324  {
325    return showWarningMessageOnNoReporting;
326  }
327  
328  /**
329   * Set the controller showing the warning message or not if there is no reporting design on the workflow.
330   * @param showWarningMessageOnNoReporting  true to show the warning message.
331   */
332  public void setShowWarningMessageOnNoReporting(boolean showWarningMessageOnNoReporting)
333  {
334    this.showWarningMessageOnNoReporting = showWarningMessageOnNoReporting;
335  }
336}