KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > externaltools > internal > program > launchConfigurations > BackgroundResourceRefresher


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.externaltools.internal.program.launchConfigurations;
12
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.debug.core.DebugEvent;
20 import org.eclipse.debug.core.DebugPlugin;
21 import org.eclipse.debug.core.IDebugEventSetListener;
22 import org.eclipse.debug.core.ILaunchConfiguration;
23 import org.eclipse.debug.core.model.IProcess;
24 import org.eclipse.debug.ui.RefreshTab;
25 import org.eclipse.ui.externaltools.internal.model.ExternalToolsPlugin;
26
27 /**
28  * Refreshes resources as specified by a launch configuration, when
29  * an associated process terminates.
30  */

31 public class BackgroundResourceRefresher implements IDebugEventSetListener {
32
33     private ILaunchConfiguration fConfiguration;
34     private IProcess fProcess;
35     
36     public BackgroundResourceRefresher(ILaunchConfiguration configuration, IProcess process) {
37         fConfiguration = configuration;
38         fProcess = process;
39     }
40     
41     /**
42      * If the process has already terminated, resource refreshing is done
43      * immediately in the current thread. Otherwise, refreshing is done when the
44      * process terminates.
45      */

46     public void startBackgroundRefresh() {
47         synchronized (fProcess) {
48             if (fProcess.isTerminated()) {
49                 refresh();
50             } else {
51                 DebugPlugin.getDefault().addDebugEventListener(this);
52             }
53         }
54     }
55     
56     /* (non-Javadoc)
57      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
58      */

59     public void handleDebugEvents(DebugEvent[] events) {
60         for (int i = 0; i < events.length; i++) {
61             DebugEvent event = events[i];
62             if (event.getSource() == fProcess && event.getKind() == DebugEvent.TERMINATE) {
63                 DebugPlugin.getDefault().removeDebugEventListener(this);
64                 refresh();
65                 break;
66             }
67         }
68     }
69     
70     /**
71      * Submits a job to do the refresh
72      */

73     protected void refresh() {
74         Job job= new Job(ExternalToolsProgramMessages.BackgroundResourceRefresher_0) {
75             public IStatus run(IProgressMonitor monitor) {
76                 try {
77                     RefreshTab.refreshResources(fConfiguration, monitor);
78                 } catch (CoreException e) {
79                     ExternalToolsPlugin.getDefault().log(e);
80                     return e.getStatus();
81                 }
82                 return Status.OK_STATUS;
83             }
84         };
85         job.schedule();
86     }
87 }
88
Popular Tags