View Javadoc

1   /*
2    * $Id: QueryJob.java,v 1.4 2005/05/31 19:37:20 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.editor.visualeditors.restriction;
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.jobs.Job;
15  
16  /***
17   * A subclass of {@link org.eclipse.core.runtime.jobs.Job} that 
18   * performs a computation and makes the result of that computation available 
19   * via a call to {@link #getQueryResult() }.
20   * 
21   * @author aperezlo
22   */
23  public abstract class QueryJob extends Job
24  {
25     private Object _queryAnswer;
26     
27     /***
28      * Creates a query job with the name "QueryJob".
29      */
30     protected QueryJob()
31     {
32        super("QueryJob");
33     }
34     
35     /***
36      * Returns the result of the query - this method blocks until processing 
37      * is complete. 
38      * 
39      * @return the result of the query
40      */
41     public synchronized Object getQueryResult()
42     {
43        while(_queryAnswer == null)
44        {
45           try
46           {
47              wait(200);
48              Thread.yield();
49           }
50           catch (InterruptedException e)
51           {
52              e.printStackTrace();
53              break;
54           }
55        }
56        return _queryAnswer;
57     }
58     
59     /***
60      * This method must be overrided by subclasses to perform the operations
61      * desired by this QueryJob.
62      * 
63      * @return an Object representing the results of the computation
64      */
65     protected abstract Object run();
66  
67     
68     /*
69      *  (non-Javadoc)
70      * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
71      */
72     protected synchronized IStatus run(IProgressMonitor monitor)
73     {
74        try
75        {
76           _queryAnswer = run();
77        }
78        finally
79        {
80           monitor.done();
81           notifyAll();
82        }
83        return Status.OK_STATUS;
84     }
85  }