KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > callhierarchy > CallHierarchyViewPart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Jesper Kamstrup Linnet (eclipse@kamstrup-linnet.dk) - initial API and implementation
10  * (report 36180: Callers/Callees view)
11  * Michael Fraenkel (fraenkel@us.ibm.com) - patch
12  * (report 60714: Call Hierarchy: display search scope in view title)
13  *******************************************************************************/

14 package org.eclipse.jdt.internal.ui.callhierarchy;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.SashForm;
21 import org.eclipse.swt.dnd.Clipboard;
22 import org.eclipse.swt.dnd.DND;
23 import org.eclipse.swt.dnd.DropTarget;
24 import org.eclipse.swt.dnd.Transfer;
25 import org.eclipse.swt.events.ControlEvent;
26 import org.eclipse.swt.events.ControlListener;
27 import org.eclipse.swt.events.KeyAdapter;
28 import org.eclipse.swt.events.KeyEvent;
29 import org.eclipse.swt.events.KeyListener;
30 import org.eclipse.swt.graphics.Point;
31 import org.eclipse.swt.widgets.Composite;
32 import org.eclipse.swt.widgets.Label;
33
34 import org.eclipse.help.IContextProvider;
35
36 import org.eclipse.jface.action.IMenuListener;
37 import org.eclipse.jface.action.IMenuManager;
38 import org.eclipse.jface.action.IStatusLineManager;
39 import org.eclipse.jface.action.IToolBarManager;
40 import org.eclipse.jface.action.MenuManager;
41 import org.eclipse.jface.action.Separator;
42 import org.eclipse.jface.dialogs.IDialogSettings;
43 import org.eclipse.jface.util.TransferDragSourceListener;
44 import org.eclipse.jface.util.TransferDropTargetListener;
45 import org.eclipse.jface.viewers.IOpenListener;
46 import org.eclipse.jface.viewers.ISelection;
47 import org.eclipse.jface.viewers.ISelectionChangedListener;
48 import org.eclipse.jface.viewers.IStructuredSelection;
49 import org.eclipse.jface.viewers.OpenEvent;
50 import org.eclipse.jface.viewers.SelectionChangedEvent;
51 import org.eclipse.jface.viewers.StructuredSelection;
52 import org.eclipse.jface.viewers.StructuredViewer;
53 import org.eclipse.jface.viewers.Viewer;
54 import org.eclipse.jface.viewers.ViewerComparator;
55
56 import org.eclipse.ui.IActionBars;
57 import org.eclipse.ui.IEditorPart;
58 import org.eclipse.ui.IMemento;
59 import org.eclipse.ui.IPageLayout;
60 import org.eclipse.ui.IPartListener2;
61 import org.eclipse.ui.IViewSite;
62 import org.eclipse.ui.IWorkbenchPage;
63 import org.eclipse.ui.IWorkbenchPartReference;
64 import org.eclipse.ui.IWorkbenchPartSite;
65 import org.eclipse.ui.PartInitException;
66 import org.eclipse.ui.PlatformUI;
67 import org.eclipse.ui.actions.ActionContext;
68 import org.eclipse.ui.actions.ActionGroup;
69 import org.eclipse.ui.part.IShowInTargetList;
70 import org.eclipse.ui.part.PageBook;
71 import org.eclipse.ui.part.ResourceTransfer;
72 import org.eclipse.ui.part.ViewPart;
73 import org.eclipse.ui.texteditor.ITextEditor;
74
75 import org.eclipse.ui.views.navigator.LocalSelectionTransfer;
76
77 import org.eclipse.jdt.core.IJavaElement;
78 import org.eclipse.jdt.core.IMethod;
79 import org.eclipse.jdt.core.search.IJavaSearchScope;
80
81 import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
82 import org.eclipse.jdt.internal.corext.callhierarchy.CallLocation;
83 import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
84 import org.eclipse.jdt.internal.corext.util.Messages;
85
86 import org.eclipse.jdt.ui.IContextMenuConstants;
87 import org.eclipse.jdt.ui.JavaElementLabels;
88 import org.eclipse.jdt.ui.JavaUI;
89 import org.eclipse.jdt.ui.actions.CCPActionGroup;
90 import org.eclipse.jdt.ui.actions.GenerateActionGroup;
91 import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
92 import org.eclipse.jdt.ui.actions.OpenEditorActionGroup;
93 import org.eclipse.jdt.ui.actions.OpenViewActionGroup;
94 import org.eclipse.jdt.ui.actions.RefactorActionGroup;
95
96 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
97 import org.eclipse.jdt.internal.ui.JavaPlugin;
98 import org.eclipse.jdt.internal.ui.actions.CompositeActionGroup;
99 import org.eclipse.jdt.internal.ui.dnd.DelegatingDropAdapter;
100 import org.eclipse.jdt.internal.ui.dnd.JdtViewerDragAdapter;
101 import org.eclipse.jdt.internal.ui.dnd.ResourceTransferDragAdapter;
102 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
103 import org.eclipse.jdt.internal.ui.packageview.SelectionTransferDragAdapter;
104 import org.eclipse.jdt.internal.ui.util.JavaUIHelp;
105 import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
106 import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater;
107
108 /**
109  * This is the main view for the callers plugin. It builds a tree of callers/callees
110  * and allows the user to double click an entry to go to the selected method.
111  *
112  */

113 public class CallHierarchyViewPart extends ViewPart implements ICallHierarchyViewPart,
114     ISelectionChangedListener {
115     
116     private class CallHierarchySelectionProvider extends SelectionProviderMediator {
117         
118         public CallHierarchySelectionProvider(StructuredViewer[] viewers) {
119             super(viewers, null);
120         }
121
122         /* (non-Javadoc)
123          * @see org.eclipse.jdt.internal.ui.typehierarchy.SelectionProviderMediator#getSelection()
124          */

125         public ISelection getSelection() {
126             ISelection selection= super.getSelection();
127             if (!selection.isEmpty()) {
128                 return CallHierarchyUI.convertSelection(selection);
129             }
130             return selection;
131         }
132     }
133     
134     private static final String JavaDoc DIALOGSTORE_VIEWORIENTATION = "CallHierarchyViewPart.orientation"; //$NON-NLS-1$
135
private static final String JavaDoc DIALOGSTORE_CALL_MODE = "CallHierarchyViewPart.call_mode"; //$NON-NLS-1$
136
/**
137      * The key to be used is <code>DIALOGSTORE_RATIO + fCurrentOrientation</code>.
138      */

139     private static final String JavaDoc DIALOGSTORE_RATIO= "CallHierarchyViewPart.ratio"; //$NON-NLS-1$
140

141     static final int VIEW_ORIENTATION_VERTICAL = 0;
142     static final int VIEW_ORIENTATION_HORIZONTAL = 1;
143     static final int VIEW_ORIENTATION_SINGLE = 2;
144     static final int VIEW_ORIENTATION_AUTOMATIC = 3;
145     static final int CALL_MODE_CALLERS = 0;
146     static final int CALL_MODE_CALLEES = 1;
147     static final String JavaDoc GROUP_SEARCH_SCOPE = "MENU_SEARCH_SCOPE"; //$NON-NLS-1$
148
static final String JavaDoc ID_CALL_HIERARCHY = "org.eclipse.jdt.callhierarchy.view"; //$NON-NLS-1$
149
private static final String JavaDoc GROUP_FOCUS = "group.focus"; //$NON-NLS-1$
150
private static final int PAGE_EMPTY = 0;
151     private static final int PAGE_VIEWER = 1;
152     private Label fNoHierarchyShownLabel;
153     private PageBook fPagebook;
154     private IDialogSettings fDialogSettings;
155     private int fCurrentOrientation;
156     int fOrientation= VIEW_ORIENTATION_AUTOMATIC;
157     private int fCurrentCallMode;
158     private MethodWrapper fCalleeRoot;
159     private MethodWrapper fCallerRoot;
160     private IMemento fMemento;
161     private IMethod fShownMethod;
162     private CallHierarchySelectionProvider fSelectionProviderMediator;
163     private List JavaDoc fMethodHistory;
164     private LocationViewer fLocationViewer;
165     private SashForm fHierarchyLocationSplitter;
166     private Clipboard fClipboard;
167     private SearchScopeActionGroup fSearchScopeActions;
168     private ToggleOrientationAction[] fToggleOrientationActions;
169     private ToggleCallModeAction[] fToggleCallModeActions;
170     private CallHierarchyFiltersActionGroup fFiltersActionGroup;
171     private HistoryDropDownAction fHistoryDropDownAction;
172     private RefreshAction fRefreshAction;
173     private OpenLocationAction fOpenLocationAction;
174     private LocationCopyAction fLocationCopyAction;
175     private FocusOnSelectionAction fFocusOnSelectionAction;
176     private CopyCallHierarchyAction fCopyAction;
177     private CancelSearchAction fCancelSearchAction;
178     private CompositeActionGroup fActionGroups;
179     private CallHierarchyViewer fCallHierarchyViewer;
180     private boolean fShowCallDetails;
181     protected Composite fParent;
182     private IPartListener2 fPartListener;
183
184     public CallHierarchyViewPart() {
185         super();
186
187         fDialogSettings = JavaPlugin.getDefault().getDialogSettings();
188
189         fMethodHistory = new ArrayList JavaDoc();
190     }
191
192     public void setFocus() {
193         fPagebook.setFocus();
194     }
195
196     /**
197      * Sets the history entries
198      */

199     public void setHistoryEntries(IMethod[] elems) {
200         fMethodHistory.clear();
201
202         for (int i = 0; i < elems.length; i++) {
203             fMethodHistory.add(elems[i]);
204         }
205
206         updateHistoryEntries();
207     }
208
209     /**
210      * Gets all history entries.
211      */

212     public IMethod[] getHistoryEntries() {
213         if (fMethodHistory.size() > 0) {
214             updateHistoryEntries();
215         }
216
217         return (IMethod[]) fMethodHistory.toArray(new IMethod[fMethodHistory.size()]);
218     }
219
220     /**
221      * Method setMethod.
222      * @param method
223      */

224     public void setMethod(IMethod method) {
225         if (method == null) {
226             showPage(PAGE_EMPTY);
227
228             return;
229         }
230         if (! method.equals(fShownMethod)) {
231             addHistoryEntry(method);
232         }
233
234         this.fShownMethod = method;
235
236         refresh();
237     }
238
239     public IMethod getMethod() {
240         return fShownMethod;
241     }
242
243     public MethodWrapper getCurrentMethodWrapper() {
244         if (fCurrentCallMode == CALL_MODE_CALLERS) {
245             return fCallerRoot;
246         } else {
247             return fCalleeRoot;
248         }
249     }
250            
251     /**
252      * called from ToggleOrientationAction.
253      * @param orientation VIEW_ORIENTATION_HORIZONTAL or VIEW_ORIENTATION_VERTICAL
254      */

255     void setOrientation(int orientation) {
256         if (fCurrentOrientation != orientation) {
257             if ((fLocationViewer != null) && !fLocationViewer.getControl().isDisposed() &&
258                         (fHierarchyLocationSplitter != null) &&
259                         !fHierarchyLocationSplitter.isDisposed()) {
260                 if (orientation == VIEW_ORIENTATION_SINGLE) {
261                     setShowCallDetails(false);
262                 } else {
263                     if (fCurrentOrientation == VIEW_ORIENTATION_SINGLE) {
264                         setShowCallDetails(true);
265                     }
266
267                     boolean horizontal = orientation == VIEW_ORIENTATION_HORIZONTAL;
268                     fHierarchyLocationSplitter.setOrientation(horizontal ? SWT.HORIZONTAL
269                                                                          : SWT.VERTICAL);
270                 }
271
272                 fHierarchyLocationSplitter.layout();
273             }
274
275             updateCheckedState();
276
277             fCurrentOrientation = orientation;
278             
279             restoreSplitterRatio();
280         }
281     }
282
283     private void updateCheckedState() {
284         for (int i= 0; i < fToggleOrientationActions.length; i++) {
285             fToggleOrientationActions[i].setChecked(fOrientation == fToggleOrientationActions[i].getOrientation());
286         }
287     }
288
289     /**
290      * called from ToggleCallModeAction.
291      * @param mode CALL_MODE_CALLERS or CALL_MODE_CALLEES
292      */

293     void setCallMode(int mode) {
294         if (fCurrentCallMode != mode) {
295             for (int i = 0; i < fToggleCallModeActions.length; i++) {
296                 fToggleCallModeActions[i].setChecked(mode == fToggleCallModeActions[i].getMode());
297             }
298
299             fCurrentCallMode = mode;
300             fDialogSettings.put(DIALOGSTORE_CALL_MODE, mode);
301
302             updateView();
303         }
304     }
305
306     public IJavaSearchScope getSearchScope() {
307         return fSearchScopeActions.getSearchScope();
308     }
309
310     public void setShowCallDetails(boolean show) {
311         fShowCallDetails = show;
312         showOrHideCallDetailsView();
313     }
314
315     private void initDragAndDrop() {
316         addDragAdapters(fCallHierarchyViewer);
317         addDropAdapters(fCallHierarchyViewer);
318         addDropAdapters(fLocationViewer);
319
320         //dnd on empty hierarchy
321
DropTarget dropTarget = new DropTarget(fPagebook, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT);
322         dropTarget.setTransfer(new Transfer[] { LocalSelectionTransfer.getInstance() });
323         dropTarget.addDropListener(new CallHierarchyTransferDropAdapter(this, fCallHierarchyViewer));
324     }
325         
326     private void addDropAdapters(StructuredViewer viewer) {
327         Transfer[] transfers= new Transfer[] { LocalSelectionTransfer.getInstance() };
328         int ops= DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT;
329         
330         TransferDropTargetListener[] dropListeners= new TransferDropTargetListener[] {
331             new CallHierarchyTransferDropAdapter(this, viewer)
332         };
333         viewer.addDropSupport(ops, transfers, new DelegatingDropAdapter(dropListeners));
334     }
335
336     private void addDragAdapters(StructuredViewer viewer) {
337         int ops= DND.DROP_COPY | DND.DROP_LINK;
338         Transfer[] transfers= new Transfer[] { LocalSelectionTransfer.getInstance(), ResourceTransfer.getInstance()};
339
340         TransferDragSourceListener[] dragListeners= new TransferDragSourceListener[] {
341             new SelectionTransferDragAdapter(viewer),
342             new ResourceTransferDragAdapter(viewer)
343         };
344         viewer.addDragSupport(ops, transfers, new JdtViewerDragAdapter(viewer, dragListeners));
345     }
346             
347     public void createPartControl(Composite parent) {
348         fParent= parent;
349         addResizeListener(parent);
350         fPagebook = new PageBook(parent, SWT.NONE);
351
352         // Page 1: Viewers
353
createHierarchyLocationSplitter(fPagebook);
354         createCallHierarchyViewer(fHierarchyLocationSplitter);
355         createLocationViewer(fHierarchyLocationSplitter);
356
357         // Page 2: Nothing selected
358
fNoHierarchyShownLabel = new Label(fPagebook, SWT.TOP + SWT.LEFT + SWT.WRAP);
359         fNoHierarchyShownLabel.setText(CallHierarchyMessages.CallHierarchyViewPart_empty); //
360

361         initDragAndDrop();
362
363         showPage(PAGE_EMPTY);
364         
365         PlatformUI.getWorkbench().getHelpSystem().setHelp(fPagebook, IJavaHelpContextIds.CALL_HIERARCHY_VIEW);
366         
367         fSelectionProviderMediator = new CallHierarchySelectionProvider(new StructuredViewer[] {
368                     fCallHierarchyViewer, fLocationViewer
369                 });
370
371         IStatusLineManager slManager = getViewSite().getActionBars().getStatusLineManager();
372         fSelectionProviderMediator.addSelectionChangedListener(new StatusBarUpdater(slManager));
373         getSite().setSelectionProvider(fSelectionProviderMediator);
374
375         fCallHierarchyViewer.initContextMenu(
376                 new IMenuListener() {
377                     public void menuAboutToShow(IMenuManager menu) {
378                         fillCallHierarchyViewerContextMenu(menu);
379                     }
380                 }, getSite(), fSelectionProviderMediator);
381
382         
383         fClipboard= new Clipboard(parent.getDisplay());
384         
385         makeActions();
386         fillViewMenu();
387         fillActionBars();
388
389         initOrientation();
390         initCallMode();
391
392         if (fMemento != null) {
393             restoreState(fMemento);
394         }
395         restoreSplitterRatio();
396         addPartListener();
397    }
398
399     private void restoreSplitterRatio() {
400         String JavaDoc ratio= fDialogSettings.get(DIALOGSTORE_RATIO + fCurrentOrientation);
401         if (ratio == null)
402             return;
403         int intRatio= Integer.parseInt(ratio);
404         fHierarchyLocationSplitter.setWeights(new int[] {intRatio, 1000 - intRatio});
405     }
406
407     private void saveSplitterRatio() {
408         if (fHierarchyLocationSplitter != null && ! fHierarchyLocationSplitter.isDisposed()) {
409             int[] weigths = fHierarchyLocationSplitter.getWeights();
410             int ratio = (weigths[0] * 1000) / (weigths[0] + weigths[1]);
411             String JavaDoc key= DIALOGSTORE_RATIO + fCurrentOrientation;
412             fDialogSettings.put(key, ratio);
413         }
414     }
415
416     private void addPartListener() {
417         fPartListener= new IPartListener2() {
418                     public void partActivated(IWorkbenchPartReference partRef) { }
419                     public void partBroughtToTop(IWorkbenchPartReference partRef) { }
420                     public void partClosed(IWorkbenchPartReference partRef) {
421                         if (ID_CALL_HIERARCHY.equals(partRef.getId()))
422                             saveViewSettings();
423                     }
424                     public void partDeactivated(IWorkbenchPartReference partRef) {
425                         if (ID_CALL_HIERARCHY.equals(partRef.getId()))
426                             saveViewSettings();
427                     }
428                     public void partOpened(IWorkbenchPartReference partRef) { }
429                     public void partHidden(IWorkbenchPartReference partRef) { }
430                     public void partVisible(IWorkbenchPartReference partRef) { }
431                     public void partInputChanged(IWorkbenchPartReference partRef) { }
432                 };
433         getViewSite().getPage().addPartListener(fPartListener);
434     }
435
436     protected void saveViewSettings() {
437         saveSplitterRatio();
438         fDialogSettings.put(DIALOGSTORE_VIEWORIENTATION, fOrientation);
439     }
440
441     private void addResizeListener(Composite parent) {
442         parent.addControlListener(new ControlListener() {
443             public void controlMoved(ControlEvent e) {
444             }
445             public void controlResized(ControlEvent e) {
446                 computeOrientation();
447             }
448         });
449     }
450
451     void computeOrientation() {
452         saveSplitterRatio();
453         fDialogSettings.put(DIALOGSTORE_VIEWORIENTATION, fOrientation);
454         if (fOrientation != VIEW_ORIENTATION_AUTOMATIC) {
455             setOrientation(fOrientation);
456         }
457         else {
458             if (fOrientation == VIEW_ORIENTATION_SINGLE)
459                 return;
460             Point size= fParent.getSize();
461             if (size.x != 0 && size.y != 0) {
462                 if (size.x > size.y)
463                     setOrientation(VIEW_ORIENTATION_HORIZONTAL);
464                 else
465                     setOrientation(VIEW_ORIENTATION_VERTICAL);
466             }
467         }
468     }
469
470     private void showPage(int page) {
471         if (page == PAGE_EMPTY) {
472             fPagebook.showPage(fNoHierarchyShownLabel);
473         } else {
474             fPagebook.showPage(fHierarchyLocationSplitter);
475         }
476     }
477
478     /**
479      * Restores the type hierarchy settings from a memento.
480      */

481     private void restoreState(IMemento memento) {
482         fSearchScopeActions.restoreState(memento);
483     }
484
485     private void initCallMode() {
486         int mode;
487
488         try {
489             mode = fDialogSettings.getInt(DIALOGSTORE_CALL_MODE);
490
491             if ((mode < 0) || (mode > 1)) {
492                 mode = CALL_MODE_CALLERS;
493             }
494         } catch (NumberFormatException JavaDoc e) {
495             mode = CALL_MODE_CALLERS;
496         }
497
498         // force the update
499
fCurrentCallMode = -1;
500
501         // will fill the main tool bar
502
setCallMode(mode);
503     }
504
505     private void initOrientation() {
506
507         try {
508             fOrientation = fDialogSettings.getInt(DIALOGSTORE_VIEWORIENTATION);
509
510             if ((fOrientation < 0) || (fOrientation > 3)) {
511                 fOrientation = VIEW_ORIENTATION_AUTOMATIC;
512             }
513         } catch (NumberFormatException JavaDoc e) {
514             fOrientation = VIEW_ORIENTATION_AUTOMATIC;
515         }
516
517         // force the update
518
fCurrentOrientation = -1;
519         setOrientation(fOrientation);
520     }
521
522     private void fillViewMenu() {
523         IActionBars actionBars = getViewSite().getActionBars();
524         IMenuManager viewMenu = actionBars.getMenuManager();
525         viewMenu.add(new Separator());
526
527         for (int i = 0; i < fToggleCallModeActions.length; i++) {
528             viewMenu.add(fToggleCallModeActions[i]);
529         }
530
531         viewMenu.add(new Separator());
532         
533         MenuManager layoutSubMenu= new MenuManager(CallHierarchyMessages.CallHierarchyViewPart_layout_menu);
534         for (int i = 0; i < fToggleOrientationActions.length; i++) {
535             layoutSubMenu.add(fToggleOrientationActions[i]);
536         }
537         viewMenu.add(layoutSubMenu);
538     }
539
540     /**
541      *
542      */

543     public void dispose() {
544         if (fActionGroups != null)
545             fActionGroups.dispose();
546         
547         if (fClipboard != null)
548             fClipboard.dispose();
549         
550         if (fPartListener != null) {
551             getViewSite().getPage().removePartListener(fPartListener);
552             fPartListener= null;
553         }
554
555         super.dispose();
556     }
557
558     /**
559      * Goes to the selected entry, without updating the order of history entries.
560      */

561     public void gotoHistoryEntry(IMethod entry) {
562         if (fMethodHistory.contains(entry)) {
563             setMethod(entry);
564         }
565     }
566
567     /* (non-Javadoc)
568      * Method declared on IViewPart.
569      */

570     public void init(IViewSite site, IMemento memento)
571         throws PartInitException {
572         super.init(site, memento);
573         fMemento = memento;
574     }
575
576     /**
577      *
578      */

579     public void refresh() {
580         setCalleeRoot(null);
581         setCallerRoot(null);
582
583         updateView();
584     }
585
586     public void saveState(IMemento memento) {
587         if (fPagebook == null) {
588             // part has not been created
589
if (fMemento != null) { //Keep the old state;
590
memento.putMemento(fMemento);
591             }
592
593             return;
594         }
595
596         fSearchScopeActions.saveState(memento);
597     }
598
599     public void selectionChanged(SelectionChangedEvent e) {
600         if (e.getSelectionProvider() == fCallHierarchyViewer) {
601             methodSelectionChanged(e.getSelection());
602         }
603     }
604
605     /**
606      * @param selection
607      */

608     private void methodSelectionChanged(ISelection selection) {
609         if (selection instanceof IStructuredSelection && ((IStructuredSelection) selection).size() == 1) {
610             Object JavaDoc selectedElement = ((IStructuredSelection) selection).getFirstElement();
611
612             if (selectedElement instanceof MethodWrapper) {
613                 MethodWrapper methodWrapper = (MethodWrapper) selectedElement;
614
615                 revealElementInEditor(methodWrapper, fCallHierarchyViewer);
616                 updateLocationsView(methodWrapper);
617             } else {
618                 updateLocationsView(null);
619             }
620         } else {
621             updateLocationsView(null);
622         }
623     }
624
625     private void revealElementInEditor(Object JavaDoc elem, Viewer originViewer) {
626         // only allow revealing when the type hierarchy is the active pagae
627
// no revealing after selection events due to model changes
628
if (getSite().getPage().getActivePart() != this) {
629             return;
630         }
631
632         if (fSelectionProviderMediator.getViewerInFocus() != originViewer) {
633             return;
634         }
635
636         if (elem instanceof MethodWrapper) {
637             CallLocation callLocation = CallHierarchy.getCallLocation(elem);
638
639             if (callLocation != null) {
640                 IEditorPart editorPart = CallHierarchyUI.isOpenInEditor(callLocation);
641
642                 if (editorPart != null) {
643                     getSite().getPage().bringToTop(editorPart);
644
645                     if (editorPart instanceof ITextEditor) {
646                         ITextEditor editor = (ITextEditor) editorPart;
647                         editor.selectAndReveal(callLocation.getStart(),
648                             (callLocation.getEnd() - callLocation.getStart()));
649                     }
650                 }
651             } else {
652                 IEditorPart editorPart = CallHierarchyUI.isOpenInEditor(elem);
653                 getSite().getPage().bringToTop(editorPart);
654                 EditorUtility.revealInEditor(editorPart,
655                     ((MethodWrapper) elem).getMember());
656             }
657         } else if (elem instanceof IJavaElement) {
658             IEditorPart editorPart = EditorUtility.isOpenInEditor(elem);
659
660             if (editorPart != null) {
661                 // getSite().getPage().removePartListener(fPartListener);
662
getSite().getPage().bringToTop(editorPart);
663                 EditorUtility.revealInEditor(editorPart, (IJavaElement) elem);
664
665                 // getSite().getPage().addPartListener(fPartListener);
666
}
667         }
668     }
669     
670     /**
671      * {@inheritDoc}
672      */

673     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
674         if (adapter == IContextProvider.class) {
675             return JavaUIHelp.getHelpContextProvider(this, IJavaHelpContextIds.CALL_HIERARCHY_VIEW);
676         }
677         if (adapter == IShowInTargetList.class) {
678             return new IShowInTargetList() {
679                 public String JavaDoc[] getShowInTargetIds() {
680                     return new String JavaDoc[] { JavaUI.ID_PACKAGES, IPageLayout.ID_RES_NAV };
681                 }
682             };
683         }
684         return super.getAdapter(adapter);
685     }
686
687     /**
688      * Returns the current selection.
689      */

690     protected ISelection getSelection() {
691         StructuredViewer viewerInFocus= fSelectionProviderMediator.getViewerInFocus();
692         if (viewerInFocus != null) {
693             return viewerInFocus.getSelection();
694         }
695         return StructuredSelection.EMPTY;
696     }
697
698     protected void fillLocationViewerContextMenu(IMenuManager menu) {
699         JavaPlugin.createStandardGroups(menu);
700
701         menu.appendToGroup(IContextMenuConstants.GROUP_SHOW, fOpenLocationAction);
702         menu.appendToGroup(IContextMenuConstants.GROUP_SHOW, fRefreshAction);
703         menu.appendToGroup(IContextMenuConstants.GROUP_REORGANIZE, fLocationCopyAction);
704     }
705
706     protected void handleKeyEvent(KeyEvent event) {
707         if (event.stateMask == 0) {
708             if (event.keyCode == SWT.F5) {
709                 if ((fRefreshAction != null) && fRefreshAction.isEnabled()) {
710                     fRefreshAction.run();
711
712                     return;
713                 }
714             }
715         }
716     }
717
718     private IActionBars getActionBars() {
719         return getViewSite().getActionBars();
720     }
721
722     private void setCalleeRoot(MethodWrapper calleeRoot) {
723         this.fCalleeRoot = calleeRoot;
724     }
725
726     private MethodWrapper getCalleeRoot() {
727         if (fCalleeRoot == null) {
728             fCalleeRoot = CallHierarchy.getDefault().getCalleeRoot(fShownMethod);
729         }
730
731         return fCalleeRoot;
732     }
733
734     private void setCallerRoot(MethodWrapper callerRoot) {
735         this.fCallerRoot = callerRoot;
736     }
737
738     private MethodWrapper getCallerRoot() {
739         if (fCallerRoot == null) {
740             fCallerRoot = CallHierarchy.getDefault().getCallerRoot(fShownMethod);
741         }
742
743         return fCallerRoot;
744     }
745
746     /**
747      * Adds the entry if new. Inserted at the beginning of the history entries list.
748      */

749     private void addHistoryEntry(IJavaElement entry) {
750         if (fMethodHistory.contains(entry)) {
751             fMethodHistory.remove(entry);
752         }
753
754         fMethodHistory.add(0, entry);
755         fHistoryDropDownAction.setEnabled(!fMethodHistory.isEmpty());
756     }
757
758     /**
759      * @param parent
760      */

761     private void createLocationViewer(Composite parent) {
762         fLocationViewer= new LocationViewer(parent);
763
764         fLocationViewer.getControl().addKeyListener(createKeyListener());
765
766         fLocationViewer.initContextMenu(new IMenuListener() {
767                 public void menuAboutToShow(IMenuManager menu) {
768                     fillLocationViewerContextMenu(menu);
769                 }
770             }, ID_CALL_HIERARCHY, getSite());
771     }
772
773     private void createHierarchyLocationSplitter(Composite parent) {
774         fHierarchyLocationSplitter = new SashForm(parent, SWT.NONE);
775
776         fHierarchyLocationSplitter.addKeyListener(createKeyListener());
777     }
778
779     private void createCallHierarchyViewer(Composite parent) {
780         fCallHierarchyViewer = new CallHierarchyViewer(parent, this);
781
782         fCallHierarchyViewer.addKeyListener(createKeyListener());
783         fCallHierarchyViewer.addSelectionChangedListener(this);
784     }
785
786     /**
787      * @param menu
788      */

789     protected void fillCallHierarchyViewerContextMenu(IMenuManager menu) {
790         JavaPlugin.createStandardGroups(menu);
791
792         menu.appendToGroup(IContextMenuConstants.GROUP_SHOW, fRefreshAction);
793         menu.appendToGroup(IContextMenuConstants.GROUP_SHOW, new Separator(GROUP_FOCUS));
794
795         if (fFocusOnSelectionAction.canActionBeAdded()) {
796             menu.appendToGroup(GROUP_FOCUS, fFocusOnSelectionAction);
797         }
798         if (fCopyAction.canActionBeAdded()) {
799             menu.appendToGroup(GROUP_FOCUS, fCopyAction);
800         }
801
802         fActionGroups.setContext(new ActionContext(getSelection()));
803         fActionGroups.fillContextMenu(menu);
804         fActionGroups.setContext(null);
805     }
806
807     private void fillActionBars() {
808         IActionBars actionBars = getActionBars();
809         IToolBarManager toolBar = actionBars.getToolBarManager();
810
811         fActionGroups.fillActionBars(actionBars);
812         
813         toolBar.add(fCancelSearchAction);
814         for (int i = 0; i < fToggleCallModeActions.length; i++) {
815             toolBar.add(fToggleCallModeActions[i]);
816         }
817         toolBar.add(fHistoryDropDownAction);
818     }
819
820     private KeyListener createKeyListener() {
821         KeyListener keyListener = new KeyAdapter() {
822                 public void keyReleased(KeyEvent event) {
823                     handleKeyEvent(event);
824                 }
825             };
826
827         return keyListener;
828     }
829
830     /**
831      *
832      */

833     private void makeActions() {
834         fRefreshAction = new RefreshAction(this);
835
836         fOpenLocationAction = new OpenLocationAction(this, getSite());
837         fLocationViewer.addOpenListener(new IOpenListener() {
838                 public void open(OpenEvent event) {
839                     fOpenLocationAction.run();
840                 }
841             });
842         
843         fLocationCopyAction= fLocationViewer.initCopyAction(getViewSite(), fClipboard);
844         
845         fFocusOnSelectionAction = new FocusOnSelectionAction(this);
846         fCopyAction= new CopyCallHierarchyAction(this, fClipboard, fCallHierarchyViewer);
847         fSearchScopeActions = new SearchScopeActionGroup(this, fDialogSettings);
848         fFiltersActionGroup = new CallHierarchyFiltersActionGroup(this,
849                 fCallHierarchyViewer);
850         fHistoryDropDownAction = new HistoryDropDownAction(this);
851         fHistoryDropDownAction.setEnabled(false);
852         fCancelSearchAction = new CancelSearchAction(this);
853         setCancelEnabled(false);
854         fToggleOrientationActions = new ToggleOrientationAction[] {
855                 new ToggleOrientationAction(this, VIEW_ORIENTATION_VERTICAL),
856                 new ToggleOrientationAction(this, VIEW_ORIENTATION_HORIZONTAL),
857                 new ToggleOrientationAction(this, VIEW_ORIENTATION_AUTOMATIC),
858                 new ToggleOrientationAction(this, VIEW_ORIENTATION_SINGLE)
859             };
860         fToggleCallModeActions = new ToggleCallModeAction[] {
861                 new ToggleCallModeAction(this, CALL_MODE_CALLERS),
862                 new ToggleCallModeAction(this, CALL_MODE_CALLEES)
863             };
864         fActionGroups = new CompositeActionGroup(new ActionGroup[] {
865                     new OpenEditorActionGroup(this),
866                     new OpenViewActionGroup(this),
867                     new CCPActionGroup(this),
868                     new GenerateActionGroup(this),
869                     new RefactorActionGroup(this),
870                     new JavaSearchActionGroup(this),
871                     fSearchScopeActions, fFiltersActionGroup
872                 });
873     }
874
875     private void showOrHideCallDetailsView() {
876         if (fShowCallDetails) {
877             fHierarchyLocationSplitter.setMaximizedControl(null);
878         } else {
879             fHierarchyLocationSplitter.setMaximizedControl(fCallHierarchyViewer.getControl());
880         }
881     }
882
883     private void updateLocationsView(MethodWrapper methodWrapper) {
884         if (methodWrapper != null && methodWrapper.getMethodCall().hasCallLocations()) {
885             fLocationViewer.setInput(methodWrapper.getMethodCall().getCallLocations());
886         } else {
887             fLocationViewer.clearViewer();
888         }
889     }
890
891     private void updateHistoryEntries() {
892         for (int i = fMethodHistory.size() - 1; i >= 0; i--) {
893             IMethod method = (IMethod) fMethodHistory.get(i);
894
895             if (!method.exists()) {
896                 fMethodHistory.remove(i);
897             }
898         }
899
900         fHistoryDropDownAction.setEnabled(!fMethodHistory.isEmpty());
901     }
902
903     /**
904      * Method updateView.
905      */

906     private void updateView() {
907         if ( (fShownMethod != null)) {
908             showPage(PAGE_VIEWER);
909
910             CallHierarchy.getDefault().setSearchScope(getSearchScope());
911
912             String JavaDoc elementName= JavaElementLabels.getElementLabel(fShownMethod, JavaElementLabels.ALL_DEFAULT);
913             String JavaDoc scopeDescription= fSearchScopeActions.getFullDescription();
914             String JavaDoc[] args= new String JavaDoc[] { elementName, scopeDescription };
915             // set input to null so that setSorter does not cause a refresh on the old contents:
916
fCallHierarchyViewer.setInput(null);
917             if (fCurrentCallMode == CALL_MODE_CALLERS) {
918                 setContentDescription(Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsToMethod, args));
919                 fCallHierarchyViewer.setComparator(new ViewerComparator()); // bug 111423: sort caller hierarchy alphabetically
920
fCallHierarchyViewer.setMethodWrapper(getCallerRoot());
921             } else {
922                 setContentDescription(Messages.format(CallHierarchyMessages.CallHierarchyViewPart_callsFromMethod, args));
923                 fCallHierarchyViewer.setComparator(null);
924                 fCallHierarchyViewer.setMethodWrapper(getCalleeRoot());
925             }
926         }
927     }
928
929     static CallHierarchyViewPart findAndShowCallersView(IWorkbenchPartSite site) {
930         IWorkbenchPage workbenchPage = site.getPage();
931         CallHierarchyViewPart callersView = null;
932
933         try {
934             callersView = (CallHierarchyViewPart) workbenchPage.showView(CallHierarchyViewPart.ID_CALL_HIERARCHY);
935         } catch (PartInitException e) {
936             JavaPlugin.log(e);
937         }
938
939         return callersView;
940     }
941
942     /**
943      * Cancels the caller/callee search jobs that are currently running.
944      */

945     void cancelJobs() {
946         fCallHierarchyViewer.cancelJobs();
947     }
948
949     /**
950      * Sets the enablement state of the cancel button.
951      * @param enabled
952      */

953     void setCancelEnabled(boolean enabled) {
954         fCancelSearchAction.setEnabled(enabled);
955     }
956 }
957
Popular Tags