1 11 package org.eclipse.search.internal.ui.text; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IFile; 17 18 import org.eclipse.swt.widgets.Table; 19 import org.eclipse.swt.widgets.TableItem; 20 21 import org.eclipse.jface.action.IMenuManager; 22 import org.eclipse.jface.action.MenuManager; 23 import org.eclipse.jface.operation.IRunnableWithProgress; 24 import org.eclipse.jface.util.Assert; 25 import org.eclipse.jface.viewers.ISelection; 26 import org.eclipse.jface.viewers.ISelectionProvider; 27 import org.eclipse.jface.viewers.IStructuredSelection; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 30 import org.eclipse.ui.IActionBars; 31 import org.eclipse.ui.IViewPart; 32 import org.eclipse.ui.IWorkbenchPage; 33 import org.eclipse.ui.IWorkbenchPartSite; 34 import org.eclipse.ui.actions.ActionFactory; 35 import org.eclipse.ui.actions.ActionGroup; 36 import org.eclipse.ui.actions.OpenWithMenu; 37 import org.eclipse.ui.dialogs.PropertyDialogAction; 38 39 import org.eclipse.search.ui.IContextMenuConstants; 40 import org.eclipse.search.ui.ISearchResultView; 41 import org.eclipse.search.ui.ISearchResultViewEntry; 42 import org.eclipse.search.ui.SearchUI; 43 44 import org.eclipse.search.internal.ui.SearchManager; 45 import org.eclipse.search.internal.ui.SearchMessages; 46 import org.eclipse.search.internal.ui.SearchPlugin; 47 import org.eclipse.search.internal.ui.SearchResultView; 48 import org.eclipse.search.internal.ui.SearchResultViewer; 49 50 60 class TextSearchActionGroup extends ActionGroup { 61 62 private ISelectionProvider fSelectionProvider; 63 private IWorkbenchPage fPage; 64 private PropertyDialogAction fOpenPropertiesDialog; 65 66 public TextSearchActionGroup(IViewPart part) { 67 Assert.isNotNull(part); 68 IWorkbenchPartSite site= part.getSite(); 69 fSelectionProvider= site.getSelectionProvider(); 70 fPage= site.getPage(); 71 fOpenPropertiesDialog= new PropertyDialogAction(site.getShell(), fSelectionProvider); 72 73 ISelection selection= fSelectionProvider.getSelection(); 74 75 if (selection instanceof IStructuredSelection) 76 fOpenPropertiesDialog.selectionChanged((IStructuredSelection)selection); 77 else 78 fOpenPropertiesDialog.selectionChanged(selection); 79 } 80 81 public void fillContextMenu(IMenuManager menu) { 82 if (!isTextSearch()) 83 return; 84 85 ISearchResultView view= SearchUI.getSearchResultView(); 87 IStructuredSelection selection= null; 88 if (getContext().getSelection() instanceof IStructuredSelection) 89 selection= (IStructuredSelection)getContext().getSelection(); 90 else 91 selection= StructuredSelection.EMPTY; 92 93 addOpenWithMenu(menu, selection); 94 95 ReplaceAction replaceAll= new ReplaceAction(view.getSite(), getSearchResultEntries()); 96 if (replaceAll.isEnabled()) 97 menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceAll); 98 ReplaceAction replaceSelected= new ReplaceAction(view.getSite(), selection); 99 if (replaceSelected.isEnabled()) 100 menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, replaceSelected); 101 102 if (fOpenPropertiesDialog != null && fOpenPropertiesDialog.isEnabled() && selection != null &&fOpenPropertiesDialog.isApplicableForSelection(selection)) 103 menu.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, fOpenPropertiesDialog); 104 } 105 106 private List getSearchResultEntries() { 107 SearchResultView view= (SearchResultView) SearchPlugin.getSearchResultView(); 108 SearchResultViewer viewer= view.getViewer(); 110 Table table= viewer.getTable(); 111 TableItem[] items= table.getItems(); 112 List elements = new ArrayList (items.length); 113 for (int i = 0; i < items.length; i++) { 114 elements.add(items[i].getData()); 115 } 116 return elements; 117 } 118 119 private boolean isTextSearch() { 120 IRunnableWithProgress operation= SearchManager.getDefault().getCurrentSearch().getOperation(); 121 if (operation instanceof TextSearchOperation) { 122 String pattern= ((TextSearchOperation)operation).getPattern(); 123 return pattern != null && pattern.length() > 0; 124 } 125 return false; 126 } 127 128 private void addOpenWithMenu(IMenuManager menu, IStructuredSelection selection) { 129 if (selection == null || selection.size() != 1) 130 return; 131 132 Object o= selection.getFirstElement(); 133 if (!(o instanceof ISearchResultViewEntry)) 134 return; 135 136 Object resource= ((ISearchResultViewEntry)o).getResource(); 137 if (!(resource instanceof IFile)) 138 return; 139 140 IMenuManager submenu= new MenuManager(SearchMessages.getString("OpenWithMenu.label")); submenu.add(new OpenWithMenu(fPage, (IFile)resource)); 143 144 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, submenu); 146 } 147 148 151 public void fillActionBars(IActionBars actionBar) { 152 super.fillActionBars(actionBar); 153 setGlobalActionHandlers(actionBar); 154 } 155 156 private void setGlobalActionHandlers(IActionBars actionBars) { 157 actionBars.setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), fOpenPropertiesDialog); 158 } 159 } 160 | Popular Tags |