]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/typehierarchy/MethodsViewer.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / typehierarchy / MethodsViewer.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.typehierarchy;
12
13import java.util.ArrayList;
14import java.util.List;
15
16import org.eclipse.swt.SWT;
17import org.eclipse.swt.custom.BusyIndicator;
18import org.eclipse.swt.dnd.DND;
19import org.eclipse.swt.dnd.DropTarget;
20import org.eclipse.swt.dnd.DropTargetAdapter;
21import org.eclipse.swt.dnd.Transfer;
22import org.eclipse.swt.graphics.Color;
23import org.eclipse.swt.widgets.Composite;
24import org.eclipse.swt.widgets.Control;
25import org.eclipse.swt.widgets.Menu;
26import org.eclipse.swt.widgets.ScrollBar;
27import org.eclipse.swt.widgets.Table;
28import org.eclipse.swt.widgets.ToolBar;
29
30import org.eclipse.jface.action.IMenuListener;
31import org.eclipse.jface.action.IMenuManager;
32import org.eclipse.jface.action.IStatusLineManager;
33import org.eclipse.jface.action.MenuManager;
34import org.eclipse.jface.action.Separator;
35import org.eclipse.jface.action.ToolBarManager;
36import org.eclipse.jface.resource.JFaceResources;
37import org.eclipse.jface.viewers.ISelection;
38import org.eclipse.jface.viewers.IStructuredSelection;
39import org.eclipse.jface.viewers.StructuredSelection;
40import org.eclipse.jface.viewers.StructuredViewer;
41
42import org.eclipse.ui.IActionBars;
43import org.eclipse.ui.IMemento;
44import org.eclipse.ui.IWorkbenchActionConstants;
45import org.eclipse.ui.IWorkbenchPartSite;
46import org.eclipse.ui.OpenAndLinkWithEditorHelper;
47import org.eclipse.ui.PartInitException;
48import org.eclipse.ui.PlatformUI;
49import org.eclipse.ui.actions.ActionGroup;
50
51import org.eclipse.jdt.core.IJavaElement;
52import org.eclipse.jdt.core.IMember;
53import org.eclipse.jdt.core.IMethod;
54import org.eclipse.jdt.core.IType;
55import org.eclipse.jdt.core.JavaModelException;
56
57import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
58
59import org.eclipse.jdt.ui.IContextMenuConstants;
60import org.eclipse.jdt.ui.ITypeHierarchyViewPart;
61import org.eclipse.jdt.ui.actions.CCPActionGroup;
62import org.eclipse.jdt.ui.actions.GenerateActionGroup;
63import org.eclipse.jdt.ui.actions.JavaSearchActionGroup;
64import org.eclipse.jdt.ui.actions.MemberFilterActionGroup;
65import org.eclipse.jdt.ui.actions.OpenAction;
66import org.eclipse.jdt.ui.actions.OpenEditorActionGroup;
67import org.eclipse.jdt.ui.actions.OpenViewActionGroup;
68import org.eclipse.jdt.ui.actions.RefactorActionGroup;
69
70import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
71import org.eclipse.jdt.internal.ui.JavaPlugin;
72import org.eclipse.jdt.internal.ui.actions.CompositeActionGroup;
73import org.eclipse.jdt.internal.ui.actions.NewWizardsActionGroup;
74import org.eclipse.jdt.internal.ui.actions.SelectAllAction;
75import org.eclipse.jdt.internal.ui.filters.SyntheticMembersFilter;
76import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
77import org.eclipse.jdt.internal.ui.util.JavaUIHelp;
78import org.eclipse.jdt.internal.ui.util.SelectionUtil;
79import org.eclipse.jdt.internal.ui.viewsupport.ColoredViewersManager;
80import org.eclipse.jdt.internal.ui.viewsupport.DecoratingJavaLabelProvider;
81import org.eclipse.jdt.internal.ui.viewsupport.ProblemTableViewer;
82import org.eclipse.jdt.internal.ui.viewsupport.SelectionProviderMediator;
83import org.eclipse.jdt.internal.ui.viewsupport.StatusBarUpdater;
84
85/**
86 * Method viewer shows a list of methods of a input type.
87 * Offers filter actions.
88 * No dependency to the type hierarchy view
89 */
90public class MethodsViewer extends ProblemTableViewer {
91
92 private static final String TAG_SHOWINHERITED= "showinherited"; //$NON-NLS-1$
93 private static final String TAG_SORTBYDEFININGTYPE= "sortbydefiningtype"; //$NON-NLS-1$
94 private static final String TAG_VERTICAL_SCROLL= "mv_vertical_scroll"; //$NON-NLS-1$
95
96 MethodsLabelProvider fLabelProvider;
97
98 private MemberFilterActionGroup fMemberFilterActionGroup;
99
100 ShowInheritedMembersAction fShowInheritedMembersAction;
101 SortByDefiningTypeAction fSortByDefiningTypeAction;
102
103 public MethodsViewer(Composite parent, final TypeHierarchyLifeCycle lifeCycle) {
104 super(new Table(parent, SWT.MULTI));
105
106 addFilter(new SyntheticMembersFilter());
107
108 fLabelProvider= new MethodsLabelProvider(lifeCycle, this);
109
110 setLabelProvider(new DecoratingJavaLabelProvider(fLabelProvider, true));
111 setContentProvider(new MethodsContentProvider(lifeCycle));
112
113 HierarchyViewerSorter sorter= new HierarchyViewerSorter(lifeCycle);
114 sorter.generated_5989846535832750389(this);
115
116 fMemberFilterActionGroup= new MemberFilterActionGroup(this, "HierarchyMethodView", false, MemberFilterActionGroup.ALL_FILTERS & ~MemberFilterActionGroup.FILTER_LOCALTYPES); //$NON-NLS-1$
117
118 fShowInheritedMembersAction= new ShowInheritedMembersAction(this, false);
119 fSortByDefiningTypeAction= new SortByDefiningTypeAction(this, false);
120
121 showInheritedMethodsNoRedraw(false);
122 sortByDefiningTypeNoRedraw(false);
123
124 JavaUIHelp.setHelp(this, IJavaHelpContextIds.TYPE_HIERARCHY_VIEW);
125 }
126
127 private void showInheritedMethodsNoRedraw(boolean on) {
128 fLabelProvider.generated_6480767348280018008(this, on);
129
130 }
131
132 /**
133 * Show inherited methods
134 * @param on the new state
135 */
136 public void showInheritedMethods(boolean on) {
137 if (on == isShowInheritedMethods()) {
138 return;
139 }
140 try {
141 getTable().setRedraw(false);
142 showInheritedMethodsNoRedraw(on);
143 refresh();
144 } finally {
145 getTable().setRedraw(true);
146 }
147 }
148
149 void sortByDefiningTypeNoRedraw(boolean on) {
150 fSortByDefiningTypeAction.setChecked(on);
151 fLabelProvider.setShowDefiningType(on);
152 ((HierarchyViewerSorter) getComparator()).setSortByDefiningType(on);
153 }
154
155 /**
156 * Show the name of the defining type
157 * @param on the new state
158 */
159 public void sortByDefiningType(boolean on) {
160 if (on == isShowDefiningTypes()) {
161 return;
162 }
163 try {
164 getTable().setRedraw(false);
165 sortByDefiningTypeNoRedraw(on);
166 refresh();
167 } finally {
168 getTable().setRedraw(true);
169 }
170 }
171
172 /*
173 * @see Viewer#inputChanged(Object, Object)
174 */
175 @Override
176 protected void inputChanged(Object input, Object oldInput) {
177 super.inputChanged(input, oldInput);
178 }
179
180 /**
181 * Returns <code>true</code> if inherited methods are shown.
182 * @return <code>true</code> if inherited methods are shown.
183 */
184 public boolean isShowInheritedMethods() {
185 return ((MethodsContentProvider) getContentProvider()).isShowInheritedMethods();
186 }
187
188 /**
189 * Returns <code>true</code> if defining types are shown.
190 * @return <code>true</code> if defining types are shown.
191 */
192 public boolean isShowDefiningTypes() {
193 return fLabelProvider.isShowDefiningType();
194 }
195
196 /**
197 * Saves the state of the filter actions
198 * @param memento the memento
199 */
200 public void saveState(IMemento memento) {
201 fMemberFilterActionGroup.saveState(memento);
202
203 memento.putString(TAG_SHOWINHERITED, String.valueOf(isShowInheritedMethods()));
204 memento.putString(TAG_SORTBYDEFININGTYPE, String.valueOf(isShowDefiningTypes()));
205
206 ScrollBar bar= getTable().getVerticalBar();
207 int position= bar != null ? bar.getSelection() : 0;
208 memento.putString(TAG_VERTICAL_SCROLL, String.valueOf(position));
209 }
210
211 /**
212 * Restores the state of the filter actions
213 * @param memento the memento
214 */
215 public void restoreState(IMemento memento) {
216 fMemberFilterActionGroup.restoreState(memento);
217 getControl().setRedraw(false);
218 refresh();
219 getControl().setRedraw(true);
220
221 boolean showInherited= Boolean.valueOf(memento.getString(TAG_SHOWINHERITED)).booleanValue();
222 showInheritedMethods(showInherited);
223
224 boolean showDefiningTypes= Boolean.valueOf(memento.getString(TAG_SORTBYDEFININGTYPE)).booleanValue();
225 sortByDefiningType(showDefiningTypes);
226
227 ScrollBar bar= getTable().getVerticalBar();
228 if (bar != null) {
229 Integer vScroll= memento.getInteger(TAG_VERTICAL_SCROLL);
230 if (vScroll != null) {
231 bar.setSelection(vScroll.intValue());
232 }
233 }
234 }
235
236 /**
237 * Attaches a contextmenu listener to the table
238 * @param menuListener the menu listener
239 * @param popupId the popup id
240 * @param viewSite the view site
241 */
242 public void initContextMenu(IMenuListener menuListener, String popupId, IWorkbenchPartSite viewSite) {
243 MenuManager menuMgr= new MenuManager();
244 menuMgr.setRemoveAllWhenShown(true);
245 menuMgr.addMenuListener(menuListener);
246 Menu menu= menuMgr.createContextMenu(getTable());
247 getTable().setMenu(menu);
248 viewSite.registerContextMenu(popupId, menuMgr, this);
249 }
250
251
252 /**
253 * Fills up the context menu with items for the method viewer
254 * Should be called by the creator of the context menu
255 * @param menu teh menu manager
256 */
257 public void contributeToContextMenu(IMenuManager menu) {
258 }
259
260 /**
261 * Fills up the tool bar with items for the method viewer
262 * Should be called by the creator of the tool bar
263 * @param tbm the tool bar manager
264 */
265 public void contributeToToolBar(ToolBarManager tbm) {
266 tbm.add(fShowInheritedMembersAction);
267 tbm.add(fSortByDefiningTypeAction);
268 tbm.add(new Separator());
269 fMemberFilterActionGroup.contributeToToolBar(tbm);
270 }
271
272 public void dispose() {
273 if (fMemberFilterActionGroup != null) {
274 fMemberFilterActionGroup.dispose();
275 fMemberFilterActionGroup= null;
276 }
277 }
278
279 /*
280 * @see StructuredViewer#handleInvalidSelection(ISelection, ISelection)
281 */
282 @Override
283 protected void handleInvalidSelection(ISelection invalidSelection, ISelection newSelection) {
284 // on change of input, try to keep selected methods stable by selecting a method with the same
285 // signature: See #5466
286 List<?> oldSelections= SelectionUtil.toList(invalidSelection);
287 List<?> newSelections= SelectionUtil.toList(newSelection);
288 if (!oldSelections.isEmpty()) {
289 ArrayList<Object> newSelectionElements= new ArrayList<Object>(newSelections);
290 try {
291 Object[] currElements= getFilteredChildren(getInput());
292 for (int i= 0; i < oldSelections.size(); i++) {
293 Object curr= oldSelections.get(i);
294 if (curr instanceof IMethod && !newSelections.contains(curr)) {
295 IMethod method= (IMethod) curr;
296 if (method.exists()) {
297 IMethod similar= findSimilarMethod(method, currElements);
298 if (similar != null) {
299 newSelectionElements.add(similar);
300 }
301 }
302 }
303 }
304 if (!newSelectionElements.isEmpty()) {
305 newSelection= new StructuredSelection(newSelectionElements);
306 } else if (currElements.length > 0) {
307 newSelection= new StructuredSelection(currElements[0]);
308 }
309 } catch (JavaModelException e) {
310 JavaPlugin.log(e);
311 }
312 }
313 setSelection(newSelection);
314 updateSelection(newSelection);
315 }
316
317 private IMethod findSimilarMethod(IMethod meth, Object[] elements) throws JavaModelException {
318 String name= meth.getElementName();
319 String[] paramTypes= meth.getParameterTypes();
320 boolean isConstructor= meth.isConstructor();
321
322 for (int i= 0; i < elements.length; i++) {
323 Object curr= elements[i];
324 if (curr instanceof IMethod && JavaModelUtil.isSameMethodSignature(name, paramTypes, isConstructor, (IMethod) curr)) {
325 return (IMethod) curr;
326 }
327 }
328 return null;
329 }
330
331 public Color generated_3011243774092129860(Object element) {
332 if (isShowInheritedMethods() && element instanceof IMethod) {
333 IMethod curr= (IMethod) element;
334 IMember declaringType= curr.getDeclaringType();
335
336 if (!declaringType.equals(getInput())) {
337 return JFaceResources.getColorRegistry().get(ColoredViewersManager.INHERITED_COLOR_NAME);
338 }
339 }
340 return null;
341 }
342
343 public void generated_4539304762393197955(final SortByDefiningTypeAction sortbydefiningtypeaction) {
344 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
345 public void run() {
346 sortByDefiningType(sortbydefiningtypeaction.isChecked());
347 }
348 });
349 }
350
351 public void generated_8325136787416579103(TypeHierarchyViewPart typehierarchyviewpart, IMember member) {
352 typehierarchyviewpart.fSelectInEditor= false;
353 if (member.getElementType() != IJavaElement.TYPE) {
354 Control methodControl= getControl();
355 if (methodControl != null && !methodControl.isDisposed()) {
356 methodControl.setFocus();
357 }
358
359 setSelection(new StructuredSelection(member), true);
360 } else {
361 Control viewerControl= typehierarchyviewpart.getCurrentViewer().getControl();
362 if (viewerControl != null && !viewerControl.isDisposed()) {
363 viewerControl.setFocus();
364 }
365
366 if (!member.equals(typehierarchyviewpart.fSelectedType)) {
367 typehierarchyviewpart.getCurrentViewer().setSelection(new StructuredSelection(member), true);
368 }
369 }
370 }
371
372 public Control generated_356516589937347256(final TypeHierarchyViewPart typehierarchyviewpart) {
373 initContextMenu(new IMenuListener() {
374 public void menuAboutToShow(IMenuManager menu) {
375 typehierarchyviewpart.fillMethodsViewerContextMenu(menu);
376 }
377 }, IContextMenuConstants.TARGET_ID_MEMBERS_VIEW, typehierarchyviewpart.getSite());
378 addPostSelectionChangedListener(typehierarchyviewpart.fSelectionChangedListener);
379
380 new OpenAndLinkWithEditorHelper(this) {
381 @Override
382 protected void activate(ISelection selection) {
383 try {
384 final Object selectedElement= SelectionUtil.getSingleElement(selection);
385 if (EditorUtility.isOpenInEditor(selectedElement) != null)
386 EditorUtility.openInEditor(selectedElement, true);
387 } catch (PartInitException ex) {
388 // ignore if no editor input can be found
389 }
390 }
391
392 @Override
393 protected void linkToEditor(ISelection selection) {
394 // do nothing: this is handled in more detail by the part itself
395 }
396
397 @Override
398 protected void open(ISelection selection, boolean activate) {
399 if (selection instanceof IStructuredSelection)
400 typehierarchyviewpart.fOpenAction.run((IStructuredSelection)selection);
401 }
402 };
403
404 Control control= getTable();
405 control.addKeyListener(typehierarchyviewpart.createKeyListener());
406 return control;
407 }
408
409 public DropTarget generated_1359601309272706539(TypeHierarchyViewPart typehierarchyviewpart) {
410 for (int i= 0; i < typehierarchyviewpart.fAllViewers.length; i++) {
411 typehierarchyviewpart.addDragAdapters(typehierarchyviewpart.fAllViewers[i]);
412 typehierarchyviewpart.addDropAdapters(typehierarchyviewpart.fAllViewers[i]);
413 }
414 typehierarchyviewpart.addDragAdapters(this);
415 addDropSupport(DND.DROP_NONE, new Transfer[0], new DropTargetAdapter());
416
417 //DND on empty hierarchy
418 DropTarget dropTarget = new DropTarget(typehierarchyviewpart.fPagebook, DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK | DND.DROP_DEFAULT);
419 return dropTarget;
420 }
421
422 public IActionBars generated_3457398075108290302(TypeHierarchyViewPart typehierarchyviewpart, ToolBar methodViewerToolBar) {
423 int layout;
424 try {
425 layout= typehierarchyviewpart.fDialogSettings.getInt(TypeHierarchyViewPart.DIALOGSTORE_VIEWLAYOUT);
426 if (layout < 0 || layout > 3) {
427 layout= ITypeHierarchyViewPart.VIEW_LAYOUT_AUTOMATIC;
428 }
429 } catch (NumberFormatException e) {
430 layout= ITypeHierarchyViewPart.VIEW_LAYOUT_AUTOMATIC;
431 }
432 // force the update
433 typehierarchyviewpart.fCurrentLayout= -1;
434 // will fill the main tool bar
435 typehierarchyviewpart.setViewLayout(layout);
436
437 typehierarchyviewpart.showQualifiedTypeNames(typehierarchyviewpart.fDialogSettings.getBoolean(TypeHierarchyViewPart.DIALOGSTORE_QUALIFIED_NAMES));
438 typehierarchyviewpart.setLinkingEnabled(typehierarchyviewpart.fDialogSettings.getBoolean(TypeHierarchyViewPart.DIALOGSTORE_LINKEDITORS));
439
440 // set the filter menu items
441 IActionBars actionBars= typehierarchyviewpart.getViewSite().getActionBars();
442 IMenuManager viewMenu= actionBars.getMenuManager();
443 for (int i= 0; i < typehierarchyviewpart.fViewActions.length; i++) {
444 ToggleViewAction action= typehierarchyviewpart.fViewActions[i];
445 viewMenu.add(action);
446 action.setEnabled(false);
447 }
448 viewMenu.add(new Separator());
449
450 typehierarchyviewpart.fWorkingSetActionGroup.fillViewMenu(viewMenu);
451
452 viewMenu.add(new Separator());
453
454 IMenuManager layoutSubMenu= new MenuManager(TypeHierarchyMessages.TypeHierarchyViewPart_layout_submenu);
455 viewMenu.add(layoutSubMenu);
456 for (int i= 0; i < typehierarchyviewpart.fToggleOrientationActions.length; i++) {
457 layoutSubMenu.add(typehierarchyviewpart.fToggleOrientationActions[i]);
458 }
459 viewMenu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
460 viewMenu.add(typehierarchyviewpart.fShowQualifiedTypeNamesAction);
461 viewMenu.add(typehierarchyviewpart.fToggleLinkingAction);
462
463
464 // fill the method viewer tool bar
465 ToolBarManager lowertbmanager= new ToolBarManager(methodViewerToolBar);
466 lowertbmanager.add(typehierarchyviewpart.fEnableMemberFilterAction);
467 lowertbmanager.add(new Separator());
468 contributeToToolBar(lowertbmanager);
469 lowertbmanager.update(true);
470
471 // selection provider
472 int nHierarchyViewers= typehierarchyviewpart.fAllViewers.length;
473 StructuredViewer[] trackedViewers= new StructuredViewer[nHierarchyViewers + 1];
474 for (int i= 0; i < nHierarchyViewers; i++) {
475 trackedViewers[i]= typehierarchyviewpart.fAllViewers[i];
476 }
477 trackedViewers[nHierarchyViewers]= this;
478 typehierarchyviewpart.fSelectionProviderMediator= new SelectionProviderMediator(trackedViewers, typehierarchyviewpart.getCurrentViewer());
479 IStatusLineManager slManager= typehierarchyviewpart.getViewSite().getActionBars().getStatusLineManager();
480 typehierarchyviewpart.fSelectionProviderMediator.addSelectionChangedListener(new StatusBarUpdater(slManager));
481
482 typehierarchyviewpart.getSite().setSelectionProvider(typehierarchyviewpart.fSelectionProviderMediator);
483 typehierarchyviewpart.getSite().getPage().addPartListener(typehierarchyviewpart.fPartListener);
484
485 if (typehierarchyviewpart.fMemento != null)
486 typehierarchyviewpart.restoreState(typehierarchyviewpart.fMemento);
487 else
488 typehierarchyviewpart.setViewerVisibility(false);
489
490 PlatformUI.getWorkbench().getHelpSystem().setHelp(typehierarchyviewpart.fPagebook, IJavaHelpContextIds.TYPE_HIERARCHY_VIEW);
491
492
493 typehierarchyviewpart.fActionGroups= new CompositeActionGroup(new ActionGroup[] {
494 new NewWizardsActionGroup(typehierarchyviewpart.getSite()),
495 new OpenEditorActionGroup(typehierarchyviewpart),
496 new OpenViewActionGroup(typehierarchyviewpart),
497 new CCPActionGroup(typehierarchyviewpart),
498 new GenerateActionGroup(typehierarchyviewpart),
499 new RefactorActionGroup(typehierarchyviewpart),
500 new JavaSearchActionGroup(typehierarchyviewpart)
501 });
502
503 typehierarchyviewpart.fActionGroups.fillActionBars(actionBars);
504 typehierarchyviewpart.fSelectAllAction= new SelectAllAction(this);
505 typehierarchyviewpart.fOpenAction= new OpenAction(typehierarchyviewpart.getSite());
506 return actionBars;
507 }
508
509 public void generated_3262187007324139874(TypeHierarchyViewPart typehierarchyviewpart, boolean on) {
510 if (on != typehierarchyviewpart.fIsEnableMemberFilter) {
511 typehierarchyviewpart.fIsEnableMemberFilter= on;
512 if (!on) {
513 IType methodViewerInput= (IType) getInput();
514 typehierarchyviewpart.setMemberFilter(null);
515 typehierarchyviewpart.updateHierarchyViewer(true);
516 typehierarchyviewpart.updateToolTipAndDescription();
517
518 if (methodViewerInput != null && typehierarchyviewpart.getCurrentViewer().isElementShown(methodViewerInput)) {
519 // avoid that the method view changes content by selecting the previous input
520 typehierarchyviewpart.internalSelectType(methodViewerInput, true);
521 } else if (typehierarchyviewpart.fSelectedType != null) {
522 // choose a input that exists
523 typehierarchyviewpart.internalSelectType(typehierarchyviewpart.fSelectedType, true);
524 typehierarchyviewpart.updateMethodViewer(typehierarchyviewpart.fSelectedType);
525 }
526 } else {
527 typehierarchyviewpart.methodSelectionChanged(getSelection());
528 }
529 }
530 }
531
532 public Object generated_8968129359984973293() {
533 Object methodViewerInput= getInput();
534 refresh();
535 return methodViewerInput;
536 }
537
538 public void generated_5765350795097787896(final ShowInheritedMembersAction showinheritedmembersaction) {
539 BusyIndicator.showWhile(getControl().getDisplay(), new Runnable() {
540 public void run() {
541 showInheritedMethods(showinheritedmembersaction.isChecked());
542 }
543 });
544 }
545
546
547
548}