1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Add action to the outliner.

This commit is contained in:
Alain Magloire 2003-10-15 02:50:21 +00:00
parent 44529a00c3
commit a8ea8170c1
2 changed files with 149 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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.MakeUIImages;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin; import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.ui.IWorkingCopyManager; 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.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider; import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider; 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.graphics.Image;
import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.IActionBars; import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput; 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.ContentOutlinePage;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
@ -207,10 +214,12 @@ public class MakefileContentOutlinePage extends ContentOutlinePage implements IC
protected MakefileEditor fEditor; protected MakefileEditor fEditor;
protected Object fInput; protected Object fInput;
protected AddBuildTargetAction fAddBuildTargetAction;
public MakefileContentOutlinePage(MakefileEditor editor) { public MakefileContentOutlinePage(MakefileEditor editor) {
super(); super();
fEditor = editor; fEditor = editor;
fAddBuildTargetAction = new AddBuildTargetAction(this);
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -224,6 +233,32 @@ public class MakefileContentOutlinePage extends ContentOutlinePage implements IC
if (fInput != null) { if (fInput != null) {
viewer.setInput(fInput); 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(); update();
} }
public Object getInput() {
return fInput;
}
/** /**
* Updates the outline page. * Updates the outline page.
*/ */