diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTarget.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTarget.java index cea07848e87..4fc48ffdfa1 100644 --- a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTarget.java +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTarget.java @@ -11,6 +11,7 @@ package org.eclipse.cdt.make.core; import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; @@ -22,6 +23,8 @@ public interface IMakeTarget extends IAdaptable, IMakeCommonBuildInfo { String getName(); String getTargetBuilderID(); + IProject getProject(); + /** * @deprecated */ diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTargetManager.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTargetManager.java index 0b7c77f87f0..c6d8dc21337 100644 --- a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTargetManager.java +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/core/IMakeTargetManager.java @@ -16,10 +16,25 @@ import org.eclipse.core.runtime.CoreException; public interface IMakeTargetManager { IMakeTarget createTarget(IProject project, String targetName, String targetBuilderID) throws CoreException; + /** + * Adds target to manager. + * @param target + * @throws CoreException + */ + void addTarget(IMakeTarget target) throws CoreException; + + /** + * Adds target to manager on a specific projects folder. + * @param container + * @param target + * @throws CoreException + */ void addTarget(IContainer container, IMakeTarget target) throws CoreException; void removeTarget(IMakeTarget target) throws CoreException; void renameTarget(IMakeTarget target, String name) throws CoreException; - + + boolean targetExists(IMakeTarget target); + IMakeTarget[] getTargets(IContainer container) throws CoreException; IMakeTarget findTarget(IContainer container, String name) throws CoreException; diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTarget.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTarget.java index e607326a812..798da822c8a 100644 --- a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTarget.java +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTarget.java @@ -38,6 +38,7 @@ import org.eclipse.osgi.service.environment.Constants; public class MakeTarget extends PlatformObject implements IMakeTarget { private final MakeTargetManager manager; + private final IProject project; private String name; private boolean isDefaultBuildCmd; private boolean isStopOnError; @@ -50,6 +51,7 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { MakeTarget(MakeTargetManager manager, IProject project, String targetBuilderID, String name) throws CoreException { this.manager = manager; + this.project = project; this.targetBuilderID = targetBuilderID; this.name = name; IMakeBuilderInfo info = MakeCorePlugin.createBuildInfo(project, manager.getBuilderID(targetBuilderID)); @@ -61,6 +63,10 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { buildEnvironment = info.getEnvironment(); } + public IProject getProject() { + return project; + } + public void setContainer(IContainer container) { this.container = container; } @@ -103,7 +109,7 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { if (isDefaultBuildCmd()) { IMakeBuilderInfo info; try { - info = MakeCorePlugin.createBuildInfo(container.getProject(), manager.getBuilderID(targetBuilderID)); + info = MakeCorePlugin.createBuildInfo(getProject(), manager.getBuilderID(targetBuilderID)); return info.getBuildCommand(); } catch (CoreException e) { } @@ -176,7 +182,7 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { public String[] getErrorParsers() { IMakeBuilderInfo projectInfo; try { - projectInfo = MakeCorePlugin.createBuildInfo(container.getProject(), manager.getBuilderID(targetBuilderID)); + projectInfo = MakeCorePlugin.createBuildInfo(getProject(), manager.getBuilderID(targetBuilderID)); return projectInfo.getErrorParsers(); } catch (CoreException e) { } @@ -249,7 +255,6 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { } public void build(IProgressMonitor monitor) throws CoreException { - final IProject project = container.getProject(); final String builderID = manager.getBuilderID(targetBuilderID); final HashMap infoMap = new HashMap(); @@ -265,7 +270,7 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { if (container != null) { info.setBuildAttribute(IMakeCommonBuildInfo.BUILD_LOCATION, container.getFullPath().toString()); } - IMakeBuilderInfo projectInfo = MakeCorePlugin.createBuildInfo(project, builderID); + IMakeBuilderInfo projectInfo = MakeCorePlugin.createBuildInfo(getProject(), builderID); info.setErrorParsers(projectInfo.getErrorParsers()); IWorkspaceRunnable op = new IWorkspaceRunnable() { @@ -301,7 +306,7 @@ public class MakeTarget extends PlatformObject implements IMakeTarget { public Object getAdapter(Class adapter) { if (adapter.equals(IProject.class)) { - return container.getProject(); + return getProject(); } else if (adapter.equals(IResource.class)) { return container; } diff --git a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java index 1e3eabd3ddb..1ed60d2f148 100644 --- a/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java +++ b/build/org.eclipse.cdt.make.core/src/org/eclipse/cdt/make/internal/core/MakeTargetManager.java @@ -59,19 +59,19 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis return new MakeTarget(this, project, targetBuilderID, name); } + public void addTarget(IMakeTarget target) throws CoreException { + addTarget(null, target); + } + public void addTarget(IContainer container, IMakeTarget target) throws CoreException { if (container instanceof IWorkspaceRoot) { throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeMessages.getString("MakeTargetManager.add_to_workspace_root"), null)); //$NON-NLS-1$ } - if (target.getContainer() != null) { - throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeMessages.getString("MakeTargetManager.add_temporary_target"), null)); //$NON-NLS-1$ - } - IProject project = container.getProject(); - ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project); + ProjectTargets projectTargets = (ProjectTargets)projectMap.get(target.getProject()); if (projectTargets == null) { - projectTargets = readTargets(project); + projectTargets = readTargets(target.getProject()); } - ((MakeTarget) target).setContainer(container); + ((MakeTarget) target).setContainer(container == null ? target.getProject() : container); projectTargets.add((MakeTarget) target); try { writeTargets(projectTargets); @@ -82,11 +82,18 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis notifyListeners(new MakeTargetEvent(this, MakeTargetEvent.TARGET_ADD, target)); } - public void removeTarget(IMakeTarget target) throws CoreException { - IProject project = target.getContainer().getProject(); - ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project); + public boolean targetExists(IMakeTarget target) { + ProjectTargets projectTargets = (ProjectTargets)projectMap.get(target.getProject()); if (projectTargets == null) { - projectTargets = readTargets(project); + projectTargets = readTargets(target.getProject()); + } + return projectTargets.contains((MakeTarget) target); + } + + public void removeTarget(IMakeTarget target) throws CoreException { + ProjectTargets projectTargets = (ProjectTargets)projectMap.get(target.getProject()); + if (projectTargets == null) { + projectTargets = readTargets(target.getProject()); } if (projectTargets.remove((MakeTarget) target)) { try { @@ -100,10 +107,9 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis } public void renameTarget(IMakeTarget target, String name) throws CoreException { - IProject project = target.getContainer().getProject(); - ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project); + ProjectTargets projectTargets = (ProjectTargets)projectMap.get(target.getProject()); if (projectTargets == null) { - projectTargets = readTargets(project); + projectTargets = readTargets(target.getProject()); } if (!projectTargets.contains((MakeTarget)target)) { throw new CoreException(new Status(IStatus.ERROR, MakeCorePlugin.getUniqueIdentifier(), -1, MakeMessages.getString("MakeTargetManager.target_exists"), null)); //$NON-NLS-1$ @@ -250,8 +256,7 @@ public class MakeTargetManager implements IMakeTargetManager, IResourceChangeLis protected void updateTarget(MakeTarget target) throws CoreException { if (target.getContainer() != null ) { // target has not been added to manager. - IProject project = target.getContainer().getProject(); - ProjectTargets projectTargets = (ProjectTargets)projectMap.get(project); + ProjectTargets projectTargets = (ProjectTargets)projectMap.get(target.getProject()); if (projectTargets == null || !projectTargets.contains(target)) { return; // target has not been added to manager. } diff --git a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/AddBuildTargetAction.java b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/AddBuildTargetAction.java index 421d685805a..2d4f0b36072 100644 --- a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/AddBuildTargetAction.java +++ b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/AddBuildTargetAction.java @@ -19,6 +19,7 @@ import org.eclipse.cdt.make.core.MakeCorePlugin; import org.eclipse.cdt.make.core.makefile.ITargetRule; import org.eclipse.cdt.make.internal.ui.MakeUIPlugin; import org.eclipse.cdt.make.ui.dialogs.MakeTargetDialog; +import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.action.Action; @@ -68,18 +69,15 @@ public class AddBuildTargetAction extends Action { sbMakefileTarget.append(' ').append(name); } } - String buildName = sbBuildName.toString(); + String buildName = generateUniqueName(file.getParent(), sbBuildName.toString()); String makefileTarget = sbMakefileTarget.toString(); - IMakeTarget target; + IMakeTarget target = null; try { - target = manager.findTarget(file.getParent(), buildName); - if (target == null) { - String[] ids = manager.getTargetBuilders(file.getProject()); - if (ids.length > 0) { - target = manager.createTarget(file.getProject(), buildName, ids[0]); - target.setBuildTarget(makefileTarget); - manager.addTarget(file.getParent(), target); - } + String[] ids = manager.getTargetBuilders(file.getProject()); + if (ids.length > 0) { + target = manager.createTarget(file.getProject(), buildName, ids[0]); + target.setBuildAttribute(IMakeTarget.BUILD_TARGET, makefileTarget); + target.setContainer(file.getParent()); } } catch (CoreException e) { MakeUIPlugin.errorDialog(shell, MakeUIPlugin.getResourceString("AddBuildTargetAction.exception.internal"), e.toString(), e); //$NON-NLS-1$ @@ -99,6 +97,20 @@ public class AddBuildTargetAction extends Action { } } + private String generateUniqueName(IContainer container, String targetString) { + String newName = targetString; + int i = 0; + IMakeTargetManager manager = MakeCorePlugin.getDefault().getTargetManager(); + try { + while (manager.findTarget(container, newName) != null) { + i++; + newName = targetString + " (" + Integer.toString(i) + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + } catch (CoreException e) { + } + return newName; + } + public boolean canActionBeAdded(ISelection selection) { ITargetRule[] rules = getTargetRules(selection); for (int i = 0; i < rules.length; i++) { diff --git a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/ui/dialogs/MakeTargetDialog.java b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/ui/dialogs/MakeTargetDialog.java index 07b0e3b468f..079554f5362 100644 --- a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/ui/dialogs/MakeTargetDialog.java +++ b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/ui/dialogs/MakeTargetDialog.java @@ -91,7 +91,7 @@ public class MakeTargetDialog extends Dialog { buildCommand = target.getBuildCommand(); buildArguments = target.getBuildArguments(); targetName = target.getName(); - targetString = target.getBuildTarget(); + targetString = target.getBuildAttribute(IMakeTarget.BUILD_TARGET, "all"); targetBuildID = target.getTargetBuilderID(); runAllBuilders = target.runAllBuilders(); } @@ -125,7 +125,7 @@ public class MakeTargetDialog extends Dialog { private String getTitle() { String title; - if (fTarget == null) { + if (fTarget == null || !MakeCorePlugin.getDefault().getTargetManager().targetExists(fTarget)) { title = MakeUIPlugin.getResourceString("MakeTargetDialog.title.createMakeTarget"); //$NON-NLS-1$ } else { title = MakeUIPlugin.getResourceString("MakeTargetDialog.title.modifyMakeTarget"); //$NON-NLS-1$ @@ -298,10 +298,10 @@ public class MakeTargetDialog extends Dialog { } protected void createButtonsForButtonBar(Composite parent) { - if (fTarget != null) { - createButton(parent, IDialogConstants.OK_ID, MakeUIPlugin.getResourceString("MakeTargetDialog.button.update"), true); //$NON-NLS-1$ - } else { + if (fTarget == null || !MakeCorePlugin.getDefault().getTargetManager().targetExists(fTarget)) { createButton(parent, IDialogConstants.OK_ID, MakeUIPlugin.getResourceString("MakeTargetDialog.button.create"), true); //$NON-NLS-1$ + } else { + createButton(parent, IDialogConstants.OK_ID, MakeUIPlugin.getResourceString("MakeTargetDialog.button.update"), true); //$NON-NLS-1$ } createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); //do this here because setting the text will set enablement on the ok @@ -333,7 +333,7 @@ public class MakeTargetDialog extends Dialog { } protected boolean targetHasChanged() { - if (initializing || fTarget == null) + if (initializing || fTarget == null || !MakeCorePlugin.getDefault().getTargetManager().targetExists(fTarget)) return true; if (isStopOnError != isStopOnError()) return true; @@ -421,16 +421,16 @@ public class MakeTargetDialog extends Dialog { } else { path = new Path(bldLine.substring(start, end)); } - target.setBuildCommand(path); + target.setBuildAttribute(IMakeTarget.BUILD_COMMAND, path.toString()); String args = ""; //$NON-NLS-1$ if (end != -1) { args = bldLine.substring(end + 1); } - target.setBuildArguments(args); + target.setBuildAttribute(IMakeTarget.BUILD_ARGUMENTS, args); } - target.setBuildTarget(getTarget()); + target.setBuildAttribute(IMakeTarget.BUILD_TARGET, getTarget()); - if (fTarget == null) { + if (fTarget == null || !MakeCorePlugin.getDefault().getTargetManager().targetExists(fTarget)) { fTargetManager.addTarget(fContainer, target); } else { if (!target.getName().equals(getTargetName())) {