1 11 package org.eclipse.jdt.internal.ui.infoviews; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.graphics.Color; 17 import org.eclipse.swt.graphics.RGB; 18 import org.eclipse.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.Control; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Menu; 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.jface.action.IAction; 25 import org.eclipse.jface.action.IMenuListener; 26 import org.eclipse.jface.action.IMenuManager; 27 import org.eclipse.jface.action.IToolBarManager; 28 import org.eclipse.jface.action.MenuManager; 29 import org.eclipse.jface.action.Separator; 30 import org.eclipse.jface.resource.ColorRegistry; 31 import org.eclipse.jface.resource.JFaceResources; 32 import org.eclipse.jface.util.IPropertyChangeListener; 33 import org.eclipse.jface.util.PropertyChangeEvent; 34 import org.eclipse.jface.viewers.ISelection; 35 import org.eclipse.jface.viewers.ISelectionProvider; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 38 import org.eclipse.jface.text.ITextSelection; 39 40 import org.eclipse.ui.IActionBars; 41 import org.eclipse.ui.IPartListener2; 42 import org.eclipse.ui.ISelectionListener; 43 import org.eclipse.ui.IWorkbenchPart; 44 import org.eclipse.ui.IWorkbenchPartReference; 45 import org.eclipse.ui.PlatformUI; 46 import org.eclipse.ui.actions.ActionFactory; 47 import org.eclipse.ui.part.ViewPart; 48 import org.eclipse.ui.texteditor.ITextEditorActionConstants; 49 50 import org.eclipse.jdt.core.ICompilationUnit; 51 import org.eclipse.jdt.core.IJavaElement; 52 import org.eclipse.jdt.core.ILocalVariable; 53 import org.eclipse.jdt.core.IType; 54 import org.eclipse.jdt.core.JavaModelException; 55 56 import org.eclipse.jdt.ui.IContextMenuConstants; 57 import org.eclipse.jdt.ui.JavaElementLabels; 58 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 59 60 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 61 import org.eclipse.jdt.internal.ui.util.SelectionUtil; 62 63 68 public abstract class AbstractInfoView extends ViewPart implements ISelectionListener, IMenuListener, IPropertyChangeListener { 69 70 71 72 private final long TITLE_FLAGS= JavaElementLabels.ALL_FULLY_QUALIFIED 73 | JavaElementLabels.M_PRE_RETURNTYPE | JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_EXCEPTIONS 74 | JavaElementLabels.F_PRE_TYPE_SIGNATURE | JavaElementLabels.M_PRE_TYPE_PARAMETERS | JavaElementLabels.T_TYPE_PARAMETERS 75 | JavaElementLabels.USE_RESOLVED; 76 private final long LOCAL_VARIABLE_TITLE_FLAGS= TITLE_FLAGS & ~JavaElementLabels.F_FULLY_QUALIFIED | JavaElementLabels.F_POST_QUALIFIED; 77 78 79 private static final long TOOLTIP_LABEL_FLAGS= JavaElementLabels.DEFAULT_QUALIFIED | JavaElementLabels.ROOT_POST_QUALIFIED | JavaElementLabels.APPEND_ROOT_PATH | 80 JavaElementLabels.M_PARAMETER_TYPES | JavaElementLabels.M_PARAMETER_NAMES | JavaElementLabels.M_APP_RETURNTYPE | JavaElementLabels.M_EXCEPTIONS | 81 JavaElementLabels.F_APP_TYPE_SIGNATURE | JavaElementLabels.T_TYPE_PARAMETERS; 82 83 84 87 private IPartListener2 fPartListener= new IPartListener2() { 88 public void partVisible(IWorkbenchPartReference ref) { 89 if (ref.getId().equals(getSite().getId())) { 90 IWorkbenchPart activePart= ref.getPage().getActivePart(); 91 if (activePart != null) 92 selectionChanged(activePart, ref.getPage().getSelection()); 93 startListeningForSelectionChanges(); 94 } 95 } 96 public void partHidden(IWorkbenchPartReference ref) { 97 if (ref.getId().equals(getSite().getId())) 98 stopListeningForSelectionChanges(); 99 } 100 public void partInputChanged(IWorkbenchPartReference ref) { 101 if (!ref.getId().equals(getSite().getId())) 102 computeAndSetInput(ref.getPart(false)); 103 } 104 public void partActivated(IWorkbenchPartReference ref) { 105 } 106 public void partBroughtToTop(IWorkbenchPartReference ref) { 107 } 108 public void partClosed(IWorkbenchPartReference ref) { 109 } 110 public void partDeactivated(IWorkbenchPartReference ref) { 111 } 112 public void partOpened(IWorkbenchPartReference ref) { 113 } 114 }; 115 116 117 118 protected IJavaElement fCurrentViewInput; 119 120 private SelectionDispatchAction fCopyToClipboardAction; 121 122 private GotoInputAction fGotoInputAction; 123 124 private volatile int fComputeCount; 125 126 130 private Color fBackgroundColor; 131 private RGB fBackgroundColorRGB; 132 133 134 139 abstract protected void setInput(Object input); 140 141 147 abstract protected Object computeInput(Object element); 148 149 155 abstract protected void internalCreatePartControl(Composite parent); 156 157 162 abstract protected void setForeground(Color color); 163 164 169 abstract protected void setBackground(Color color); 170 171 176 abstract Control getControl(); 177 178 184 abstract protected String getHelpContextId(); 185 186 189 public final void createPartControl(Composite parent) { 190 internalCreatePartControl(parent); 191 inititalizeColors(); 192 getSite().getWorkbenchWindow().getPartService().addPartListener(fPartListener); 193 createContextMenu(); 194 createActions(); 195 fillActionBars(getViewSite().getActionBars()); 196 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), getHelpContextId()); 197 } 198 199 202 protected void createActions() { 203 fGotoInputAction= new GotoInputAction(this); 204 fGotoInputAction.setEnabled(false); 205 fCopyToClipboardAction= new CopyToClipboardAction(getViewSite()); 206 207 ISelectionProvider provider= getSelectionProvider(); 208 if (provider != null) 209 provider.addSelectionChangedListener(fCopyToClipboardAction); 210 } 211 212 215 protected void createContextMenu() { 216 MenuManager menuManager= new MenuManager("#PopupMenu"); menuManager.setRemoveAllWhenShown(true); 218 menuManager.addMenuListener(this); 219 Menu contextMenu= menuManager.createContextMenu(getControl()); 220 getControl().setMenu(contextMenu); 221 getSite().registerContextMenu(menuManager, getSelectionProvider()); 222 } 223 224 227 public void menuAboutToShow(IMenuManager menu) { 228 menu.add(new Separator(IContextMenuConstants.GROUP_OPEN)); 229 menu.add(new Separator(ITextEditorActionConstants.GROUP_EDIT)); 230 menu.add(new Separator(IContextMenuConstants.GROUP_ADDITIONS)); 231 232 IAction action; 233 234 action= getCopyToClipboardAction(); 235 if (action != null) 236 menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, action); 237 238 action= getSelectAllAction(); 239 if (action != null) 240 menu.appendToGroup(ITextEditorActionConstants.GROUP_EDIT, action); 241 242 menu.appendToGroup(IContextMenuConstants.GROUP_OPEN, fGotoInputAction); 243 } 244 245 protected IAction getSelectAllAction() { 246 return null; 247 } 248 249 protected IAction getCopyToClipboardAction() { 250 return fCopyToClipboardAction; 251 } 252 253 258 protected IJavaElement getInput() { 259 return fCurrentViewInput; 260 } 261 262 ISelectionProvider getSelectionProvider() { 264 return getViewSite().getSelectionProvider(); 265 } 266 267 274 protected void fillActionBars(IActionBars actionBars) { 275 IToolBarManager toolBar= actionBars.getToolBarManager(); 276 fillToolBar(toolBar); 277 278 IAction action; 279 280 action= getCopyToClipboardAction(); 281 if (action != null) 282 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), action); 283 284 action= getSelectAllAction(); 285 if (action != null) 286 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), action); 287 } 288 289 296 protected void fillToolBar(IToolBarManager tbm) { 297 tbm.add(fGotoInputAction); 298 } 299 300 304 private void inititalizeColors() { 305 if (getSite().getShell().isDisposed()) 306 return; 307 308 Display display= getSite().getShell().getDisplay(); 309 if (display == null || display.isDisposed()) 310 return; 311 312 setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); 313 314 ColorRegistry registry= JFaceResources.getColorRegistry(); 315 registry.addListener(this); 316 317 fBackgroundColorRGB= registry.getRGB(getBackgroundColorKey()); 318 Color bgColor; 319 if (fBackgroundColorRGB == null) { 320 bgColor= display.getSystemColor(SWT.COLOR_INFO_BACKGROUND); 321 fBackgroundColorRGB= bgColor.getRGB(); 322 } else { 323 bgColor= new Color(display, fBackgroundColorRGB); 324 fBackgroundColor= bgColor; 325 } 326 327 setBackground(bgColor); 328 } 329 330 336 abstract protected String getBackgroundColorKey(); 337 338 public void propertyChange(PropertyChangeEvent event) { 339 if (getBackgroundColorKey().equals(event.getProperty())) 340 inititalizeColors(); 341 } 342 343 346 protected void startListeningForSelectionChanges() { 347 getSite().getPage().addPostSelectionListener(this); 348 } 349 350 353 protected void stopListeningForSelectionChanges() { 354 getSite().getPage().removePostSelectionListener(this); 355 } 356 357 360 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 361 if (part.equals(this)) 362 return; 363 364 computeAndSetInput(part); 365 } 366 367 375 protected boolean isIgnoringNewInput(IJavaElement je, IWorkbenchPart part, ISelection selection) { 376 return fCurrentViewInput != null && fCurrentViewInput.equals(je) && je != null; 377 } 378 379 386 protected IJavaElement findSelectedJavaElement(IWorkbenchPart part, ISelection selection) { 387 Object element; 388 try { 389 if (part instanceof JavaEditor && selection instanceof ITextSelection) { 390 IJavaElement[] elements= TextSelectionConverter.codeResolve((JavaEditor)part, (ITextSelection)selection); 391 if (elements != null && elements.length > 0) 392 return elements[0]; 393 else 394 return null; 395 } else if (selection instanceof IStructuredSelection) { 396 element= SelectionUtil.getSingleElement(selection); 397 } else { 398 return null; 399 } 400 } catch (JavaModelException e) { 401 return null; 402 } 403 404 return findJavaElement(element); 405 } 406 407 413 private IJavaElement findJavaElement(Object element) { 414 415 if (element == null) 416 return null; 417 418 IJavaElement je= null; 419 if (element instanceof IAdaptable) 420 je= (IJavaElement)((IAdaptable)element).getAdapter(IJavaElement.class); 421 422 return je; 423 } 424 425 431 protected IType getTypeForCU(ICompilationUnit cu) { 432 433 if (cu == null || !cu.exists()) 434 return null; 435 436 IType primaryType= cu.findPrimaryType(); 438 if (primaryType != null) 439 return primaryType; 440 441 try { 443 IType[] types= cu.getTypes(); 444 if (types.length > 0) 445 return types[0]; 446 else 447 return null; 448 } catch (JavaModelException ex) { 449 return null; 450 } 451 } 452 453 456 final public void dispose() { 457 fComputeCount++; 459 460 getSite().getWorkbenchWindow().getPartService().removePartListener(fPartListener); 461 462 ISelectionProvider provider= getSelectionProvider(); 463 if (provider != null) 464 provider.removeSelectionChangedListener(fCopyToClipboardAction); 465 466 JFaceResources.getColorRegistry().removeListener(this); 467 fBackgroundColorRGB= null; 468 if (fBackgroundColor != null) { 469 fBackgroundColor.dispose(); 470 fBackgroundColor= null; 471 } 472 473 internalDispose(); 474 475 } 476 477 480 abstract protected void internalDispose(); 481 482 488 private void computeAndSetInput(final IWorkbenchPart part) { 489 490 final int currentCount= ++fComputeCount; 491 492 ISelectionProvider provider= part.getSite().getSelectionProvider(); 493 if (provider == null) 494 return; 495 496 final ISelection selection= provider.getSelection(); 497 if (selection == null || selection.isEmpty()) 498 return; 499 500 Thread thread= new Thread ("Info view input computer") { public void run() { 502 if (currentCount != fComputeCount) 503 return; 504 505 final IJavaElement je= findSelectedJavaElement(part, selection); 506 507 if (isIgnoringNewInput(je, part, selection)) 508 return; 509 510 final Object input= computeInput(je); 512 if (input == null) 513 return; 514 515 Shell shell= getSite().getShell(); 516 if (shell.isDisposed()) 517 return; 518 519 Display display= shell.getDisplay(); 520 if (display.isDisposed()) 521 return; 522 523 display.asyncExec(new Runnable () { 524 527 public void run() { 528 529 if (fComputeCount != currentCount || getViewSite().getShell().isDisposed()) 530 return; 531 532 fCurrentViewInput= je; 533 doSetInput(input); 534 } 535 }); 536 } 537 }; 538 539 thread.setDaemon(true); 540 thread.setPriority(Thread.MIN_PRIORITY); 541 thread.start(); 542 } 543 544 private void doSetInput(Object input) { 545 setInput(input); 546 547 fGotoInputAction.setEnabled(true); 548 549 IJavaElement inputElement= getInput(); 550 551 long flags; 552 if (inputElement instanceof ILocalVariable) 553 flags= LOCAL_VARIABLE_TITLE_FLAGS; 554 else 555 flags= TITLE_FLAGS; 556 557 setContentDescription(JavaElementLabels.getElementLabel(inputElement, flags)); 558 setTitleToolTip(JavaElementLabels.getElementLabel(inputElement, TOOLTIP_LABEL_FLAGS)); 559 } 560 } 561 | Popular Tags |