~vil/pydev/upstream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Created on Oct 18, 2004
 *
 * @author Fabio Zadrozny
 */
package org.python.pydev.utils;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.actions.WorkspaceModifyOperation;

/**
 * Helper class for executing an action and showing its progress.
 * 
 * @author Fabio Zadrozny
 */
public class ProgressOperation extends WorkspaceModifyOperation {
    private final ProgressAction action;

    public IProgressMonitor monitor;
    public int estimatedTaskUnits = 10000;

    public ProgressOperation(ProgressAction action) {
        super();
        this.action = action;
    }

    protected void execute(IProgressMonitor monitor) throws CoreException,
            InvocationTargetException, InterruptedException {

        try {
            this.monitor = monitor;
            action.monitor = monitor;
            monitor.beginTask("Action being executed...", estimatedTaskUnits);
            action.run();
            monitor.done();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
    /**
     * @param shell
     * 
     */
    public static void startAction(Shell shell, ProgressAction action) {
        ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(
                shell);
        monitorDialog.setBlockOnOpen(false);
        try {
            IRunnableWithProgress operation = new ProgressOperation(action);
            monitorDialog.run(false, false, operation);
            // Perform the action
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}