KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > jobs > RunnableContextJobAdapter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.jobs;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.core.jobs.AbstractEclipseJob;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
30 import org.eclipse.jface.operation.IRunnableContext;
31 import org.eclipse.jface.operation.IRunnableWithProgress;
32 import org.eclipse.swt.widgets.Display;
33
34
35 /**
36  * This class provides some convinience methods to execute a job within
37  * an {@link IRunnableContext}.
38  *
39  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
40  * @version $Rev$, $Date$
41  */

42 public class RunnableContextJobAdapter
43 {
44
45     /**
46      * Executes the given job within a new {@link ProgressMonitorDialog}.
47      *
48      * @param job the job to execute
49      */

50     public static void execute( final AbstractEclipseJob job )
51     {
52         execute( job, null );
53     }
54
55
56     /**
57      * Executes the given job within the given runnable context and enabled error handling
58      *
59      * @param runnableContext the runnable context
60      * @param job the job to execute
61      */

62     public static void execute( final AbstractEclipseJob job, IRunnableContext runnableContext )
63     {
64         execute( job, runnableContext, true );
65     }
66
67
68     /**
69      * Executes the given job within the given runnable context.
70      *
71      * @param runnableContext the runnable context
72      * @param job the job to execute
73      * @param handleError true to handle errors
74      */

75     public static void execute( final AbstractEclipseJob job, IRunnableContext runnableContext, boolean handleError )
76     {
77
78         if ( runnableContext == null )
79         {
80             runnableContext = new ProgressMonitorDialog( Display.getDefault().getActiveShell() );
81         }
82
83         IRunnableWithProgress runnable = new IRunnableWithProgress()
84         {
85             public void run( IProgressMonitor ipm ) throws InterruptedException JavaDoc
86             {
87                 job.setExternalProgressMonitor( ipm );
88                 job.execute();
89                 job.join();
90             }
91         };
92
93         try
94         {
95             runnableContext.run( true, true, runnable );
96         }
97         catch ( Exception JavaDoc ex )
98         {
99             BrowserCommonActivator.getDefault().getExceptionHandler().handleException(
100                 new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, IStatus.ERROR, ex.getMessage(), ex ) );
101         }
102
103         if ( handleError && !job.getExternalResult().isOK() )
104         {
105             IStatus status = job.getExternalResult();
106             BrowserCommonActivator.getDefault().getExceptionHandler().handleException( status );
107         }
108
109     }
110
111 }
112
Popular Tags