001/**
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: tao $'
006 * '$Date: 2010-06-03 16:45:10 -0700 (Thu, 03 Jun 2010) $' 
007 * '$Revision: 24730 $'
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.awt.event.MouseListener;
032import java.util.Iterator;
033import java.util.List;
034import java.util.Vector;
035
036import javax.swing.JTree;
037
038import org.kepler.configuration.ConfigurationProperty;
039import org.kepler.gui.AnnotatedPTree;
040import org.kepler.gui.ComponentLibraryTab;
041import org.kepler.gui.popups.LibraryPopupListener;
042import org.kepler.module.workflowschedulergui.Initialize;
043import org.kepler.objectmanager.library.LibraryManager;
044import org.kepler.sms.NamedOntModel;
045import org.kepler.sms.OntologyCatalog;
046
047import ptolemy.actor.gui.TableauFrame;
048import ptolemy.kernel.util.IllegalActionException;
049import ptolemy.kernel.util.NameDuplicationException;
050import ptolemy.moml.EntityLibrary;
051import ptolemy.vergil.tree.EntityTreeModel;
052
053/**
054 * Class represents the component library tab on the left side of the scheduler 
055 * @author tao
056 *
057 */
058public class WorkflowSchedulerComponentLibraryTab extends ComponentLibraryTab
059{
060  //private String[] entityNameFilter ={"Components", "Projects", "Statistics", "Actors","Directors", "Remote Components"}; 
061  private EntityLibrary newLibrary;
062  private Vector<String> nameFilter = new Vector<String>();
063  private static final String SEARCHFILTER = "componentSearchFilter";
064  private static final String NAME = "name";
065  
066  /**
067   * Constructor
068   * @param parent parent frame of this tab
069   */
070  public WorkflowSchedulerComponentLibraryTab(TableauFrame parent) 
071  {
072    super(parent);  
073    readOntologyLibraryName();
074    readComponentFilter();
075  }
076  
077  
078  /*
079   * Init the library tree for library panel.
080   */
081  protected void initLibraryTree()
082  {
083  // get the tree model from the LibraryIndex
084    _libraryModel = LibraryManager.getInstance().getTreeModel(getFilterFile());
085    removeChildEntityFromModel(); 
086    _libraryModel = new EntityTreeModel(newLibrary);
087    _library = new AnnotatedPTree(_libraryModel, this);
088    MouseListener mListener = new LibraryPopupListener(_library);
089    _library.setMouseListener(mListener);
090    _library.initAnotatedPTree();
091    LibraryManager.getInstance().addLibraryJTree((JTree) _library);
092
093    LibraryActionHandler handler = new LibraryActionHandler();
094    _library.addListener(handler);
095    _library.setRootVisible(false);
096  }
097  
098  /*
099   * Remove a child entity from the model
100   */
101  private void removeChildEntityFromModel()
102  {
103    if (_libraryModel != null) 
104    {
105      
106      Object rootObj =  _libraryModel.getRoot();
107      //System.out.println("======root object is "+rootObj.getClass().getName());
108      if(rootObj != null && rootObj instanceof EntityLibrary)
109      {
110        //System.out.println("in if statement");
111        EntityLibrary root = (EntityLibrary)rootObj;
112        newLibrary = new EntityLibrary(root.workspace());
113        //System.out.println("original size is "+_libraryModel.getChildCount(root));
114        for(int i=0; i<_libraryModel.getChildCount(root); i++)
115        {
116          //System.out.println("in for loop2");
117          Object childObj = _libraryModel.getChild(root, i);
118          if(childObj != null && childObj instanceof EntityLibrary)
119          {
120            EntityLibrary child = (EntityLibrary)childObj;
121            //System.out.println("child name is "+child.getName());
122            if(child.getName() != null)
123            {
124              boolean match = false;
125              for(int j=0; j<nameFilter.size();j++)
126              {
127                String entityName = nameFilter.elementAt(j);
128                if(child.getName().equals(entityName))
129                {
130                  match = true;
131                  break;
132                }
133              }
134              if(!match)
135              {
136                try
137                {
138                  EntityLibrary newOne = (EntityLibrary) child.clone();
139                  //System.out.println("Add child "+child.getName()+" to the new libarry");
140                  newOne.setContainer(newLibrary);
141                }
142                catch (IllegalActionException e) 
143                {
144                  e.printStackTrace();
145                } 
146                catch (NameDuplicationException e) 
147                {
148                  e.printStackTrace();
149                }
150                catch (CloneNotSupportedException e)
151                {
152                  e.printStackTrace();
153                }
154              }            
155            }
156          }
157        }
158     
159      }
160     
161      
162    }
163  }
164  
165  
166  /*
167   * Read the name of ontology library and put them into the filter.
168   */
169  private void readOntologyLibraryName()
170  {
171    OntologyCatalog ontology = OntologyCatalog.instance();
172    Iterator<NamedOntModel> it =ontology.getNamedOntModels(true);
173    while(it.hasNext())
174    {
175      NamedOntModel model = it.next();
176      if(model != null)
177      {
178        nameFilter.add(model.getTopLevelLabel());
179      }
180    }
181  }
182  
183  /*
184   * Read local component filter from configuration file.
185   */
186  private void readComponentFilter()
187  {
188    ConfigurationProperty configuration = Initialize.getCurrentModuleProperty();
189    if(configuration != null)
190    {
191      
192        ConfigurationProperty filterProperty = configuration.getProperty(SEARCHFILTER);
193        if( filterProperty != null)
194        {
195          List<ConfigurationProperty> nameList =  filterProperty.getProperties(NAME);
196          if(nameList != null)
197          {
198            Iterator<ConfigurationProperty> iter =nameList.iterator();
199            while(iter.hasNext())
200            {
201              nameFilter.add(iter.next().getValue());
202            }
203          }
204        }     
205     
206    }
207  }
208
209
210}