3b1136a4765189065f69f19ccfbeaf15b07e664c
[phpeclipse.git] / net.sourceforge.phpeclipse.externaltools / src / net / sourceforge / phpdt / externaltools / internal / program / launchConfigurations / BackgroundResourceRefresher.java
1 package net.sourceforge.phpdt.externaltools.internal.program.launchConfigurations;
2
3 /**********************************************************************
4 Copyright (c) 2002 IBM Corp. and others. All rights reserved.
5 This file is made available under the terms of the Common Public License v1.0
6 which accompanies this distribution, and is available at
7 http://www.eclipse.org/legal/cpl-v10.html
8  
9 Contributors:
10 **********************************************************************/
11
12 import java.lang.reflect.InvocationTargetException;
13
14 import net.sourceforge.phpdt.externaltools.launchConfigurations.ExternalToolsUtil;
15 import net.sourceforge.phpdt.externaltools.variable.ExpandVariableContext;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
17
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.debug.core.DebugEvent;
21 import org.eclipse.debug.core.DebugPlugin;
22 import org.eclipse.debug.core.IDebugEventSetListener;
23 import org.eclipse.debug.core.ILaunchConfiguration;
24 import org.eclipse.debug.core.model.IProcess;
25 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
26 import org.eclipse.jface.operation.IRunnableWithProgress;
27
28 /**
29  * Refreshes resources as specified by a lanunch configuration, when 
30  * an associated process terminates.
31  */
32 public class BackgroundResourceRefresher implements IDebugEventSetListener, Runnable, IRunnableWithProgress  {
33
34         private ExpandVariableContext fContext;
35         private ILaunchConfiguration fConfiguration;
36         private IProcess fProcess;
37         
38         public BackgroundResourceRefresher(ILaunchConfiguration configuration, IProcess process, ExpandVariableContext context) {
39                 fConfiguration = configuration;
40                 fProcess = process;
41                 fContext = context;
42         }
43         
44         /**
45          * If the process has already terminated, resource refreshing is done
46          * immediately in the current thread. Otherwise, refreshing is done when the
47          * process terminates.
48          */
49         public void startBackgroundRefresh() {
50                 synchronized (fProcess) {
51                         if (fProcess.isTerminated()) {
52                                 refresh();
53                         } else {
54                                 DebugPlugin.getDefault().addDebugEventListener(this);
55                         }
56                 }
57         }
58         
59         /**
60          * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent)
61          */
62         public void handleDebugEvents(DebugEvent[] events) {
63                 for (int i = 0; i < events.length; i++) {
64                         DebugEvent event = events[i];
65                         if (event.getSource() == fProcess && event.getKind() == DebugEvent.TERMINATE) {
66                                 DebugPlugin.getDefault().removeDebugEventListener(this);
67                                 refresh();
68                                 break;
69                         }
70                 }
71         }
72         
73         /**
74          * Submits a runnable to do the refresh
75          */
76         protected void refresh() {
77                 PHPeclipsePlugin.getStandardDisplay().asyncExec(this);
78         }
79         
80         /** 
81          * Creates a dialog to run the refresh
82          * 
83          * @see java.lang.Runnable#run()
84          */
85         public void run() {
86                 ProgressMonitorDialog dialog = new ProgressMonitorDialog(PHPeclipsePlugin.getStandardDisplay().getActiveShell());
87                 try {
88                         dialog.run(true, true, this);
89                 } catch (InvocationTargetException e) {
90                         // report the exception
91                 } catch (InterruptedException e) {
92                 }
93         }
94         /**
95          * Peforms the refresh
96          * 
97          * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
98          */
99         public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
100                 try {
101                         ExternalToolsUtil.refreshResources(fConfiguration, fContext, monitor);
102                 } catch (CoreException e) {
103                         throw new InvocationTargetException(e);
104                 }                               
105         }
106
107 }