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

Reorganize the actions of the cview into groups.

This commit is contained in:
Alain Magloire 2004-02-16 01:36:28 +00:00
parent 39a6ce2544
commit 47f1f6f040
11 changed files with 1291 additions and 691 deletions

View file

@ -1,3 +1,15 @@
2004-02-15 Alain Magloire
Reorganize the action of the CView and distribute them
in different action group.
New files:
* src/org/eclipse/cdt/internal/ui/cview/Buildgroup.java
* src/org/eclipse/cdt/internal/ui/cview/OpenFileGroup.java
* src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java
* src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java
* src/org/eclipse/cdt/internal/ui/cview/GotoActionGroup.java
2004-02-12 John Camelon
Updated Content Assist feature to not use IASTCompletionKind.SCOPED_REFERENCE

View file

@ -0,0 +1,145 @@
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation 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:
* IBM Corporation - initial API and implementation
************************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import java.util.Iterator;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.BuildAction;
import org.eclipse.ui.ide.IDEActionFactory;
/**
* This is the action group for workspace actions such as Build
*/
public class BuildGroup extends CViewActionGroup {
private BuildAction buildAction;
private BuildAction rebuildAction;
// Menu tags for the build
final String BUILD_GROUP_MARKER = "buildGroup";
final String BUILD_GROUP_MARKER_END = "end-buildGroup";
public BuildGroup(CView cview) {
super(cview);
}
public void fillActionBars(IActionBars actionBars) {
actionBars.setGlobalActionHandler(IDEActionFactory.BUILD.getId(), buildAction);
actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT.getId(), rebuildAction);
}
/**
* Adds the build actions to the context menu.
* <p>
* The following conditions apply: build-only projects selected, auto build
* disabled, at least one * builder present
* </p>
* <p>
* No disabled action should be on the context menu.
* </p>
*
* @param menu
* context menu to add actions to
*/
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
boolean isProjectSelection = true;
boolean hasOpenProjects = false;
boolean hasClosedProjects = false;
boolean hasBuilder = true; // false if any project is closed or does
// not have builder
menu.add(new GroupMarker(BUILD_GROUP_MARKER));
Iterator resources = selection.iterator();
while (resources.hasNext() && (!hasOpenProjects || !hasClosedProjects || hasBuilder || isProjectSelection)) {
Object next = resources.next();
IProject project = null;
if (next instanceof IProject) {
project = (IProject) next;
} else if (next instanceof IAdaptable) {
project = (IProject) ((IAdaptable) next).getAdapter(IProject.class);
}
if (project == null) {
isProjectSelection = false;
continue;
}
if (project.isOpen()) {
hasOpenProjects = true;
if (hasBuilder && !hasBuilder(project)) {
hasBuilder = false;
}
} else {
hasClosedProjects = true;
hasBuilder = false;
}
}
// Allow manual incremental build only if auto build is off.
//if (!selection.isEmpty() && isProjectSelection
// && !ResourcesPlugin.getWorkspace().isAutoBuilding()
// && hasBuilder) {
if (!selection.isEmpty() && isProjectSelection && hasBuilder) {
buildAction.selectionChanged(selection);
menu.add(buildAction);
rebuildAction.selectionChanged(selection);
menu.add(rebuildAction);
}
menu.add(new GroupMarker(BUILD_GROUP_MARKER_END));
}
/**
* Handles a key pressed event by invoking the appropriate action.
*/
public void handleKeyPressed(KeyEvent event) {
}
/**
* Returns whether there are builders configured on the given project.
*
* @return <code>true</code> if it has builders, <code>false</code> if
* not, or if this could not be determined
*/
boolean hasBuilder(IProject project) {
try {
ICommand[] commands = project.getDescription().getBuildSpec();
if (commands.length > 0) return true;
} catch (CoreException e) {
// Cannot determine if project has builders. Project is closed
// or does not exist. Fall through to return false.
}
return false;
}
protected void makeActions() {
Shell shell = getCView().getSite().getShell();
buildAction = new BuildAction(shell, IncrementalProjectBuilder.INCREMENTAL_BUILD);
rebuildAction = new BuildAction(shell, IncrementalProjectBuilder.FULL_BUILD);
}
public void updateActionBars() {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
buildAction.selectionChanged(selection);
rebuildAction.selectionChanged(selection);
}
}

View file

@ -1,13 +1,10 @@
package org.eclipse.cdt.internal.ui.cview;
/*
* (c) Copyright QNX Software Systems Ltd. 2002.
* All Rights Reserved.
* (c) Copyright QNX Software Systems Ltd. 2002. All Rights Reserved.
*/
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.IArchive;
@ -40,7 +37,6 @@ import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
@ -52,11 +48,13 @@ import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.IOpenListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeViewerListener;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeExpansionEvent;
@ -69,6 +67,8 @@ import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Tree;
@ -85,7 +85,6 @@ import org.eclipse.ui.IWorkingSetManager;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ResourceWorkingSetFilter;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
import org.eclipse.ui.part.ISetSelectionTarget;
import org.eclipse.ui.part.IShowInTarget;
import org.eclipse.ui.part.PluginTransfer;
@ -100,25 +99,22 @@ import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
* CView
*
*/
public class CView extends ViewPart implements ISetSelectionTarget,
IPropertyChangeListener, IShowInTarget {
public class CView extends ViewPart implements ISetSelectionTarget, IPropertyChangeListener, IShowInTarget {
ProblemTreeViewer viewer;
IMemento memento;
CViewActionGroup actionGroup;
WorkingSetFilterActionGroup wsFilterActionGroup;
FrameList frameList;
CViewFrameSource frameSource;
CLibFilter clibFilter = new CLibFilter();
CPatternFilter patternFilter = new CPatternFilter();
ResourceWorkingSetFilter workingSetFilter = new ResourceWorkingSetFilter();
ActionContributionItem adjustWorkingSetContributions [] = new ActionContributionItem[5];
private boolean dragDetected;
private Listener dragDetectListener;
// Persistance tags.
static final String TAG_SELECTION = "selection"; //$NON-NLS-1$
@ -138,45 +134,43 @@ public class CView extends ViewPart implements ISetSelectionTarget,
final String WORKING_GROUP_MARKER_END = "end-workingSetGroup";
private IPartListener partListener = new IPartListener() {
public void partActivated(IWorkbenchPart part) {
if (part instanceof IEditorPart) {
editorActivated((IEditorPart) part);
}
}
public void partBroughtToTop(IWorkbenchPart part) {}
public void partClosed(IWorkbenchPart part) {}
public void partDeactivated(IWorkbenchPart part) {}
public void partOpened(IWorkbenchPart part) {}
public void partBroughtToTop(IWorkbenchPart part) {
}
public void partClosed(IWorkbenchPart part) {
}
public void partDeactivated(IWorkbenchPart part) {
}
public void partOpened(IWorkbenchPart part) {
}
};
private IPropertyChangeListener workingSetListener = new IPropertyChangeListener() {
private void doViewerUpdate() {
viewer.getControl().setRedraw(false);
viewer.refresh();
viewer.getControl().setRedraw(true);
}
public void propertyChange(PropertyChangeEvent ev) {
String prop = ev.getProperty();
if(prop == null) {
String property = ev.getProperty();
Object newValue = ev.getNewValue();
Object oldValue = ev.getOldValue();
IWorkingSet filterWorkingSet = workingSetFilter.getWorkingSet();
if (property == null) {
return;
}
if(prop.equals(WorkingSetFilterActionGroup.CHANGE_WORKING_SET)) {
workingSetFilter.setWorkingSet((IWorkingSet)ev.getNewValue());
doViewerUpdate();
} else if(prop.equals(IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE)){
if(ev.getOldValue() instanceof IWorkingSet && workingSetFilter.getWorkingSet() != null) {
if(workingSetFilter.getWorkingSet().equals(ev.getOldValue())) {
doViewerUpdate();
}
}
} else if(prop.equals(IWorkingSetManager.CHANGE_WORKING_SET_REMOVE)) {
if(ev.getOldValue() instanceof IWorkingSet && workingSetFilter.getWorkingSet() != null) {
if(workingSetFilter.getWorkingSet().equals(ev.getOldValue())) {
workingSetFilter.setWorkingSet(null);
doViewerUpdate();
}
}
if (IWorkingSetManager.CHANGE_WORKING_SET_REMOVE.equals(property) && oldValue == filterWorkingSet) {
setWorkingSet(null);
} else if (IWorkingSetManager.CHANGE_WORKING_SET_NAME_CHANGE.equals(property) && newValue == filterWorkingSet) {
updateTitle();
} else if (IWorkingSetManager.CHANGE_WORKING_SET_CONTENT_CHANGE.equals(property) && newValue == filterWorkingSet) {
getViewer().refresh();
}
}
};
@ -194,17 +188,20 @@ public class CView extends ViewPart implements ISetSelectionTarget,
}
/**
* Reveal and select the passed element selection in self's visual component
* Reveal and select the passed element selection in self's visual
* component
*
* @see ISetSelectionTarget#selectReveal()
*/
public void selectReveal(ISelection selection) {
IStructuredSelection ssel = convertSelectionToCElement(selection);
IStructuredSelection ssel = SelectionConverter.convertSelectionToCElements(selection);
if (!ssel.isEmpty()) {
getViewer().setSelection(ssel, true);
}
}
private ITreeViewerListener expansionListener = new ITreeViewerListener() {
public void treeCollapsed(TreeExpansionEvent event) {
}
@ -215,6 +212,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
Control ctrl = viewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
ctrl.getDisplay().asyncExec(new Runnable() {
public void run() {
Control ctrl = viewer.getControl();
if (ctrl != null && !ctrl.isDisposed()) {
@ -228,13 +226,25 @@ public class CView extends ViewPart implements ISetSelectionTarget,
};
/**
* Handles double clicks in viewer.
* Opens editor if file double-clicked.
* Handles an open event from the viewer.
* Opens an editor on the selected file.
*
* @param event the open event
*/
protected void handleOpen(OpenEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
getActionGroup().runDefaultAction(selection);
}
/**
* Handles double clicks in viewer. Opens editor if file double-clicked.
*/
protected void handleDoubleClick(DoubleClickEvent event) {
IStructuredSelection s = (IStructuredSelection) event.getSelection();
IAdaptable element = (IAdaptable)s.getFirstElement();
IEditorPart part = null;
Object o = s.getFirstElement();
if (o instanceof IAdaptable) {
IAdaptable element = (IAdaptable) o;
//System.out.println ("Double click on " + element);
try {
@ -248,8 +258,9 @@ public class CView extends ViewPart implements ISetSelectionTarget,
}
} catch (Exception e) {
}
if (part == null && viewer.isExpandable(element)) {
viewer.setExpandedState(element, !viewer.getExpandedState(element));
}
if (part == null && viewer.isExpandable(o)) {
viewer.setExpandedState(o, !viewer.getExpandedState(o));
}
}
@ -267,20 +278,33 @@ public class CView extends ViewPart implements ISetSelectionTarget,
*
*/
protected void handleKeyReleased(KeyEvent event) {
if (getActionGroup() != null) {
getActionGroup().handleKeyReleased(event);
}
}
/**
* Handles selection changed in viewer.
* Updates global actions.
* Links to editor (if option enabled)
* Handles selection changed in viewer. Updates global actions. Links to
* editor (if option enabled)
*/
void handleSelectionChanged(SelectionChangedEvent event) {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
updateStatusLine(sel);
if (getActionGroup() != null) {
getActionGroup().runDefaultAction(sel);
final IStructuredSelection selection = (IStructuredSelection) event.getSelection();
updateStatusLine(selection);
updateActionBars(selection);
dragDetected = false;
if (isLinkingEnabled()) {
getSite().getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
if (dragDetected == false) {
// only synchronize with editor when the selection is
// not the result
// of a drag. Fixes bug 22274.
linkToEditor(selection);
}
}
});
}
linkToEditor(sel);
}
/**
@ -295,7 +319,8 @@ public class CView extends ViewPart implements ISetSelectionTarget,
/**
* Sets the action group.
*
* @param actionGroup the action group
* @param actionGroup
* the action group
*/
protected void setActionGroup(CViewActionGroup actionGroup) {
this.actionGroup = actionGroup;
@ -305,13 +330,12 @@ public class CView extends ViewPart implements ISetSelectionTarget,
* Answer the property defined by key.
*/
public Object getAdapter(Class key) {
if (key.equals(ISelectionProvider.class))
return viewer;
if (key.equals(ISelectionProvider.class)) return viewer;
return super.getAdapter(key);
}
/* (non-Javadoc)
* Method declared on IViewPart.
/*
* (non-Javadoc) Method declared on IViewPart.
*/
public void init(IViewSite site, IMemento memento) throws PartInitException {
super.init(site, memento);
@ -337,7 +361,8 @@ public class CView extends ViewPart implements ISetSelectionTarget,
/**
* Adds the filters to the viewer.
*
* @param viewer the viewer
* @param viewer
* the viewer
*/
void initFilters(TreeViewer viewer) {
viewer.addFilter(patternFilter);
@ -351,29 +376,25 @@ public class CView extends ViewPart implements ISetSelectionTarget,
void initDragAndDrop() {
int ops = DND.DROP_COPY | DND.DROP_MOVE;
Transfer[] dragTransfers =
new Transfer[] {
ResourceTransfer.getInstance(),
FileTransfer.getInstance(),
CLocalSelectionTransfer.getInstance(),
PluginTransfer.getInstance()};
Transfer[] dragTransfers = new Transfer[] { ResourceTransfer.getInstance(), FileTransfer.getInstance(),
CLocalSelectionTransfer.getInstance(), PluginTransfer.getInstance()};
TransferDragSourceListener[] dragListeners =
new TransferDragSourceListener[] {
new ResourceTransferDragAdapter(viewer),
new LocalSelectionTransferDragAdapter(viewer),
new FileTransferDragAdapter(viewer)};
TransferDragSourceListener[] dragListeners = new TransferDragSourceListener[] { new ResourceTransferDragAdapter(viewer),
new LocalSelectionTransferDragAdapter(viewer), new FileTransferDragAdapter(viewer)};
viewer.addDragSupport(ops, dragTransfers, new DelegatingDragAdapter(viewer, dragListeners));
Transfer[] dropTransfers =
new Transfer[] {
ResourceTransfer.getInstance(),
FileTransfer.getInstance(),
LocalSelectionTransfer.getInstance(),
PluginTransfer.getInstance()};
Transfer[] dropTransfers = new Transfer[] { ResourceTransfer.getInstance(), FileTransfer.getInstance(),
LocalSelectionTransfer.getInstance(), PluginTransfer.getInstance()};
viewer.addDropSupport(ops, dropTransfers, new CViewDropAdapter(viewer));
dragDetectListener = new Listener() {
public void handleEvent(Event event) {
dragDetected = true;
}
};
viewer.getControl().addListener(SWT.DragDetect, dragDetectListener);
}
@ -410,8 +431,6 @@ public class CView extends ViewPart implements ISetSelectionTarget,
viewer.setLabelProvider(new DecoratingLabelProvider(cProvider, decorator));
}
/**
* Initializes and registers the context menu.
*/
@ -419,6 +438,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
CView.this.fillContextMenu(manager);
}
@ -429,26 +449,60 @@ public class CView extends ViewPart implements ISetSelectionTarget,
getSite().registerContextMenu(menuMgr, viewer);
}
/**
* Restores the working set filter from the persistence store.
*/
void initWorkingSetFilter() {
// FIXME: the memento does not work if we close the view
// and reopen we should save this somewhere else.
// but it goes to pretty much all the settings 8-(
if (memento == null) {
return;
}
String wsname = memento.getString(TAG_WORKINGSET);
if (wsname != null && wsname.equals("") == false) { //$NON-NLS-1$
IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager();
IWorkingSet workingSet = wsmanager.getWorkingSet(wsname);
if (workingSet != null) {
// Only initialize filter. Don't set working set into viewer.
// Working set is set via WorkingSetFilterActionGroup
// during action creation.
workingSetFilter.setWorkingSet(workingSet);
}
}
}
/**
* Add listeners to the viewer.
*/
protected void initListeners(TreeViewer viewer) {
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
handleDoubleClick(event);
}
});
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
handleSelectionChanged(event);
}
});
viewer.addOpenListener(new IOpenListener() {
public void open(OpenEvent event) {
handleOpen(event);
}
});
viewer.getControl().addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
handleKeyPressed(e);
}
public void keyReleased(KeyEvent e) {
handleKeyReleased(e);
}
@ -467,19 +521,20 @@ public class CView extends ViewPart implements ISetSelectionTarget,
CUIPlugin.getDefault().getProblemMarkerManager().addListener(viewer);
CUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this);
initFilters(viewer);
initListeners(viewer);
initCElementSorter();
initFrameList();
initDragAndDrop();
updateTitle();
if (memento != null) {
restoreFilters();
} else {
initFilterFromPreferences();
}
initFilters(viewer);
initWorkingSetFilter();
initListeners(viewer);
initCElementSorter();
initFrameList();
initDragAndDrop();
updateTitle();
viewer.setInput(CoreModel.getDefault().getCModel());
initContextMenu();
@ -487,7 +542,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
// Make the Actions for the Context Menu
makeActions();
getActionGroup().fillActionBars(getViewSite().getActionBars());
updateActionBars((IStructuredSelection) viewer.getSelection());
//Add the property changes after all of the UI work has been done.
IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager();
@ -498,8 +553,9 @@ public class CView extends ViewPart implements ISetSelectionTarget,
getSite().setSelectionProvider(viewer);
getSite().getPage().addPartListener(partListener);
if (memento != null)
if (memento != null) {
restoreState(memento);
}
memento = null;
}
@ -517,8 +573,8 @@ public class CView extends ViewPart implements ISetSelectionTarget,
return new StandardCElementLabelProvider();
}
/* (non-Javadoc)
* Method declared on IWorkbenchPart.
/*
* (non-Javadoc) Method declared on IWorkbenchPart.
*/
public void dispose() {
getSite().getPage().removePartListener(partListener);
@ -533,12 +589,17 @@ public class CView extends ViewPart implements ISetSelectionTarget,
IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager();
wsmanager.removePropertyChangeListener(workingSetListener);
Control control = viewer.getControl();
if (dragDetectListener != null && control != null && control.isDisposed() == false) {
control.removeListener(SWT.DragDetect, dragDetectListener);
}
super.dispose();
}
/**
* An editor has been activated. Set the selection in this navigator
* to be the editor's input, if linking is enabled.
* An editor has been activated. Set the selection in this navigator to be
* the editor's input, if linking is enabled.
*/
void editorActivated(IEditorPart editor) {
if (!CPluginPreferencePage.isLinkToEditor()) {
@ -566,6 +627,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
/**
* Returns the pattern filter for this view.
*
* @return the pattern filter
*/
CPatternFilter getPatternFilter() {
@ -574,6 +636,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
/**
* Returns the working set filter for this view.
*
* @return the working set
*/
public IWorkingSet getWorkingSet() {
@ -604,36 +667,23 @@ public class CView extends ViewPart implements ISetSelectionTarget,
* Create self's action objects
*/
void makeActions() {
wsFilterActionGroup = new WorkingSetFilterActionGroup(getViewSite().getShell(), workingSetListener);
setActionGroup(new MainActionGroup(this));
}
/**
* Called when the context menu is about to open.
* Delegates to the action group using the viewer's selection as the action context.
* Called when the context menu is about to open. Delegates to the action
* group using the viewer's selection as the action context.
*
* @since 2.0
*/
protected void fillContextMenu(IMenuManager menu) {
IStructuredSelection selection =
(IStructuredSelection) getViewer().getSelection();
getActionGroup().setContext(new ActionContext(selection));
getActionGroup().fillContextMenu(menu);
IStructuredSelection selection = (IStructuredSelection) getViewer().getSelection();
CViewActionGroup actionGroup = getActionGroup();
if (actionGroup != null) {
actionGroup.setContext(new ActionContext(selection));
actionGroup.fillContextMenu(menu);
actionGroup.setContext(null);
}
IStructuredSelection convertSelectionToCElement(ISelection s) {
List converted = new ArrayList();
if (s instanceof StructuredSelection) {
Object[] elements= ((StructuredSelection)s).toArray();
for (int i= 0; i < elements.length; i++) {
Object e = elements[i];
if (e instanceof IAdaptable) {
ICElement c = (ICElement)((IAdaptable)e).getAdapter(ICElement.class);
if (c != null)
converted.add(c);
}
}
}
return new StructuredSelection(converted.toArray());
}
/**
@ -655,7 +705,8 @@ public class CView extends ViewPart implements ISetSelectionTarget,
/**
* Returns the message to show in the status line.
*
* @param selection the current selection
* @param selection
* the current selection
* @return the status line message
*/
String getStatusLineMessage(IStructuredSelection selection) {
@ -697,6 +748,20 @@ public class CView extends ViewPart implements ISetSelectionTarget,
return "";//$NON-NLS-1$
}
/**
* Updates the action bar actions.
*
* @param selection
* the current selection
*/
protected void updateActionBars(IStructuredSelection selection) {
CViewActionGroup group = getActionGroup();
if (group != null) {
group.setContext(new ActionContext(selection));
group.updateActionBars();
}
}
void updateTitle() {
Object input = getViewer().getInput();
String viewName = getConfigurationElement().getAttribute("name"); //$NON-NLS-1$
@ -714,17 +779,41 @@ public class CView extends ViewPart implements ISetSelectionTarget,
/**
* Updates the message shown in the status line.
*
* @param selection the current selection
* @param selection
* the current selection
*/
void updateStatusLine(IStructuredSelection selection) {
String msg = getStatusLineMessage(selection);
getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
}
/*
*/
public void setWorkingSet(IWorkingSet workingSet) {
TreeViewer treeViewer = getViewer();
Object[] expanded = treeViewer.getExpandedElements();
ISelection selection = treeViewer.getSelection();
workingSetFilter.setWorkingSet(workingSet);
/*
* if (workingSet != null) { settings.put(STORE_WORKING_SET,
* workingSet.getName()); } else { settings.put(STORE_WORKING_SET, "");
* //$NON-NLS-1$ }
*/
updateTitle();
treeViewer.refresh();
treeViewer.setExpandedElements(expanded);
if (selection.isEmpty() == false && selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
treeViewer.reveal(structuredSelection.getFirstElement());
}
}
/**
* Sets the decorator for the package explorer.
*
* @param decorator a label decorator or <code>null</code> for no decorations.
* @param decorator
* a label decorator or <code>null</code> for no decorations.
*/
public void setLabelDecorator(ILabelDecorator decorator) {
ILabelProvider cProvider = createLabelProvider();
@ -732,8 +821,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
}
public void propertyChange(PropertyChangeEvent event) {
if (viewer == null)
return;
if (viewer == null) return;
boolean refreshViewer = false;
@ -743,24 +831,32 @@ public class CView extends ViewPart implements ISetSelectionTarget,
refreshViewer = true;
}
if (refreshViewer)
viewer.refresh();
if (refreshViewer) viewer.refresh();
}
/**
* Returns whether the navigator selection automatically tracks the active
* editor.
*
* @return <code>true</code> if linking is enabled, <code>false</code>
* if not
*/
public boolean isLinkingEnabled() {
return CPluginPreferencePage.isLinkToEditor();
}
/**
* Links to editor (if option enabled)
*/
void linkToEditor(IStructuredSelection selection) {
if (!CPluginPreferencePage.isLinkToEditor()) {
// ignore selection changes if the package explorer is not the active
// part.
// In this case the selection change isn't triggered by a user.
if (!isActivePart()) {
return;
}
// ignore selection changes if the package explorer is not the active part.
// In this case the selection change isn't triggered by a user.
if (!isActivePart())
return;
Object obj= selection.getFirstElement();
if (selection.size() == 1) {
Object obj = selection.getFirstElement();
if (obj instanceof ISourceReference) {
ITranslationUnit tu = ((ISourceReference) obj).getTranslationUnit();
if (tu != null) {
@ -775,14 +871,15 @@ public class CView extends ViewPart implements ISetSelectionTarget,
}
}
}
}
private boolean isActivePart() {
return this == getSite().getPage().getActivePart();
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see IViewPartInputProvider#getViewPartInput()
*/
public Object getViewPartInput() {
@ -816,22 +913,12 @@ public class CView extends ViewPart implements ISetSelectionTarget,
String show = memento.getString(TAG_SHOWLIBRARIES);
if (show != null) {
getLibraryFilter().setShowLibraries(show.equals("true")); //$NON-NLS-1$
}
else
} else {
initFilterFromPreferences();
}
}
void restoreState(IMemento memento) {
//Restore the working set before we re-build the tree
String wsname = memento.getString(TAG_WORKINGSET);
if(wsname != null) {
IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager();
IWorkingSet set = wsmanager.getWorkingSet(wsname);
wsFilterActionGroup.setWorkingSet(set);
} else {
wsFilterActionGroup.setWorkingSet(null);
}
//ICelement container = CElementFactory.getDefault().getRoot();
CoreModel factory = CoreModel.getDefault();
IMemento childMem = memento.getChild(TAG_EXPANDED);
@ -910,8 +997,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
Object o = expandedElements[i];
// Do not save expanded binary files are libraries.
if (o instanceof IParent
&& ! (o instanceof IArchiveContainer || o instanceof IBinaryContainer
|| o instanceof IBinary || o instanceof IArchive)) {
&& !(o instanceof IArchiveContainer || o instanceof IBinaryContainer || o instanceof IBinary || o instanceof IArchive)) {
IMemento elementMem = expandedMem.createChild(TAG_ELEMENT);
ICElement e = (ICElement) o;
IResource res = e.getResource();
@ -960,8 +1046,7 @@ public class CView extends ViewPart implements ISetSelectionTarget,
//save library filter
boolean showLibraries = getLibraryFilter().getShowLibraries();
String show = "true"; //$NON-NLS-1$
if (!showLibraries)
show= "false"; //$NON-NLS-1$
if (!showLibraries) show = "false"; //$NON-NLS-1$
memento.putString(TAG_SHOWLIBRARIES, show);
//Save the working set away
@ -973,7 +1058,9 @@ public class CView extends ViewPart implements ISetSelectionTarget,
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see org.eclipse.ui.part.IShowInTarget#show(org.eclipse.ui.part.ShowInContext)
*/
public boolean show(ShowInContext context) {

View file

@ -14,24 +14,25 @@ import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.plugin.AbstractUIPlugin;
/**
* This is the action group for all the resource navigator actions.
* This is the action group for all the view actions.
* It delegates to several subgroups for most of the actions.
*
* @see GotoActionGroup
* @see OpenActionGroup
* @see OpenFileGroup
* @see RefactorActionGroup
* @see SortAndFilterActionGroup
* @see WorkspaceActionGroup
*
* @since 2.0
*/
public abstract class CViewActionGroup extends ActionGroup {
@ -80,11 +81,31 @@ public abstract class CViewActionGroup extends ActionGroup {
public void handleKeyPressed(KeyEvent event) {
}
/**
* Handles a key released event by invoking the appropriate action.
* Does nothing by default.
*/
public void handleKeyReleased(KeyEvent event) {
}
/**
* Makes the actions contained in this action group.
*/
protected abstract void makeActions();
/**
* Called when the context menu is about to open.
* Override to add your own context dependent menu contributions.
*/
public abstract void fillContextMenu(IMenuManager menu);
/* (non-Javadoc)
* @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
*/
public abstract void fillActionBars(IActionBars actionBars);
public abstract void updateActionBars();
/**
* Runs the default action in the group.
* Does nothing by default.

View file

@ -10,6 +10,11 @@
###############################################################################
OpenWithMenu.label=Open Wit&h
BuildAction.label=&Build Project
RebuildAction.label=Rebuild Pro&ject
CollapseAllAction.label=Collapse All
CollapseAllAction.tooltip=Collapse All
CollapseAllAction.description=Collapse All
@ -20,3 +25,26 @@ CopyAction.toolTip = Copy
PasteAction.title=&Paste
PasteAction.toolTip = Paste
NewWizardsActionGroup.new=Ne&w
OpenProjectAction.dialog.title=Open Project
OpenProjectAction.dialog.message=Select project(s) to be opened
OpenProjectAction.error.message=Problems while opening projects
RefreshAction.label= Re&fresh
RefreshAction.toolTip= Refresh
RefreshAction.progressMessage= Refreshing...
RefreshAction.error.title= Refresh Problems
RefreshAction.error.message= Problems occurred refreshing the selected resources.
RefreshAction.locationDeleted.title= Project location has been deleted
RefreshAction.locationDeleted.message= The location for project {0} ({1}) has been deleted.\n Delete {0} from the workspace?
ToggleLinkingAction.label=Lin&k With Editor
ToggleLinkingAction.tooltip=Link with Editor
ToggleLinkingAction.description=Link with active editor
SearchAction.label=Search
FileSearchAction.label=Search
FilterSelectionAction.label= Filters ...
ShowLibrariesAction.label = Show Reference Libs

View file

@ -0,0 +1,109 @@
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.views.framelist.BackAction;
import org.eclipse.ui.views.framelist.ForwardAction;
import org.eclipse.ui.views.framelist.FrameList;
import org.eclipse.ui.views.framelist.GoIntoAction;
import org.eclipse.ui.views.framelist.UpAction;
/**
* This is the action group for the goto actions.
*/
public class GotoActionGroup extends CViewActionGroup {
private BackAction backAction;
private ForwardAction forwardAction;
private GoIntoAction goIntoAction;
private UpAction upAction;
public GotoActionGroup(CView cview) {
super(cview);
}
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection celements = (IStructuredSelection) getContext().getSelection();
IStructuredSelection selection = SelectionConverter.convertSelectionToResources(celements);
if (selection.size() == 1) {
if (SelectionConverter.allResourcesAreOfType(selection, IResource.FOLDER)) {
menu.add(goIntoAction);
} else {
IStructuredSelection resourceSelection = SelectionConverter.allResources(selection, IResource.PROJECT);
if (resourceSelection != null && !resourceSelection.isEmpty()) {
IProject project = (IProject) resourceSelection.getFirstElement();
if (project.isOpen()) {
menu.add(goIntoAction);
}
}
}
}
}
public void fillActionBars(IActionBars actionBars) {
actionBars.setGlobalActionHandler(IWorkbenchActionConstants.GO_INTO, goIntoAction);
actionBars.setGlobalActionHandler(ActionFactory.BACK.getId(), backAction);
actionBars.setGlobalActionHandler(ActionFactory.FORWARD.getId(), forwardAction);
actionBars.setGlobalActionHandler(IWorkbenchActionConstants.UP, upAction);
IToolBarManager toolBar = actionBars.getToolBarManager();
toolBar.add(backAction);
toolBar.add(forwardAction);
toolBar.add(upAction);
}
protected void makeActions() {
FrameList frameList = getCView().getFrameList();
goIntoAction = new GoIntoAction(frameList);
backAction = new BackAction(frameList);
forwardAction = new ForwardAction(frameList);
upAction = new UpAction(frameList);
}
/* (non-Javadoc)
*/
public void updateActionBars() {
ActionContext context = getContext();
boolean enable = false;
// Fix for bug 26126. Resource change listener could call
// updateActionBars without a context being set.
// This should never happen because resource navigator sets
// context immediately after this group is created.
if (context != null) {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
if (selection.size() == 1) {
Object object = selection.getFirstElement();
if (object instanceof IAdaptable) {
IResource resource = (IResource)((IAdaptable)object).getAdapter(IResource.class);
if (resource instanceof IProject) {
enable = ((IProject) resource).isOpen();
} else if (resource instanceof IFolder) {
enable = true;
}
}
}
}
goIntoAction.setEnabled(enable);
// the rest of the actions update by listening to frame list changes
}
}

View file

@ -10,9 +10,6 @@
************************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.ui.IContextMenuConstants;
@ -21,88 +18,57 @@ import org.eclipse.cdt.internal.ui.editor.FileSearchActionInWorkingSet;
import org.eclipse.cdt.internal.ui.editor.OpenIncludeAction;
import org.eclipse.cdt.internal.ui.editor.SearchDialogAction;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.GroupMarker;
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.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.actions.ActionContext;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.AddBookmarkAction;
import org.eclipse.ui.actions.BuildAction;
import org.eclipse.ui.actions.CloseResourceAction;
import org.eclipse.ui.actions.AddTaskAction;
import org.eclipse.ui.actions.ExportResourcesAction;
import org.eclipse.ui.actions.ImportResourcesAction;
import org.eclipse.ui.actions.NewWizardMenu;
import org.eclipse.ui.actions.OpenFileAction;
import org.eclipse.ui.actions.OpenInNewWindowAction;
import org.eclipse.ui.actions.OpenResourceAction;
import org.eclipse.ui.actions.OpenSystemEditorAction;
import org.eclipse.ui.actions.OpenWithMenu;
import org.eclipse.ui.actions.RefreshAction;
import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
import org.eclipse.ui.dialogs.PropertyDialogAction;
import org.eclipse.ui.ide.IDEActionFactory;
import org.eclipse.ui.views.framelist.BackAction;
import org.eclipse.ui.views.framelist.ForwardAction;
import org.eclipse.ui.views.framelist.FrameList;
import org.eclipse.ui.views.framelist.GoIntoAction;
import org.eclipse.ui.views.framelist.UpAction;
/**
* The main action group for the cview.
* This contains a few actions and several subgroups.
* The main action group for the cview. This contains a few actions and several
* subgroups.
*/
public class MainActionGroup extends CViewActionGroup {
// Actions for Menu context.
AddBookmarkAction addBookmarkAction;
OpenFileAction openFileAction;
OpenSystemEditorAction openSystemEditorAction;
AddTaskAction addTaskAction;
PropertyDialogAction propertyDialogAction;
ImportResourcesAction importAction;
ExportResourcesAction exportAction;
RefreshAction refreshAction;
CloseResourceAction closeProjectAction;
OpenResourceAction openProjectAction;
RefactorActionGroup refactorGroup;
BuildAction buildAction;
BuildAction rebuildAction;
// CElement action
OpenIncludeAction openIncludeAction;
BackAction backAction;
ForwardAction forwardAction;
GoIntoAction goIntoAction;
UpAction upAction;
// Collapsing
CollapseAllAction collapseAllAction;
WorkingSetFilterActionGroup wsFilterActionGroup;
//ToggleLinkingAction toggleLinkingAction;
ShowLibrariesAction clibFilterAction;
@ -110,12 +76,14 @@ public class MainActionGroup extends CViewActionGroup {
FileSearchAction fFileSearchAction;
FileSearchActionInWorkingSet fFileSearchActionInWorkingSet;
SearchDialogAction fSearchDialogAction;
FilterSelectionAction patternFilterAction;
// Menu tags for the build
final String BUILD_GROUP_MARKER = "buildGroup";
final String BUILD_GROUP_MARKER_END = "end-buildGroup";
BuildGroup buildGroup;
OpenFileGroup openFileGroup;
GotoActionGroup gotoGroup;
RefactorActionGroup refactorGroup;
OpenProjectGroup openProjectGroup;
WorkingSetFilterActionGroup workingSetGroup;
public MainActionGroup(CView cview) {
super(cview);
@ -126,85 +94,80 @@ public class MainActionGroup extends CViewActionGroup {
*/
public void handleKeyPressed(KeyEvent event) {
refactorGroup.handleKeyPressed(event);
openFileGroup.handleKeyPressed(event);
openProjectGroup.handleKeyPressed(event);
gotoGroup.handleKeyPressed(event);
buildGroup.handleKeyPressed(event);
}
/**
* Create the KeyListener for doing the refresh on the viewer.
* Handles key events in viewer.
*/
void initRefreshKey() {
final Viewer viewer = getCView().getViewer();
viewer.getControl().addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent event) {
if (event.keyCode == SWT.F5) {
refreshAction.selectionChanged(
(IStructuredSelection)viewer.getSelection());
if (refreshAction.isEnabled())
refreshAction.run();
}
}
});
public void handleKeyReleased(KeyEvent event) {
refactorGroup.handleKeyReleased(event);
openFileGroup.handleKeyReleased(event);
openProjectGroup.handleKeyReleased(event);
gotoGroup.handleKeyReleased(event);
buildGroup.handleKeyReleased(event);
}
protected void makeActions() {
final Viewer viewer = getCView().getViewer();
FrameList framelist = getCView().getFrameList();
Shell shell = getCView().getViewSite().getShell();
openIncludeAction = new OpenIncludeAction (viewer);
openFileAction = new OpenFileAction(getCView().getSite().getPage());
openSystemEditorAction = new OpenSystemEditorAction(getCView().getSite().getPage());
refreshAction = new RefreshAction(shell);
initRefreshKey();
buildAction = new BuildAction(shell, IncrementalProjectBuilder.INCREMENTAL_BUILD);
rebuildAction = new BuildAction(shell, IncrementalProjectBuilder.FULL_BUILD);
openFileGroup = new OpenFileGroup(getCView());
openProjectGroup = new OpenProjectGroup(getCView());
gotoGroup = new GotoActionGroup(getCView());
buildGroup = new BuildGroup(getCView());
refactorGroup = new RefactorActionGroup(getCView());
IWorkspace workspace = CUIPlugin.getWorkspace();
openProjectAction = new OpenResourceAction(shell);
workspace.addResourceChangeListener(openProjectAction, IResourceChangeEvent.POST_CHANGE);
closeProjectAction = new CloseResourceAction(shell);
workspace.addResourceChangeListener(closeProjectAction, IResourceChangeEvent.POST_CHANGE);
openIncludeAction = new OpenIncludeAction(viewer);
//sortByNameAction = new SortViewAction(this, false);
//sortByTypeAction = new SortViewAction(this, true);
patternFilterAction = new FilterSelectionAction(shell, getCView(), "Filters...");
clibFilterAction = new ShowLibrariesAction(shell, getCView(), "Show Referenced Libs");
patternFilterAction = new FilterSelectionAction(shell, getCView(), CViewMessages.getString("FilterSelectionAction.label")); //$NON-NLS-1$
clibFilterAction = new ShowLibrariesAction(shell, getCView(), CViewMessages.getString("ShowLibrariesAction.label")); //$NON-NLS-1$
//wsFilterActionGroup = new WorkingSetFilterActionGroup(getCView().getViewSite().getShell(), workingSetListener);
IPropertyChangeListener workingSetUpdater = new IPropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
String property = event.getProperty();
goIntoAction = new GoIntoAction(framelist);
backAction = new BackAction(framelist);
forwardAction = new ForwardAction(framelist);
upAction = new UpAction(framelist);
if (WorkingSetFilterActionGroup.CHANGE_WORKING_SET.equals(property)) {
Object newValue = event.getNewValue();
if (newValue instanceof IWorkingSet) {
getCView().setWorkingSet((IWorkingSet) newValue);
} else if (newValue == null) {
getCView().setWorkingSet(null);
}
}
}
};
workingSetGroup = new WorkingSetFilterActionGroup(shell, workingSetUpdater);
workingSetGroup.setWorkingSet(getCView().getWorkingSet());
addBookmarkAction = new AddBookmarkAction(shell);
//propertyDialogAction = new PropertyDialogAction(shell, viewer);
propertyDialogAction = new PropertyDialogAction(shell,
new ISelectionProvider () {
addTaskAction = new AddTaskAction(shell);
propertyDialogAction = new PropertyDialogAction(shell, new ISelectionProvider() {
public void addSelectionChangedListener(ISelectionChangedListener listener) {
viewer.addSelectionChangedListener(listener);
}
public ISelection getSelection() {
return convertSelection (viewer.getSelection ());
return SelectionConverter.convertSelectionToResources(viewer.getSelection());
}
public void removeSelectionChangedListener(ISelectionChangedListener listener) {
viewer.removeSelectionChangedListener(listener);
}
public void setSelection(ISelection selection) {
viewer.setSelection(selection);
}
});
IActionBars actionBars = getCView().getViewSite().getActionBars();
actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), addBookmarkAction);
actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT.getId(), buildAction);
actionBars.setGlobalActionHandler(IDEActionFactory.REBUILD_PROJECT.getId(), rebuildAction);
actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction);
actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), closeProjectAction);
// Importing/exporting.
importAction = new ImportResourcesAction(getCView().getSite().getWorkbenchWindow());
exportAction = new ExportResourcesAction(getCView().getSite().getWorkbenchWindow());
@ -215,227 +178,101 @@ public class MainActionGroup extends CViewActionGroup {
fSearchDialogAction = new SearchDialogAction(viewer, getCView().getViewSite().getWorkbenchWindow());
}
/**
* Called when the context menu is about to open.
* Override to add your own context dependent menu contributions.
* Called when the context menu is about to open. Override to add your own
* context dependent menu contributions.
*/
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection selection= (IStructuredSelection) getCView().getViewer().getSelection();
IStructuredSelection celements = (IStructuredSelection) getCView().getViewer().getSelection();
IStructuredSelection resources = SelectionConverter.convertSelectionToResources(celements);
if (selection.isEmpty()) {
if (resources.isEmpty()) {
new NewWizardMenu(menu, getCView().getSite().getWorkbenchWindow(), false);
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
return;
}
updateActions (convertSelection(selection));
//updateActions (selection);
addNewMenu(menu, selection);
//updateActions(resources);
addNewMenu(menu, resources);
menu.add(new Separator());
addOpenMenu(menu, selection);
gotoGroup.fillContextMenu(menu);
menu.add(new Separator());
addBuildMenu(menu, selection);
openFileGroup.fillContextMenu(menu);
menu.add(new Separator());
buildGroup.fillContextMenu(menu);
menu.add(new Separator());
refactorGroup.fillContextMenu(menu);
menu.add(new Separator());
importAction.selectionChanged(selection);
exportAction.selectionChanged(selection);
importAction.selectionChanged(resources);
menu.add(importAction);
exportAction.selectionChanged(resources);
menu.add(exportAction);
menu.add(new Separator());
addRefreshMenu (menu, selection);
openProjectGroup.fillContextMenu(menu);
addBookMarkMenu(menu, resources);
menu.add(new Separator());
addCloseMenu(menu, selection);
menu.add(new Separator());
addBookMarkMenu (menu, selection);
menu.add(new Separator());
addSearchMenu(menu, selection);
//menu.add(new Separator());
addSearchMenu(menu, resources);
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS + "-end")); //$NON-NLS-1$
addPropertyMenu(menu, selection);
addPropertyMenu(menu, resources);
}
/**
* Extends the superclass implementation to set the context in the subgroups.
* Extends the superclass implementation to set the context in the
* subgroups.
*/
public void setContext(ActionContext context) {
super.setContext(context);
//gotoGroup.setContext(context);
//openGroup.setContext(context);
gotoGroup.setContext(context);
openFileGroup.setContext(context);
openProjectGroup.setContext(context);
refactorGroup.setContext(context);
buildGroup.setContext(context);
//sortAndFilterGroup.setContext(context);
//workspaceGroup.setContext(context);
}
void addNewMenu(IMenuManager menu, IStructuredSelection selection) {
MenuManager newMenu = new MenuManager("New");
IAdaptable element = (IAdaptable)selection.getFirstElement();
IResource resource = (IResource)element.getAdapter(IResource.class);
newMenu.add(goIntoAction);
MenuManager newMenu = new MenuManager(CViewMessages.getString("NewWizardsActionGroup.new")); //$NON-NLS-1$
new NewWizardMenu(newMenu, getCView().getSite().getWorkbenchWindow(), false);
menu.add(newMenu);
if (resource == null)
return;
menu.add (new Separator ());
if (selection.size() == 1 && resource instanceof IContainer) {
menu.add(goIntoAction);
}
MenuManager gotoMenu = new MenuManager("GoTo");
menu.add(gotoMenu);
if (getCView().getViewer().isExpandable(element)) {
gotoMenu.add(backAction);
gotoMenu.add(forwardAction);
gotoMenu.add(upAction);
}
}
void addOpenMenu(IMenuManager menu, IStructuredSelection selection) {
IAdaptable element = (IAdaptable)selection.getFirstElement();
IResource resource = (IResource)element.getAdapter(IResource.class);
if (resource == null)
return;
// Create a menu flyout.
//MenuManager submenu= new MenuManager("Open With"); //$NON-NLS-1$
//submenu.add(new OpenWithMenu(getSite().getPage(), (IFile) resource));
//menu.add(submenu);
if (resource instanceof IFile)
menu.add(openFileAction);
fillOpenWithMenu(menu, selection);
fillOpenToMenu(menu, selection);
}
void addBuildMenu(IMenuManager menu, IStructuredSelection selection) {
IAdaptable element = (IAdaptable)selection.getFirstElement();
IResource resource = (IResource)element.getAdapter(IResource.class);
if (resource == null) {
return;
}
menu.add(new GroupMarker(BUILD_GROUP_MARKER));
if (resource instanceof IProject && hasBuilder((IProject) resource)) {
buildAction.selectionChanged(selection);
menu.add(buildAction);
rebuildAction.selectionChanged(selection);
menu.add(rebuildAction);
}
menu.add(new GroupMarker(BUILD_GROUP_MARKER_END));
}
void addRefreshMenu (IMenuManager menu, IStructuredSelection selection) {
menu.add(refreshAction);
}
void addCloseMenu (IMenuManager menu, IStructuredSelection selection) {
IAdaptable element = (IAdaptable)selection.getFirstElement();
IResource resource = (IResource)element.getAdapter(IResource.class);
if (resource == null)
return;
if (resource instanceof IProject) {
menu.add(closeProjectAction);
}
}
void addBookMarkMenu(IMenuManager menu, IStructuredSelection selection) {
IAdaptable element = (IAdaptable)selection.getFirstElement();
Object obj = selection.getFirstElement();
if (obj instanceof IAdaptable) {
IAdaptable element = (IAdaptable) obj;
IResource resource = (IResource) element.getAdapter(IResource.class);
if (resource == null)
return;
if (resource instanceof IFile) {
addBookmarkAction.selectionChanged(selection);
menu.add(addBookmarkAction);
}
}
}
void addPropertyMenu(IMenuManager menu, IStructuredSelection selection) {
propertyDialogAction.selectionChanged(convertSelection(selection));
propertyDialogAction.selectionChanged(selection);
if (propertyDialogAction.isApplicableForSelection()) {
menu.add(propertyDialogAction);
}
}
/**
* Add "open with" actions to the context sensitive menu.
* @param menu the context sensitive menu
* @param selection the current selection in the project explorer
*/
void fillOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
IAdaptable element = (IAdaptable)selection.getFirstElement();
IResource resource = (IResource)element.getAdapter(IResource.class);
if (resource == null)
return;
// If one file is selected get it.
// Otherwise, do not show the "open with" menu.
if (selection.size() != 1)
return;
if (!(resource instanceof IFile))
return;
// Create a menu flyout.
MenuManager submenu = new MenuManager("Open With"); //$NON-NLS-1$
submenu.add(new OpenWithMenu(getCView().getSite().getPage(), (IFile) resource));
// Add the submenu.
menu.add(submenu);
}
/**
* Add "open to" actions to the context sensitive menu.
* @param menu the context sensitive menu
* @param selection the current selection in the project explorer
*/
void fillOpenToMenu(IMenuManager menu, IStructuredSelection selection)
{
IAdaptable element = (IAdaptable)selection.getFirstElement();
IResource resource = (IResource)element.getAdapter(IResource.class);
if (resource == null)
return;
// If one file is selected get it.
// Otherwise, do not show the "open with" menu.
if (selection.size() != 1)
return;
if (!(resource instanceof IContainer))
return;
menu.add(new OpenInNewWindowAction(getCView().getSite().getWorkbenchWindow(), resource));
}
void addSearchMenu(IMenuManager menu, IStructuredSelection selection) {
IAdaptable element = (IAdaptable) selection.getFirstElement();
if (element instanceof ITranslationUnit ||
element instanceof ICProject)
if (element instanceof ITranslationUnit || element instanceof ICProject) {
return;
}
MenuManager search = new MenuManager("Search", IContextMenuConstants.GROUP_SEARCH); //$NON-NLS-1$
MenuManager search = new MenuManager(CViewMessages.getString("SearchAction.label"), IContextMenuConstants.GROUP_SEARCH); //$NON-NLS-1$
if (SearchDialogAction.canActionBeAdded(selection)) {
search.add(fSearchDialogAction);
}
if (FileSearchAction.canActionBeAdded(selection)) {
MenuManager fileSearch = new MenuManager("File Search");
MenuManager fileSearch = new MenuManager(CViewMessages.getString("FileSearchAction.label"));//$NON-NLS-1$
fileSearch.add(fFileSearchAction);
fileSearch.add(fFileSearchActionInWorkingSet);
search.add(fileSearch);
@ -444,102 +281,70 @@ public class MainActionGroup extends CViewActionGroup {
menu.add(search);
}
boolean hasBuilder(IProject project) {
try {
ICommand[] commands = project.getDescription().getBuildSpec();
if (commands.length > 0)
return true;
}
catch (CoreException e) {
// Cannot determine if project has builders. Project is closed
// or does not exist. Fall through to return false.
}
return false;
}
public void runDefaultAction(IStructuredSelection selection) {
updateActions(convertSelection(selection));
updateGlobalActions(convertSelection(selection));
openFileGroup.runDefaultAction(selection);
openProjectGroup.runDefaultAction(selection);
gotoGroup.runDefaultAction(selection);
buildGroup.runDefaultAction(selection);
refactorGroup.runDefaultAction(selection);
//workingSetGroup.runDefaultAction(selection);
}
/**
* Updates all actions with the given selection.
* Necessary when popping up a menu, because some of the enablement criteria
* may have changed, even if the selection in the viewer hasn't.
* E.g. A project was opened or closed.
* Updates all actions with the given selection. Necessary when popping up
* a menu, because some of the enablement criteria may have changed, even
* if the selection in the viewer hasn't. E.g. A project was opened or
* closed.
*/
void updateActions(IStructuredSelection selection) {
goIntoAction.update();
refreshAction.selectionChanged(selection);
openFileAction.selectionChanged(selection);
openSystemEditorAction.selectionChanged(selection);
propertyDialogAction.selectionChanged(selection);
importAction.selectionChanged(selection);
exportAction.selectionChanged(selection);
refactorGroup.updateActions(selection);
public void updateActionBars() {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
propertyDialogAction.setEnabled(selection.size() == 1);
//sortByTypeAction.selectionChanged(selection);
//sortByNameAction.selectionChanged(selection);
}
/**
* Updates the global actions with the given selection.
* Be sure to invoke after actions objects have updated, since can* methods delegate to action objects.
*/
void updateGlobalActions(IStructuredSelection selection) {
addBookmarkAction.selectionChanged(selection);
addTaskAction.selectionChanged(selection);
// Ensure Copy global action targets correct action,
// either copyProjectAction or copyResourceAction,
// depending on selection.
IActionBars actionBars = getCView().getViewSite().getActionBars();
actionBars.updateActionBars();
refreshAction.selectionChanged(selection);
buildAction.selectionChanged(selection);
rebuildAction.selectionChanged(selection);
openProjectAction.selectionChanged(selection);
closeProjectAction.selectionChanged(selection);
openFileGroup.updateActionBars();
openProjectGroup.updateActionBars();
gotoGroup.updateActionBars();
buildGroup.updateActionBars();
refactorGroup.updateActionBars();
workingSetGroup.updateActionBars();
}
public void fillActionBars(IActionBars actionBars) {
actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), addBookmarkAction);
actionBars.setGlobalActionHandler(IDEActionFactory.ADD_TASK.getId(), addTaskAction);
workingSetGroup.fillActionBars(actionBars);
gotoGroup.fillActionBars(actionBars);
refactorGroup.fillActionBars(actionBars);
openFileGroup.fillActionBars(actionBars);
openProjectGroup.fillActionBars(actionBars);
buildGroup.fillActionBars(actionBars);
IToolBarManager toolBar = actionBars.getToolBarManager();
toolBar.add(backAction);
toolBar.add(forwardAction);
toolBar.add(upAction);
toolBar.add(new Separator());
toolBar.add(collapseAllAction);
actionBars.updateActionBars();
//wsFilterActionGroup.fillActionBars(actionBars);
//toolBar.add(toggleLinkingAction);
//actionBars.updateActionBars();
IMenuManager menu = actionBars.getMenuManager();
//menu.add(toggleLinkingAction);
//menu.add (clibFilterAction);
menu.add(patternFilterAction);
refactorGroup.fillActionBars(actionBars);
}
public void dispose() {
IWorkspace workspace = CUIPlugin.getWorkspace();
workspace.removeResourceChangeListener(closeProjectAction);
workspace.removeResourceChangeListener(openProjectAction);
refactorGroup.dispose();
openFileGroup.dispose();
openProjectGroup.dispose();
gotoGroup.dispose();
buildGroup.dispose();
super.dispose();
}
static IStructuredSelection convertSelection(ISelection s) {
List converted = new ArrayList();
if (s instanceof StructuredSelection) {
Object[] elements= ((StructuredSelection)s).toArray();
for (int i= 0; i < elements.length; i++) {
Object e = elements[i];
if (e instanceof IAdaptable) {
IResource r = (IResource)((IAdaptable)e).getAdapter(IResource.class);
if (r != null)
converted.add(r);
}
}
}
return new StructuredSelection(converted.toArray());
}
}

View file

@ -0,0 +1,132 @@
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.OpenFileAction;
import org.eclipse.ui.actions.OpenInNewWindowAction;
import org.eclipse.ui.actions.OpenWithMenu;
/**
* This is the action group for the open actions.
*/
public class OpenFileGroup extends CViewActionGroup {
private OpenFileAction openFileAction;
public OpenFileGroup(CView cview) {
super(cview);
}
protected void makeActions() {
openFileAction = new OpenFileAction(getCView().getSite().getPage());
}
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection celements = (IStructuredSelection) getContext().getSelection();
IStructuredSelection selection = SelectionConverter.convertSelectionToResources(celements);
boolean anyResourceSelected = !selection.isEmpty()
&& SelectionConverter.allResourcesAreOfType(selection, IResource.PROJECT | IResource.FOLDER | IResource.FILE);
boolean onlyFilesSelected = !selection.isEmpty() && SelectionConverter.allResourcesAreOfType(selection, IResource.FILE);
if (onlyFilesSelected) {
openFileAction.selectionChanged(selection);
menu.add(openFileAction);
fillOpenWithMenu(menu, selection);
}
if (anyResourceSelected) {
addNewWindowAction(menu, selection);
}
}
/**
* Adds the OpenWith submenu to the context menu.
*
* @param menu
* the context menu
* @param selection
* the current selection
*/
private void fillOpenWithMenu(IMenuManager menu, IStructuredSelection selection) {
// Only supported if exactly one file is selected.
if (selection.size() != 1) {
return;
}
Object element = selection.getFirstElement();
if (!(element instanceof IFile)) {
return;
}
MenuManager submenu = new MenuManager(CViewMessages.getString("OpenWithMenu.label")); //$NON-NLS-1$
submenu.add(new OpenWithMenu(getCView().getSite().getPage(), (IFile) element));
menu.add(submenu);
}
/**
* Adds the Open in New Window action to the context menu.
*
* @param menu
* the context menu
* @param selection
* the current selection
*/
private void addNewWindowAction(IMenuManager menu, IStructuredSelection selection) {
// Only supported if exactly one container (i.e open project or folder) is selected.
if (selection.size() != 1) {
return;
}
Object element = selection.getFirstElement();
if (!(element instanceof IContainer)) {
return;
}
if (element instanceof IProject && !(((IProject) element).isOpen())) {
return;
}
menu.add(new OpenInNewWindowAction(getCView().getSite().getWorkbenchWindow(), (IContainer) element));
}
/* (non-Javadoc)
* @see org.eclipse.ui.actions.ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)
*/
public void fillActionBars(IActionBars actionBars) {
}
/* (non-Javadoc)
* @see org.eclipse.ui.actions.ActionGroup#updateActionBars()
*/
public void updateActionBars() {
}
/**
* Runs the default action (open file).
*/
public void runDefaultAction(IStructuredSelection selection) {
Object obj = selection.getFirstElement();
if (obj instanceof IAdaptable) {
IResource element = (IResource)((IAdaptable)obj).getAdapter(IResource.class);
if (element instanceof IFile) {
openFileAction.selectionChanged(selection);
openFileAction.run();
}
}
}
}

View file

@ -0,0 +1,154 @@
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation 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:
* IBM Corporation - initial API and implementation
************************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import java.util.Iterator;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.CloseResourceAction;
import org.eclipse.ui.actions.OpenResourceAction;
import org.eclipse.ui.actions.RefreshAction;
import org.eclipse.ui.ide.IDEActionFactory;
/**
* This is the action group for actions such as Refresh Local, and Open/Close
* Project.
*/
public class OpenProjectGroup extends CViewActionGroup {
private OpenResourceAction openProjectAction;
private CloseResourceAction closeProjectAction;
private RefreshAction refreshAction;
public OpenProjectGroup(CView cview) {
super(cview);
}
public void fillActionBars(IActionBars actionBars) {
actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction);
actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), closeProjectAction);
}
/**
* Adds the open project, close project and refresh resource actions to the
* context menu.
* <p>
* refresh-no closed project selected
* </p>
* <p>
* Both the open project and close project action may be on the menu at the
* same time.
* </p>
* <p>
* No disabled action should be on the context menu.
* </p>
*
* @param menu
* context menu to add actions to
*/
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
boolean isProjectSelection = true;
boolean hasOpenProjects = false;
boolean hasClosedProjects = false;
Iterator resources = selection.iterator();
while (resources.hasNext() && (!hasOpenProjects || !hasClosedProjects || isProjectSelection)) {
Object next = resources.next();
IProject project = null;
if (next instanceof IProject) {
project = (IProject) next;
} else if (next instanceof IAdaptable) {
IResource res = (IResource) ((IAdaptable) next).getAdapter(IResource.class);
if (res instanceof IProject) {
project = (IProject)res;
}
}
if (project == null) {
isProjectSelection = false;
continue;
}
if (project.isOpen()) {
hasOpenProjects = true;
} else {
hasClosedProjects = true;
}
}
if (!hasClosedProjects) {
refreshAction.selectionChanged(selection);
menu.add(refreshAction);
}
if (isProjectSelection) {
if (hasClosedProjects) {
openProjectAction.selectionChanged(selection);
menu.add(openProjectAction);
}
if (hasOpenProjects) {
closeProjectAction.selectionChanged(selection);
menu.add(closeProjectAction);
}
}
}
/**
* Handles a key pressed event by invoking the appropriate action.
*/
public void handleKeyPressed(KeyEvent event) {
if (event.keyCode == SWT.F5 && event.stateMask == 0) {
if (refreshAction.isEnabled()) {
refreshAction.refreshAll();
}
}
}
protected void makeActions() {
Shell shell = getCView().getSite().getShell();
IWorkspace workspace = CUIPlugin.getWorkspace();
openProjectAction = new OpenResourceAction(shell);
workspace.addResourceChangeListener(openProjectAction, IResourceChangeEvent.POST_CHANGE);
closeProjectAction = new CloseResourceAction(shell);
workspace.addResourceChangeListener(closeProjectAction, IResourceChangeEvent.POST_CHANGE);
refreshAction = new RefreshAction(shell);
refreshAction.setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$
refreshAction.setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$
refreshAction.setHoverImageDescriptor(getImageDescriptor("clcl16/refresh_nav.gif"));//$NON-NLS-1$
}
public void updateActionBars() {
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
refreshAction.selectionChanged(selection);
openProjectAction.selectionChanged(selection);
closeProjectAction.selectionChanged(selection);
}
public void dispose() {
IWorkspace workspace = CUIPlugin.getWorkspace();
workspace.removeResourceChangeListener(closeProjectAction);
workspace.removeResourceChangeListener(openProjectAction);
}
}

View file

@ -10,10 +10,7 @@
************************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import java.util.Iterator;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
@ -57,13 +54,11 @@ public class RefactorActionGroup extends CViewActionGroup {
}
public void fillContextMenu(IMenuManager menu) {
IStructuredSelection selection = MainActionGroup.convertSelection(getCView().getViewer().getSelection());
//(IStructuredSelection) getContext().getSelection();
IStructuredSelection celements = (IStructuredSelection) getContext().getSelection();
IStructuredSelection selection = SelectionConverter.convertSelectionToResources(celements);
boolean anyResourceSelected =
!selection.isEmpty()
&& allResourcesAreOfType(selection,
IResource.PROJECT | IResource.FOLDER | IResource.FILE);
boolean anyResourceSelected = !selection.isEmpty()
&& SelectionConverter.allResourcesAreOfType(selection, IResource.PROJECT | IResource.FOLDER | IResource.FILE);
copyAction.selectionChanged(selection);
menu.add(copyAction);
@ -131,8 +126,9 @@ public class RefactorActionGroup extends CViewActionGroup {
deleteAction.setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_HOVER));
}
public void updateActions(IStructuredSelection selection) {
//IStructuredSelection selection = (IStructuredSelection) getContext().getSelection();
public void updateActionBars() {
IStructuredSelection celements = (IStructuredSelection) getContext().getSelection();
IStructuredSelection selection = SelectionConverter.convertSelectionToResources(celements);
copyAction.selectionChanged(selection);
pasteAction.selectionChanged(selection);
@ -140,20 +136,4 @@ public class RefactorActionGroup extends CViewActionGroup {
moveAction.selectionChanged(selection);
renameAction.selectionChanged(selection);
}
public static boolean allResourcesAreOfType(IStructuredSelection selection, int resourceMask) {
Iterator resources = selection.iterator();
while (resources.hasNext()) {
Object next = resources.next();
IAdaptable element = (IAdaptable)next;
IResource resource = (IResource)element.getAdapter(IResource.class);
if (resource == null)
return false;
if ((resource.getType() & resourceMask) == 0)
return false;
}
return true;
}
}

View file

@ -0,0 +1,127 @@
/**********************************************************************
* Created on 25-Mar-2003
*
* Copyright (c) 2002,2003 QNX Software Systems Ltd. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v0.5
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v05.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.internal.ui.cview;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
public class SelectionConverter {
public static IStructuredSelection convertSelectionToCElements(ISelection s) {
List converted = new ArrayList();
if (s instanceof StructuredSelection) {
Object[] elements = ((StructuredSelection) s).toArray();
for (int i = 0; i < elements.length; i++) {
Object e = elements[i];
if (e instanceof IAdaptable) {
ICElement c = (ICElement) ((IAdaptable) e).getAdapter(ICElement.class);
if (c != null) {
converted.add(c);
}
}
}
}
return new StructuredSelection(converted.toArray());
}
public static IStructuredSelection convertSelectionToResources(ISelection s) {
List converted = new ArrayList();
if (s instanceof StructuredSelection) {
Object[] elements = ((StructuredSelection) s).toArray();
for (int i = 0; i < elements.length; i++) {
Object e = elements[i];
if (e instanceof IAdaptable) {
IResource r = (IResource) ((IAdaptable) e).getAdapter(IResource.class);
if (r != null) {
converted.add(r);
}
}
}
}
return new StructuredSelection(converted.toArray());
}
public static boolean allResourcesAreOfType(IStructuredSelection selection, int resourceMask) {
Iterator resources = selection.iterator();
while (resources.hasNext()) {
Object next = resources.next();
if (next instanceof IAdaptable) {
IAdaptable element = (IAdaptable) next;
IResource resource = (IResource) element.getAdapter(IResource.class);
if (resource == null) {
return false;
}
if (!resourceIsType(resource, resourceMask)) {
return false;
}
}
}
return true;
}
/**
* Returns the selection adapted to IResource. Returns null if any of the entries are not adaptable.
*
* @param selection
* the selection
* @param resourceMask
* resource mask formed by bitwise OR of resource type constants (defined on <code>IResource</code>)
* @return IStructuredSelection or null if any of the entries are not adaptable.
* @see IResource#getType()
*/
public static IStructuredSelection allResources(IStructuredSelection selection, int resourceMask) {
Iterator adaptables = selection.iterator();
List result = new ArrayList();
while (adaptables.hasNext()) {
Object next = adaptables.next();
if (next instanceof IAdaptable) {
IResource resource = (IResource)((IAdaptable) next).getAdapter(IResource.class);
if (resource == null) {
return null;
} else if (resourceIsType(resource, resourceMask)) {
result.add(resource);
}
} else {
return null;
}
}
return new StructuredSelection(result);
}
/**
* Returns whether the type of the given resource is among the specified
* resource types.
*
* @param resource the resource
* @param resourceMask resource mask formed by bitwise OR of resource type
* constants (defined on <code>IResource</code>)
* @return <code>true</code> if the resources has a matching type, and
* <code>false</code> otherwise
* @see IResource#getType()
*/
public static boolean resourceIsType(IResource resource, int resourceMask) {
return (resource.getType() & resourceMask) != 0;
}
}