Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 907bb5fee0a2dd6b06a9e86b689654ac27ef91aa (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
 * Rapperswil, University of applied sciences and others
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Institute for Software - initial API and implementation
 *     Marc-Andre Laperle
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.implementmethod;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.ltk.ui.refactoring.RefactoringWizard;

/**
 * @author Mirko Stocker
 */
public class ImplementMethodWizard extends RefactoringWizard {
	private final ImplementMethodRefactoring refactoring;
	private Map<MethodToImplementConfig, ParameterNamesInputPage> pagesMap = new HashMap<MethodToImplementConfig, ParameterNamesInputPage>();

	public ImplementMethodWizard(ImplementMethodRefactoring refactoring) {
		super(refactoring, DIALOG_BASED_USER_INTERFACE | PREVIEW_EXPAND_FIRST_NODE);
		setDefaultPageTitle(Messages.ImplementMethodInputPage_PageTitle);
		this.refactoring = refactoring;
	}

	@Override
	protected void addUserInputPages() {
		addPage(new ImplementMethodInputPage(refactoring.getRefactoringData(), this));
		ImplementMethodData data = refactoring.getRefactoringData();
		for (MethodToImplementConfig config : data.getMethodDeclarations()) {
			if (config.getParaHandler().needsAdditionalArgumentNames()) {
				ParameterNamesInputPage page = new ParameterNamesInputPage(config, this);
				pagesMap.put(config, page);
				addPage(page);
			}
		}
	}

	public ParameterNamesInputPage getPageForConfig(MethodToImplementConfig config) {
		return pagesMap.get(config);
	}

	/**
	 * When canceling the wizard, CRefactoringContext gets disposed and releases the lock on
	 * the index but the preview jobs might still be running and access the index or an index-based
	 * AST so we need to make sure they are done before disposing the cache
	 * <p>
	 * When proceeding to the last page and finishing the wizard, the refactoring will run
	 * and possibly use concurrently the same ASTs that the jobs use, so we need to make
	 * sure the jobs are joined.
	 */
	protected void cancelAndJoinPreviewJobs() {
		boolean isOnePreviewJobRunning = false;
		for (ParameterNamesInputPage parameterNamesInputPage : pagesMap.values()) {
			isOnePreviewJobRunning |= parameterNamesInputPage.cancelPreviewJob();
		}

		// There are good chances that one job is still running, show a progress bar to the user,
		// join everything.
		if (isOnePreviewJobRunning) {
			try {
				getContainer().run(false, false, new IRunnableWithProgress() {
					@Override
					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
						monitor.beginTask(Messages.ImplementMethodRefactoringWizard_CancelingPreviewGeneration,
								pagesMap.size() + 1);
						monitor.worked(1);

						for (ParameterNamesInputPage parameterNamesInputPage : pagesMap.values()) {
							parameterNamesInputPage.joinPreviewJob();
							monitor.worked(1);
						}

						monitor.done();
					}
				});
			} catch (InvocationTargetException e) {
				CUIPlugin.log(e);
			} catch (InterruptedException e) {
				// ignore since not cancelable
			}
		} else {
			// We don't take any chances, we still join everything. But there are good chances that
			// the jobs are stopped so we don't show a progress bar.
			for (ParameterNamesInputPage parameterNamesInputPage : pagesMap.values()) {
				parameterNamesInputPage.joinPreviewJob();
			}
		}
	}

	@Override
	public boolean performCancel() {
		cancelAndJoinPreviewJobs();
		return super.performCancel();
	}

	@Override
	public boolean performFinish() {
		cancelAndJoinPreviewJobs();
		return super.performFinish();
	}
}

Back to the top