summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor.ui/src/rba/tool/editor/ui/editor/model/edit/refactoring/RBAModelSyncUtil.java
blob: ae6cfb3cedb95d46a99d538488ebf6e254ce491e (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package rba.tool.editor.ui.editor.model.edit.refactoring;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.service.OperationCanceledError;
import org.eclipse.xtext.ui.editor.XtextEditor;
import org.eclipse.xtext.ui.editor.model.XtextDocument;
import org.eclipse.xtext.util.concurrent.IUnitOfWork;

import com.google.inject.Inject;

public class RBAModelSyncUtil {

    @Inject(optional = true)
    private IWorkbench workbench;

    @Inject(optional = true)
    private IWorkspace workspace;

    public void totalSync(final boolean saveAll, boolean useProgressDialog) throws InvocationTargetException, InterruptedException {
        totalSync(saveAll, useProgressDialog, true);
    }

    public void totalSync(final boolean saveAll, boolean useProgressDialog, boolean fork) throws InvocationTargetException, InterruptedException {
        if (Display.getCurrent() != null && workbench != null) {
            if (useProgressDialog) {
                workbench.getProgressService().run(fork, true, new IRunnableWithProgress() {
                    @Override
                    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                        doReconcileAndBuild(saveAll, monitor);
                    }
                });
            } else {
                doReconcileAndBuild(saveAll, null);
            }
        }
    }

    private void doReconcileAndBuild(final boolean saveAll, IProgressMonitor monitor) throws InterruptedException {
        try {
            SubMonitor progress = SubMonitor.convert(monitor, 10);
            reconcileAllEditors(workbench, saveAll, progress.newChild(1));
            if (progress.isCanceled()) {
                throw new InterruptedException();
            }
            waitForBuild(progress.newChild(4));
            if (progress.isCanceled()) {
                throw new InterruptedException();
            }
            yieldToQueuedDisplayJobs(progress.newChild(1));
            if (progress.isCanceled()) {
                throw new InterruptedException();
            }
            waitForAutoBuild(progress.newChild(4));
        } catch (OperationCanceledException e) {
            throw new InterruptedException();
        }
    }

    public void totalSync(final boolean saveAll) throws InvocationTargetException, InterruptedException {
        totalSync(saveAll, true);
    }

    public void reconcileAllEditors(IWorkbench workbench, final boolean saveAll, final IProgressMonitor monitor) {
        for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
            for (IWorkbenchPage page : window.getPages()) {
                for (IEditorReference editorReference : page.getEditorReferences()) {
                    if (monitor.isCanceled())
                        return;
                    final IEditorPart editor = editorReference.getEditor(false);
                    if (editor != null) {
                        if (editor instanceof XtextEditor) {
                            waitForReconciler((XtextEditor) editor);
                        }
                        if (saveAll) {
                            Display display = workbench.getDisplay();
                            display.syncExec(new Runnable() {
                                @Override
                                public void run() {
                                    if (editor.isDirty()) {
                                        editor.doSave(monitor);
                                    }
                                }
                            });
                        }
                    }
                }
            }
        }
    }

    /**
     * this methods blocks until the following jobs have finished, - the reconciler - the editor validation job - the dirty
     * state editor updater job
     */
    public void waitForReconciler(XtextEditor editor) {
        try {
            editor.getDocument().readOnly(new IUnitOfWork.Void<XtextResource>() {
                @Override
                public void process(XtextResource state) throws Exception {
                    // this doesn't execute before the reconciler has finished
                }
            });
            // reconciling schedules both, validation and dirty state
            Job validationJob = ((XtextDocument) editor.getDocument()).getValidationJob();
            validationJob.join();
            editor.getDirtyStateEditorSupport().waitForUpdateEditorJob();
        } catch (OperationCanceledException e) {
        } catch (OperationCanceledError e) {
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void waitForBuild(IProgressMonitor monitor) {
        try {
            workspace.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
        } catch (CoreException e) {
            throw new OperationCanceledException(e.getMessage());
        }
    }

    /**
     * @deprecated we should not rely on auto build to be triggered. Use {@link #waitForBuild(IProgressMonitor)} instead.
     */
    @Deprecated
    public void waitForAutoBuild(IProgressMonitor monitor) {
        boolean wasInterrupted = false;
        do {
            try {
                Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, monitor);
                wasInterrupted = false;
            } catch (OperationCanceledException e) {
                throw e;
            } catch (InterruptedException e) {
                wasInterrupted = true;
            }
        } while (wasInterrupted);
    }

    public void yieldToQueuedDisplayJobs(IProgressMonitor monitor) {
        yieldToQueuedDisplayJobs(monitor, 50000);
    }

    public void yieldToQueuedDisplayJobs(IProgressMonitor monitor, int maxJobsToYieldTo) {
        int count = 0;
        if (Display.getCurrent() != null) {
            while (count < maxJobsToYieldTo && Display.getCurrent().readAndDispatch()) {
                if (monitor.isCanceled())
                    throw new OperationCanceledException();
                ++count;
            }
            if (count == maxJobsToYieldTo) {
                System.out.println("maxJobsToYieldTo probably exceeded. Worked: " + count);
            }
        }
    }

}