@Override protected void buttonSelected(Button button, int index) { try { switch (index) { case ADD_TARGET: { MakeTargetDialog dialog = new MakeTargetDialog(getControl().getShell(), fContainer); dialog.open(); } break; case REMOVE_TARGET: IMakeTargetManager manager = MakeCorePlugin.getDefault().getTargetManager(); manager.removeTarget( (IMakeTarget) ((IStructuredSelection) getViewer().getSelection()).getFirstElement()); break; case EDIT_TARGET: { MakeTargetDialog dialog = new MakeTargetDialog( getControl().getShell(), (IMakeTarget) ((IStructuredSelection) getViewer().getSelection()).getFirstElement()); dialog.open(); } break; } } catch (CoreException e) { MakeUIPlugin.errorDialog( getControl().getShell(), MakeUIPlugin.getResourceString("TargetListViewer.exception.error"), // $NON-NLS-1$ MakeUIPlugin.getResourceString("TargetListViewer.exception.message"), // $NON-NLS-1$ e); } }
/* (non-Javadoc) * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) */ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { if (this.viewer == null) { MakeCorePlugin.getDefault().getTargetManager().addListener(this); } this.viewer = (StructuredViewer) viewer; IWorkspace oldWorkspace = null; IWorkspace newWorkspace = null; if (oldInput instanceof IWorkspace) { oldWorkspace = (IWorkspace) oldInput; } else if (oldInput instanceof IContainer) { oldWorkspace = ((IContainer) oldInput).getWorkspace(); } else if (oldInput instanceof TargetSourceContainer) { oldWorkspace = ((TargetSourceContainer) oldInput).getContainer().getWorkspace(); } if (newInput instanceof IWorkspace) { newWorkspace = (IWorkspace) newInput; } else if (newInput instanceof IContainer) { newWorkspace = ((IContainer) newInput).getWorkspace(); } else if (newInput instanceof TargetSourceContainer) { newWorkspace = ((TargetSourceContainer) newInput).getContainer().getWorkspace(); } if (oldWorkspace != newWorkspace) { ICProjectDescriptionManager mngr = CoreModel.getDefault().getProjectDescriptionManager(); if (oldWorkspace != null) { InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID).removePreferenceChangeListener(this); mngr.removeCProjectDescriptionListener(this); oldWorkspace.removeResourceChangeListener(this); } if (newWorkspace != null) { newWorkspace.addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE); mngr.addCProjectDescriptionListener(this, CProjectDescriptionEvent.APPLIED); InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID).addPreferenceChangeListener(this); } } }
/** * Create {@code MakeTarget} from basic data elements available during copy/paste or drag/drop * operations. The other data will be set to default. * * @param name - name of make target being created. * @param targetStr - build target. * @param command - make command. ("make" by default). * @param container - container where to place the target. * @return newly created {@link IMakeTarget}. * @throws CoreException if there was a problem creating new make target. */ public static IMakeTarget createMakeTarget( String name, String targetStr, String command, IContainer container) throws CoreException { IMakeTargetManager makeTargetManager = MakeCorePlugin.getDefault().getTargetManager(); IProject project = container.getProject(); String[] ids = makeTargetManager.getTargetBuilders(project); String builderId = ids[0]; // IMakeTarget attributes IMakeTarget newMakeTarget = makeTargetManager.createTarget(project, name, builderId); if (targetStr != null) { newMakeTarget.setBuildAttribute(IMakeTarget.BUILD_TARGET, targetStr); } // IMakeCommonBuildInfo attributes String projectBuildCommand = getProjectBuildCommand(project); if (command != null && command.length() > 0 && !command.equals(projectBuildCommand)) { newMakeTarget.setBuildAttribute(IMakeCommonBuildInfo.BUILD_COMMAND, command); newMakeTarget.setUseDefaultBuildCmd(false); } else { newMakeTarget.setBuildAttribute(IMakeCommonBuildInfo.BUILD_COMMAND, projectBuildCommand); newMakeTarget.setUseDefaultBuildCmd(true); } return newMakeTarget; }
/** * Generate a new unique non-existent name of the kind of "Copy (2) of name". * * @param targetName - name from where generate unique name. * @param container - container where the target belongs. * @return generated name. * @throws CoreException if {@code findTarget} having a problem. */ private static String generateUniqueName(String targetName, IContainer container) throws CoreException { IMakeTargetManager makeTargetManager = MakeCorePlugin.getDefault().getTargetManager(); // Try "name" String newName = targetName; if (makeTargetManager.findTarget(container, newName) == null) { return newName; } // Try "Copy of name" newName = MessageFormat.format( MakeUIPlugin.getResourceString("MakeTargetDnD.copyOf.uniqueName"), // $NON-NLS-1$ new Object[] {targetName}); if (makeTargetManager.findTarget(container, newName) == null) { return newName; } // Try "Copy (2) of name" for (int counter = 1; ; counter++) { newName = MessageFormat.format( MakeUIPlugin.getResourceString( "MakeTargetDnD.countedCopyOf.uniqueName"), //$NON-NLS-1$ new Object[] {counter, targetName}); if (makeTargetManager.findTarget(container, newName) == null) { return newName; } } }
/** * Copy/move one make target to the specified container. * * @param makeTarget - make target. * @param container - container to copy/move to. * @param operation - copying operation. Should be one of {@link org.eclipse.swt.dnd.DND} * operations. * @param shell - shell to display user warnings. * @param offerOverwriteDialog - whether overwrite dialog is provided. * @throws CoreException on the failure of {@link IMakeTargetManager} or {@link IMakeTarget} * operation. * @see DND#DROP_NONE * @see DND#DROP_COPY * @see DND#DROP_MOVE * @see DND#DROP_LINK * @see DND#DROP_DEFAULT */ public static void copyOneTarget( IMakeTarget makeTarget, IContainer container, final int operation, Shell shell, boolean offerOverwriteDialog) throws CoreException { IMakeTargetManager makeTargetManager = MakeCorePlugin.getDefault().getTargetManager(); IMakeTarget exists = makeTargetManager.findTarget(container, makeTarget.getName()); if (exists != null) { int userAnswer = IDialogConstants.CANCEL_ID; if (offerOverwriteDialog) { userAnswer = overwriteMakeTargetDialog(makeTarget.getName(), shell); } else { userAnswer = RENAME_ID; } if (userAnswer == IDialogConstants.YES_ID || userAnswer == IDialogConstants.YES_TO_ALL_ID) { copyTargetData(makeTarget, exists); if (operation == DND.DROP_MOVE) { makeTargetManager.removeTarget(makeTarget); } } else if (userAnswer == RENAME_ID || userAnswer == RENAME_TO_ALL_ID) { String name = generateUniqueName(makeTarget.getName(), container); IMakeTarget newMakeTarget = cloneTarget(name, makeTarget, container.getProject()); newMakeTarget.setContainer(container); int dialogReturnCode = Window.OK; if (userAnswer == RENAME_ID) { MakeTargetDialog dialog; try { dialog = new MakeTargetDialog(shell, newMakeTarget); dialogReturnCode = dialog.open(); } catch (CoreException e) { MakeUIPlugin.errorDialog( shell, MakeUIPlugin.getResourceString("AddBuildTargetAction.exception.internal"), e.toString(), e); //$NON-NLS-1$ } } else if (userAnswer == RENAME_TO_ALL_ID) { makeTargetManager.addTarget(container, newMakeTarget); } if (operation == DND.DROP_MOVE && dialogReturnCode != Window.CANCEL) { makeTargetManager.removeTarget(makeTarget); } } } else { makeTargetManager.addTarget( container, cloneTarget(makeTarget.getName(), makeTarget, container.getProject())); if (operation == DND.DROP_MOVE) { makeTargetManager.removeTarget(makeTarget); } } }
/** * Creating a copy of IMakeTarget in a different project. * * @param name - name of new target. * @param makeTarget - make target. * @param project - project where to assign the make target. * @return newly created make target. * @throws CoreException if there is a problem with creating or copying the target. */ private static IMakeTarget cloneTarget(String name, IMakeTarget makeTarget, IProject project) throws CoreException { IMakeTargetManager makeTargetManager = MakeCorePlugin.getDefault().getTargetManager(); String[] ids = makeTargetManager.getTargetBuilders(project); String builderId = ids[0]; IMakeTarget newMakeTarget = makeTargetManager.createTarget(project, name, builderId); copyTargetData(makeTarget, newMakeTarget); if (makeTarget .getName() .equals(makeTarget.getBuildAttribute(IMakeTarget.BUILD_TARGET, ""))) { // $NON-NLS-1$ newMakeTarget.setBuildAttribute(IMakeTarget.BUILD_TARGET, name); } return newMakeTarget; }
/** * @param project - project where to get build command from * @return build command from project settings. */ public static String getProjectBuildCommand(IProject project) { IMakeTargetManager targetManager = MakeCorePlugin.getDefault().getTargetManager(); String[] targetBuilders = targetManager.getTargetBuilders(project); if (targetBuilders == null || targetBuilders.length == 0) { return DEFAULT_BUILD_COMMAND; } String builderId = targetManager.getBuilderID(targetBuilders[0]); String buildCommand = DEFAULT_BUILD_COMMAND; try { IMakeBuilderInfo buildInfo = MakeCorePlugin.createBuildInfo(project, builderId); buildCommand = buildInfo.getBuildCommand().toString().trim(); } catch (CoreException e) { // keep default value } return buildCommand; }
@Override public boolean visit(IResourceProxy proxy) throws CoreException { try { if (proxy.getType() != IResource.FOLDER && proxy.getType() != IResource.PROJECT) { return false; } IContainer container = (IContainer) proxy.requestResource(); monitor.subTask(container.getProjectRelativePath().toString()); QualifiedName qName = new QualifiedName("org.eclipse.cdt.make", "goals"); // $NON-NLS-1$ //$NON-NLS-2$ String goal = container.getPersistentProperty(qName); if (goal != null) { goal = goal.trim(); IMakeTargetManager manager = MakeCorePlugin.getDefault().getTargetManager(); String[] builder = manager.getTargetBuilders(container.getProject()); IMakeTarget target = manager.createTarget(container.getProject(), goal, builder[0]); target.setBuildAttribute(IMakeTarget.BUILD_TARGET, goal); manager.addTarget(container, target); container.setPersistentProperty(qName, null); } return true; } finally { if (--nextProgress <= 0) { // we have exhausted the current increment, so report progress monitor.worked(1); worked++; if (worked >= halfWay) { // we have passed the current halfway point, so double the // increment and reset the halfway point. currentIncrement *= 2; halfWay += (TOTAL_WORK - halfWay) / 2; } // reset the progress counter to another full increment nextProgress = currentIncrement; } } }
/* (non-Javadoc) * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) */ public Object[] getChildren(Object obj) { if (obj instanceof IWorkspaceRoot) { try { return MakeCorePlugin.getDefault().getTargetManager().getTargetBuilderProjects(); } catch (CoreException e) { MakeCorePlugin.log(e); } } else if (obj instanceof IContainer) { IContainer container = (IContainer) obj; ArrayList<Object> children = new ArrayList<Object>(); boolean isAddingSourceRoots = !bFlatten && (container instanceof IProject) && CCorePlugin.showSourceRootsAtTopOfProject(); // add source roots if necessary if (isAddingSourceRoots) { IProject project = (IProject) container; ICSourceEntry[] srcEntries = getSourceEntries(project); for (ICSourceEntry srcEntry : srcEntries) { if (!srcEntry.getFullPath().equals(project.getFullPath())) { children.add(new TargetSourceContainer(srcEntry)); } } } // add regular folders try { IResource[] resources = container.members(); for (IResource rc : resources) { if (rc instanceof IContainer) { if (!(isAddingSourceRoots && isSourceEntry(rc))) { children.add(rc); } } } } catch (CoreException e) { MakeCorePlugin.log(e); } // finally add targets try { IMakeTarget[] targets = MakeCorePlugin.getDefault().getTargetManager().getTargets(container); children.addAll(Arrays.asList(targets)); } catch (CoreException e) { MakeCorePlugin.log(e); } return children.toArray(); } else if (obj instanceof TargetSourceContainer) { ArrayList<Object> children = new ArrayList<Object>(); try { IContainer container = ((TargetSourceContainer) obj).getContainer(); IResource[] resources = container.members(); for (IResource rc : resources) { if (rc instanceof IContainer) { children.add(rc); } } children.addAll( Arrays.asList(MakeCorePlugin.getDefault().getTargetManager().getTargets(container))); } catch (CoreException e) { MakeCorePlugin.log(e); } return children.toArray(); } return new Object[0]; }
/* (non-Javadoc) * @see org.eclipse.jface.viewers.IContentProvider#dispose() */ public void dispose() { if (viewer != null) { MakeCorePlugin.getDefault().getTargetManager().removeListener(this); } }