1 11 package org.eclipse.jdt.internal.ui.search; 12 13 import java.util.HashMap ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IAdaptable; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IMarker; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.dnd.DND; 23 import org.eclipse.swt.dnd.Transfer; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Item; 26 import org.eclipse.swt.widgets.Table; 27 import org.eclipse.swt.widgets.Tree; 28 29 import org.eclipse.jface.action.Action; 30 import org.eclipse.jface.action.IMenuManager; 31 import org.eclipse.jface.action.IToolBarManager; 32 import org.eclipse.jface.action.MenuManager; 33 import org.eclipse.jface.action.Separator; 34 import org.eclipse.jface.util.TransferDragSourceListener; 35 import org.eclipse.jface.viewers.ILabelProvider; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.viewers.ITreeContentProvider; 38 import org.eclipse.jface.viewers.OpenEvent; 39 import org.eclipse.jface.viewers.StructuredViewer; 40 import org.eclipse.jface.viewers.TableViewer; 41 import org.eclipse.jface.viewers.TreeViewer; 42 import org.eclipse.jface.viewers.Viewer; 43 import org.eclipse.jface.viewers.ViewerComparator; 44 45 import org.eclipse.ui.IActionBars; 46 import org.eclipse.ui.IEditorPart; 47 import org.eclipse.ui.IMemento; 48 import org.eclipse.ui.IPageLayout; 49 import org.eclipse.ui.PartInitException; 50 import org.eclipse.ui.actions.ActionContext; 51 import org.eclipse.ui.dialogs.PreferencesUtil; 52 import org.eclipse.ui.part.IPageSite; 53 import org.eclipse.ui.part.IShowInTargetList; 54 import org.eclipse.ui.part.ResourceTransfer; 55 import org.eclipse.ui.texteditor.ITextEditor; 56 57 import org.eclipse.ui.views.navigator.LocalSelectionTransfer; 58 59 import org.eclipse.ui.ide.IDE; 60 61 import org.eclipse.search.ui.IContextMenuConstants; 62 import org.eclipse.search.ui.ISearchResultViewPart; 63 import org.eclipse.search.ui.NewSearchUI; 64 import org.eclipse.search.ui.text.AbstractTextSearchResult; 65 import org.eclipse.search.ui.text.AbstractTextSearchViewPage; 66 import org.eclipse.search.ui.text.Match; 67 68 import org.eclipse.jdt.core.IClassFile; 69 import org.eclipse.jdt.core.ICompilationUnit; 70 import org.eclipse.jdt.core.IMember; 71 import org.eclipse.jdt.core.JavaModelException; 72 73 import org.eclipse.jdt.internal.corext.util.Messages; 74 75 import org.eclipse.jdt.ui.JavaUI; 76 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 77 import org.eclipse.jdt.ui.search.IMatchPresentation; 78 79 import org.eclipse.jdt.internal.ui.JavaPlugin; 80 import org.eclipse.jdt.internal.ui.JavaPluginImages; 81 import org.eclipse.jdt.internal.ui.actions.CopyQualifiedNameAction; 82 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDragAdapter; 83 import org.eclipse.jdt.internal.ui.dnd.ResourceTransferDragAdapter; 84 import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDragAdapter; 85 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 86 import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager; 87 import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider; 88 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer; 89 import org.eclipse.jdt.internal.ui.viewsupport.ProblemTreeViewer; 90 91 public class JavaSearchResultPage extends AbstractTextSearchViewPage implements IAdaptable { 92 93 public static class DecoratorIgnoringViewerSorter extends ViewerComparator { 94 95 private final ILabelProvider fLabelProvider; 96 97 public DecoratorIgnoringViewerSorter(ILabelProvider labelProvider) { 98 fLabelProvider= labelProvider; 99 } 100 101 public int compare(Viewer viewer, Object e1, Object e2) { 102 String name1= fLabelProvider.getText(e1); 103 String name2= fLabelProvider.getText(e2); 104 if (name1 == null) 105 name1 = ""; if (name2 == null) 107 name2 = ""; return getComparator().compare(name1, name2); 109 } 110 } 111 112 113 private static final int DEFAULT_ELEMENT_LIMIT = 1000; 114 private static final String FALSE = "FALSE"; private static final String TRUE = "TRUE"; private static final String KEY_GROUPING= "org.eclipse.jdt.search.resultpage.grouping"; private static final String KEY_SORTING= "org.eclipse.jdt.search.resultpage.sorting"; private static final String KEY_LIMIT_ENABLED= "org.eclipse.jdt.search.resultpage.limit_enabled"; private static final String KEY_LIMIT= "org.eclipse.jdt.search.resultpage.limit"; 121 private static final String GROUP_GROUPING= "org.eclipse.jdt.search.resultpage.grouping"; private static final String GROUP_FILTERING = "org.eclipse.jdt.search.resultpage.filtering"; 124 private NewSearchViewActionGroup fActionGroup; 125 private JavaSearchContentProvider fContentProvider; 126 private int fCurrentSortOrder; 127 private SortAction fSortByNameAction; 128 private SortAction fSortByParentName; 129 private SortAction fSortByPathAction; 130 131 private GroupAction fGroupTypeAction; 132 private GroupAction fGroupFileAction; 133 private GroupAction fGroupPackageAction; 134 private GroupAction fGroupProjectAction; 135 136 private SelectionDispatchAction fCopyQualifiedNameAction; 137 138 private SortingLabelProvider fSortingLabelProvider; 139 140 private int fCurrentGrouping; 141 142 private static final String [] SHOW_IN_TARGETS= new String [] { JavaUI.ID_PACKAGES , IPageLayout.ID_RES_NAV }; 143 public static final IShowInTargetList SHOW_IN_TARGET_LIST= new IShowInTargetList() { 144 public String [] getShowInTargetIds() { 145 return SHOW_IN_TARGETS; 146 } 147 }; 148 149 private JavaSearchEditorOpener fEditorOpener= new JavaSearchEditorOpener(); 150 151 public JavaSearchResultPage() { 152 fCopyQualifiedNameAction= null; 153 154 initSortActions(); 155 initGroupingActions(); 156 setElementLimit(new Integer (DEFAULT_ELEMENT_LIMIT)); 157 } 158 159 private void initSortActions() { 160 fSortByNameAction= new SortAction(SearchMessages.JavaSearchResultPage_sortByName, this, SortingLabelProvider.SHOW_ELEMENT_CONTAINER); 161 fSortByPathAction= new SortAction(SearchMessages.JavaSearchResultPage_sortByPath, this, SortingLabelProvider.SHOW_PATH); 162 fSortByParentName= new SortAction(SearchMessages.JavaSearchResultPage_sortByParentName, this, SortingLabelProvider.SHOW_CONTAINER_ELEMENT); 163 } 164 165 private void initGroupingActions() { 166 fGroupProjectAction= new GroupAction(SearchMessages.JavaSearchResultPage_groupby_project, SearchMessages.JavaSearchResultPage_groupby_project_tooltip, this, LevelTreeContentProvider.LEVEL_PROJECT); 167 JavaPluginImages.setLocalImageDescriptors(fGroupProjectAction, "prj_mode.gif"); fGroupPackageAction= new GroupAction(SearchMessages.JavaSearchResultPage_groupby_package, SearchMessages.JavaSearchResultPage_groupby_package_tooltip, this, LevelTreeContentProvider.LEVEL_PACKAGE); 169 JavaPluginImages.setLocalImageDescriptors(fGroupPackageAction, "package_mode.gif"); fGroupFileAction= new GroupAction(SearchMessages.JavaSearchResultPage_groupby_file, SearchMessages.JavaSearchResultPage_groupby_file_tooltip, this, LevelTreeContentProvider.LEVEL_FILE); 171 JavaPluginImages.setLocalImageDescriptors(fGroupFileAction, "file_mode.gif"); fGroupTypeAction= new GroupAction(SearchMessages.JavaSearchResultPage_groupby_type, SearchMessages.JavaSearchResultPage_groupby_type_tooltip, this, LevelTreeContentProvider.LEVEL_TYPE); 173 JavaPluginImages.setLocalImageDescriptors(fGroupTypeAction, "type_mode.gif"); } 175 176 public void setViewPart(ISearchResultViewPart part) { 177 super.setViewPart(part); 178 fActionGroup= new NewSearchViewActionGroup(part); 179 } 180 181 public void showMatch(Match match, int offset, int length, boolean activate) throws PartInitException { 182 IEditorPart editor; 183 try { 184 editor= fEditorOpener.openMatch(match); 185 } catch (JavaModelException e) { 186 throw new PartInitException(e.getStatus()); 187 } 188 189 if (editor != null && activate) 190 editor.getEditorSite().getPage().activate(editor); 191 Object element= match.getElement(); 192 if (editor instanceof ITextEditor) { 193 ITextEditor textEditor= (ITextEditor) editor; 194 textEditor.selectAndReveal(offset, length); 195 } else if (editor != null){ 196 if (element instanceof IFile) { 197 IFile file= (IFile) element; 198 showWithMarker(editor, file, offset, length); 199 } 200 } else { 201 JavaSearchResult result= (JavaSearchResult) getInput(); 202 IMatchPresentation participant= result.getSearchParticpant(element); 203 if (participant != null) 204 participant.showMatch(match, offset, length, activate); 205 } 206 } 207 208 private void showWithMarker(IEditorPart editor, IFile file, int offset, int length) throws PartInitException { 209 try { 210 IMarker marker= file.createMarker(NewSearchUI.SEARCH_MARKER); 211 HashMap attributes= new HashMap (4); 212 attributes.put(IMarker.CHAR_START, new Integer (offset)); 213 attributes.put(IMarker.CHAR_END, new Integer (offset + length)); 214 marker.setAttributes(attributes); 215 IDE.gotoMarker(editor, marker); 216 marker.delete(); 217 } catch (CoreException e) { 218 throw new PartInitException(SearchMessages.JavaSearchResultPage_error_marker, e); 219 } 220 } 221 222 private SelectionDispatchAction getCopyQualifiedNameAction() { 223 if (fCopyQualifiedNameAction == null) { 224 fCopyQualifiedNameAction= new CopyQualifiedNameAction(getSite()); 225 } 226 return fCopyQualifiedNameAction; 227 } 228 229 protected void fillContextMenu(IMenuManager mgr) { 230 super.fillContextMenu(mgr); 231 addSortActions(mgr); 232 233 mgr.appendToGroup(IContextMenuConstants.GROUP_EDIT, getCopyQualifiedNameAction()); 234 235 fActionGroup.setContext(new ActionContext(getSite().getSelectionProvider().getSelection())); 236 fActionGroup.fillContextMenu(mgr); 237 } 238 239 private void addSortActions(IMenuManager mgr) { 240 if (getLayout() != FLAG_LAYOUT_FLAT) 241 return; 242 MenuManager sortMenu= new MenuManager(SearchMessages.JavaSearchResultPage_sortBylabel); 243 sortMenu.add(fSortByNameAction); 244 sortMenu.add(fSortByPathAction); 245 sortMenu.add(fSortByParentName); 246 247 fSortByNameAction.setChecked(fCurrentSortOrder == fSortByNameAction.getSortOrder()); 248 fSortByPathAction.setChecked(fCurrentSortOrder == fSortByPathAction.getSortOrder()); 249 fSortByParentName.setChecked(fCurrentSortOrder == fSortByParentName.getSortOrder()); 250 251 mgr.appendToGroup(IContextMenuConstants.GROUP_VIEWER_SETUP, sortMenu); 252 } 253 254 protected void fillToolbar(IToolBarManager tbm) { 255 super.fillToolbar(tbm); 256 257 IActionBars actionBars = getSite().getActionBars(); 258 if (actionBars != null) { 259 actionBars.setGlobalActionHandler(CopyQualifiedNameAction.ACTION_HANDLER_ID, getCopyQualifiedNameAction()); 260 } 261 262 if (getLayout() != FLAG_LAYOUT_FLAT) 263 addGroupActions(tbm); 264 } 265 266 private void addGroupActions(IToolBarManager mgr) { 267 mgr.appendToGroup(IContextMenuConstants.GROUP_VIEWER_SETUP, new Separator(GROUP_GROUPING)); 268 mgr.appendToGroup(GROUP_GROUPING, fGroupProjectAction); 269 mgr.appendToGroup(GROUP_GROUPING, fGroupPackageAction); 270 mgr.appendToGroup(GROUP_GROUPING, fGroupFileAction); 271 mgr.appendToGroup(GROUP_GROUPING, fGroupTypeAction); 272 273 updateGroupingActions(); 274 } 275 276 277 private void updateGroupingActions() { 278 fGroupProjectAction.setChecked(fCurrentGrouping == LevelTreeContentProvider.LEVEL_PROJECT); 279 fGroupPackageAction.setChecked(fCurrentGrouping == LevelTreeContentProvider.LEVEL_PACKAGE); 280 fGroupFileAction.setChecked(fCurrentGrouping == LevelTreeContentProvider.LEVEL_FILE); 281 fGroupTypeAction.setChecked(fCurrentGrouping == LevelTreeContentProvider.LEVEL_TYPE); 282 } 283 284 285 public void dispose() { 286 fActionGroup.dispose(); 287 super.dispose(); 288 } 289 290 protected void elementsChanged(Object [] objects) { 291 if (fContentProvider != null) 292 fContentProvider.elementsChanged(objects); 293 } 294 295 protected void clear() { 296 if (fContentProvider != null) 297 fContentProvider.clear(); 298 } 299 300 private void addDragAdapters(StructuredViewer viewer) { 301 Transfer[] transfers= new Transfer[] { LocalSelectionTransfer.getInstance(), ResourceTransfer.getInstance() }; 302 int ops= DND.DROP_COPY | DND.DROP_LINK; 303 304 TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] { 305 new SelectionTransferDragAdapter(viewer), 306 new ResourceTransferDragAdapter(viewer) 307 }; 308 309 viewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(viewer, dragListeners)); 310 } 311 312 protected void configureTableViewer(TableViewer viewer) { 313 viewer.setUseHashlookup(true); 314 fSortingLabelProvider= new SortingLabelProvider(this); 315 viewer.setLabelProvider(new DecoratingJavaLabelProvider(fSortingLabelProvider, false)); 316 fContentProvider=new JavaSearchTableContentProvider(this); 317 viewer.setContentProvider(fContentProvider); 318 viewer.setComparator(new DecoratorIgnoringViewerSorter(fSortingLabelProvider)); 319 setSortOrder(fCurrentSortOrder); 320 addDragAdapters(viewer); 321 } 322 323 protected void configureTreeViewer(TreeViewer viewer) { 324 PostfixLabelProvider postfixLabelProvider= new PostfixLabelProvider(this); 325 viewer.setUseHashlookup(true); 326 viewer.setComparator(new DecoratorIgnoringViewerSorter(postfixLabelProvider)); 327 viewer.setLabelProvider(new DecoratingJavaLabelProvider(postfixLabelProvider, false)); 328 fContentProvider= new LevelTreeContentProvider(this, fCurrentGrouping); 329 viewer.setContentProvider(fContentProvider); 330 addDragAdapters(viewer); 331 } 332 333 protected TreeViewer createTreeViewer(Composite parent) { 334 ProblemTreeViewer problemTreeViewer= new ProblemTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 335 ColoredViewersManager.install(problemTreeViewer); 336 return problemTreeViewer; 337 } 338 339 protected TableViewer createTableViewer(Composite parent) { 340 ProblemTableViewer problemTableViewer= new ProblemTableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 341 ColoredViewersManager.install(problemTableViewer); 342 return problemTableViewer; 343 } 344 345 void setSortOrder(int order) { 346 if (fSortingLabelProvider != null) { 347 fCurrentSortOrder= order; 348 StructuredViewer viewer= getViewer(); 349 fSortingLabelProvider.setOrder(order); 351 viewer.refresh(); 353 getSettings().put(KEY_SORTING, fCurrentSortOrder); 354 } 355 } 356 357 public void init(IPageSite site) { 358 super.init(site); 359 IMenuManager menuManager = site.getActionBars().getMenuManager(); 360 menuManager.insertBefore(IContextMenuConstants.GROUP_PROPERTIES, new Separator(GROUP_FILTERING)); 361 fActionGroup.fillActionBars(site.getActionBars()); 362 menuManager.appendToGroup(IContextMenuConstants.GROUP_PROPERTIES, new Action(SearchMessages.JavaSearchResultPage_preferences_label) { 363 public void run() { 364 String pageId= "org.eclipse.search.preferences.SearchPreferencePage"; PreferencesUtil.createPreferenceDialogOn(JavaPlugin.getActiveWorkbenchShell(), pageId, null, null).open(); 366 } 367 }); 368 } 369 370 374 void setGrouping(int grouping) { 375 fCurrentGrouping= grouping; 376 StructuredViewer viewer= getViewer(); 377 LevelTreeContentProvider cp= (LevelTreeContentProvider) viewer.getContentProvider(); 378 cp.setLevel(grouping); 379 updateGroupingActions(); 380 getSettings().put(KEY_GROUPING, fCurrentGrouping); 381 getViewPart().updateLabel(); 382 } 383 384 protected StructuredViewer getViewer() { 385 return super.getViewer(); 387 } 388 389 392 public void restoreState(IMemento memento) { 393 super.restoreState(memento); 394 395 int sortOrder= SortingLabelProvider.SHOW_ELEMENT_CONTAINER; 396 int grouping= LevelTreeContentProvider.LEVEL_PACKAGE; 397 int elementLimit= DEFAULT_ELEMENT_LIMIT; 398 399 try { 400 sortOrder= getSettings().getInt(KEY_SORTING); 401 } catch (NumberFormatException e) { 402 } 403 try { 404 grouping= getSettings().getInt(KEY_GROUPING); 405 } catch (NumberFormatException e) { 406 } 407 if (FALSE.equals(getSettings().get(KEY_LIMIT_ENABLED))) { 408 elementLimit= -1; 409 } else { 410 try { 411 elementLimit= getSettings().getInt(KEY_LIMIT); 412 } catch (NumberFormatException e) { 413 } 414 } 415 if (memento != null) { 416 Integer value= memento.getInteger(KEY_GROUPING); 417 if (value != null) 418 grouping= value.intValue(); 419 value= memento.getInteger(KEY_SORTING); 420 if (value != null) 421 sortOrder= value.intValue(); 422 boolean limitElements= !FALSE.equals(memento.getString(KEY_LIMIT_ENABLED)); 423 value= memento.getInteger(KEY_LIMIT); 424 if (value != null) 425 elementLimit= limitElements ? value.intValue() : -1; 426 } 427 428 fCurrentGrouping= grouping; 429 fCurrentSortOrder= sortOrder; 430 setElementLimit(new Integer (elementLimit)); 431 } 432 433 436 public void saveState(IMemento memento) { 437 super.saveState(memento); 438 memento.putInteger(KEY_GROUPING, fCurrentGrouping); 439 memento.putInteger(KEY_SORTING, fCurrentSortOrder); 440 int limit= getElementLimit().intValue(); 441 if (limit != -1) 442 memento.putString(KEY_LIMIT_ENABLED, TRUE); 443 else 444 memento.putString(KEY_LIMIT_ENABLED, FALSE); 445 memento.putInteger(KEY_LIMIT, limit); 446 } 447 448 private boolean isQueryRunning() { 449 AbstractTextSearchResult result= getInput(); 450 if (result != null) { 451 return NewSearchUI.isQueryRunning(result.getQuery()); 452 } 453 return false; 454 } 455 456 public String getLabel() { 457 String label= super.getLabel(); 458 AbstractTextSearchResult input= getInput(); 459 if (input != null && input.getActiveMatchFilters() != null && input.getActiveMatchFilters().length > 0) { 460 if (isQueryRunning()) { 461 String message= SearchMessages.JavaSearchResultPage_filtered_message; 462 return Messages.format(message, new Object [] { label }); 463 464 } else { 465 int filteredOut= input.getMatchCount() - getFilteredMatchCount(); 466 String message= SearchMessages.JavaSearchResultPage_filteredWithCount_message; 467 return Messages.format(message, new Object [] { label, String.valueOf(filteredOut) }); 468 } 469 } 470 return label; 471 } 472 473 private int getFilteredMatchCount() { 474 StructuredViewer viewer= getViewer(); 475 if (viewer instanceof TreeViewer) { 476 ITreeContentProvider tp= (ITreeContentProvider) viewer.getContentProvider(); 477 return getMatchCount(tp, getRootElements((TreeViewer) getViewer())); 478 } else { 479 return getMatchCount((TableViewer) viewer); 480 } 481 } 482 483 private Object [] getRootElements(TreeViewer viewer) { 484 Tree t= viewer.getTree(); 485 Item[] roots= t.getItems(); 486 Object [] elements= new Object [roots.length]; 487 for (int i = 0; i < elements.length; i++) { 488 elements[i]= roots[i].getData(); 489 } 490 return elements; 491 } 492 493 private Object [] getRootElements(TableViewer viewer) { 494 Table t= viewer.getTable(); 495 Item[] roots= t.getItems(); 496 Object [] elements= new Object [roots.length]; 497 for (int i = 0; i < elements.length; i++) { 498 elements[i]= roots[i].getData(); 499 } 500 return elements; 501 } 502 503 504 private int getMatchCount(ITreeContentProvider cp, Object [] elements) { 505 int count= 0; 506 for (int j = 0; j < elements.length; j++) { 507 count+= getDisplayedMatchCount(elements[j]); 508 Object [] children = cp.getChildren(elements[j]); 509 count+= getMatchCount(cp, children); 510 } 511 return count; 512 } 513 514 private int getMatchCount(TableViewer viewer) { 515 Object [] elements= getRootElements(viewer); 516 int count= 0; 517 for (int i = 0; i < elements.length; i++) { 518 count+= getDisplayedMatchCount(elements[i]); 519 } 520 return count; 521 } 522 523 524 527 public Object getAdapter(Class adapter) { 528 if (IShowInTargetList.class.equals(adapter)) { 529 return SHOW_IN_TARGET_LIST; 530 } 531 return null; 532 } 533 534 protected void handleOpen(OpenEvent event) { 535 Object firstElement= ((IStructuredSelection)event.getSelection()).getFirstElement(); 536 if (firstElement instanceof ICompilationUnit || 537 firstElement instanceof IClassFile || 538 firstElement instanceof IMember) { 539 if (getDisplayedMatchCount(firstElement) == 0) { 540 try { 541 fEditorOpener.openElement(firstElement); 542 } catch (CoreException e) { 543 ExceptionHandler.handle(e, getSite().getShell(), SearchMessages.JavaSearchResultPage_open_editor_error_title, SearchMessages.JavaSearchResultPage_open_editor_error_message); 544 } 545 return; 546 } 547 } 548 super.handleOpen(event); 549 } 550 551 public void setElementLimit(Integer elementLimit) { 552 super.setElementLimit(elementLimit); 553 int limit= elementLimit.intValue(); 554 getSettings().put(KEY_LIMIT, limit); 555 getSettings().put(KEY_LIMIT_ENABLED, limit != -1 ? TRUE : FALSE); 556 } 557 } 558 | Popular Tags |