From a8ea8170c1d025c55128b8d4505cf14c75402351 Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Wed, 15 Oct 2003 02:50:21 +0000 Subject: [PATCH] Add action to the outliner. --- .../ui/editor/AddBuildTargetAction.java | 110 ++++++++++++++++++ .../ui/editor/MakefileContentOutlinePage.java | 39 +++++++ 2 files changed, 149 insertions(+) create mode 100644 build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/AddBuildTargetAction.java 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 new file mode 100644 index 00000000000..9112ea00ff7 --- /dev/null +++ b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/AddBuildTargetAction.java @@ -0,0 +1,110 @@ +/********************************************************************** + * Copyright (c) 2002,2003 QNX Software Systems and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * QNX Software Systems - Initial API and implementation +***********************************************************************/ +package org.eclipse.cdt.make.internal.ui.editor; + +import java.util.List; + +import org.eclipse.cdt.make.core.IMakeTarget; +import org.eclipse.cdt.make.core.IMakeTargetManager; +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.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IFileEditorInput; + +/** + */ +public class AddBuildTargetAction extends Action { + + MakefileContentOutlinePage fOutliner; + + public AddBuildTargetAction(MakefileContentOutlinePage outliner) { + super("Add To Build Target"); + setDescription("Add To Build Target"); + setToolTipText("Add To Build Target"); + fOutliner = outliner; + } + + /* (non-Javadoc) + * @see org.eclipse.jface.action.IAction#run() + */ + public void run() { + IMakeTargetManager manager = MakeCorePlugin.getDefault().getTargetManager(); + IFile file = getFile(); + ITargetRule rule = getTargetRule(fOutliner.getSelection()); + Shell shell = fOutliner.getControl().getShell(); + if (file != null && rule != null && shell != null) { + String name = rule.getTarget().toString().trim(); + IMakeTarget target = manager.findTarget(file.getParent(), name); + if (target != null) { + MakeTargetDialog dialog; + try { + dialog = new MakeTargetDialog(shell, target); + dialog.open(); + } catch (CoreException e) { + MakeUIPlugin.errorDialog(shell, "Internal Error", "", e); + } + } else { + try { + String[] ids = manager.getTargetBuilders(file.getProject()); + if (ids.length > 0) { + target = manager.createTarget(file.getProject(), name, ids[0]); + target.setContainer(file.getParent()); + target.setBuildTarget(name); + manager.addTarget(file.getParent(), target); + } + } catch (CoreException e) { + MakeUIPlugin.errorDialog(shell, "Internal Error", "", e); + } + } + } + } + + public boolean canActionBeAdded(ISelection selection) { + ITargetRule rule = getTargetRule(selection); + if (rule != null) { + IFile file = getFile(); + if (file != null) { + return MakeCorePlugin.getDefault().getTargetManager().hasTargetBuilder(file.getProject()); + } + } + return false; + } + + + private IFile getFile() { + Object input = fOutliner.getInput(); + if (input instanceof IFileEditorInput) { + return ((IFileEditorInput)input).getFile(); + } + return null; + } + + private ITargetRule getTargetRule(ISelection sel) { + if (!sel.isEmpty() && sel instanceof IStructuredSelection) { + List list= ((IStructuredSelection)sel).toList(); + if (list.size() == 1) { + Object element= list.get(0); + if (element instanceof ITargetRule) { + return (ITargetRule)element; + } + } + } + return null; + } + +} diff --git a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/MakefileContentOutlinePage.java b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/MakefileContentOutlinePage.java index ec27fbdf07f..f1215a66f15 100644 --- a/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/MakefileContentOutlinePage.java +++ b/build/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/editor/MakefileContentOutlinePage.java @@ -30,7 +30,11 @@ import org.eclipse.cdt.make.internal.core.makefile.NullMakefile; import org.eclipse.cdt.make.internal.ui.MakeUIImages; import org.eclipse.cdt.make.internal.ui.MakeUIPlugin; import org.eclipse.cdt.make.ui.IWorkingCopyManager; +import org.eclipse.jface.action.IMenuListener; +import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.IToolBarManager; +import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.action.Separator; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.LabelProvider; @@ -39,8 +43,11 @@ import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Menu; import org.eclipse.ui.IActionBars; import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IWorkbenchActionConstants; +import org.eclipse.ui.part.IPageSite; import org.eclipse.ui.views.contentoutline.ContentOutlinePage; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; @@ -207,10 +214,12 @@ public class MakefileContentOutlinePage extends ContentOutlinePage implements IC protected MakefileEditor fEditor; protected Object fInput; + protected AddBuildTargetAction fAddBuildTargetAction; public MakefileContentOutlinePage(MakefileEditor editor) { super(); fEditor = editor; + fAddBuildTargetAction = new AddBuildTargetAction(this); } /* (non-Javadoc) @@ -224,6 +233,32 @@ public class MakefileContentOutlinePage extends ContentOutlinePage implements IC if (fInput != null) { viewer.setInput(fInput); } + + MenuManager manager= new MenuManager("#MakefileOutlinerContext"); + manager.setRemoveAllWhenShown(true); + manager.addMenuListener(new IMenuListener() { + public void menuAboutToShow(IMenuManager m) { + contextMenuAboutToShow(m); + } + }); + Control tree = viewer.getControl(); + Menu menu = manager.createContextMenu(tree); + tree.setMenu(menu); + + IPageSite site= getSite(); + site.registerContextMenu(MakeUIPlugin.getPluginId() + ".outline", manager, viewer); //$NON-NLS-1$ + site.setSelectionProvider(viewer); + + } + + /** + * called to create the context menu of the outline + */ + protected void contextMenuAboutToShow(IMenuManager menu) { + if (fAddBuildTargetAction.canActionBeAdded(getSelection())) + menu.add(fAddBuildTargetAction); + menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); + menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS+"-end"));//$NON-NLS-1$ } /** @@ -234,6 +269,10 @@ public class MakefileContentOutlinePage extends ContentOutlinePage implements IC update(); } + public Object getInput() { + return fInput; + } + /** * Updates the outline page. */