View Javadoc

1   /*
2    * $Id: RefreshLibraryJob.java,v 1.6 2005/06/01 19:55:51 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.libraries;
10  
11  import org.eclipse.core.runtime.IProgressMonitor;
12  import org.eclipse.core.runtime.IStatus;
13  import org.eclipse.core.runtime.Status;
14  import org.eclipse.core.runtime.SubProgressMonitor;
15  import org.eclipse.core.runtime.jobs.Job;
16  
17  import com.bbn.swede.core.OWLCore;
18  
19  /***
20   * An Eclipse job that updates a library.  This is the preferred way 
21   * to refresh a library because it provides support for grouping 
22   * operations into families, and also for Eclipse provided scheduling 
23   * and background processing.
24   * 
25   * @see Job
26   * @author aperezlo
27   */
28  public class RefreshLibraryJob extends Job implements Runnable
29  {
30     private ILibraryDescriptor _library;
31     private String _jobFamily;
32     private IProgressMonitor _progress;
33     
34     /***
35      * Create a new refresh job.
36      * 
37      * @param ild the library to be updated
38      * @param family the job family name, or null
39      */
40     public RefreshLibraryJob(ILibraryDescriptor ild, String family)
41     {
42        this(ild, family, false);
43     }
44     
45     /***
46      * Creates a job to refresh an Ontology Library.
47      * @param libraryDescriptor The library
48      * @param jobFamily The name of the job family
49      * @param isUser <code>true</code> if this is a user job, <code>false</code>
50      *               if not
51      */
52     public RefreshLibraryJob(ILibraryDescriptor libraryDescriptor, String jobFamily, boolean isUser)
53     {
54        super("Refreshing Library...");
55        if(libraryDescriptor.getName() != null)
56        {
57           setName("Refreshing '" + libraryDescriptor.getName() + "'...");
58        }
59        _jobFamily = jobFamily;
60        setPriority(Job.BUILD);
61        setUser(isUser);
62        _library = libraryDescriptor;
63     }
64  
65  
66     
67     /***
68      * Compares family names.  
69      * 
70      * @param family Family name to compare to
71      * @return <code>true</code> if this job belongs to the specified family,
72      *         <code>false</code> if not.
73      * @see org.eclipse.core.internal.jobs.InternalJob#belongsTo(java.lang.Object)
74      */
75     public boolean belongsTo(Object family)
76     {
77        boolean toReturn = false;
78        if(_jobFamily != null)
79        {
80           if(_jobFamily.equals(family))
81           {
82              toReturn = true;
83           }
84        }
85        return toReturn;
86     }
87     
88     /* (non-Javadoc)
89      * @see java.lang.Runnable#run()
90      */
91     public void run()
92     {
93        String name = _library.getName();
94        IStatus toReturn = null;
95        _progress.beginTask("Refreshing '" + name + "'...", 100);
96        try
97        {
98           toReturn = new Status(IStatus.OK, OWLCore.getID(), IStatus.OK, "Refresh pending...", null); 
99           if(_library != null)
100          {
101             toReturn = _library.refresh(new SubProgressMonitor(_progress, 100));
102          }
103       }
104       finally
105       {
106          _progress.done();
107          if(_progress.isCanceled())
108          {
109             toReturn = Status.CANCEL_STATUS;
110          }
111          done(toReturn);
112       }
113    }
114    
115    
116    /***
117     * This operation does not respect cancelation.
118     * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
119     */
120    protected IStatus run(IProgressMonitor monitor)
121    {
122       _progress = monitor;
123       Thread t = new Thread(this);
124       setThread(t);
125       t.start();
126       return Job.ASYNC_FINISH;
127    }
128 }