From 84cfa94f9beb23de390961120d0f327e38e0c07b Mon Sep 17 00:00:00 2001 From: Alain Magloire Date: Wed, 11 Feb 2004 05:20:09 +0000 Subject: [PATCH] Reogornize the CView Action menus --- core/org.eclipse.cdt.ui/ChangeLog | 12 + .../cdt/internal/ui/ICHelpContextIds.java | 80 +- .../eclipse/cdt/internal/ui/cview/CView.java | 803 +++++------------- .../internal/ui/cview/CViewActionGroup.java | 97 +++ .../internal/ui/cview/CViewFrameSource.java | 2 +- .../ui/cview/CViewMessages.properties | 6 + .../internal/ui/cview/CViewMoveAction.java | 84 ++ .../internal/ui/cview/CViewRenameAction.java | 66 ++ .../cdt/internal/ui/cview/CopyAction.java | 201 +++++ .../internal/ui/cview/MainActionGroup.java | 545 ++++++++++++ .../cdt/internal/ui/cview/PasteAction.java | 224 +++++ .../ui/cview/RefactorActionGroup.java | 159 ++++ 12 files changed, 1650 insertions(+), 629 deletions(-) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMoveAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewRenameAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CopyAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/PasteAction.java create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java diff --git a/core/org.eclipse.cdt.ui/ChangeLog b/core/org.eclipse.cdt.ui/ChangeLog index 2586462e7ac..978174113f4 100644 --- a/core/org.eclipse.cdt.ui/ChangeLog +++ b/core/org.eclipse.cdt.ui/ChangeLog @@ -1,3 +1,15 @@ +2004-02-11 Alain Magloire + + Reorganize the CView to better deal with the menu actions. + + * src/org/eclipse/cdt/internal/ui/cview/CopyAction.java + * src/org/eclipse/cdt/internal/ui/cview/PasteAction.java + * src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java + * src/org/eclipse/cdt/internal/ui/cview/CViewMoveAction.java + * src/org/eclipse/cdt/internal/ui/cview/CViewRenameAction.java + * src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java + * src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java + 2004-02-10 Hoda Amer Content Assist Preference : Reduced search options to two choices: Current file (plus its included files) + Current project (plus its included paths) diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java index 1a8cb51ce10..a5ad6383140 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/ICHelpContextIds.java @@ -3,59 +3,65 @@ package org.eclipse.cdt.internal.ui; import org.eclipse.cdt.ui.CUIPlugin; /* - * (c) Copyright IBM Corp. 2000, 2001. - * All Rights Reserved. + * (c) Copyright IBM Corp. 2000, 2001. All Rights Reserved. */ - + /** * Help context ids for the c ui. *

- * This interface contains constants only; it is not intended to be implemented - * or extended. + * This interface contains constants only; it is not intended to be implemented or extended. *

- * + * */ public interface ICHelpContextIds { - public static final String PREFIX= CUIPlugin.PLUGIN_ID + "."; + public static final String PREFIX = CUIPlugin.PLUGIN_ID + "."; // Wizard pages - public static final String NEW_CPROJECT_WIZARD_PAGE= PREFIX + "new_cproject_wizard_page_context"; - public static final String NEW_LAUNCH_WIZARD_PAGE= PREFIX + "new_launch_wizard_page_context"; - public static final String NEW_CLASS_WIZARD_PAGE= PREFIX + "new_class_wizard_page_context"; - public static final String OPEN_CLASS_WIZARD_ACTION= PREFIX + "open_class_wizard_action"; + public static final String NEW_CPROJECT_WIZARD_PAGE = PREFIX + "new_cproject_wizard_page_context"; + public static final String NEW_LAUNCH_WIZARD_PAGE = PREFIX + "new_launch_wizard_page_context"; + public static final String NEW_CLASS_WIZARD_PAGE = PREFIX + "new_class_wizard_page_context"; + public static final String OPEN_CLASS_WIZARD_ACTION = PREFIX + "open_class_wizard_action"; // Actions - public static final String FILTER_PUBLIC_ACTION= PREFIX + "filter_public_action"; - public static final String FILTER_FIELDS_ACTION= PREFIX + "filter_fields_action"; - public static final String FILTER_STATIC_ACTION= PREFIX + "filter_static_action"; + public static final String FILTER_PUBLIC_ACTION = PREFIX + "filter_public_action"; + public static final String FILTER_FIELDS_ACTION = PREFIX + "filter_fields_action"; + public static final String FILTER_STATIC_ACTION = PREFIX + "filter_static_action"; // Preference/property pages - public static final String C_PREF_PAGE= PREFIX + "new_c_pref_page_context"; - public static final String C_EDITOR_PREF_PAGE= PREFIX + "new_c_editor_pref_page_context"; - public static final String C_EDITOR_ANNOTATIONS_PREF_PAGE= PREFIX + "new_c_editor_annotations_pref_page_context"; - public static final String C_EDITOR_COLORS_PREF_PAGE= PREFIX + "new_c_editor_colors_pref_page_context"; - public static final String C_EDITOR_CONTENT_ASSIST_PREF_PAGE= PREFIX + "new_c_editor_content_assist_pref_page_context"; - public static final String TEMPLATE_PREFERENCE_PAGE= PREFIX + "new_c_templates_pref_page_context"; - public static final String LAUNCH_PROPERTY_PAGE= PREFIX + "new_launch_property_page_context"; - public static final String PROJECT_PROPERTY_PAGE= PREFIX + "new_project_property_page_context"; + public static final String C_PREF_PAGE = PREFIX + "new_c_pref_page_context"; + public static final String C_EDITOR_PREF_PAGE = PREFIX + "new_c_editor_pref_page_context"; + public static final String C_EDITOR_ANNOTATIONS_PREF_PAGE = PREFIX + "new_c_editor_annotations_pref_page_context"; + public static final String C_EDITOR_COLORS_PREF_PAGE = PREFIX + "new_c_editor_colors_pref_page_context"; + public static final String C_EDITOR_CONTENT_ASSIST_PREF_PAGE = PREFIX + "new_c_editor_content_assist_pref_page_context"; + public static final String TEMPLATE_PREFERENCE_PAGE = PREFIX + "new_c_templates_pref_page_context"; + public static final String LAUNCH_PROPERTY_PAGE = PREFIX + "new_launch_property_page_context"; + public static final String PROJECT_PROPERTY_PAGE = PREFIX + "new_project_property_page_context"; - public static final String PROJ_CONF_BLOCK= PREFIX + "new_proj_conf_block_context"; - - public static final String TODO_TASK_INPUT_DIALOG= PREFIX + "todo_task_input_dialog_context"; //$NON-NLS-1$ - public static final String TODO_TASK_PROPERTY_PAGE= PREFIX + "tasktags_property_page_context"; //$NON-NLS-1$ - public static final String TODO_TASK_PREFERENCE_PAGE= PREFIX + "tasktags_preference_page_context"; //$NON-NLS-1$ + public static final String PROJ_CONF_BLOCK = PREFIX + "new_proj_conf_block_context"; + + public static final String TODO_TASK_INPUT_DIALOG = PREFIX + "todo_task_input_dialog_context"; //$NON-NLS-1$ + public static final String TODO_TASK_PROPERTY_PAGE = PREFIX + "tasktags_property_page_context"; //$NON-NLS-1$ + public static final String TODO_TASK_PREFERENCE_PAGE = PREFIX + "tasktags_preference_page_context"; //$NON-NLS-1$ + + public static final String BINARY_PARSER_PAGE = PREFIX + "cdt_binary_parser_page"; //$NON-NLS-1$ + public static final String ERROR_PARSERS_PAGE = PREFIX + "cdt_error_parsers_page"; //$NON-NLS-1$ - public static final String BINARY_PARSER_PAGE= PREFIX + "cdt_binary_parser_page"; //$NON-NLS-1$ - public static final String ERROR_PARSERS_PAGE= PREFIX + "cdt_error_parsers_page"; //$NON-NLS-1$ - // Console view - public static final String CLEAR_CONSOLE_ACTION= PREFIX + "clear_console_action_context"; - public static final String CLEAR_CONSOLE_VIEW= PREFIX + "clear_console_view_context"; + public static final String CLEAR_CONSOLE_ACTION = PREFIX + "clear_console_action_context"; + public static final String CLEAR_CONSOLE_VIEW = PREFIX + "clear_console_view_context"; - public static final String TOGGLE_PRESENTATION_ACTION= PREFIX + "toggle_presentation_action_context"; //$NON-NLS-1$ - public static final String TOGGLE_TEXTHOVER_ACTION= PREFIX + "toggle_texthover_action_context"; //$NON-NLS-1$ + public static final String TOGGLE_PRESENTATION_ACTION = PREFIX + "toggle_presentation_action_context"; //$NON-NLS-1$ + public static final String TOGGLE_TEXTHOVER_ACTION = PREFIX + "toggle_texthover_action_context"; //$NON-NLS-1$ - public static final String COLLAPSE_ALL_ACTION= PREFIX + "collapse_all_action"; //$NON-NLS-1$ + public static final String COLLAPSE_ALL_ACTION = PREFIX + "collapse_all_action"; //$NON-NLS-1$ - public static final String C_SEARCH_PAGE= PREFIX + "c_search_page"; //$NON-NLS-1$ -} \ No newline at end of file + public static final String C_SEARCH_PAGE = PREFIX + "c_search_page"; //$NON-NLS-1$ + + public static final String COPY_ACTION = PREFIX + "copy_action_context"; //$NON-NLS-1$ + + public static final String PASTE_ACTION = PREFIX + "paste_action_context"; //$NON_NLS-1$ + + public static final String MOVE_ACTION = PREFIX + "move_action_context"; //$NON-NLS-1$ + public static final String RENAME_ACTION = PREFIX + "rename_action_context"; //$NON-NLS-1$ + +} 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 406b24cb9ec..fc972c9cbd6 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 @@ -5,6 +5,7 @@ package org.eclipse.cdt.internal.ui.cview; * All Rights Reserved. */ + import java.util.ArrayList; import java.util.List; @@ -20,47 +21,29 @@ import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.IParent; import org.eclipse.cdt.core.model.ISourceReference; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.cdt.internal.core.model.CProject; -import org.eclipse.cdt.internal.core.model.TranslationUnit; -import org.eclipse.cdt.internal.ui.IContextMenuConstants; import org.eclipse.cdt.internal.ui.StandardCElementLabelProvider; import org.eclipse.cdt.internal.ui.drag.DelegatingDragAdapter; import org.eclipse.cdt.internal.ui.drag.FileTransferDragAdapter; import org.eclipse.cdt.internal.ui.drag.LocalSelectionTransferDragAdapter; import org.eclipse.cdt.internal.ui.drag.ResourceTransferDragAdapter; import org.eclipse.cdt.internal.ui.drag.TransferDragSourceListener; -import org.eclipse.cdt.internal.ui.editor.FileSearchAction; -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.internal.ui.preferences.CPluginPreferencePage; import org.eclipse.cdt.internal.ui.util.EditorUtility; import org.eclipse.cdt.internal.ui.util.ProblemTreeViewer; -import org.eclipse.cdt.ui.*; import org.eclipse.cdt.ui.CElementContentProvider; +import org.eclipse.cdt.ui.CElementSorter; import org.eclipse.cdt.ui.CLocalSelectionTransfer; import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.PreferenceConstants; -import org.eclipse.core.resources.ICommand; -import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IFolder; -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.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.jface.action.ActionContributionItem; -import org.eclipse.jface.action.GroupMarker; import org.eclipse.jface.action.IMenuListener; import org.eclipse.jface.action.IMenuManager; -import org.eclipse.jface.action.IToolBarManager; import org.eclipse.jface.action.MenuManager; -import org.eclipse.jface.action.Separator; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.PropertyChangeEvent; @@ -88,106 +71,54 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.ScrollBar; -import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; -import org.eclipse.ui.IActionBars; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IMemento; import org.eclipse.ui.IPartListener; import org.eclipse.ui.IViewSite; -import org.eclipse.ui.IWorkbenchActionConstants; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkingSet; import org.eclipse.ui.IWorkingSetManager; import org.eclipse.ui.PartInitException; -import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ResourceWorkingSetFilter; -import org.eclipse.ui.actions.AddBookmarkAction; -import org.eclipse.ui.actions.BuildAction; -import org.eclipse.ui.actions.CloseResourceAction; -import org.eclipse.ui.actions.CopyResourceAction; -import org.eclipse.ui.actions.DeleteResourceAction; -import org.eclipse.ui.actions.MoveResourceAction; -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.RenameResourceAction; +import org.eclipse.ui.actions.ActionContext; import org.eclipse.ui.actions.WorkingSetFilterActionGroup; -import org.eclipse.ui.dialogs.PropertyDialogAction; import org.eclipse.ui.part.ISetSelectionTarget; import org.eclipse.ui.part.IShowInTarget; import org.eclipse.ui.part.PluginTransfer; import org.eclipse.ui.part.ResourceTransfer; import org.eclipse.ui.part.ShowInContext; import org.eclipse.ui.part.ViewPart; -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; import org.eclipse.ui.views.navigator.LocalSelectionTransfer; - - - -public class CView extends ViewPart implements IMenuListener, ISetSelectionTarget, +/** + * + * CView + * + */ +public class CView extends ViewPart implements ISetSelectionTarget, IPropertyChangeListener, IShowInTarget { ProblemTreeViewer viewer; IMemento memento; - // Actions for Menu context. - AddBookmarkAction addBookmarkAction; - CopyResourceAction copyResourceAction; - DeleteResourceAction deleteResourceAction; - OpenFileAction openFileAction; - OpenSystemEditorAction openSystemEditorAction; - PropertyDialogAction propertyDialogAction; - RefreshAction refreshAction; - RenameResourceAction renameResourceAction; - MoveResourceAction moveResourceAction; - - CloseResourceAction closeProjectAction; - OpenResourceAction openProjectAction; - BuildAction buildAction; - BuildAction rebuildAction; - - // CElement action - OpenIncludeAction openIncludeAction; - - BackAction backAction; - ForwardAction forwardAction; - GoIntoAction goIntoAction; - UpAction upAction; + CViewActionGroup actionGroup; WorkingSetFilterActionGroup wsFilterActionGroup; FrameList frameList; CViewFrameSource frameSource; - CPatternFilter patternFilter = new CPatternFilter (); - FilterSelectionAction patternFilterAction; - CLibFilter clibFilter = new CLibFilter (); - ShowLibrariesAction clibFilterAction; - + CPatternFilter patternFilter = new CPatternFilter (); + ResourceWorkingSetFilter workingSetFilter = new ResourceWorkingSetFilter(); ActionContributionItem adjustWorkingSetContributions [] = new ActionContributionItem[5]; - // Collapsing - CollapseAllAction collapseAllAction; - - //Search - FileSearchAction fFileSearchAction; - FileSearchActionInWorkingSet fFileSearchActionInWorkingSet; - SearchDialogAction fSearchDialogAction; // Persistance tags. static final String TAG_SELECTION= "selection"; //$NON-NLS-1$ @@ -200,15 +131,12 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge static final String TAG_FILTER = "filter"; //$NON-NLS-1$ static final String TAG_SHOWLIBRARIES = "showLibraries"; //$NON-NLS-1$ static final String TAG_WORKINGSET = "workingSet"; //$NON-NLS-1$ + static final String TAG_SORTER = "sorter"; //$NON-NLS-1$ //Menu tags final String WORKING_GROUP_MARKER = "workingSetGroup"; final String WORKING_GROUP_MARKER_END = "end-workingSetGroup"; - // Menu tags for the build - final String BUILD_GROUP_MARKER = "buildGroup"; - final String BUILD_GROUP_MARKER_END = "end-buildGroup"; - private IPartListener partListener = new IPartListener() { public void partActivated(IWorkbenchPart part) { if (part instanceof IEditorPart) { @@ -233,7 +161,6 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge if(prop == null) { return; } - if(prop.equals(WorkingSetFilterActionGroup.CHANGE_WORKING_SET)) { workingSetFilter.setWorkingSet((IWorkingSet)ev.getNewValue()); doViewerUpdate(); @@ -273,7 +200,7 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge public void selectReveal(ISelection selection) { IStructuredSelection ssel = convertSelectionToCElement(selection); if (!ssel.isEmpty()) { - getResourceViewer().setSelection(ssel, true); + getViewer().setSelection(ssel, true); } } @@ -330,35 +257,50 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge * Handles key events in viewer. */ void handleKeyPressed(KeyEvent event) { - if (event.character == SWT.DEL - && event.stateMask == 0 && deleteResourceAction.isEnabled()) { - IStructuredSelection isel = deleteResourceAction.getStructuredSelection(); - Object[] array = isel.toArray(); - for (int i = 0; i < array.length; i++){ - if (array[i] instanceof IBinaryContainer - || array[i] instanceof IArchiveContainer) { - return; - } - } - deleteResourceAction.run(); + if (getActionGroup() != null) { + getActionGroup().handleKeyPressed(event); } } + /** + * Handles a key release in the viewer. Does nothing by default. + * + */ + protected void handleKeyReleased(KeyEvent event) { + } - /* (non-Javadoc) - * Method declared on IViewPart. + /** + * Handles selection changed in viewer. + * Updates global actions. + * Links to editor (if option enabled) */ - public void init(IViewSite site,IMemento memento) throws PartInitException { - super.init(site,memento); - this.memento = memento; + void handleSelectionChanged(SelectionChangedEvent event) { + IStructuredSelection sel = (IStructuredSelection) event.getSelection(); + updateStatusLine(sel); + if (getActionGroup() != null) { + getActionGroup().runDefaultAction(sel); + } + linkToEditor(sel); } - void initFrameList() { - frameSource = new CViewFrameSource(this); - frameList = new FrameList(frameSource); - frameSource.connectTo(frameList); + /** + * Returns the action group. + * + * @return the action group + */ + protected CViewActionGroup getActionGroup() { + return actionGroup; } + /** + * Sets the action group. + * + * @param actionGroup the action group + */ + protected void setActionGroup(CViewActionGroup actionGroup) { + this.actionGroup = actionGroup; + } + /** * Answer the property defined by key. */ @@ -368,6 +310,40 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge return super.getAdapter(key); } + /* (non-Javadoc) + * Method declared on IViewPart. + */ + public void init(IViewSite site,IMemento memento) throws PartInitException { + super.init(site,memento); + this.memento = memento; + } + + /** + * init the frame source and the framelist. + */ + void initFrameList() { + frameSource = new CViewFrameSource(this); + frameList = new FrameList(frameSource); + frameSource.connectTo(frameList); + } + + /** + * Initializes the sorter. + */ + void initCElementSorter() { + viewer.setSorter(new CElementSorter()); + } + + /** + * Adds the filters to the viewer. + * + * @param viewer the viewer + */ + void initFilters(TreeViewer viewer) { + viewer.addFilter(patternFilter); + viewer.addFilter(workingSetFilter); + //viewer.addFilter(clibFilter); + } /** * Adds drag and drop support to the navigator. @@ -408,6 +384,9 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge store.setDefault(TAG_SHOWLIBRARIES, true); } + /** + * get the default preferences. + */ void initFilterFromPreferences() { CUIPlugin plugin = CUIPlugin.getDefault(); boolean show = plugin.getPreferenceStore().getBoolean(TAG_SHOWLIBRARIES); @@ -415,81 +394,45 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge } /** - * Create the KeyListener for doing the refresh on the viewer. - */ - void initRefreshKey() { - 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(); - } - } - }); - } - - /** - * 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); - updateActions(sel); - updateGlobalActions(sel); - goIntoAction.update(); - linkToEditor(sel); - } - - - /** - * @see ContentOutlinePage#createControl - */ - public void createPartControl (Composite parent) { - - viewer = createViewer(parent); - viewer.setUseHashlookup (true); + * Sets the content provider for the viewer. + */ + void initContentProvider(TreeViewer viewer) { CElementContentProvider provider = createContentProvider(); viewer.setContentProvider(provider); - setLabelDecorator(PlatformUI.getWorkbench().getDecoratorManager().getLabelDecorator()); - CUIPlugin.getDefault().getProblemMarkerManager().addListener(viewer); - CUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); + } + + /** + * Sets the label provider for the viewer. + */ + void initLabelProvider(TreeViewer viewer) { + ILabelProvider cProvider= createLabelProvider(); + ILabelDecorator decorator = CUIPlugin.getDefault().getWorkbench().getDecoratorManager().getLabelDecorator(); + viewer.setLabelProvider(new DecoratingLabelProvider(cProvider, decorator)); + } + + - // FIXME: Add Drag and Drop support. - initFrameList(); - initRefreshKey(); - updateTitle(); - initDragAndDrop(); - viewer.addFilter(patternFilter); - viewer.addFilter(workingSetFilter); - //viewer.addFilter(clibFilter); - viewer.setSorter(new CElementSorter ()); - // FIXME: Add different Sorting. - if(memento != null) - restoreFilters(); - else - initFilterFromPreferences(); - - viewer.setInput (CoreModel.getDefault().getCModel()); - - MenuManager menuMgr= new MenuManager("#PopupMenu"); //$NON-NLS-1$ + /** + * Initializes and registers the context menu. + */ + protected void initContextMenu() { + MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$ menuMgr.setRemoveAllWhenShown(true); - menuMgr.addMenuListener(this); - - Control control = viewer.getControl(); - Menu menu = menuMgr.createContextMenu(viewer.getTree ()); - control.setMenu (menu); - - // Make the Actions for the Context Menu - makeActions(); - - //Add the property changes after all of the UI work has been done. - IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager(); - wsmanager.addPropertyChangeListener(workingSetListener); + menuMgr.addMenuListener(new IMenuListener() { + public void menuAboutToShow(IMenuManager manager) { + CView.this.fillContextMenu(manager); + } + }); + TreeViewer viewer = getViewer(); + Menu menu = menuMgr.createContextMenu(viewer.getTree()); + viewer.getTree().setMenu(menu); + getSite().registerContextMenu(menuMgr, viewer); + } + /** + * Add listeners to the viewer. + */ + protected void initListeners(TreeViewer viewer) { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { handleDoubleClick(event); @@ -506,11 +449,51 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge public void keyPressed(KeyEvent e) { handleKeyPressed(e); } + public void keyReleased(KeyEvent e) { + handleKeyReleased(e); + } }); + } + + /** + * @see ContentOutlinePage#createControl + */ + public void createPartControl (Composite parent) { + + viewer = createViewer(parent); + viewer.setUseHashlookup (true); + initContentProvider(viewer); + initLabelProvider(viewer); + CUIPlugin.getDefault().getProblemMarkerManager().addListener(viewer); + CUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); + + initFilters(viewer); + initCElementSorter(); + initFrameList(); + initDragAndDrop(); + updateTitle(); + + if (memento != null) { + restoreFilters(); + } else { + initFilterFromPreferences(); + } + + viewer.setInput (CoreModel.getDefault().getCModel()); + + initContextMenu(); + + // Make the Actions for the Context Menu + makeActions(); + getActionGroup().fillActionBars(getViewSite().getActionBars()); + + + //Add the property changes after all of the UI work has been done. + IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager(); + wsmanager.addPropertyChangeListener(workingSetListener); viewer.addTreeListener(expansionListener); - getSite().registerContextMenu(menuMgr, viewer); getSite().setSelectionProvider(viewer); getSite().getPage().addPartListener(partListener); @@ -518,8 +501,6 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge restoreState (memento); memento = null; - fillActionBars(); - } protected ProblemTreeViewer createViewer(Composite parent) { @@ -545,10 +526,9 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge viewer.removeTreeListener(expansionListener); CUIPlugin.getDefault().getProblemMarkerManager().removeListener(viewer); } - IWorkspace workspace = CUIPlugin.getWorkspace(); - workspace.removeResourceChangeListener(closeProjectAction); - workspace.removeResourceChangeListener(openProjectAction); - + if (getActionGroup() != null) { + getActionGroup().dispose(); + } IWorkingSetManager wsmanager = getViewSite().getWorkbenchWindow().getWorkbench().getWorkingSetManager(); wsmanager.removePropertyChangeListener(workingSetListener); @@ -591,148 +571,52 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge return patternFilter; } -// /** -// * Returns the working set filter for this view. -// * @return the working set filter -// */ -// CWorkingSetFilter getWorkingSetFilter() { -// return workingSetFilter; -// } + /** + * Returns the working set filter for this view. + * @return the working set + */ + public IWorkingSet getWorkingSet() { + return workingSetFilter.getWorkingSet(); + } - TreeViewer getViewer () { + /** + * Returns the sorter. + */ + public CElementSorter getSorter() { + return (CElementSorter) getViewer().getSorter(); + } + + /** + * Returns the tree viewer which shows the resource hierarchy. + */ + public TreeViewer getViewer () { return viewer; } + /* + */ + public FrameList getFrameList() { + return frameList; + } + /** - * Create self's action objects - */ + * Create self's action objects + */ void makeActions() { - Shell shell = getViewSite().getShell(); - - openIncludeAction = new OpenIncludeAction (viewer); - openFileAction = new OpenFileAction(getSite().getPage()); - openSystemEditorAction = new OpenSystemEditorAction(getSite().getPage()); - refreshAction = new RefreshAction(shell); - buildAction = new BuildAction(shell, IncrementalProjectBuilder.INCREMENTAL_BUILD); - rebuildAction = new BuildAction(shell, IncrementalProjectBuilder.FULL_BUILD); - moveResourceAction = new MoveResourceAction (shell); - copyResourceAction = new CopyResourceAction(shell); - renameResourceAction = new RenameResourceAction(shell, viewer.getTree()); - deleteResourceAction = new DeleteResourceAction(shell); - - IWorkspace workspace = CUIPlugin.getWorkspace(); - - openProjectAction = new OpenResourceAction(shell); - workspace.addResourceChangeListener(openProjectAction, IResourceChangeEvent.POST_CHANGE); - closeProjectAction = new CloseResourceAction(shell); - workspace.addResourceChangeListener(closeProjectAction, IResourceChangeEvent.POST_CHANGE); - - //sortByNameAction = new SortViewAction(this, false); - //sortByTypeAction = new SortViewAction(this, true); - patternFilterAction = new FilterSelectionAction(shell, this, "Filters..."); - clibFilterAction = new ShowLibrariesAction(shell, this, "Show Referenced Libs"); - wsFilterActionGroup = new WorkingSetFilterActionGroup(getViewSite().getShell(), workingSetListener); - - goIntoAction = new GoIntoAction(frameList); - backAction = new BackAction(frameList); - forwardAction = new ForwardAction(frameList); - upAction = new UpAction(frameList); - - addBookmarkAction = new AddBookmarkAction(shell); - //propertyDialogAction = new PropertyDialogAction(shell, viewer); - propertyDialogAction = new PropertyDialogAction(shell, - new ISelectionProvider () { - public void addSelectionChangedListener(ISelectionChangedListener listener) { - viewer.addSelectionChangedListener (listener); - } - public ISelection getSelection() { - return convertSelection (viewer.getSelection ()); - } - public void removeSelectionChangedListener(ISelectionChangedListener listener) { - viewer.removeSelectionChangedListener (listener); - } - public void setSelection(ISelection selection) { - viewer.setSelection (selection); - } - }); - - IActionBars actionBars = getViewSite().getActionBars(); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.DELETE, deleteResourceAction); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.BOOKMARK, addBookmarkAction); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.REFRESH, refreshAction); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.BUILD_PROJECT, buildAction); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.REBUILD_PROJECT, rebuildAction); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.OPEN_PROJECT, openProjectAction); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.CLOSE_PROJECT, closeProjectAction); - - collapseAllAction = new CollapseAllAction(this); - - fFileSearchAction = new FileSearchAction(viewer); - fFileSearchActionInWorkingSet = new FileSearchActionInWorkingSet(viewer); - fSearchDialogAction = new SearchDialogAction(viewer, this.getViewSite().getWorkbenchWindow()); + setActionGroup(new MainActionGroup(this)); } /** - * 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. + * 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 */ - void updateActions(IStructuredSelection selection) { - copyResourceAction.selectionChanged(selection); - refreshAction.selectionChanged(selection); - moveResourceAction.selectionChanged(selection); - openFileAction.selectionChanged(selection); - openSystemEditorAction.selectionChanged(selection); - propertyDialogAction.selectionChanged(selection); - renameResourceAction.selectionChanged(selection); - //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) { - deleteResourceAction.selectionChanged(selection); - addBookmarkAction.selectionChanged(selection); - - // Ensure Copy global action targets correct action, - // either copyProjectAction or copyResourceAction, - // depending on selection. - copyResourceAction.selectionChanged(selection); - IActionBars actionBars = getViewSite().getActionBars(); - actionBars.setGlobalActionHandler(IWorkbenchActionConstants.COPY, copyResourceAction); - actionBars.updateActionBars(); - renameResourceAction.selectionChanged(selection); - - refreshAction.selectionChanged(selection); - buildAction.selectionChanged(selection); - rebuildAction.selectionChanged(selection); - openProjectAction.selectionChanged(selection); - closeProjectAction.selectionChanged(selection); - - } - - - //---- Action handling ---------------------------------------------------------- - - 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()); + protected void fillContextMenu(IMenuManager menu) { + IStructuredSelection selection = + (IStructuredSelection) getViewer().getSelection(); + getActionGroup().setContext(new ActionContext(selection)); + getActionGroup().fillContextMenu(menu); } IStructuredSelection convertSelectionToCElement(ISelection s) { @@ -751,218 +635,6 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge return new StructuredSelection(converted.toArray()); } - /** - * Called when the context menu is about to open. - * Override to add your own context dependent menu contributions. - */ - public void menuAboutToShow(IMenuManager menu) { - IStructuredSelection selection= (IStructuredSelection) viewer.getSelection(); - if (selection.isEmpty()) { - new NewWizardMenu(menu, 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 ()); - addRefreshMenu (menu, selection); - menu.add(new Separator()); - addIOMenu(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); - } - - 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); - - new NewWizardMenu(newMenu, 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 (viewer.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)); - } - - 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; - } - - - void addRefreshMenu (IMenuManager menu, IStructuredSelection selection) { - menu.add(refreshAction); - } - - void addIOMenu (IMenuManager menu, IStructuredSelection selection) { - IAdaptable element = (IAdaptable)selection.getFirstElement(); - IResource resource = (IResource)element.getAdapter(IResource.class); - if (resource == null) - return; - - menu.add(new Separator ()); - - if (resource instanceof IProject) { - menu.add(closeProjectAction); - } - - if (resource instanceof IFile || resource instanceof IFolder) { - menu.add(copyResourceAction); - menu.add(moveResourceAction); - } - - if (!(element instanceof IArchiveContainer || element instanceof IBinaryContainer)) { - menu.add(renameResourceAction); - menu.add(deleteResourceAction); - } - - } - - 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 addPropertyMenu (IMenuManager menu, IStructuredSelection selection) { - propertyDialogAction.selectionChanged(convertSelection(selection)); - if (propertyDialogAction.isApplicableForSelection()) { - menu.add(propertyDialogAction); - } - } - - - /** - * Add "open with" actions to the context sensitive menu. - * @param menu the context sensitive menu - * @param selection the current selection in the project explorer - */ - void fillOpenWithMenu(IMenuManager menu, IStructuredSelection selection) { - IAdaptable element = (IAdaptable)selection.getFirstElement(); - IResource resource = (IResource)element.getAdapter(IResource.class); - if (resource == null) - return; - - // If one file is selected get it. - // Otherwise, do not show the "open with" menu. - if (selection.size() != 1) - return; - - if (!(resource instanceof IFile)) - return; - - // Create a menu flyout. - MenuManager submenu = new MenuManager("Open With"); //$NON-NLS-1$ - submenu.add(new OpenWithMenu(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(getSite().getWorkbenchWindow(), resource)); - } - - /** * Returns the tool tip text for the given element. */ @@ -980,11 +652,11 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge } /** - * Returns the message to show in the status line. - * - * @param selection the current selection - * @return the status line message - */ + * Returns the message to show in the status line. + * + * @param selection the current selection + * @return the status line message + */ String getStatusLineMessage(IStructuredSelection selection) { if (selection.size() == 1) { Object o = selection.getFirstElement(); @@ -1039,41 +711,15 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge } /** - * Returns the tree viewer which shows the resource hierarchy. + * Updates the message shown in the status line. + * + * @param selection the current selection */ - TreeViewer getResourceViewer() { - return viewer; - } - - /** - * Updates the message shown in the status line. - * - * @param selection the current selection - */ void updateStatusLine(IStructuredSelection selection) { String msg = getStatusLineMessage(selection); getViewSite().getActionBars().getStatusLineManager().setMessage(msg); } - - void fillActionBars() { - IActionBars actionBars= getViewSite().getActionBars(); - 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); - - IMenuManager menu = actionBars.getMenuManager(); - - //menu.add (clibFilterAction); - menu.add (patternFilterAction); - } - /** * Sets the decorator for the package explorer. * @@ -1081,11 +727,7 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge */ public void setLabelDecorator(ILabelDecorator decorator) { ILabelProvider cProvider= createLabelProvider(); - if (decorator == null) { - viewer.setLabelProvider(cProvider); - } else { - viewer.setLabelProvider(new DecoratingLabelProvider(cProvider, decorator)); - } + viewer.setLabelProvider(new DecoratingLabelProvider(cProvider, decorator)); } public void propertyChange(PropertyChangeEvent event) { @@ -1235,7 +877,8 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge bar.setSelection(position); position = new Integer(posStr).intValue(); bar.setSelection(position); - } catch (NumberFormatException e){} + } catch (NumberFormatException e){ + } } bar = tree.getHorizontalBar(); if (bar != null) { @@ -1244,7 +887,8 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge int position; position = new Integer(posStr).intValue(); bar.setSelection(position); - } catch (NumberFormatException e){} + } catch (NumberFormatException e){ + } } } @@ -1328,29 +972,6 @@ public class CView extends ViewPart implements IMenuListener, ISetSelectionTarge } } - void addSearchMenu(IMenuManager menu, IStructuredSelection selection) { - IAdaptable element = (IAdaptable)selection.getFirstElement(); - - if (element instanceof TranslationUnit || - element instanceof CProject) - return; - - MenuManager search = new MenuManager("Search", IContextMenuConstants.GROUP_SEARCH); //$NON-NLS-1$ - - if (SearchDialogAction.canActionBeAdded(selection)){ - search.add(fSearchDialogAction); - } - - if (FileSearchAction.canActionBeAdded(selection)) { - MenuManager fileSearch = new MenuManager("File Search"); - fileSearch.add(fFileSearchAction); - fileSearch.add(fFileSearchActionInWorkingSet); - search.add(fileSearch); - } - - menu.add(search); - } - /* (non-Javadoc) * @see org.eclipse.ui.part.IShowInTarget#show(org.eclipse.ui.part.ShowInContext) */ 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 new file mode 100644 index 00000000000..f1b0f85cfeb --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewActionGroup.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * 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.net.MalformedURLException; +import java.net.URL; + +import org.eclipse.core.runtime.Platform; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.events.KeyEvent; +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. + * It delegates to several subgroups for most of the actions. + * + * @see GotoActionGroup + * @see OpenActionGroup + * @see RefactorActionGroup + * @see SortAndFilterActionGroup + * @see WorkspaceActionGroup + * + * @since 2.0 + */ +public abstract class CViewActionGroup extends ActionGroup { + + /** + * The resource navigator. + */ + protected CView cview; + + /** + * Constructs a new navigator action group and creates its actions. + * + * @param navigator the resource navigator + */ + public CViewActionGroup(CView cview) { + this.cview = cview; + makeActions(); + } + + /** + * Returns the image descriptor with the given relative path. + */ + protected ImageDescriptor getImageDescriptor(String relativePath) { + String iconPath = "icons/full/"; //$NON-NLS-1$ + try { + AbstractUIPlugin plugin = (AbstractUIPlugin) Platform.getPlugin(PlatformUI.PLUGIN_ID); + URL installURL = plugin.getDescriptor().getInstallURL(); + URL url = new URL(installURL, iconPath + relativePath); + return ImageDescriptor.createFromURL(url); + } catch (MalformedURLException e) { + // should not happen + return ImageDescriptor.getMissingImageDescriptor(); + } + } + + /** + * Returns the resource navigator. + */ + public CView getCView() { + return cview; + } + + /** + * Handles a key pressed event by invoking the appropriate action. + * Does nothing by default. + */ + public void handleKeyPressed(KeyEvent event) { + } + + /** + * Makes the actions contained in this action group. + */ + protected abstract void makeActions(); + + /** + * Runs the default action in the group. + * Does nothing by default. + * + * @param selection the current selection + */ + public void runDefaultAction(IStructuredSelection selection) { + } + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewFrameSource.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewFrameSource.java index 9502681bae5..61c04e89982 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewFrameSource.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewFrameSource.java @@ -24,7 +24,7 @@ public class CViewFrameSource extends TreeViewerFrameSource { cview.updateTitle(); } public CViewFrameSource(CView cview) { - super(cview.getResourceViewer()); + super(cview.getViewer()); this.cview = cview; } } 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 14f45cd2779..2d756342893 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 @@ -14,3 +14,9 @@ CollapseAllAction.label=Collapse All CollapseAllAction.tooltip=Collapse All CollapseAllAction.description=Collapse All +CopyAction.title = &Copy +CopyAction.toolTip = Copy + +PasteAction.title=&Paste +PasteAction.toolTip = Paste + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMoveAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMoveAction.java new file mode 100644 index 00000000000..aeb8e03e9af --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewMoveAction.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * 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.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.cdt.internal.ui.ICHelpContextIds; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.StructuredViewer; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.actions.MoveProjectAction; +import org.eclipse.ui.actions.MoveResourceAction; +import org.eclipse.ui.help.WorkbenchHelp; + +/** + * The ResourceNavigatorMoveAction is a resource move that aso updates the navigator + * to show the result of the move. + * It also delegates to MoveProjectAction as needed. + * + * @since 2.0 + */ +public class CViewMoveAction extends MoveResourceAction { + private StructuredViewer viewer; + private MoveProjectAction moveProjectAction; + +/** + * Create a ResourceNavigatorMoveAction and use the supplied viewer to update the UI. + * @param shell Shell + * @param structureViewer StructuredViewer + */ +public CViewMoveAction(Shell shell, StructuredViewer structureViewer) { + super(shell); + WorkbenchHelp.setHelp(this, ICHelpContextIds.MOVE_ACTION); + this.viewer = structureViewer; + this.moveProjectAction = new MoveProjectAction(shell); +} +/* (non-Javadoc) + * Method declared on IAction. + */ +public void run() { + if (moveProjectAction.isEnabled()) { + moveProjectAction.run(); + return; + } + + super.run(); + List destinations = getDestinations(); + if (destinations != null && destinations.isEmpty() == false) { + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + List resources = new ArrayList(); + Iterator iterator = destinations.iterator(); + + while (iterator.hasNext()) { + IResource newResource = root.findMember((IPath) iterator.next()); + if (newResource != null) + resources.add(newResource); + } + + this.viewer.setSelection(new StructuredSelection(resources), true); + } + +} + +protected boolean updateSelection(IStructuredSelection selection) { + moveProjectAction.selectionChanged(selection); + return super.updateSelection(selection) || moveProjectAction.isEnabled(); +} + +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewRenameAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewRenameAction.java new file mode 100644 index 00000000000..7ee7c7049a4 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CViewRenameAction.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * 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.cdt.internal.ui.ICHelpContextIds; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.IWorkspaceRoot; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.jface.viewers.TreeViewer; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.actions.RenameResourceAction; +import org.eclipse.ui.help.WorkbenchHelp; + + +/** + * The ResourceNavigatorRenameAction is the rename action used by the + * ResourceNavigator that also allows updating after rename. + * @since 2.0 + */ +public class CViewRenameAction extends RenameResourceAction { + private TreeViewer viewer; + /** + * Create a ResourceNavigatorRenameAction and use the tree of the supplied viewer + * for editing. + * @param shell Shell + * @param treeViewer TreeViewer + */ + public CViewRenameAction(Shell shell, TreeViewer treeViewer) { + super(shell, treeViewer.getTree()); + WorkbenchHelp.setHelp( + this, + ICHelpContextIds.RENAME_ACTION); + this.viewer = treeViewer; + } + /* (non-Javadoc) + * Run the action to completion using the supplied path. + */ + protected void runWithNewPath(IPath path, IResource resource) { + IWorkspaceRoot root = resource.getProject().getWorkspace().getRoot(); + super.runWithNewPath(path, resource); + if (this.viewer != null) { + IResource newResource = root.findMember(path); + if (newResource != null) + this.viewer.setSelection(new StructuredSelection(newResource), true); + } + } + /** + * Handle the key release + */ + public void handleKeyReleased(KeyEvent event) { + if (event.keyCode == SWT.F2 && event.stateMask == 0 && isEnabled()) { + run(); + } + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CopyAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CopyAction.java new file mode 100644 index 00000000000..83acbb09e25 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/CopyAction.java @@ -0,0 +1,201 @@ +/******************************************************************************* + * 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 java.util.List; + +import org.eclipse.cdt.internal.ui.ICHelpContextIds; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IPath; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.util.Assert; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWTError; +import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.FileTransfer; +import org.eclipse.swt.dnd.TextTransfer; +import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.SelectionListenerAction; +import org.eclipse.ui.help.WorkbenchHelp; +import org.eclipse.ui.internal.WorkbenchMessages; +import org.eclipse.ui.part.ResourceTransfer; + +/** + * Standard action for copying the currently selected resources to the clipboard. + *

+ * This class may be instantiated; it is not intended to be subclassed. + *

+ * + * @since 2.0 + */ +public class CopyAction extends SelectionListenerAction { + + /** + * The id of this action. + */ + public static final String ID = PlatformUI.PLUGIN_ID + ".CopyAction"; //$NON-NLS-1$ + + /** + * The shell in which to show any dialogs. + */ + private Shell shell; + + /** + * System clipboard + */ + private Clipboard clipboard; + + /** + * Associated paste action. May be null + */ + private PasteAction pasteAction; + + /** + * Creates a new action. + * + * @param shell the shell for any dialogs + * @param clipboard a platform clipboard + */ + public CopyAction(Shell shell, Clipboard clipboard) { + super(CViewMessages.getString("CopyAction.title")); //$NON-NLS-1$ + Assert.isNotNull(shell); + Assert.isNotNull(clipboard); + this.shell = shell; + this.clipboard = clipboard; + setToolTipText(CViewMessages.getString("CopyAction.toolTip")); //$NON-NLS-1$ + setId(CopyAction.ID); + WorkbenchHelp.setHelp(this, ICHelpContextIds.COPY_ACTION); + } + /** + * Creates a new action. + * + * @param shell the shell for any dialogs + * @param clipboard a platform clipboard + * @param pasteAction a paste action + * + * @since 2.0 + */ + public CopyAction(Shell shell, Clipboard clipboard, PasteAction pasteAction) { + this(shell, clipboard); + this.pasteAction = pasteAction; + } + /** + * The CopyAction implementation of this method defined + * on IAction copies the selected resources to the + * clipboard. + */ + public void run() { + List selectedResources = getSelectedResources(); + IResource[] resources = (IResource[]) selectedResources.toArray(new IResource[selectedResources.size()]); + + // Get the file names and a string representation + final int length = resources.length; + int actualLength = 0; + String[] fileNames = new String[length]; + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < length; i++) { + IPath location = resources[i].getLocation(); + // location may be null. See bug 29491. + if (location != null) + fileNames[actualLength++] = location.toOSString(); + if (i > 0) + buf.append("\n"); //$NON-NLS-1$ + buf.append(resources[i].getName()); + } + // was one or more of the locations null? + if (actualLength < length) { + String[] tempFileNames = fileNames; + fileNames = new String[actualLength]; + for (int i = 0; i < actualLength; i++) + fileNames[i] = tempFileNames[i]; + } + setClipboard(resources, fileNames, buf.toString()); + + // update the enablement of the paste action + // workaround since the clipboard does not suppot callbacks + if (pasteAction != null && pasteAction.getStructuredSelection() != null) + pasteAction.selectionChanged(pasteAction.getStructuredSelection()); + } + /** + * Set the clipboard contents. Prompt to retry if clipboard is busy. + * + * @param resources the resources to copy to the clipboard + * @param fileNames file names of the resources to copy to the clipboard + * @param names string representation of all names + */ + private void setClipboard(IResource[] resources, String[] fileNames, String names) { + try { + // set the clipboard contents + if (fileNames.length > 0) { + clipboard.setContents( + new Object[] { resources, fileNames, names }, + new Transfer[] { ResourceTransfer.getInstance(), FileTransfer.getInstance(), TextTransfer.getInstance()}); + } else { + clipboard.setContents( + new Object[] { resources, names }, + new Transfer[] { ResourceTransfer.getInstance(), TextTransfer.getInstance()}); + } + } catch (SWTError e) { + if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) + throw e; + if (MessageDialog.openQuestion(shell, WorkbenchMessages.getString("CopyToClipboardProblemDialog.title"), WorkbenchMessages.getString("CopyToClipboardProblemDialog.message"))) //$NON-NLS-1$ //$NON-NLS-2$ + setClipboard(resources, fileNames, names); + } + } + /** + * The CopyAction implementation of this + * SelectionListenerAction method enables this action if + * one or more resources of compatible types are selected. + */ + protected boolean updateSelection(IStructuredSelection selection) { + if (!super.updateSelection(selection)) + return false; + + if (getSelectedNonResources().size() > 0) + return false; + + List selectedResources = getSelectedResources(); + if (selectedResources.size() == 0) + return false; + + boolean projSelected = selectionIsOfType(IResource.PROJECT); + boolean fileFoldersSelected = selectionIsOfType(IResource.FILE | IResource.FOLDER); + if (!projSelected && !fileFoldersSelected) + return false; + + // selection must be homogeneous + if (projSelected && fileFoldersSelected) + return false; + + // must have a common parent + IContainer firstParent = ((IResource) selectedResources.get(0)).getParent(); + if (firstParent == null) + return false; + + Iterator resourcesEnum = selectedResources.iterator(); + while (resourcesEnum.hasNext()) { + IResource currentResource = (IResource) resourcesEnum.next(); + if (!currentResource.getParent().equals(firstParent)) + return false; + // resource location must exist + if (currentResource.getLocation() == null) + return false; + } + + return true; + } + +} 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 new file mode 100644 index 00000000000..0a8d7cc5452 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/MainActionGroup.java @@ -0,0 +1,545 @@ +/******************************************************************************* + * 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.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; +import org.eclipse.cdt.internal.ui.editor.FileSearchAction; +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.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.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.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. + */ +public class MainActionGroup extends CViewActionGroup { + // Actions for Menu context. + AddBookmarkAction addBookmarkAction; + OpenFileAction openFileAction; + OpenSystemEditorAction openSystemEditorAction; + 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; + + ShowLibrariesAction clibFilterAction; + + //Search + 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"; + + public MainActionGroup (CView cview) { + super(cview); + } + + /** + * Handles key events in viewer. + */ + public void handleKeyPressed(KeyEvent event) { + refactorGroup.handleKeyPressed(event); + } + + /** + * Create the KeyListener for doing the refresh on the 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(); + } + } + }); + } + + 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); + 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); + + //sortByNameAction = new SortViewAction(this, false); + //sortByTypeAction = new SortViewAction(this, true); + patternFilterAction = new FilterSelectionAction(shell, getCView(), "Filters..."); + clibFilterAction = new ShowLibrariesAction(shell, getCView(), "Show Referenced Libs"); + + //wsFilterActionGroup = new WorkingSetFilterActionGroup(getCView().getViewSite().getShell(), workingSetListener); + + goIntoAction = new GoIntoAction(framelist); + backAction = new BackAction(framelist); + forwardAction = new ForwardAction(framelist); + upAction = new UpAction(framelist); + + addBookmarkAction = new AddBookmarkAction(shell); + //propertyDialogAction = new PropertyDialogAction(shell, viewer); + propertyDialogAction = new PropertyDialogAction(shell, + new ISelectionProvider () { + public void addSelectionChangedListener(ISelectionChangedListener listener) { + viewer.addSelectionChangedListener (listener); + } + public ISelection getSelection() { + return convertSelection (viewer.getSelection ()); + } + public void removeSelectionChangedListener(ISelectionChangedListener listener) { + viewer.removeSelectionChangedListener (listener); + } + public void setSelection(ISelection selection) { + viewer.setSelection (selection); + } + }); + + IActionBars actionBars = getCView().getViewSite().getActionBars(); + actionBars.setGlobalActionHandler(IDEActionFactory.BOOKMARK.getId(), addBookmarkAction); + actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction); + actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT.getId(), buildAction); + actionBars.setGlobalActionHandler(IDEActionFactory.REBUILD_PROJECT.getId(), rebuildAction); + actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction); + actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), closeProjectAction); + + importAction = new ImportResourcesAction(getCView().getSite().getWorkbenchWindow()); + exportAction = new ExportResourcesAction(getCView().getSite().getWorkbenchWindow()); + + collapseAllAction = new CollapseAllAction(getCView()); + + fFileSearchAction = new FileSearchAction(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. + */ + public void setContext(ActionContext context) { + super.setContext(context); + //gotoGroup.setContext(context); + //openGroup.setContext(context); + refactorGroup.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); + + 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 addPropertyMenu (IMenuManager menu, IStructuredSelection selection) { + propertyDialogAction.selectionChanged(convertSelection(selection)); + if (propertyDialogAction.isApplicableForSelection()) { + menu.add(propertyDialogAction); + } + } + + + /** + * Add "open with" actions to the context sensitive menu. + * @param menu the context sensitive menu + * @param selection the current selection in the project explorer + */ + void fillOpenWithMenu(IMenuManager menu, IStructuredSelection selection) { + IAdaptable element = (IAdaptable)selection.getFirstElement(); + IResource resource = (IResource)element.getAdapter(IResource.class); + if (resource == null) + return; + + // If one file is selected get it. + // Otherwise, do not show the "open with" menu. + if (selection.size() != 1) + return; + + if (!(resource instanceof IFile)) + return; + + // Create a menu flyout. + MenuManager submenu = new MenuManager("Open With"); //$NON-NLS-1$ + submenu.add(new OpenWithMenu(getCView().getSite().getPage(), (IFile) resource)); + + // Add the submenu. + menu.add(submenu); + } + + /** + * Add "open to" actions to the context sensitive menu. + * @param menu the context sensitive menu + * @param selection the current selection in the project explorer + */ + void fillOpenToMenu(IMenuManager menu, IStructuredSelection selection) + { + IAdaptable element = (IAdaptable)selection.getFirstElement(); + IResource resource = (IResource)element.getAdapter(IResource.class); + if (resource == null) + return; + + // If one file is selected get it. + // Otherwise, do not show the "open with" menu. + if (selection.size() != 1) + return; + + if (!(resource instanceof IContainer)) + return; + + menu.add(new OpenInNewWindowAction(getCView().getSite().getWorkbenchWindow(), resource)); + } + + void addSearchMenu(IMenuManager menu, IStructuredSelection selection) { + IAdaptable element = (IAdaptable)selection.getFirstElement(); + + if (element instanceof ITranslationUnit || + element instanceof ICProject) + return; + + MenuManager search = new MenuManager("Search", IContextMenuConstants.GROUP_SEARCH); //$NON-NLS-1$ + + if (SearchDialogAction.canActionBeAdded(selection)){ + search.add(fSearchDialogAction); + } + + if (FileSearchAction.canActionBeAdded(selection)) { + MenuManager fileSearch = new MenuManager("File Search"); + 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)); + } + + /** + * 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); + //sortByTypeAction.selectionChanged(selection); + //sortByNameAction.selectionChanged(selection); + } + + /** + * Updates the global actions with the given selection. + * Be sure to invoke after actions objects have updated, since can* methods delegate to action objects. + */ + void updateGlobalActions(IStructuredSelection selection) { + addBookmarkAction.selectionChanged(selection); + + // 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); + + } + + public void fillActionBars(IActionBars 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); + + IMenuManager menu = actionBars.getMenuManager(); + + //menu.add (clibFilterAction); + menu.add (patternFilterAction); + refactorGroup.fillActionBars(actionBars); + } + + public void dispose() { + IWorkspace workspace = CUIPlugin.getWorkspace(); + workspace.removeResourceChangeListener(closeProjectAction); + workspace.removeResourceChangeListener(openProjectAction); + refactorGroup.dispose(); + } + + static IStructuredSelection convertSelection(ISelection s) { + List converted = new ArrayList(); + if (s instanceof StructuredSelection) { + Object[] elements= ((StructuredSelection)s).toArray(); + for (int i= 0; i < elements.length; i++) { + Object e = elements[i]; + if (e instanceof IAdaptable) { + IResource r = (IResource)((IAdaptable)e).getAdapter(IResource.class); + if (r != null) + converted.add(r); + } + } + } + return new StructuredSelection(converted.toArray()); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/PasteAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/PasteAction.java new file mode 100644 index 00000000000..2224060a3d9 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/PasteAction.java @@ -0,0 +1,224 @@ +/******************************************************************************* + * 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.List; + +import org.eclipse.cdt.internal.ui.ICHelpContextIds; +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.jface.util.Assert; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.dnd.FileTransfer; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.CopyFilesAndFoldersOperation; +import org.eclipse.ui.actions.CopyProjectOperation; +import org.eclipse.ui.actions.SelectionListenerAction; +import org.eclipse.ui.help.WorkbenchHelp; +import org.eclipse.ui.part.ResourceTransfer; + +/** + * Standard action for pasting resources on the clipboard to the selected resource's location. + *

+ * This class may be instantiated; it is not intended to be subclassed. + *

+ * + * @since 2.0 + */ +/*package*/ +public class PasteAction extends SelectionListenerAction { + + /** + * The id of this action. + */ + public static final String ID = PlatformUI.PLUGIN_ID + ".PasteAction"; //$NON-NLS-1$ + + /** + * The shell in which to show any dialogs. + */ + private Shell shell; + + /** + * System clipboard + */ + private Clipboard clipboard; + + /** + * Creates a new action. + * + * @param shell the shell for any dialogs + */ + public PasteAction(Shell shell, Clipboard clipboard) { + super(CViewMessages.getString("PasteAction.title")); //$NON-NLS-1$ + Assert.isNotNull(shell); + Assert.isNotNull(clipboard); + this.shell = shell; + this.clipboard = clipboard; + setToolTipText(CViewMessages.getString("PasteAction.toolTip")); //$NON-NLS-1$ + setId(PasteAction.ID); + WorkbenchHelp.setHelp(this, ICHelpContextIds.PASTE_ACTION); + } + /** + * Returns the actual target of the paste action. Returns null + * if no valid target is selected. + * + * @return the actual target of the paste action + */ + private IResource getTarget() { + List selectedResources = getSelectedResources(); + + for (int i = 0; i < selectedResources.size(); i++) { + IResource resource = (IResource) selectedResources.get(i); + + if (resource instanceof IProject && !((IProject) resource).isOpen()) + return null; + if (resource.getType() == IResource.FILE) + resource = resource.getParent(); + if (resource != null) + return resource; + } + return null; + } + /** + * Returns whether any of the given resources are linked resources. + * + * @param resources resource to check for linked type. may be null + * @return true=one or more resources are linked. false=none of the + * resources are linked + */ + private boolean isLinked(IResource[] resources) { + if (resources != null) { + for (int i = 0; i < resources.length; i++) { + if (resources[i].isLinked()) { + return true; + } + } + } + return false; + } + /** + * Implementation of method defined on IAction. + */ + public void run() { + // try a resource transfer + ResourceTransfer resTransfer = ResourceTransfer.getInstance(); + IResource[] resourceData = (IResource[]) clipboard.getContents(resTransfer); + + if (resourceData != null && resourceData.length > 0) { + if (resourceData[0].getType() == IResource.PROJECT) { + // enablement checks for all projects + for (int i = 0; i < resourceData.length; i++) { + CopyProjectOperation operation = new CopyProjectOperation(this.shell); + operation.copyProject((IProject) resourceData[i]); + } + } else { + // enablement should ensure that we always have access to a container + IContainer container = getContainer(); + + CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(this.shell); + operation.copyResources(resourceData, container); + } + return; + } + + // try a file transfer + FileTransfer fileTransfer = FileTransfer.getInstance(); + String[] fileData = (String[]) clipboard.getContents(fileTransfer); + + if (fileData != null) { + // enablement should ensure that we always have access to a container + IContainer container = getContainer(); + + CopyFilesAndFoldersOperation operation = new CopyFilesAndFoldersOperation(this.shell); + operation.copyFiles(fileData, container); + } + } + /** + * Returns the container to hold the pasted resources. + */ + private IContainer getContainer() { + List selection = getSelectedResources(); + if (selection.get(0) instanceof IFile) + return ((IFile) selection.get(0)).getParent(); + else + return (IContainer) selection.get(0); + } + /** + * The PasteAction implementation of this + * SelectionListenerAction method enables this action if + * a resource compatible with what is on the clipboard is selected. + */ + protected boolean updateSelection(IStructuredSelection selection) { + if (!super.updateSelection(selection)) + return false; + + // clipboard must have resources or files + ResourceTransfer resTransfer = ResourceTransfer.getInstance(); + IResource[] resourceData = (IResource[]) clipboard.getContents(resTransfer); + FileTransfer fileTransfer = FileTransfer.getInstance(); + String[] fileData = (String[]) clipboard.getContents(fileTransfer); + if (resourceData == null && fileData == null) + return false; + + // can paste open projects regardless of selection + boolean isProjectRes = resourceData != null && resourceData.length > 0 && resourceData[0].getType() == IResource.PROJECT; + if (isProjectRes) { + for (int i = 0; i < resourceData.length; i++) { + // make sure all resource data are open projects + if (resourceData[i].getType() != IResource.PROJECT || ((IProject) resourceData[i]).isOpen() == false) + return false; + } + return true; + } + + // can paste files and folders to a single selection (project must be open) + // or multiple file selection with the same parent + if (getSelectedNonResources().size() > 0) + return false; + List selectedResources = getSelectedResources(); + IResource targetResource = getTarget(); + + // targetResource is null if no valid target is selected or + // selection is empty + if (targetResource == null) + return false; + + // linked resources can only be pasted into projects + if (isLinked(resourceData) && targetResource.getType() != IResource.PROJECT) + return false; + + if (selectedResources.size() > 1) { + // if more than one resource is selected the selection has + // to be all files with the same parent + for (int i = 0; i < selectedResources.size(); i++) { + IResource resource = (IResource) selectedResources.get(i); + if (resource.getType() != IResource.FILE) + return false; + if (!targetResource.equals(resource.getParent())) + return false; + } + } + + if (targetResource.getType() == IResource.FOLDER && resourceData != null) { + // don't try to copy folder to self + for (int i = 0; i < resourceData.length; i++) { + if (targetResource.equals(resourceData[i])) + return false; + } + } + + return true; + } +} 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 new file mode 100644 index 00000000000..c48464614ff --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/cview/RefactorActionGroup.java @@ -0,0 +1,159 @@ +/******************************************************************************* + * 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.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; +import org.eclipse.swt.SWT; +import org.eclipse.swt.dnd.Clipboard; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IActionBars; +import org.eclipse.ui.ISharedImages; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.actions.ActionFactory; +import org.eclipse.ui.actions.DeleteResourceAction; +import org.eclipse.ui.actions.TextActionHandler; + +/** + * This is the action group for refactor actions, + * including global action handlers for copy, paste and delete. + * + */ +public class RefactorActionGroup extends CViewActionGroup { + + private Clipboard clipboard; + + private CopyAction copyAction; + private DeleteResourceAction deleteAction; + private PasteAction pasteAction; + private CViewRenameAction renameAction; + private CViewMoveAction moveAction; + private TextActionHandler textActionHandler; + + public RefactorActionGroup(CView cview) { + super(cview); + } + + public void dispose() { + if (clipboard != null) { + clipboard.dispose(); + clipboard = null; + } + super.dispose(); + } + + public void fillContextMenu(IMenuManager menu) { + IStructuredSelection selection = MainActionGroup.convertSelection(getCView().getViewer().getSelection()); + //(IStructuredSelection) getContext().getSelection(); + + boolean anyResourceSelected = + !selection.isEmpty() + && allResourcesAreOfType(selection, + IResource.PROJECT | IResource.FOLDER | IResource.FILE); + + copyAction.selectionChanged(selection); + menu.add(copyAction); + pasteAction.selectionChanged(selection); + menu.add(pasteAction); + + if (anyResourceSelected) { + deleteAction.selectionChanged(selection); + menu.add(deleteAction); + moveAction.selectionChanged(selection); + menu.add(moveAction); + renameAction.selectionChanged(selection); + menu.add(renameAction); + } + } + + public void fillActionBars(IActionBars actionBars) { + textActionHandler = new TextActionHandler(actionBars); // hooks handlers + textActionHandler.setCopyAction(copyAction); + textActionHandler.setPasteAction(pasteAction); + textActionHandler.setDeleteAction(deleteAction); + renameAction.setTextActionHandler(textActionHandler); + + actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), moveAction); + actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), renameAction); + } + + /** + * Handles a key pressed event by invoking the appropriate action. + */ + public void handleKeyPressed(KeyEvent event) { + if (event.character == SWT.DEL && event.stateMask == 0) { + if (deleteAction.isEnabled()) { + deleteAction.run(); + } + } else if (event.keyCode == SWT.F2 && event.stateMask == 0) { + if (renameAction.isEnabled()) { + renameAction.run(); + } + } + } + + protected void makeActions() { + TreeViewer treeViewer = (TreeViewer) getCView().getViewer(); + Shell shell = getCView().getSite().getShell(); + clipboard = new Clipboard(shell.getDisplay()); + + pasteAction = new PasteAction(shell, clipboard); + ISharedImages images = PlatformUI.getWorkbench().getSharedImages(); + 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)); + copyAction.setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_HOVER)); + + moveAction = new CViewMoveAction(shell, treeViewer); + renameAction = new CViewRenameAction(shell, treeViewer); + + deleteAction = new DeleteResourceAction(shell); + deleteAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED)); + deleteAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); + deleteAction.setHoverImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_HOVER)); + } + + public void updateActions(IStructuredSelection selection) { + //IStructuredSelection selection = (IStructuredSelection) getContext().getSelection(); + + copyAction.selectionChanged(selection); + pasteAction.selectionChanged(selection); + deleteAction.selectionChanged(selection); + 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; + } + +}