A massive organize imports and formatting of the sources using default Eclipse code...
[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.externaltools.ExternalToolsPlugin;
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 an
30  * associated process terminates.
31  */
32 public class BackgroundResourceRefresher implements IDebugEventSetListener,
33                 Runnable, IRunnableWithProgress {
34
35         private ExpandVariableContext fContext;
36
37         private ILaunchConfiguration fConfiguration;
38
39         private IProcess fProcess;
40
41         public BackgroundResourceRefresher(ILaunchConfiguration configuration,
42                         IProcess process, ExpandVariableContext context) {
43                 fConfiguration = configuration;
44                 fProcess = process;
45                 fContext = context;
46         }
47
48         /**
49          * If the process has already terminated, resource refreshing is done
50          * immediately in the current thread. Otherwise, refreshing is done when the
51          * process terminates.
52          */
53         public void startBackgroundRefresh() {
54                 synchronized (fProcess) {
55                         if (fProcess.isTerminated()) {
56                                 refresh();
57                         } else {
58                                 DebugPlugin.getDefault().addDebugEventListener(this);
59                         }
60                 }
61         }
62
63         /**
64          * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent)
65          */
66         public void handleDebugEvents(DebugEvent[] events) {
67                 for (int i = 0; i < events.length; i++) {
68                         DebugEvent event = events[i];
69                         if (event.getSource() == fProcess
70                                         && event.getKind() == DebugEvent.TERMINATE) {
71                                 DebugPlugin.getDefault().removeDebugEventListener(this);
72                                 refresh();
73                                 break;
74                         }
75                 }
76         }
77
78         /**
79          * Submits a runnable to do the refresh
80          */
81         protected void refresh() {
82                 ExternalToolsPlugin.getStandardDisplay().asyncExec(this);
83         }
84
85         /**
86          * Creates a dialog to run the refresh
87          * 
88          * @see java.lang.Runnable#run()
89          */
90         public void run() {
91                 ProgressMonitorDialog dialog = new ProgressMonitorDialog(
92                                 ExternalToolsPlugin.getStandardDisplay().getActiveShell());
93                 try {
94                         dialog.run(true, true, this);
95                 } catch (InvocationTargetException e) {
96                         // report the exception
97                 } catch (InterruptedException e) {
98                 }
99         }
100
101         /**
102          * Peforms the refresh
103          * 
104          * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
105          */
106         public void run(IProgressMonitor monitor) throws InvocationTargetException,
107                         InterruptedException {
108                 try {
109                         ExternalToolsUtil.refreshResources(fConfiguration, fContext,
110                                         monitor);
111                 } catch (CoreException e) {
112                         throw new InvocationTargetException(e);
113                 }
114         }
115
116 }