KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > history > GenericHistoryView


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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
12 package org.eclipse.team.internal.ui.history;
13
14 import java.util.*;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.jface.action.*;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.dnd.*;
27 import org.eclipse.swt.widgets.*;
28 import org.eclipse.team.core.RepositoryProvider;
29 import org.eclipse.team.core.history.IFileHistoryProvider;
30 import org.eclipse.team.internal.ui.*;
31 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
32 import org.eclipse.team.ui.history.*;
33 import org.eclipse.ui.*;
34 import org.eclipse.ui.actions.ActionFactory;
35 import org.eclipse.ui.ide.ResourceUtil;
36 import org.eclipse.ui.part.*;
37
38 public class GenericHistoryView extends ViewPart implements IHistoryView, IPropertyChangeListener, IShowInTarget {
39
40     private static final String JavaDoc HISTORY_VIEW_GROUP = "org.eclipse.team.ui.historyView"; //$NON-NLS-1$
41
private static final String JavaDoc NAVIGATION_GROUP = "org.eclipse.team.ui.navigation"; //$NON-NLS-1$
42
private static final int MAX_NAVIGATION_HISTORY_ENTRIES = 15;
43
44     static boolean sameSource(IHistoryPageSource source1, IHistoryPageSource source2) {
45         return source1 == source2 || (source1 != null && source2 != null && source1.equals(source2));
46     }
47     
48     class PageContainer {
49         private Page page;
50         private SubActionBars subBars;
51         private final IHistoryPageSource source;
52
53         public PageContainer(Page page, IHistoryPageSource source) {
54             this.page = page;
55             this.source = source;
56         }
57
58         public Page getPage() {
59             return page;
60         }
61
62         public void setPage(Page page) {
63             this.page = page;
64         }
65
66         public SubActionBars getSubBars() {
67             return subBars;
68         }
69
70         public void setSubBars(SubActionBars subBars) {
71             this.subBars = subBars;
72         }
73
74         public IHistoryPageSource getSource() {
75             return source;
76         }
77         
78         public boolean matches(Object JavaDoc object, IHistoryPageSource pageSource) {
79             if (page instanceof IHistoryPage) {
80                 Object JavaDoc input = ((IHistoryPage)page).getInput();
81                 if (input != null)
82                     return input.equals(object) && sameSource(getPageSourceFor(object, pageSource), getPageSourceFor(input, source));
83             }
84             return false;
85         }
86
87         public boolean canShow(Object JavaDoc object, IHistoryPageSource pageSource) {
88             if (page instanceof IHistoryPage && sameSource(getPageSourceFor(object, pageSource), getPageSourceFor(((IHistoryPage)page).getInput(), source))) {
89                 return ((IHistoryPage)page).isValidInput(object);
90             }
91             return false;
92         }
93     }
94     
95     /*
96      * The navigation history for this view.
97      * The history adds the MRU to the end so basic navigation goes backwards.
98      */

99     class NavigationHistory {
100         List JavaDoc history = new ArrayList();
101         int position;
102         private boolean navigating;
103         public int size() {
104             return history.size();
105         }
106         public void gotoPreviousEntry() {
107             if (position > 0) {
108                 position--;
109                 gotoEntry();
110             } else {
111                 position = history.size() - 1;
112                 gotoEntry();
113             }
114         }
115         private void gotoEntry() {
116             try {
117                 navigating = true;
118                 NavigationHistoryEntry currentEntry = getCurrentEntry();
119                 showHistoryPageFor(currentEntry.object, true, true, currentEntry.source);
120             } finally {
121                 navigating = false;
122             }
123         }
124         private NavigationHistoryEntry getCurrentEntry() {
125             return (NavigationHistoryEntry)history.get(position);
126         }
127         public void addEntry(Object JavaDoc object, String JavaDoc name, IHistoryPageSource source) {
128             if (!navigating) {
129                 NavigationHistoryEntry navigationHistoryEntry = new NavigationHistoryEntry(object, name, source);
130                 if (history.contains(navigationHistoryEntry)) {
131                     history.remove(navigationHistoryEntry);
132                 }
133                 history.add(navigationHistoryEntry);
134                 if (history.size() > MAX_NAVIGATION_HISTORY_ENTRIES) {
135                     history.remove(0);
136                 }
137                 position = history.size() - 1;
138             }
139             navigateAction.update();
140         }
141         public NavigationHistoryEntry[] getEntries() {
142             return (NavigationHistoryEntry[]) history.toArray(new NavigationHistoryEntry[history.size()]);
143         }
144         private NavigationHistoryEntry getEntry(int i) {
145             return (NavigationHistoryEntry)history.get(i);
146         }
147         public void gotoEntry(NavigationHistoryEntry navigationHistoryEntry) {
148             position = history.indexOf(navigationHistoryEntry);
149             gotoEntry();
150         }
151         public NavigationHistoryEntry getPreviousEntry() {
152             int next = position - 1;
153             if (next < 0)
154                 next = size() - 1;
155             return getEntry(next);
156         }
157         
158         public void updateName(IHistoryPage historyPage,
159                 IHistoryPageSource pageSource) {
160             NavigationHistoryEntry[] historyEntries = getEntries();
161             for (int i = 0; i < historyEntries.length; i++) {
162                 NavigationHistoryEntry historyEntry = historyEntries[i];
163                 if (historyEntry.matches(historyPage, pageSource))
164                     historyEntry.name = historyPage.getName();
165             }
166             navigateAction.update();
167         }
168     }
169     
170     static class NavigationHistoryEntry {
171         Object JavaDoc object;
172         String JavaDoc name;
173         IHistoryPageSource source;
174         public NavigationHistoryEntry(Object JavaDoc object, String JavaDoc name, IHistoryPageSource source) {
175             this.object = object;
176             this.name = name;
177             this.source = source;
178         }
179         public boolean equals(Object JavaDoc obj) {
180             if (obj instanceof NavigationHistoryEntry) {
181                 NavigationHistoryEntry other = (NavigationHistoryEntry) obj;
182                 return other.object.equals(this.object) && sameSource(source, other.source);
183             }
184             return false;
185         }
186         public boolean matches(IHistoryPage historyPage,
187                 IHistoryPageSource pageSource) {
188             return object.equals(historyPage.getInput())
189                     && sameSource(source, pageSource);
190         }
191         public int hashCode() {
192             return object.hashCode();
193         }
194     }
195     
196     abstract class MenuCreator implements IMenuCreator {
197         private MenuManager menuManager;
198         public void dispose() {
199             if(menuManager != null) {
200                 menuManager.dispose();
201                 menuManager = null;
202             }
203         }
204         public Menu getMenu(Control parent) {
205             Menu fMenu = null;
206             if (menuManager == null) {
207                 menuManager = new MenuManager();
208                 fMenu = menuManager.createContextMenu(parent);
209                 IAction[] actions = getDropDownActions();
210                 for (int i = actions.length - 1; i >= 0 ; i--) {
211                     IAction action = actions[i];
212                     menuManager.add(action);
213                 }
214                 updateMenuState();
215             } else {
216                 fMenu = menuManager.getMenu();
217             }
218             return fMenu;
219         }
220         protected void updateMenuState() {
221             if (menuManager != null)
222                 menuManager.update(true);
223         }
224
225         protected abstract IAction[] getDropDownActions();
226         
227         public Menu getMenu(Menu parent) {
228             return null;
229         }
230         
231         public void rebuildMenu() {
232             if(menuManager != null) {
233                 menuManager.dispose();
234                 menuManager = null;
235             }
236         }
237     }
238     
239     class NavigationHistoryAction extends Action {
240         private MenuCreator menuCreator;
241         private IAction[] actions;
242
243         public NavigationHistoryAction() {
244             menuCreator = new MenuCreator() {
245                 protected IAction[] getDropDownActions() {
246                     return getActions();
247                 }
248             };
249             setMenuCreator(menuCreator);
250             update();
251         }
252         private IAction[] createActions() {
253             NavigationHistoryEntry[] entries = getDropDownEntries();
254             List JavaDoc actions = new ArrayList();
255             for (int i = 0; i < entries.length; i++) {
256                 NavigationHistoryEntry navigationHistoryEntry = entries[i];
257                 actions.add(new NavigationHistoryEntryAction(navigationHistoryEntry));
258             }
259             return (IAction[]) actions.toArray(new IAction[actions.size()]);
260         }
261         protected NavigationHistoryEntry[] getDropDownEntries() {
262             return navigationHistory.getEntries();
263         }
264         public void run() {
265             navigationHistory.gotoPreviousEntry();
266             updateCheckState();
267         }
268         public void update() {
269             setEnabled(navigationHistory.size() > 1);
270             if (isEnabled()) {
271                 setToolTipText(NLS.bind(TeamUIMessages.GenericHistoryView_1, navigationHistory.getPreviousEntry().name));
272             } else {
273                 setToolTipText(TeamUIMessages.GenericHistoryView_2);
274             }
275             actions = null;
276             menuCreator.rebuildMenu();
277             updateCheckState();
278         }
279         
280         private void updateCheckState() {
281             IAction[] actions = getActions();
282             for (int i = 0; i < actions.length; i++) {
283                 IAction action = actions[i];
284                 if (action instanceof NavigationHistoryEntryAction) {
285                     NavigationHistoryEntryAction a = (NavigationHistoryEntryAction) action;
286                     a.update();
287                 }
288             }
289             menuCreator.updateMenuState();
290         }
291         public void dispose() {
292             menuCreator.dispose();
293         }
294         private IAction[] getActions() {
295             if (actions == null)
296                 actions = createActions();
297             return actions;
298         }
299     }
300     
301     class NavigationHistoryEntryAction extends Action {
302
303         private final NavigationHistoryEntry navigationHistoryEntry;
304
305         public NavigationHistoryEntryAction(NavigationHistoryEntry navigationHistoryEntry) {
306             super(navigationHistoryEntry.name);
307             this.navigationHistoryEntry = navigationHistoryEntry;
308         }
309         
310         public void run() {
311             navigationHistory.gotoEntry(navigationHistoryEntry);
312             navigateAction.updateCheckState();
313         }
314         
315         public void update() {
316             setChecked(navigationHistory.getCurrentEntry() == navigationHistoryEntry);
317         }
318         
319     }
320
321     /**
322      * The pagebook control, or <code>null</code> if not initialized.
323      */

324     private PageBook book;
325
326     /**
327      * View actions
328      */

329     private Action refreshAction;
330     private Action linkWithEditorAction;
331     private Action pinAction;
332     private NavigationHistoryAction navigateAction;
333
334     /**
335      * The page container for the default page.
336      */

337     private PageContainer defaultPageContainer;
338
339     /**
340      * The current page container
341      */

342     PageContainer currentPageContainer;
343     
344     /**
345      * The drop target + drop target listener
346      */

347     DropTarget dropTarget;
348     GenericHistoryDropAdapter dropAdapter;
349     
350     NavigationHistory navigationHistory = new NavigationHistory();
351     
352     /**
353      * Keeps track of the last selected element (either by selecting or opening an editor)
354      */

355     private Object JavaDoc lastSelectedElement;
356
357     private IPartListener partListener = new IPartListener() {
358         public void partActivated(IWorkbenchPart part) {
359             if (part instanceof IEditorPart)
360                 editorActivated((IEditorPart) part);
361         }
362
363         public void partBroughtToTop(IWorkbenchPart part) {
364             if (part == GenericHistoryView.this)
365                 editorActivated(getViewSite().getPage().getActiveEditor());
366         }
367
368         public void partOpened(IWorkbenchPart part) {
369             if (part == GenericHistoryView.this)
370                 editorActivated(getViewSite().getPage().getActiveEditor());
371         }
372
373         public void partClosed(IWorkbenchPart part) {
374         }
375
376         public void partDeactivated(IWorkbenchPart part) {
377         }
378     };
379
380     private IPartListener2 partListener2 = new IPartListener2() {
381         public void partActivated(IWorkbenchPartReference ref) {
382         }
383
384         public void partBroughtToTop(IWorkbenchPartReference ref) {
385         }
386
387         public void partClosed(IWorkbenchPartReference ref) {
388         }
389
390         public void partDeactivated(IWorkbenchPartReference ref) {
391         }
392
393         public void partOpened(IWorkbenchPartReference ref) {
394         }
395
396         public void partHidden(IWorkbenchPartReference ref) {
397         }
398
399         public void partVisible(IWorkbenchPartReference ref) {
400             if (ref.getPart(true) == GenericHistoryView.this)
401                 editorActivated(getViewSite().getPage().getActiveEditor());
402         }
403
404         public void partInputChanged(IWorkbenchPartReference ref) {
405         }
406     };
407
408     private ISelectionListener selectionListener = new ISelectionListener() {
409
410         public void selectionChanged(IWorkbenchPart part, ISelection selection) {
411
412             if (selection instanceof IStructuredSelection) {
413                 IStructuredSelection structSelection = (IStructuredSelection) selection;
414                 //Always take the first element - this is not intended to work with multiple selection
415
//Also, hang on to this selection for future use in case the history view is not visible
416
lastSelectedElement = structSelection.getFirstElement();
417                 
418                 if (!isLinkingEnabled() || !checkIfPageIsVisible()) {
419                     return;
420                 }
421                 
422                 if (lastSelectedElement != null){
423                     Object JavaDoc resource;
424                     if (lastSelectedElement instanceof SyncInfoModelElement) {
425                         SyncInfoModelElement syncInfoModelElement = (SyncInfoModelElement) lastSelectedElement;
426                         resource = syncInfoModelElement.getSyncInfo().getLocal();
427                     } else {
428                         resource = Utils.getAdapter(lastSelectedElement, IResource.class);
429                     }
430                     if (resource != null)
431                         showHistory((IResource) resource);
432                     else
433                         showHistory(lastSelectedElement);
434                     //reset lastSelectedElement
435
lastSelectedElement = null;
436                 }
437             }
438         }
439
440     };
441
442     private boolean linkingEnabled;
443
444     private boolean viewPinned;
445
446     /**
447      * Refreshes the global actions for the active page.
448      */

449     void refreshGlobalActionHandlers() {
450         // Clear old actions.
451
IActionBars bars = getViewSite().getActionBars();
452         bars.clearGlobalActionHandlers();
453
454         // Set new actions.
455
Map newActionHandlers = currentPageContainer.getSubBars().getGlobalActionHandlers();
456         if (newActionHandlers != null) {
457             Set keys = newActionHandlers.entrySet();
458             Iterator iter = keys.iterator();
459             while (iter.hasNext()) {
460                 Map.Entry entry = (Map.Entry) iter.next();
461                 bars.setGlobalActionHandler((String JavaDoc) entry.getKey(), (IAction) entry.getValue());
462             }
463         }
464         
465         //add refresh action handler from history view
466
bars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
467         
468     }
469
470     public void createPartControl(Composite parent) {
471         // Create the page book.
472
book = new PageBook(parent, SWT.NONE);
473
474         this.linkingEnabled = TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IFileHistoryConstants.PREF_GENERIC_HISTORYVIEW_EDITOR_LINKING);
475
476         // Create the default page rec.
477
defaultPageContainer = createDefaultPage(book);
478         
479         //Contribute toolbars
480
configureToolbars(getViewSite().getActionBars());
481
482         //add global action handler
483
getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction);
484         
485         //initialize the drag and drop
486
initDragAndDrop();
487
488         // Show the default page
489
showPageRec(defaultPageContainer);
490
491         // add listener for editor page activation - this is to support editor
492
// linking
493
getSite().getPage().addPartListener(partListener);
494         getSite().getPage().addPartListener(partListener2);
495
496         // add listener for selections
497
getSite().getPage().addPostSelectionListener(selectionListener);
498     }
499
500     private void configureToolbars(IActionBars actionBars) {
501
502         pinAction = new Action(TeamUIMessages.GenericHistoryView_PinCurrentHistory, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_PINNED)) {
503             public void run() {
504                 if (isChecked()) {
505                     //uncheck editor linking
506
linkWithEditorAction.setChecked(false);
507                     setLinkingEnabled(false);
508                 }
509                 setViewPinned(isChecked());
510             }
511         };
512         pinAction.setChecked(isViewPinned());
513         pinAction.setToolTipText(TeamUIMessages.GenericHistoryView_0);
514
515         refreshAction = new Action(TeamUIMessages.GenericHistoryView_Refresh, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_REFRESH)) {
516             public void run() {
517                 ((IHistoryPage) currentPageContainer.getPage()).refresh();
518             }
519         };
520         refreshAction.setToolTipText(TeamUIMessages.GenericHistoryView_RefreshTooltip);
521         refreshAction.setEnabled(true);
522         
523         
524         linkWithEditorAction = new Action(TeamUIMessages.GenericHistoryView_LinkWithEditor, TeamUIPlugin.getImageDescriptor(ITeamUIImages.IMG_LINK_WITH)) {
525             public void run() {
526                 if (isChecked()) {
527                     // uncheck pinned
528
pinAction.setChecked(false);
529                     setViewPinned(false);
530                 }
531                 setLinkingEnabled(isViewPinned() ? false : isChecked());
532             }
533         };
534         linkWithEditorAction.setChecked(isLinkingEnabled());
535         linkWithEditorAction.setToolTipText(TeamUIMessages.GenericHistoryView_LinkWithTooltip);
536         
537         navigateAction = new NavigationHistoryAction();
538         Utils.initAction(navigateAction, "action.previousHistory."); //$NON-NLS-1$
539

540         //Create the local tool bar
541
IToolBarManager tbm = actionBars.getToolBarManager();
542         tbm.add(new Separator(HISTORY_VIEW_GROUP));
543         tbm.appendToGroup(HISTORY_VIEW_GROUP, refreshAction);
544         tbm.appendToGroup(HISTORY_VIEW_GROUP, linkWithEditorAction);
545         tbm.appendToGroup(HISTORY_VIEW_GROUP, pinAction);
546         tbm.add(new Separator(NAVIGATION_GROUP));
547         tbm.appendToGroup(NAVIGATION_GROUP, navigateAction);
548         tbm.update(false);
549     }
550
551     boolean isLinkingEnabled() {
552         return linkingEnabled;
553     }
554
555     /**
556      * Enabled linking to the active editor
557      * @param enabled flag indicating whether linking is enabled
558      */

559     public void setLinkingEnabled(boolean enabled) {
560         this.linkingEnabled = enabled;
561
562         // remember the last setting in the dialog settings
563
TeamUIPlugin.getPlugin().getPreferenceStore().setValue(IFileHistoryConstants.PREF_GENERIC_HISTORYVIEW_EDITOR_LINKING, enabled);
564
565         // if turning linking on, update the selection to correspond to the active editor
566
if (enabled) {
567             editorActivated(getSite().getPage().getActiveEditor());
568         }
569     }
570
571     /**
572      * Sets the current view pinned
573      * @param b
574      */

575     void setViewPinned(boolean pinned) {
576         this.viewPinned = pinned;
577     }
578
579     /**
580      * Adds drag and drop support to the history view.
581      */

582     void initDragAndDrop() {
583         int ops = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
584         Transfer[] transfers = new Transfer[] {ResourceTransfer.getInstance(), PluginTransfer.getInstance()};
585
586         dropTarget = new DropTarget(book, ops);
587         dropTarget.setTransfer(transfers);
588         dropAdapter = new GenericHistoryDropAdapter(this);
589         dropTarget.addDropListener(dropAdapter);
590     }
591     
592     public void setFocus() {
593         if (isLinkingEnabled()){
594             if (lastSelectedElement != null){
595                 if (lastSelectedElement instanceof IEditorPart){
596                     editorActivated((IEditorPart) lastSelectedElement);
597                 } else {
598                     Object JavaDoc resource = Utils.getAdapter(lastSelectedElement, IResource.class);
599                     if (resource != null)
600                         showHistoryPageFor((IResource) resource, false, false, null);
601                     else
602                         showHistoryPageFor(lastSelectedElement, false, false, null);
603                 }
604                 //reset lastSelectedElement to null to prevent updating history view if it just gets focus
605
lastSelectedElement = null;
606             }
607         }
608         
609         if (currentPageContainer.page instanceof IPage){
610             ((IPage) currentPageContainer.page).setFocus();
611         }
612     }
613
614     /**
615      * Shows page contained in the given page record in this view. The page record must
616      * be one from this pagebook view.
617      * <p>
618      * The <code>PageBookView</code> implementation of this method asks the
619      * pagebook control to show the given page's control, and records that the
620      * given page is now current. Subclasses may extend.
621      * </p>
622      *
623      * @param pageContainer the page record containing the page to show
624      */

625     protected void showPageRec(PageContainer pageContainer) {
626         // If already showing do nothing
627
if (currentPageContainer == pageContainer) {
628             addNavigationHistoryEntry();
629             return;
630         }
631
632         // Hide old page.
633
if (currentPageContainer != null) {
634             currentPageContainer.getSubBars().deactivate();
635             //give the current page a chance to dispose
636
((IHistoryPage)currentPageContainer.getPage()).removePropertyChangeListener(this);
637             currentPageContainer.getPage().dispose();
638             currentPageContainer.getSubBars().dispose();
639         }
640         // Show new page.
641
currentPageContainer = pageContainer;
642     
643         Control pageControl = currentPageContainer.getPage().getControl();
644         if (pageControl != null && !pageControl.isDisposed()) {
645             // Verify that the page control is not disposed
646
// If we are closing, it may have already been disposed
647
book.showPage(pageControl);
648             ((IHistoryPage)currentPageContainer.getPage()).addPropertyChangeListener(this);
649             currentPageContainer.getSubBars().activate();
650             refreshGlobalActionHandlers();
651             // Update action bars.
652
getViewSite().getActionBars().updateActionBars();
653             addNavigationHistoryEntry();
654         }
655     }
656
657     private void addNavigationHistoryEntry() {
658         if (currentPageContainer != null) {
659             Object JavaDoc input = ((IHistoryPage)currentPageContainer.getPage()).getInput();
660             if (input != null)
661                 navigationHistory.addEntry(input, ((IHistoryPage)currentPageContainer.getPage()).getName(), currentPageContainer.getSource());
662         }
663     }
664
665     /**
666      * Initializes the given page with a page site.
667      * <p>
668      * Subclasses should call this method after
669      * the page is created but before creating its
670      * controls.
671      * </p>
672      * <p>
673      * Subclasses may override
674      * </p>
675      * @param page The page to initialize
676      */

677     protected PageSite initPage(IPageBookViewPage page) {
678         try {
679             PageSite site = new PageSite(getViewSite());
680             page.init(site);
681             return site;
682         } catch (PartInitException e) {
683             TeamUIPlugin.log(e);
684         }
685         return null;
686     }
687     
688     public IHistoryPage showHistoryFor(Object JavaDoc object, boolean force) {
689         return showHistoryPageFor(object, true, force, null);
690     }
691     
692     public IHistoryPage showHistoryPageFor(Object JavaDoc object, boolean refresh, boolean force, IHistoryPageSource pageSource) {
693         
694         // Check to see if history view is visible - if it's not, don't bother
695
// going to the trouble of fetching the history
696
if (!this.getSite().getPage().isPartVisible(this))
697             return null;
698         
699         // Ensure that there is a page source available
700
pageSource = getPageSourceFor(object, pageSource);
701         if (pageSource == null || !pageSource.canShowHistoryFor(object))
702             return null;
703         
704         // Check to see if the object is already being displayed in another page
705
IHistoryPage existingPage = checkForExistingPage(object, refresh, force, pageSource);
706         if (existingPage != null){
707             return existingPage;
708         }
709         
710         // Now check to see if this view is pinned
711
if (isViewPinned() && !force) {
712             return handlePinnedView(object, refresh, pageSource);
713         }
714         
715         // If a current page exists, see if it can handle the dropped item.
716
// Otherwise, create a new page
717
PageContainer tempPageContainer = null;
718         if (currentPageContainer!= null && currentPageContainer.canShow(object, pageSource)) {
719             tempPageContainer = currentPageContainer;
720         } else {
721             tempPageContainer = createPage(pageSource, object);
722         }
723         
724         // Set the new page to the current page for the view
725
IHistoryPage historyPage = ((IHistoryPage)tempPageContainer.getPage());
726         historyPage.setInput(object);
727         ((HistoryPage)historyPage).setHistoryView(this);
728         setContentDescription(historyPage.getName());
729         showPageRec(tempPageContainer);
730         return historyPage;
731     }
732
733     private IHistoryPageSource getPageSourceFor(Object JavaDoc object, IHistoryPageSource pageSource) {
734         if (object == null || pageSource != null)
735             return pageSource;
736         IResource resource = Utils.getResource(object);
737         if (resource == null) {
738             return (IHistoryPageSource) Utils.getAdapter(object, IHistoryPageSource.class);
739         } else {
740             if (resource.getProject() == null)
741                 return null;
742             //check to see if resource is managed
743
RepositoryProvider teamProvider = RepositoryProvider.getProvider(resource.getProject());
744             if (teamProvider == null){
745                 // couldn't find a repository provider; try showing it in a local page
746
return LocalHistoryPageSource.getInstance();
747             } else {
748                 IFileHistoryProvider fileHistory = teamProvider.getFileHistoryProvider();
749                 
750                 if (fileHistory != null) {
751                     IHistoryPageSource source = (IHistoryPageSource)Utils.getAdapter(fileHistory, IHistoryPageSource.class,true);
752                     if (source != null)
753                         return source;
754                 }
755                 return (IHistoryPageSource)Utils.getAdapter(teamProvider, IHistoryPageSource.class,true);
756             }
757         }
758     }
759
760     private IHistoryPage handlePinnedView(Object JavaDoc object, boolean refresh, IHistoryPageSource source) {
761         try {
762             // Check to see if an unpinned version of the history view exists
763
GenericHistoryView historyView = findUnpinnedHistoryView();
764             if (historyView != null){
765                 getSite().getPage().activate(historyView);
766                 return historyView.showHistoryPageFor(object, refresh, true, source);
767             }
768             // Otherwise, open another instance of the view
769
String JavaDoc id = VIEW_ID + System.currentTimeMillis();
770             IViewPart view = getSite().getPage().showView(VIEW_ID, id, IWorkbenchPage.VIEW_CREATE);
771             getSite().getPage().activate(view);
772             if (view instanceof GenericHistoryView)
773                 return ((GenericHistoryView) view).showHistoryPageFor(object, refresh, true, source);
774     
775         } catch (PartInitException e) {
776         }
777         return null;
778     }
779
780     private IHistoryPage checkForExistingPage(Object JavaDoc object, boolean refresh, boolean thisViewOnly, IHistoryPageSource pageSource) {
781         //first check to see if the main history view contains the current resource
782
IHistoryPage tempPage = checkForExistingPage(object, refresh, pageSource);
783         if (tempPage != null || thisViewOnly)
784             return tempPage;
785         tempPage = searchHistoryViewsForObject(object, refresh, pageSource);
786         if (tempPage != null)
787             getSite().getPage().bringToTop((IWorkbenchPart)tempPage.getHistoryView());
788         return tempPage;
789     }
790     
791     private IHistoryPage checkForExistingPage(Object JavaDoc object, boolean refresh, IHistoryPageSource pageSource) {
792         //first check to see if the main history view contains the current resource
793
if (currentPageContainer != null) {
794             if (currentPageContainer.matches(object, pageSource)){
795                 //current page contains object, so just refresh it
796
IHistoryPage tempPage =((IHistoryPage)currentPageContainer.getPage());
797                 if (refresh)
798                     tempPage.refresh();
799                 
800                 return tempPage;
801             }
802         }
803         return null;
804     }
805
806     private IHistoryPage searchHistoryViewsForObject(Object JavaDoc object, boolean refresh, IHistoryPageSource pageSource) {
807         IWorkbenchPage page = getSite().getPage();
808         IViewReference[] historyViews = page.getViewReferences();
809         for (int i = 0; i < historyViews.length; i++) {
810             if (historyViews[i].getId().equals(VIEW_ID)){
811                 IViewPart historyView = historyViews[i].getView(true);
812                 if (historyView instanceof GenericHistoryView) {
813                     GenericHistoryView ghv = (GenericHistoryView)historyView;
814                     IHistoryPage historyPage = ghv.checkForExistingPage(object, refresh, pageSource);
815                     if (historyPage != null) {
816                         return historyPage;
817                     }
818                 }
819             }
820         }
821         return null;
822     }
823     
824     public GenericHistoryView findUnpinnedHistoryView(){
825         IWorkbenchPage page = getSite().getPage();
826         IViewReference[] historyViews = page.getViewReferences();
827         for (int i = 0; i < historyViews.length; i++) {
828             if (historyViews[i].getId().equals(VIEW_ID)){
829                 IViewPart historyView = historyViews[i].getView(false);
830                 if (!((GenericHistoryView)historyView).isViewPinned())
831                     return (GenericHistoryView) historyView;
832             }
833         }
834         return null;
835     }
836     
837     boolean isViewPinned() {
838         return viewPinned;
839     }
840
841     private PageContainer createPage(IHistoryPageSource source, Object JavaDoc object) {
842         Page page = source.createPage(object);
843         PageSite site = initPage(page);
844         ((IHistoryPage) page).setSite(new WorkbenchHistoryPageSite(this, page.getSite()));
845         page.createControl(book);
846         PageContainer container = new PageContainer(page, source);
847         container.setSubBars((SubActionBars) site.getActionBars());
848         return container;
849     }
850
851     protected PageContainer createDefaultPage(PageBook book) {
852         GenericHistoryViewDefaultPage page = new GenericHistoryViewDefaultPage();
853         PageSite site = initPage(page);
854         page.createControl(book);
855         PageContainer container = new PageContainer(page, null);
856         container.setSubBars((SubActionBars) site.getActionBars());
857         return container;
858     }
859
860     /**
861      * An editor has been activated. Fetch the history if the file is shared and the history view
862      * is visible in the current page.
863      *
864      * @param editor the active editor
865      */

866     protected void editorActivated(IEditorPart editor) {
867         //If this history view is not visible, keep track of this editor
868
//for future use
869
if (editor != null && !checkIfPageIsVisible())
870             lastSelectedElement = editor;
871         
872         //Only fetch contents if the view is shown in the current page.
873
if (editor == null || !isLinkingEnabled() || !checkIfPageIsVisible() || isViewPinned()) {
874             return;
875         }
876         IEditorInput input = editor.getEditorInput();
877
878         IFile file = ResourceUtil.getFile(input);
879         if (file != null) {
880             showHistory(file); /* don't fetch if already cached */
881         } else {
882             //see if it adapts to an IHistoryPageSource
883
Object JavaDoc pageSource = Utils.getAdapter(input, IHistoryPageSource.class);
884             if (pageSource != null)
885                 showHistory(input);
886         }
887     }
888
889     private boolean checkIfPageIsVisible() {
890         return getViewSite().getPage().isPartVisible(this);
891     }
892
893     public void dispose() {
894         super.dispose();
895         //Remove the drop listener
896
if (dropTarget != null && !dropTarget.isDisposed())
897             dropTarget.removeDropListener(dropAdapter);
898         //Call dispose on current and default pages
899
((IHistoryPage)currentPageContainer.getPage()).removePropertyChangeListener(this);
900         currentPageContainer.getPage().dispose();
901         defaultPageContainer.getPage().dispose();
902         currentPageContainer = null;
903         defaultPageContainer = null;
904         //Remove the part listeners
905
getSite().getPage().removePartListener(partListener);
906         getSite().getPage().removePartListener(partListener2);
907         //Remove the selection listener
908
getSite().getPage().removePostSelectionListener(selectionListener);
909         navigateAction.dispose();
910     }
911
912     public IHistoryPage showHistoryFor(Object JavaDoc object) {
913          return showHistoryFor(object, false);
914     }
915
916     public IHistoryPage getHistoryPage() {
917         if (currentPageContainer != null &&
918             currentPageContainer.getPage() != null)
919             return (IHistoryPage) currentPageContainer.getPage();
920         
921         return (IHistoryPage) defaultPageContainer.getPage();
922     }
923
924     /* (non-Javadoc)
925      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
926      */

927     public void propertyChange(PropertyChangeEvent event) {
928         if (event.getSource() == currentPageContainer.getPage()) {
929             if (event.getProperty().equals(IHistoryPage.P_NAME)) {
930                 Display.getDefault().asyncExec(new Runnable JavaDoc() {
931                     public void run() {
932                         IHistoryPage historyPage = (IHistoryPage) currentPageContainer.getPage();
933                         setContentDescription(historyPage.getName());
934                         navigationHistory.updateName(historyPage, currentPageContainer.getSource());
935                     }
936                 });
937             } else if (event.getProperty().equals(IHistoryPage.P_DESCRIPTION)) {
938                 // We don't show the description
939
}
940         }
941     }
942
943     public IHistoryView findAppropriateHistoryViewFor(Object JavaDoc input,
944             IHistoryPageSource pageSource) {
945         // First, check to see if the input and pageSource of this view match the input
946
IHistoryPage page = searchHistoryViewsForObject(input, false, pageSource);
947         if (page != null) {
948             return page.getHistoryView();
949         }
950         return findUnpinnedHistoryView();
951     }
952     
953     private void showHistory(Object JavaDoc object) {
954         // Only show the history if the input differs
955
// (i.e. don't do the change if the input is the same but the page source differs; bug 167648)
956
if (getHistoryPage().getInput() != object)
957             showHistoryPageFor(object, false, false, null);
958     }
959
960     public boolean show(ShowInContext context) {
961         ISelection selection = context.getSelection();
962         if (selection instanceof IStructuredSelection) {
963             IStructuredSelection ss = (IStructuredSelection) selection;
964             if (ss.size() == 1) {
965                 // If we can show the selection, return.
966
// Otherwise, fall through and attempt to show the input
967
if ((showHistoryFor(ss.getFirstElement()) != null))
968                     return true;
969             }
970         }
971         if (context.getInput() != null) {
972             return (showHistoryFor(context.getInput()) != null);
973         }
974         return false;
975     }
976 }
977
Popular Tags