From 47f1f6f0402b0177d7c7edd80758948037ef0cff Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Mon, 16 Feb 2004 01:36:28 +0000 Subject: [PATCH] Reorganize the actions of the cview into groups. --- core/org.eclipse.cdt.ui/ChangeLog | 12 + .../cdt/internal/ui/cview/BuildGroup.java | 145 ++++ .../eclipse/cdt/internal/ui/cview/CView.java | 649 ++++++++++-------- .../internal/ui/cview/CViewActionGroup.java | 27 +- .../ui/cview/CViewMessages.properties | 28 + .../internal/ui/cview/GotoActionGroup.java | 109 +++ .../internal/ui/cview/MainActionGroup.java | 563 +++++---------- .../cdt/internal/ui/cview/OpenFileGroup.java | 132 ++++ .../internal/ui/cview/OpenProjectGroup.java | 154 +++++ .../ui/cview/RefactorActionGroup.java | 36 +- .../internal/ui/cview/SelectionConverter.java | 127 ++++ 11 files changed, 1291 insertions(+), 691 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/BuildGroup.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/GotoActionGroup.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenFileGroup.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index f7ea99b1b25..aee2ed82463 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -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 diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/BuildGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/BuildGroup.java new file mode 100644 index 00000000000..91d63250df1 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/BuildGroup.java @@ -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. + *

+ * The following conditions apply: build-only projects selected, auto build + * disabled, at least one * builder present + *

+ *

+ * No disabled action should be on the context menu. + *

+ * + * @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 true if it has builders, false 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); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java index a13c1af3763..849b3eb0085 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CView.java @@ -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; @@ -98,35 +97,32 @@ 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 (); - + 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$ - static final String TAG_EXPANDED= "expanded"; //$NON-NLS-1$ - static final String TAG_ELEMENT= "element"; //$NON-NLS-1$ - static final String TAG_PATH= "path"; //$NON-NLS-1$ - static final String TAG_VERTICAL_POSITION= "verticalPosition"; //$NON-NLS-1$ - static final String TAG_HORIZONTAL_POSITION= "horizontalPosition"; //$NON-NLS-1$ + static final String TAG_SELECTION = "selection"; //$NON-NLS-1$ + static final String TAG_EXPANDED = "expanded"; //$NON-NLS-1$ + static final String TAG_ELEMENT = "element"; //$NON-NLS-1$ + static final String TAG_PATH = "path"; //$NON-NLS-1$ + static final String TAG_VERTICAL_POSITION = "verticalPosition"; //$NON-NLS-1$ + static final String TAG_HORIZONTAL_POSITION = "horizontalPosition"; //$NON-NLS-1$ static final String TAG_FILTERS = "filters"; //$NON-NLS-1$ static final String TAG_FILTER = "filter"; //$NON-NLS-1$ static final String TAG_SHOWLIBRARIES = "showLibraries"; //$NON-NLS-1$ @@ -138,49 +134,47 @@ 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) {} - }; - - private IPropertyChangeListener workingSetListener = new IPropertyChangeListener() { - private void doViewerUpdate() { - viewer.getControl().setRedraw(false); - viewer.refresh(); - viewer.getControl().setRedraw(true); + + 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() { + 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(); } } }; - + public CView() { super(); } @@ -194,29 +188,33 @@ 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() { + private ITreeViewerListener expansionListener = new ITreeViewerListener() { + public void treeCollapsed(TreeExpansionEvent event) { } - + public void treeExpanded(TreeExpansionEvent event) { - final Object element= event.getElement(); + final Object element = event.getElement(); if (element instanceof IParent) { //viewer.refresh (element); - Control ctrl= viewer.getControl(); + Control ctrl = viewer.getControl(); if (ctrl != null && !ctrl.isDisposed()) { ctrl.getDisplay().asyncExec(new Runnable() { + public void run() { - Control ctrl= viewer.getControl(); + Control ctrl = viewer.getControl(); if (ctrl != null && !ctrl.isDisposed()) { viewer.expandToLevel(element, 1); } @@ -228,64 +226,90 @@ public class CView extends ViewPart implements ISetSelectionTarget, }; /** - * 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; - //System.out.println ("Double click on " + element); + * 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); + } - try { - part = EditorUtility.openInEditor(element); - if (part != null) { - IWorkbenchPage page = getSite().getPage(); - page.bringToTop(part); - if (element instanceof ISourceReference) { - EditorUtility.revealInEditor(part, (ICElement)element); + /** + * Handles double clicks in viewer. Opens editor if file double-clicked. + */ + protected void handleDoubleClick(DoubleClickEvent event) { + IStructuredSelection s = (IStructuredSelection) event.getSelection(); + IEditorPart part = null; + Object o = s.getFirstElement(); + if (o instanceof IAdaptable) { + IAdaptable element = (IAdaptable) o; + //System.out.println ("Double click on " + element); + + try { + part = EditorUtility.openInEditor(element); + if (part != null) { + IWorkbenchPage page = getSite().getPage(); + page.bringToTop(part); + if (element instanceof ISourceReference) { + EditorUtility.revealInEditor(part, (ICElement) element); + } } + } catch (Exception e) { } - } 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)); } } /** - * Handles key events in viewer. - */ + * Handles key events in viewer. + */ void handleKeyPressed(KeyEvent event) { if (getActionGroup() != null) { getActionGroup().handleKeyPressed(event); } } - /** - * Handles a key release in the viewer. Does nothing by default. - * - */ - protected void handleKeyReleased(KeyEvent event) { - } + /** + * Handles a key release in the viewer. Does nothing by default. + * + */ + 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); } /** * Returns the action group. - * + * * @return the action group */ protected CViewActionGroup getActionGroup() { @@ -294,27 +318,27 @@ 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; } - + /** - * Answer the property defined by key. - */ + * 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); + public void init(IViewSite site, IMemento memento) throws PartInitException { + super.init(site, memento); this.memento = memento; } @@ -336,8 +360,9 @@ 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,33 +376,29 @@ 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); } - /** + /** * Initializes the default preferences */ public static void initDefaults(IPreferenceStore store) { @@ -400,17 +421,15 @@ public class CView extends ViewPart implements ISetSelectionTarget, CElementContentProvider provider = createContentProvider(); viewer.setContentProvider(provider); } - + /** * Sets the label provider for the viewer. */ void initLabelProvider(TreeViewer viewer) { - ILabelProvider cProvider= createLabelProvider(); + ILabelProvider cProvider = createLabelProvider(); ILabelDecorator decorator = CUIPlugin.getDefault().getWorkbench().getDecoratorManager().getLabelDecorator(); 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); } @@ -456,39 +510,40 @@ public class CView extends ViewPart implements ISetSelectionTarget, } /** - * @see ContentOutlinePage#createControl - */ - public void createPartControl (Composite parent) { + * @see ContentOutlinePage#createControl + */ + public void createPartControl(Composite parent) { viewer = createViewer(parent); - viewer.setUseHashlookup (true); + viewer.setUseHashlookup(true); initContentProvider(viewer); initLabelProvider(viewer); CUIPlugin.getDefault().getProblemMarkerManager().addListener(viewer); CUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); - initFilters(viewer); - initListeners(viewer); - initCElementSorter(); - initFrameList(); - initDragAndDrop(); - updateTitle(); - if (memento != null) { restoreFilters(); } else { initFilterFromPreferences(); } - viewer.setInput (CoreModel.getDefault().getCModel()); + initFilters(viewer); + initWorkingSetFilter(); + initListeners(viewer); + initCElementSorter(); + initFrameList(); + initDragAndDrop(); + updateTitle(); + + viewer.setInput(CoreModel.getDefault().getCModel()); initContextMenu(); // 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(); wsmanager.addPropertyChangeListener(workingSetListener); @@ -498,27 +553,28 @@ public class CView extends ViewPart implements ISetSelectionTarget, getSite().setSelectionProvider(viewer); getSite().getPage().addPartListener(partListener); - if (memento != null) - restoreState (memento); + if (memento != null) { + restoreState(memento); + } memento = null; } protected ProblemTreeViewer createViewer(Composite parent) { - return new ProblemTreeViewer (parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); + return new ProblemTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); } protected CElementContentProvider createContentProvider() { - boolean showCUChildren= CPluginPreferencePage.showCompilationUnitChildren(); + boolean showCUChildren = CPluginPreferencePage.showCompilationUnitChildren(); return new CElementContentProvider(showCUChildren, true); } - protected StandardCElementLabelProvider createLabelProvider () { + protected StandardCElementLabelProvider createLabelProvider() { 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()) { @@ -560,29 +621,31 @@ public class CView extends ViewPart implements ISetSelectionTarget, } } - CLibFilter getLibraryFilter () { + CLibFilter getLibraryFilter() { return clibFilter; } - /** - * Returns the pattern filter for this view. - * @return the pattern filter - */ + /** + * Returns the pattern filter for this view. + * + * @return the pattern filter + */ CPatternFilter getPatternFilter() { return patternFilter; } - + /** * Returns the working set filter for this view. + * * @return the working set */ public IWorkingSet getWorkingSet() { return workingSetFilter.getWorkingSet(); } - /** - * Returns the sorter. - */ + /** + * Returns the sorter. + */ public CElementSorter getSorter() { return (CElementSorter) getViewer().getSorter(); } @@ -590,7 +653,7 @@ public class CView extends ViewPart implements ISetSelectionTarget, /** * Returns the tree viewer which shows the resource hierarchy. */ - public TreeViewer getViewer () { + public TreeViewer getViewer() { return viewer; } @@ -598,47 +661,34 @@ public class CView extends ViewPart implements ISetSelectionTarget, */ public FrameList getFrameList() { return frameList; - } + } /** - * Create self's action objects + * 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 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); - } - } + IStructuredSelection selection = (IStructuredSelection) getViewer().getSelection(); + CViewActionGroup actionGroup = getActionGroup(); + if (actionGroup != null) { + actionGroup.setContext(new ActionContext(selection)); + actionGroup.fillContextMenu(menu); + actionGroup.setContext(null); } - return new StructuredSelection(converted.toArray()); } /** - * Returns the tool tip text for the given element. - */ + * Returns the tool tip text for the given element. + */ String getToolTipText(Object element) { if (element instanceof IResource) { IPath path = ((IResource) element).getFullPath(); @@ -649,13 +699,14 @@ public class CView extends ViewPart implements ISetSelectionTarget, } } else { return ((ILabelProvider) viewer.getLabelProvider()).getText(element); - } + } } /** * 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) { @@ -664,11 +715,11 @@ public class CView extends ViewPart implements ISetSelectionTarget, if (o instanceof IResource) { return ((IResource) o).getFullPath().makeRelative().toString(); } else if (o instanceof ICElement) { - ICElement celement = (ICElement)o; - IResource res = (IResource)celement.getAdapter(IResource.class); + ICElement celement = (ICElement) o; + IResource res = (IResource) celement.getAdapter(IResource.class); if (res != null) { return res.getFullPath().toString(); - } else if (celement.getElementType() == ICElement.C_VCONTAINER) { + } else if (celement.getElementType() == ICElement.C_VCONTAINER) { if (celement instanceof IBinaryContainer) { ICProject cproj = celement.getCProject(); if (cproj != null) { @@ -680,11 +731,11 @@ public class CView extends ViewPart implements ISetSelectionTarget, return cproj.getPath() + " - archives"; } } else if (celement instanceof IBinaryModule) { - IBinary bin = ((IBinaryModule)celement).getBinary(); + IBinary bin = ((IBinaryModule) celement).getBinary(); return bin.getPath() + ":" + celement.getElementName(); } } else if (celement.getElementType() > ICElement.C_UNIT) { - return celement.getPath().toString() + " - [" + celement.getElementName() +"]"; + return celement.getPath().toString() + " - [" + celement.getElementName() + "]"; } return celement.getElementName(); } else { @@ -697,15 +748,29 @@ public class CView extends ViewPart implements ISetSelectionTarget, return "";//$NON-NLS-1$ } - void updateTitle () { - Object input= getViewer().getInput(); - String viewName= getConfigurationElement().getAttribute("name"); //$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$ if (input == null || (input instanceof ICModel)) { setTitle(viewName); setTitleToolTip(""); //$NON-NLS-1$ } else { ILabelProvider labelProvider = (ILabelProvider) getViewer().getLabelProvider(); - String inputText= labelProvider.getText(input); + String inputText = labelProvider.getText(input); setTitle(inputText); setTitleToolTip(getToolTipText(input)); } @@ -713,60 +778,91 @@ 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 null for no decorations. + * + * @param decorator + * a label decorator or null for no decorations. */ public void setLabelDecorator(ILabelDecorator decorator) { - ILabelProvider cProvider= createLabelProvider(); + ILabelProvider cProvider = createLabelProvider(); viewer.setLabelProvider(new DecoratingLabelProvider(cProvider, decorator)); } public void propertyChange(PropertyChangeEvent event) { - if (viewer == null) - return; + if (viewer == null) return; - boolean refreshViewer= false; + boolean refreshViewer = false; if (event.getProperty() == PreferenceConstants.PREF_SHOW_CU_CHILDREN) { - boolean showCUChildren= CPluginPreferencePage.showCompilationUnitChildren(); - ((CElementContentProvider)viewer.getContentProvider()).setProvideMembers(showCUChildren); - refreshViewer= true; + boolean showCUChildren = CPluginPreferencePage.showCompilationUnitChildren(); + ((CElementContentProvider) viewer.getContentProvider()).setProvideMembers(showCUChildren); + refreshViewer = true; } - if (refreshViewer) - viewer.refresh(); + if (refreshViewer) viewer.refresh(); + } + + /** + * Returns whether the navigator selection automatically tracks the active + * editor. + * + * @return true if linking is enabled, false + * 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(); + ITranslationUnit tu = ((ISourceReference) obj).getTranslationUnit(); if (tu != null) { - IEditorPart part= EditorUtility.isOpenInEditor(obj); + IEditorPart part = EditorUtility.isOpenInEditor(obj); if (part != null) { - IWorkbenchPage page= getSite().getPage(); + IWorkbenchPage page = getSite().getPage(); page.bringToTop(part); if (obj instanceof ICElement) { EditorUtility.revealInEditor(part, (ICElement) obj); @@ -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() { @@ -793,7 +890,7 @@ public class CView extends ViewPart implements ISetSelectionTarget, } public void collapseAll() { - viewer.getControl().setRedraw(false); + viewer.getControl().setRedraw(false); viewer.collapseToLevel(getViewPartInput(), TreeViewer.ALL_LEVELS); viewer.getControl().setRedraw(true); } @@ -802,10 +899,10 @@ public class CView extends ViewPart implements ISetSelectionTarget, // restore pattern filters IMemento filtersMem = memento.getChild(TAG_FILTERS); if (filtersMem != null) { - IMemento children[]= filtersMem.getChildren(TAG_FILTER); - String filters[]= new String[children.length]; + IMemento children[] = filtersMem.getChildren(TAG_FILTER); + String filters[] = new String[children.length]; for (int i = 0; i < children.length; i++) { - filters[i]= children[i].getString(TAG_ELEMENT); + filters[i] = children[i].getString(TAG_ELEMENT); } getPatternFilter().setPatterns(filters); } else { @@ -816,30 +913,20 @@ 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); - if(childMem != null) { + if (childMem != null) { ArrayList elements = new ArrayList(); IMemento[] elementMem = childMem.getChildren(TAG_ELEMENT); - for (int i = 0; i < elementMem.length; i++){ - String p = elementMem[i].getString(TAG_PATH); + for (int i = 0; i < elementMem.length; i++) { + String p = elementMem[i].getString(TAG_PATH); if (p != null) { IPath path = new Path(p); ICElement element = factory.create(path); @@ -851,10 +938,10 @@ public class CView extends ViewPart implements ISetSelectionTarget, viewer.setExpandedElements(elements.toArray()); } childMem = memento.getChild(TAG_SELECTION); - if(childMem != null) { + if (childMem != null) { ArrayList list = new ArrayList(); IMemento[] elementMem = childMem.getChildren(TAG_ELEMENT); - for (int i = 0; i < elementMem.length; i++){ + for (int i = 0; i < elementMem.length; i++) { String p = elementMem[i].getString(TAG_PATH); if (p != null) { IPath path = new Path(p); @@ -878,7 +965,7 @@ public class CView extends ViewPart implements ISetSelectionTarget, bar.setSelection(position); position = new Integer(posStr).intValue(); bar.setSelection(position); - } catch (NumberFormatException e){ + } catch (NumberFormatException e) { } } bar = tree.getHorizontalBar(); @@ -888,9 +975,9 @@ public class CView extends ViewPart implements ISetSelectionTarget, int position; position = new Integer(posStr).intValue(); bar.setSelection(position); - } catch (NumberFormatException e){ + } catch (NumberFormatException e) { } - } + } } public void saveState(IMemento memento) { @@ -901,7 +988,7 @@ public class CView extends ViewPart implements ISetSelectionTarget, return; } - //save expanded elements + //save expanded elements Tree tree = viewer.getTree(); Object expandedElements[] = viewer.getExpandedElements(); if (expandedElements.length > 0) { @@ -910,10 +997,9 @@ 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; + ICElement e = (ICElement) o; IResource res = e.getResource(); if (res != null) { elementMem.putString(TAG_PATH, res.getLocation().toOSString()); @@ -923,16 +1009,16 @@ public class CView extends ViewPart implements ISetSelectionTarget, } //save selection - Object elements[] = ((IStructuredSelection)viewer.getSelection()).toArray(); - if(elements.length > 0) { + Object elements[] = ((IStructuredSelection) viewer.getSelection()).toArray(); + if (elements.length > 0) { IMemento selectionMem = memento.createChild(TAG_SELECTION); for (int i = 0; i < elements.length; i++) { if (elements[i] instanceof ICElement) { - ICElement e = (ICElement)elements[i]; - IResource r = e.getResource(); + ICElement e = (ICElement) elements[i]; + IResource r = e.getResource(); if (r != null) { IMemento elementMem = selectionMem.createChild(TAG_ELEMENT); - elementMem.putString(TAG_PATH,r.getLocation().toString()); + elementMem.putString(TAG_PATH, r.getLocation().toString()); } } } @@ -940,54 +1026,55 @@ public class CView extends ViewPart implements ISetSelectionTarget, //save vertical position ScrollBar bar = tree.getVerticalBar(); - int position = bar != null ? bar.getSelection():0; - memento.putString(TAG_VERTICAL_POSITION,String.valueOf(position)); + int position = bar != null ? bar.getSelection() : 0; + memento.putString(TAG_VERTICAL_POSITION, String.valueOf(position)); //save horizontal position - bar = tree.getHorizontalBar(); - position = bar != null ? bar.getSelection():0; - memento.putString(TAG_HORIZONTAL_POSITION,String.valueOf(position)); + bar = tree.getHorizontalBar(); + position = bar != null ? bar.getSelection() : 0; + memento.putString(TAG_HORIZONTAL_POSITION, String.valueOf(position)); //save filters String filters[] = getPatternFilter().getPatterns(); - if(filters.length > 0) { + if (filters.length > 0) { IMemento filtersMem = memento.createChild(TAG_FILTERS); - for (int i = 0; i < filters.length; i++){ + for (int i = 0; i < filters.length; i++) { IMemento child = filtersMem.createChild(TAG_FILTER); - child.putString(TAG_ELEMENT,filters[i]); + child.putString(TAG_ELEMENT, filters[i]); } } //save library filter - boolean showLibraries= getLibraryFilter().getShowLibraries(); - String show= "true"; //$NON-NLS-1$ - if (!showLibraries) - show= "false"; //$NON-NLS-1$ + boolean showLibraries = getLibraryFilter().getShowLibraries(); + String show = "true"; //$NON-NLS-1$ + if (!showLibraries) show = "false"; //$NON-NLS-1$ memento.putString(TAG_SHOWLIBRARIES, show); //Save the working set away - if(workingSetFilter.getWorkingSet() != null) { + if (workingSetFilter.getWorkingSet() != null) { String wsname = workingSetFilter.getWorkingSet().getName(); - if(wsname != null) { + if (wsname != null) { memento.putString(TAG_WORKINGSET, wsname); } } } - - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see org.eclipse.ui.part.IShowInTarget#show(org.eclipse.ui.part.ShowInContext) */ public boolean show(ShowInContext context) { //@@@ Do something with the selection later? //ISelection selection = context.getSelection(); try { - IEditorInput input = (IEditorInput)context.getInput(); - if(input != null) { - IResource res = (IResource)input.getAdapter(IResource.class); - if(res != null) { + IEditorInput input = (IEditorInput) context.getInput(); + if (input != null) { + IResource res = (IResource) input.getAdapter(IResource.class); + if (res != null) { selectReveal(new StructuredSelection(res)); } } - } catch(Exception ex) { + } catch (Exception ex) { /* Ignore */ } return false; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java index f1b0f85cfeb..b66fbe620da 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java @@ -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. diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMessages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMessages.properties index 2d756342893..f023aa6b1f2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMessages.properties +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMessages.properties @@ -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 diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/GotoActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/GotoActionGroup.java new file mode 100644 index 00000000000..7390d2bf3ac --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/GotoActionGroup.java @@ -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 + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java index 0a8d7cc5452..620e255f7cb 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java @@ -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,14 +76,16 @@ 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) { + public MainActionGroup(CView cview) { super(cview); } @@ -126,420 +94,257 @@ 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); - - goIntoAction = new GoIntoAction(framelist); - backAction = new BackAction(framelist); - forwardAction = new ForwardAction(framelist); - upAction = new UpAction(framelist); + IPropertyChangeListener workingSetUpdater = new IPropertyChangeListener() { + public void propertyChange(PropertyChangeEvent event) { + String property = event.getProperty(); + + 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 () { - public void addSelectionChangedListener(ISelectionChangedListener listener) { - viewer.addSelectionChangedListener (listener); + 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 ()); + + public ISelection getSelection() { + return SelectionConverter.convertSelectionToResources(viewer.getSelection()); } + public void removeSelectionChangedListener(ISelectionChangedListener listener) { - viewer.removeSelectionChangedListener (listener); + viewer.removeSelectionChangedListener(listener); } - public void setSelection(ISelection selection) { - viewer.setSelection (selection); + + 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()); - + collapseAllAction = new CollapseAllAction(getCView()); fFileSearchAction = new FileSearchAction(viewer); - fFileSearchActionInWorkingSet = new FileSearchActionInWorkingSet(viewer); + fFileSearchActionInWorkingSet = new FileSearchActionInWorkingSet(viewer); 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. - */ - public void fillContextMenu(IMenuManager menu) { - IStructuredSelection selection= (IStructuredSelection) getCView().getViewer().getSelection(); - - if (selection.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); - menu.add(new Separator()); - addOpenMenu(menu, selection); - menu.add(new Separator()); - addBuildMenu(menu, selection); - menu.add(new Separator ()); - refactorGroup.fillContextMenu(menu); - menu.add(new Separator()); - importAction.selectionChanged(selection); - exportAction.selectionChanged(selection); - menu.add(importAction); - menu.add(exportAction); - menu.add(new Separator()); - addRefreshMenu (menu, selection); - 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()); - menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); - menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS+"-end"));//$NON-NLS-1$ - addPropertyMenu(menu, selection); } /** - * Extends the superclass implementation to set the context in the subgroups. + * Called when the context menu is about to open. Override to add your own + * context dependent menu contributions. + */ + public void fillContextMenu(IMenuManager menu) { + IStructuredSelection celements = (IStructuredSelection) getCView().getViewer().getSelection(); + IStructuredSelection resources = SelectionConverter.convertSelectionToResources(celements); + + 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(resources); + addNewMenu(menu, resources); + menu.add(new Separator()); + gotoGroup.fillContextMenu(menu); + menu.add(new Separator()); + openFileGroup.fillContextMenu(menu); + menu.add(new Separator()); + buildGroup.fillContextMenu(menu); + menu.add(new Separator()); + refactorGroup.fillContextMenu(menu); + menu.add(new Separator()); + importAction.selectionChanged(resources); + menu.add(importAction); + exportAction.selectionChanged(resources); + menu.add(exportAction); + menu.add(new Separator()); + openProjectGroup.fillContextMenu(menu); + addBookMarkMenu(menu, resources); + 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, resources); + } + + /** + * 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); - + void addNewMenu(IMenuManager menu, IStructuredSelection selection) { + 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(); - IResource resource = (IResource)element.getAdapter(IResource.class); - if (resource == null) - return; - if (resource instanceof IFile) { - menu.add(addBookmarkAction); + void addBookMarkMenu(IMenuManager menu, IStructuredSelection selection) { + Object obj = selection.getFirstElement(); + if (obj instanceof IAdaptable) { + IAdaptable element = (IAdaptable) obj; + IResource resource = (IResource) element.getAdapter(IResource.class); + if (resource instanceof IFile) { + addBookmarkAction.selectionChanged(selection); + menu.add(addBookmarkAction); + } } } - void addPropertyMenu (IMenuManager menu, IStructuredSelection selection) { - propertyDialogAction.selectionChanged(convertSelection(selection)); + void addPropertyMenu(IMenuManager menu, IStructuredSelection selection) { + propertyDialogAction.selectionChanged(selection); if (propertyDialogAction.isApplicableForSelection()) { menu.add(propertyDialogAction); } } + void addSearchMenu(IMenuManager menu, IStructuredSelection selection) { + IAdaptable element = (IAdaptable) selection.getFirstElement(); - /** - * 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) + if (element instanceof ITranslationUnit || element instanceof ICProject) { return; + } - // If one file is selected get it. - // Otherwise, do not show the "open with" menu. - if (selection.size() != 1) - return; + MenuManager search = new MenuManager(CViewMessages.getString("SearchAction.label"), IContextMenuConstants.GROUP_SEARCH); //$NON-NLS-1$ - 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) - return; - - MenuManager search = new MenuManager("Search", IContextMenuConstants.GROUP_SEARCH); //$NON-NLS-1$ - - if (SearchDialogAction.canActionBeAdded(selection)){ + 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); } - + 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) { + //sortByNameAction.selectionChanged(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) { + 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(); + menu.add(patternFilterAction); + } - 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()); + public void dispose() { + IWorkspace workspace = CUIPlugin.getWorkspace(); + refactorGroup.dispose(); + openFileGroup.dispose(); + openProjectGroup.dispose(); + gotoGroup.dispose(); + buildGroup.dispose(); + super.dispose(); } + } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenFileGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenFileGroup.java new file mode 100644 index 00000000000..8ffe21cc236 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenFileGroup.java @@ -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(); + } + } + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java new file mode 100644 index 00000000000..1893a1183b8 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/OpenProjectGroup.java @@ -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. + *

+ * refresh-no closed project selected + *

+ *

+ * Both the open project and close project action may be on the menu at the + * same time. + *

+ *

+ * No disabled action should be on the context menu. + *

+ * + * @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); + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java index c48464614ff..e47fb13cf5a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java @@ -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); @@ -116,7 +111,7 @@ public class RefactorActionGroup extends CViewActionGroup { pasteAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED)); pasteAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); pasteAction.setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_HOVER)); - + copyAction = new CopyAction(shell, clipboard, pasteAction); copyAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED)); copyAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); @@ -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; - } - } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java new file mode 100644 index 00000000000..f9ad2894734 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/SelectionConverter.java @@ -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 IResource) + * @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 IResource) + * @return true if the resources has a matching type, and + * false otherwise + * @see IResource#getType() + */ + public static boolean resourceIsType(IResource resource, int resourceMask) { + return (resource.getType() & resourceMask) != 0; + } + +}