KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > view > DependenciesView


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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.view;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.Preferences;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IToolBarManager;
20 import org.eclipse.jface.action.Separator;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.pde.core.plugin.IPlugin;
24 import org.eclipse.pde.core.plugin.IPluginModel;
25 import org.eclipse.pde.core.plugin.IPluginModelBase;
26 import org.eclipse.pde.internal.core.PDECore;
27 import org.eclipse.pde.internal.core.builders.DependencyLoop;
28 import org.eclipse.pde.internal.core.builders.DependencyLoopFinder;
29 import org.eclipse.pde.internal.ui.IHelpContextIds;
30 import org.eclipse.pde.internal.ui.IPreferenceConstants;
31 import org.eclipse.pde.internal.ui.PDEPlugin;
32 import org.eclipse.pde.internal.ui.PDEPluginImages;
33 import org.eclipse.pde.internal.ui.PDEUIMessages;
34 import org.eclipse.pde.internal.ui.editor.plugin.LoopDialog;
35 import org.eclipse.swt.custom.BusyIndicator;
36 import org.eclipse.swt.graphics.Image;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.ui.IActionBars;
39 import org.eclipse.ui.IMemento;
40 import org.eclipse.ui.IPropertyListener;
41 import org.eclipse.ui.IViewSite;
42 import org.eclipse.ui.IWorkbenchPart;
43 import org.eclipse.ui.IWorkbenchPartSite;
44 import org.eclipse.ui.PartInitException;
45 import org.eclipse.ui.PlatformUI;
46 import org.eclipse.ui.part.IPage;
47 import org.eclipse.ui.part.IPageBookViewPage;
48 import org.eclipse.ui.part.PageBook;
49 import org.eclipse.ui.part.PageBookView;
50
51 public class DependenciesView extends PageBookView implements
52         IPreferenceConstants, IHelpContextIds {
53     
54     static class DummyPart implements IWorkbenchPart {
55         public void addPropertyListener(IPropertyListener listener) {/* dummy */
56         }
57
58         public void createPartControl(Composite parent) {/* dummy */
59         }
60
61         public void dispose() {/* dummy */
62         }
63
64         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
65             return null;
66         }
67
68         public IWorkbenchPartSite getSite() {
69             return null;
70         }
71
72         public String JavaDoc getTitle() {
73             return null;
74         }
75
76         public Image getTitleImage() {
77             return null;
78         }
79
80         public String JavaDoc getTitleToolTip() {
81             return null;
82         }
83
84         public void removePropertyListener(IPropertyListener listener) {/* dummy */
85         }
86
87         public void setFocus() {/* dummy */
88         }
89     }
90
91     class ShowLoopsAction extends Action {
92
93         public ShowLoopsAction() {
94             super("", AS_PUSH_BUTTON); //$NON-NLS-1$
95
setText(PDEUIMessages.DependenciesView_ShowLoopsAction_label);
96             setDescription(PDEUIMessages.DependenciesView_ShowLoopsAction_description);
97             setToolTipText(PDEUIMessages.DependenciesView_ShowLoopsAction_tooltip);
98             setImageDescriptor(PDEPluginImages.DESC_DEP_LOOP);
99             setDisabledImageDescriptor(PDEPluginImages.DESC_DEP_LOOP_DISABLED);
100             setEnabled(false);
101         }
102
103         /*
104          * @see Action#actionPerformed
105          */

106         public void run() {
107             LoopDialog dialog = new LoopDialog(PDEPlugin
108                     .getActiveWorkbenchShell(), fLoops);
109             dialog.open();
110         }
111     }
112
113     class ShowCalleesAction extends Action {
114
115         public ShowCalleesAction() {
116             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
117
setText(PDEUIMessages.DependenciesView_ShowCalleesAction_label);
118             setDescription(PDEUIMessages.DependenciesView_ShowCalleesAction_description);
119             setToolTipText(PDEUIMessages.DependenciesView_ShowCalleesAction_tooltip);
120             setImageDescriptor(PDEPluginImages.DESC_CALLEES);
121             setDisabledImageDescriptor(PDEPluginImages.DESC_CALLEES_DISABLED);
122         }
123
124         /*
125          * @see Action#actionPerformed
126          */

127         public void run() {
128             if (isChecked()) {
129                 fPreferences.setValue(DEPS_VIEW_SHOW_CALLERS, false);
130                 setViewType(false);
131             }
132         }
133     }
134
135     class ShowCallersAction extends Action {
136         public ShowCallersAction() {
137             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
138
setText(PDEUIMessages.DependenciesView_ShowCallersAction_label);
139             setDescription(PDEUIMessages.DependenciesView_ShowCallersAction_description);
140             setToolTipText(PDEUIMessages.DependenciesView_ShowCallersAction_tooltip);
141             setImageDescriptor(PDEPluginImages.DESC_CALLERS);
142             setDisabledImageDescriptor(PDEPluginImages.DESC_CALLERS_DISABLED);
143         }
144
145         /*
146          * @see Action#actionPerformed
147          */

148         public void run() {
149             if (isChecked()) {
150                 fPreferences.setValue(DEPS_VIEW_SHOW_CALLERS, true);
151                 setViewType(true);
152             }
153         }
154     }
155
156     class ShowListAction extends Action {
157         public ShowListAction() {
158             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
159
setText(PDEUIMessages.DependenciesView_ShowListAction_label);
160             setDescription(PDEUIMessages.DependenciesView_ShowListAction_description);
161             setToolTipText(PDEUIMessages.DependenciesView_ShowListAction_tooltip);
162             setImageDescriptor(PDEPluginImages.DESC_FLAT_LAYOUT);
163             setDisabledImageDescriptor(PDEPluginImages.DESC_FLAT_LAYOUT_DISABLED);
164         }
165
166         /*
167          * @see Action#actionPerformed
168          */

169         public void run() {
170             if (isChecked()) {
171                 fPreferences.setValue(DEPS_VIEW_SHOW_LIST, true);
172                 setPresentation(true);
173             }
174         }
175     }
176
177     class ShowTreeAction extends Action {
178
179         public ShowTreeAction() {
180             super("", AS_RADIO_BUTTON); //$NON-NLS-1$
181
setText(PDEUIMessages.DependenciesView_ShowTreeAction_label);
182             setDescription(PDEUIMessages.DependenciesView_ShowTreeAction_description);
183             setToolTipText(PDEUIMessages.DependenciesView_ShowTreeAction_tooltip);
184             setImageDescriptor(PDEPluginImages.DESC_HIERARCHICAL_LAYOUT);
185             setDisabledImageDescriptor(PDEPluginImages.DESC_HIERARCHICAL_LAYOUT_DISABLED);
186         }
187
188         /*
189          * @see Action#actionPerformed
190          */

191         public void run() {
192             if (isChecked()) {
193                 fPreferences.setValue(DEPS_VIEW_SHOW_LIST, false);
194                 setPresentation(false);
195             }
196         }
197     }
198
199     protected static final IWorkbenchPart PART_CALLEES_LIST = new DummyPart();
200
201     protected static final IWorkbenchPart PART_CALLEES_TREE = new DummyPart();
202
203     protected static final IWorkbenchPart PART_CALLERS_LIST = new DummyPart();
204
205     protected static final IWorkbenchPart PART_CALLERS_TREE = new DummyPart();
206
207     public static final String JavaDoc TREE_ACTION_GROUP = "tree"; //$NON-NLS-1$
208

209     protected static final String JavaDoc MEMENTO_KEY_INPUT = "inputPluginId"; //$NON-NLS-1$
210

211     private static final DependencyLoop[] NO_LOOPS = new DependencyLoop[0];
212     
213     private Map JavaDoc fPagesToParts;
214
215     private Map JavaDoc fPartsToPages;
216
217     private Object JavaDoc fInput;
218
219     private Preferences fPreferences = PDEPlugin.getDefault()
220             .getPluginPreferences();
221
222     private ShowCalleesAction fShowCallees;
223
224     private ShowCallersAction fShowCallers;
225
226     private ShowListAction fShowList;
227
228     private ShowTreeAction fShowTree;
229     
230     private ShowLoopsAction fShowLoops;
231
232     // history of input elements (as Strings). No duplicates
233
private ArrayList JavaDoc fInputHistory;
234     
235     private DependencyLoop[] fLoops;
236     
237     private HistoryDropDownAction fHistoryDropDownAction;
238     /**
239      *
240      */

241     public DependenciesView() {
242         super();
243         fPartsToPages = new HashMap JavaDoc(4);
244         fPagesToParts = new HashMap JavaDoc(4);
245         fInputHistory= new ArrayList JavaDoc();
246         fLoops = NO_LOOPS;
247     }
248
249     private void contributeToActionBars(IActionBars actionBars) {
250         contributeToLocalToolBar(actionBars.getToolBarManager());
251         actionBars.updateActionBars();
252     }
253
254     private void contributeToLocalToolBar(IToolBarManager manager) {
255         manager.add(new Separator(TREE_ACTION_GROUP));
256         manager.add(new Separator("type")); //$NON-NLS-1$
257
manager.appendToGroup("type", fShowCallees); //$NON-NLS-1$
258
manager.appendToGroup("type", fShowCallers); //$NON-NLS-1$
259
manager.add(new Separator("presentation")); //$NON-NLS-1$
260
manager.appendToGroup("presentation", fShowTree); //$NON-NLS-1$
261
manager.appendToGroup("presentation", fShowList); //$NON-NLS-1$
262
manager.add(new Separator("history")); //$NON-NLS-1$
263
manager.add(fShowLoops);
264         manager.add(fHistoryDropDownAction);
265     }
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see org.eclipse.ui.part.PageBookView#createDefaultPage(org.eclipse.ui.part.PageBook)
271      */

272     protected IPage createDefaultPage(PageBook book) {
273         if (fPreferences.getBoolean(DEPS_VIEW_SHOW_CALLERS)) {
274             if (fPreferences.getBoolean(DEPS_VIEW_SHOW_LIST)) {
275                 return createPage(PART_CALLERS_LIST);
276
277             }
278             return createPage(PART_CALLERS_TREE);
279         }
280         if (fPreferences.getBoolean(DEPS_VIEW_SHOW_LIST)) {
281             return createPage(PART_CALLEES_LIST);
282
283         }
284         return createPage(PART_CALLEES_TREE);
285     }
286
287     /**
288      * part of the part constants
289      */

290     private DependenciesViewPage createPage(IWorkbenchPart part) {
291         DependenciesViewPage page;
292         if (part == PART_CALLEES_TREE) {
293             page = new DependenciesViewTreePage(this,
294                     new CalleesTreeContentProvider(this));
295         } else if (part == PART_CALLEES_LIST) {
296             page = new DependenciesViewListPage(this,
297                     new CalleesListContentProvider(this));
298         } else if (part == PART_CALLERS_TREE) {
299             page = new DependenciesViewTreePage(this,
300                     new CallersTreeContentProvider(this));
301         } else {
302             page = new DependenciesViewListPage(this,
303                     new CallersListContentProvider(this));
304         }
305
306         initPage(page);
307         page.createControl(getPageBook());
308         fPartsToPages.put(part, page);
309         fPagesToParts.put(page, part);
310         return page;
311     }
312
313     /*
314      * (non-Javadoc)
315      *
316      * @see org.eclipse.ui.part.PageBookView#createPartControl(org.eclipse.swt.widgets.Composite)
317      */

318     public void createPartControl(Composite parent) {
319         super.createPartControl(parent);
320         fShowCallees = new ShowCalleesAction();
321         fShowCallees.setChecked(!fPreferences
322                 .getBoolean(DEPS_VIEW_SHOW_CALLERS));
323         fShowCallers = new ShowCallersAction();
324         fShowCallers
325                 .setChecked(fPreferences.getBoolean(DEPS_VIEW_SHOW_CALLERS));
326
327         fShowTree = new ShowTreeAction();
328         fShowTree.setChecked(!fPreferences.getBoolean(DEPS_VIEW_SHOW_LIST));
329         fShowList = new ShowListAction();
330         fShowList.setChecked(fPreferences.getBoolean(DEPS_VIEW_SHOW_LIST));
331         
332         fShowLoops = new ShowLoopsAction();
333         fShowLoops.setEnabled(fLoops != NO_LOOPS);
334         
335         fHistoryDropDownAction= new HistoryDropDownAction(this);
336         fHistoryDropDownAction.setEnabled(!fInputHistory.isEmpty());
337         
338         IActionBars actionBars = getViewSite().getActionBars();
339         contributeToActionBars(actionBars);
340
341         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IHelpContextIds.DEPENDENCIES_VIEW);
342     }
343
344     /*
345      * (non-Javadoc)
346      *
347      * @see org.eclipse.ui.part.PageBookView#doCreatePage(org.eclipse.ui.IWorkbenchPart)
348      */

349     protected PageRec doCreatePage(IWorkbenchPart part) {
350         IPageBookViewPage page = (IPageBookViewPage) fPartsToPages.get(part);
351         if (page == null && !fPartsToPages.containsKey(part)) {
352             page = createPage(part);
353         }
354         if (page != null) {
355             return new PageRec(part, page);
356         }
357         return null;
358     }
359
360     /*
361      * (non-Javadoc)
362      *
363      * @see org.eclipse.ui.part.PageBookView#doDestroyPage(org.eclipse.ui.IWorkbenchPart,
364      * org.eclipse.ui.part.PageBookView.PageRec)
365      */

366     protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
367         IPage page = pageRecord.page;
368         page.dispose();
369         pageRecord.dispose();
370
371         // empty cross-reference cache
372
fPartsToPages.remove(part);
373     }
374
375     /*
376      * (non-Javadoc)
377      *
378      * @see org.eclipse.ui.part.PageBookView#getBootstrapPart()
379      */

380     protected IWorkbenchPart getBootstrapPart() {
381         if (fPreferences.getBoolean(DEPS_VIEW_SHOW_CALLERS)) {
382             if (fPreferences.getBoolean(DEPS_VIEW_SHOW_LIST)) {
383                 return PART_CALLERS_LIST;
384
385             }
386             return PART_CALLERS_TREE;
387         }
388         if (fPreferences.getBoolean(DEPS_VIEW_SHOW_LIST)) {
389             return PART_CALLEES_LIST;
390
391         }
392         return PART_CALLEES_TREE;
393     }
394
395     /*
396      * (non-Javadoc)
397      *
398      * @see org.eclipse.ui.part.ViewPart#init(org.eclipse.ui.IViewSite,
399      * org.eclipse.ui.IMemento)
400      */

401     public void init(IViewSite site, IMemento memento) throws PartInitException {
402         super.init(site, memento);
403         if(memento == null)
404             return;
405         String JavaDoc id = memento.getString(MEMENTO_KEY_INPUT);
406         if (id != null) {
407             IPluginModelBase plugin = PDECore.getDefault().getModelManager().findModel(id);
408             if (plugin != null) {
409                 fInput = plugin;
410                 addHistoryEntry(id);
411                 findLoops();
412             }
413         }
414     }
415
416     /*
417      * (non-Javadoc)
418      *
419      * @see org.eclipse.ui.part.PageBookView#isImportant(org.eclipse.ui.IWorkbenchPart)
420      */

421     protected boolean isImportant(IWorkbenchPart part) {
422         return part instanceof DummyPart;
423     }
424
425     public void openTo(Object JavaDoc object) {
426         if (object != null && !object.equals(fInput)) {
427             if(object instanceof IPluginModelBase){
428                 String JavaDoc id =((IPluginModelBase)object).getPluginBase().getId();
429                 addHistoryEntry(id);
430             }
431         }
432         updateInput(object);
433     }
434
435     private void updateInput(Object JavaDoc object) {
436         fInput = object;
437         findLoops();
438         ((DependenciesViewPage) getCurrentPage()).setInput(object);
439     }
440
441     /**
442      *
443      */

444     private void findLoops() {
445         fLoops = NO_LOOPS;
446         if (fInput != null && fInput instanceof IPluginModel) {
447             BusyIndicator.showWhile(PDEPlugin.getActiveWorkbenchShell()
448                     .getDisplay(), new Runnable JavaDoc() {
449                 /*
450                  * (non-Javadoc)
451                  *
452                  * @see java.lang.Runnable#run()
453                  */

454                 public void run() {
455                     IPlugin plugin = ((IPluginModel) fInput).getPlugin();
456                     DependencyLoop[] loops = DependencyLoopFinder
457                             .findLoops(plugin);
458                     if (loops.length > 0) {
459                         fLoops = loops;
460                     }
461                 }
462             });
463         }
464         if(fShowLoops != null)
465             fShowLoops.setEnabled(fLoops != NO_LOOPS);
466     }
467
468     /*
469      * (non-Javadoc)
470      *
471      * @see org.eclipse.ui.part.ViewPart#saveState(org.eclipse.ui.IMemento)
472      */

473     public void saveState(IMemento memento) {
474         super.saveState(memento);
475         if (fInput != null && fInput instanceof IPluginModelBase) {
476             String JavaDoc inputPluginId = ((IPluginModelBase) fInput).getPluginBase()
477                     .getId();
478             memento.putString(MEMENTO_KEY_INPUT, inputPluginId);
479         }
480     }
481
482     void setPresentation(boolean listNotTree) {
483         IWorkbenchPart currentPart = getCurrentContributingPart();
484         if (listNotTree) {
485             if (currentPart == PART_CALLEES_TREE) {
486                 partActivated(PART_CALLEES_LIST);
487             } else if (currentPart == PART_CALLERS_TREE) {
488                 partActivated(PART_CALLERS_LIST);
489             }
490
491         } else {
492             if (currentPart == PART_CALLEES_LIST) {
493                 partActivated(PART_CALLEES_TREE);
494             } else if (currentPart == PART_CALLERS_LIST) {
495                 partActivated(PART_CALLERS_TREE);
496             }
497
498         }
499     }
500
501     void setViewType(boolean callers) {
502         IWorkbenchPart currentPart = getCurrentContributingPart();
503         if (callers) {
504             if (currentPart == PART_CALLEES_TREE) {
505                 partActivated(PART_CALLERS_TREE);
506             } else if (currentPart == PART_CALLEES_LIST) {
507                 partActivated(PART_CALLERS_LIST);
508             }
509
510         } else {
511             if (currentPart == PART_CALLERS_TREE) {
512                 partActivated(PART_CALLEES_TREE);
513             } else if (currentPart == PART_CALLERS_LIST) {
514                 partActivated(PART_CALLEES_LIST);
515             }
516
517         }
518     }
519
520     /*
521      * (non-Javadoc)
522      *
523      * @see org.eclipse.ui.part.PageBookView#showPageRec(org.eclipse.ui.part.PageBookView.PageRec)
524      */

525     protected void showPageRec(PageRec pageRec) {
526         IPage currPage = getCurrentPage();
527         IStructuredSelection selection = null;
528         if (currPage instanceof DependenciesViewPage)
529             selection = ((DependenciesViewPage)currPage).getSelection();
530         IPage p = pageRec.page;
531         ((DependenciesViewPage) p).setInput(fInput);
532         super.showPageRec(pageRec);
533         updateTitle(fInput);
534         ((DependenciesViewPage) p).setSelection(selection);
535     }
536
537     void updateTitle(Object JavaDoc newInput) {
538         if (newInput == null) {
539             setContentDescription(""); //$NON-NLS-1$
540
} else if (!newInput.equals(PDECore.getDefault().getModelManager())) {
541             String JavaDoc name = PDEPlugin.getDefault().getLabelProvider().getText(
542                     newInput);
543             String JavaDoc title;
544             if (getCurrentContributingPart() == PART_CALLEES_TREE) {
545                 title = NLS.bind(PDEUIMessages.DependenciesView_callees_tree_title, name);
546             } else if (getCurrentContributingPart() == PART_CALLEES_LIST) {
547                 title = NLS.bind(PDEUIMessages.DependenciesView_callees_list_title, name);
548             } else if (getCurrentContributingPart() == PART_CALLERS_TREE) {
549                 title = NLS.bind(PDEUIMessages.DependenciesView_callers_tree_title, name);
550             } else {
551                 title = NLS.bind(PDEUIMessages.DependenciesView_callers_list_title, name);
552             }
553             if(fLoops != NO_LOOPS){
554                 title = title + " " + PDEUIMessages.DependenciesView_cycles_title; //$NON-NLS-1$
555
}
556             setContentDescription(title);
557         }
558         setTitleToolTip(getTitle());
559     }
560     
561     /**
562      * Adds the entry if new. Inserted at the beginning of the history entries list.
563      * @param entry The new entry
564      */

565     private void addHistoryEntry(String JavaDoc entry) {
566         if (fInputHistory.contains(entry)) {
567             fInputHistory.remove(entry);
568         }
569         fInputHistory.add(0, entry);
570         if (fHistoryDropDownAction != null)
571             fHistoryDropDownAction.setEnabled(true);
572     }
573     
574     private void updateHistoryEntries() {
575         for (int i= fInputHistory.size() - 1; i >= 0; i--) {
576             String JavaDoc type= (String JavaDoc) fInputHistory.get(i);
577             if (PDECore.getDefault().getModelManager().findModel(type)==null) {
578                 fInputHistory.remove(i);
579             }
580         }
581         if (fHistoryDropDownAction != null)
582             fHistoryDropDownAction.setEnabled(!fInputHistory.isEmpty());
583     }
584     
585     /**
586      * Goes to the selected entry, without updating the order of history entries.
587      * @param entry The entry to open
588      */

589     public void gotoHistoryEntry(String JavaDoc entry) {
590         if (fInputHistory.contains(entry)) {
591             updateInput(PDECore.getDefault().getModelManager().findModel(entry));
592         }
593     }
594     
595     /**
596      * Gets all history entries.
597      * @return All history entries
598      */

599     public String JavaDoc[] getHistoryEntries() {
600         if (fInputHistory.size() > 0) {
601             updateHistoryEntries();
602         }
603         return (String JavaDoc[]) fInputHistory.toArray(new String JavaDoc[fInputHistory.size()]);
604     }
605     
606     /**
607      * Sets the history entries
608      * @param elems The history elements to set
609      */

610     public void setHistoryEntries(String JavaDoc[] elems) {
611         fInputHistory.clear();
612         for (int i= 0; i < elems.length; i++) {
613             fInputHistory.add(elems[i]);
614         }
615         updateHistoryEntries();
616     }
617     /**
618      * @return Returns the fInput.
619      */

620     public String JavaDoc getInput() {
621         if(fInput!=null){
622             return ((IPluginModelBase)fInput).getPluginBase().getId();
623         }
624         return null;
625     }
626 }
627
Popular Tags