Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8bf984e0bbbda0feea1b5ed73d2dc044e786c0ac (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 Axel Mueller 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:
 *     Axel Mueller - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.ui.actions;

import org.eclipse.cdt.make.core.IMakeTarget;
import org.eclipse.cdt.make.core.MakeCorePlugin;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.internal.ui.preferences.MakePreferencePage;
import org.eclipse.cdt.make.ui.TargetBuild;
import org.eclipse.cdt.make.ui.dialogs.BuildTargetDialog;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.window.Window;

/**
 * Rebuild last target of selected resource or project.
 * Search is done non-recursively.
 * If no valid last target is found, show the build target dialog.
 *
 * @since 7.0
 *
 * @noextend This class is not intended to be subclassed by clients.
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class BuildLastTargetAction extends AbstractTargetAction {

	@Override
	public void run(IAction action) {
		IContainer container = getSelectedContainer();
		if (container != null) {
			String name = null;
			if (MakePreferencePage.useProjectLastMakeTarget()) {
				try {
					name = (String) container.getProject().getSessionProperty(
							new QualifiedName(MakeUIPlugin.getUniqueIdentifier(), TargetBuild.LAST_TARGET_CONTAINER));
					if (name != null) {
						IContainer lastTargetContainer;
						if (name.length() == 0)
							lastTargetContainer = container.getProject();
						else
							lastTargetContainer = container.getProject().getFolder(new Path(name));
						if (lastTargetContainer.exists())
							container = lastTargetContainer;
						name = (String) container.getSessionProperty(
								new QualifiedName(MakeUIPlugin.getUniqueIdentifier(), TargetBuild.LAST_TARGET));
					}
				} catch (CoreException e) {
				}
			} else {
				if (MakePreferencePage.useProjectRootForLastMakeTarget()) {
					container = container.getProject();
				}
				try {
					name = (String) container.getSessionProperty(
							new QualifiedName(MakeUIPlugin.getUniqueIdentifier(), TargetBuild.LAST_TARGET));
				} catch (CoreException e) {
				}
			}
			try {
				boolean showDialog = true;
				if (name != null) {
					IMakeTarget target = MakeCorePlugin.getDefault().getTargetManager().findTarget(container, name);
					if (target != null) {
						TargetBuild.buildTargets(getShell(), new IMakeTarget[] { target });
						showDialog = false;
						IPath path = container.getProjectRelativePath();
						container.getProject().setSessionProperty(new QualifiedName(MakeUIPlugin.getUniqueIdentifier(),
								TargetBuild.LAST_TARGET_CONTAINER), path.toString());
					}
				}

				// no last target found, let the user decide
				if (showDialog) {
					boolean recursive = MakePreferencePage.useProjectLastMakeTarget();
					BuildTargetDialog dialog = new BuildTargetDialog(getShell(), container, recursive);
					if (dialog.open() == Window.OK) {
						IMakeTarget target = dialog.getTarget();
						if (target != null) {
							container.setSessionProperty(
									new QualifiedName(MakeUIPlugin.getUniqueIdentifier(), TargetBuild.LAST_TARGET),
									target.getName());
							IPath path = target.getContainer().getProjectRelativePath();
							container.getProject()
									.setSessionProperty(new QualifiedName(MakeUIPlugin.getUniqueIdentifier(),
											TargetBuild.LAST_TARGET_CONTAINER), path.toString());
						}
					}
				}
			} catch (CoreException e) {
			}
		}
	}

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

Back to the top