KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search2 > internal > ui > SearchViewManager


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
12 package org.eclipse.search2.internal.ui;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.LinkedList JavaDoc;
16
17 import org.eclipse.ui.IViewReference;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.WorkbenchException;
23
24 import org.eclipse.search.ui.IQueryListener;
25 import org.eclipse.search.ui.ISearchQuery;
26 import org.eclipse.search.ui.ISearchResultViewPart;
27 import org.eclipse.search.ui.NewSearchUI;
28
29 import org.eclipse.search.internal.ui.SearchPlugin;
30 import org.eclipse.search.internal.ui.util.ExceptionHandler;
31
32 /**
33  *
34  */

35 public class SearchViewManager {
36     
37     private IQueryListener fNewQueryListener;
38     private int fViewCount= 0;
39     
40     private LinkedList JavaDoc fLRUSearchViews;
41     
42     
43     public SearchViewManager(QueryManager queryManager) {
44         fNewQueryListener= new IQueryListener() {
45
46             public void queryAdded(ISearchQuery query) {
47                 showNewSearchQuery(query);
48             }
49
50             public void queryRemoved(ISearchQuery query) {}
51             public void queryStarting(ISearchQuery query) {}
52             public void queryFinished(ISearchQuery query) {}
53             
54         };
55         
56         queryManager.addQueryListener(fNewQueryListener);
57         
58         fLRUSearchViews= new LinkedList JavaDoc();
59         
60     }
61     
62     public void dispose(QueryManager queryManager) {
63         queryManager.removeQueryListener(fNewQueryListener);
64     }
65     
66     
67     protected boolean showNewSearchQuery(ISearchQuery query) {
68         if (!fLRUSearchViews.isEmpty()) {
69             SearchView view= (SearchView) fLRUSearchViews.getFirst();
70             view.showSearchResult(query.getSearchResult());
71             return true;
72         }
73         return false;
74     }
75
76     public ISearchResultViewPart activateSearchView(boolean useForNewSearch) {
77         IWorkbenchPage activePage= SearchPlugin.getActivePage();
78         
79         String JavaDoc defaultPerspectiveId= NewSearchUI.getDefaultPerspectiveId();
80         if (defaultPerspectiveId != null) {
81             IWorkbenchWindow window= activePage.getWorkbenchWindow();
82             if (window != null && window.getShell() != null && !window.getShell().isDisposed()) {
83                 try {
84                     activePage= PlatformUI.getWorkbench().showPerspective(defaultPerspectiveId, window);
85                 } catch (WorkbenchException ex) {
86                     // show view in current perspective
87
}
88             }
89         }
90         if (activePage != null) {
91             try {
92                 ISearchResultViewPart viewPart= findLRUSearchResultView(activePage, useForNewSearch);
93                 String JavaDoc secondaryId= null;
94                 if (viewPart == null) {
95                     if (activePage.findViewReference(NewSearchUI.SEARCH_VIEW_ID) != null) {
96                         secondaryId= String.valueOf(++fViewCount); // avoid a secondary ID because of bug 125315
97
}
98                 } else {
99                     secondaryId= viewPart.getViewSite().getSecondaryId();
100                 }
101                 return (ISearchResultViewPart) activePage.showView(NewSearchUI.SEARCH_VIEW_ID, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
102             } catch (PartInitException ex) {
103                 ExceptionHandler.handle(ex, SearchMessages.Search_Error_openResultView_title, SearchMessages.Search_Error_openResultView_message);
104             }
105         }
106         return null;
107     }
108     
109     public void activateSearchView(ISearchResultViewPart viewPart) {
110         try {
111             IWorkbenchPage activePage= viewPart.getSite().getPage();
112             String JavaDoc secondaryId= viewPart.getViewSite().getSecondaryId();
113             activePage.showView(NewSearchUI.SEARCH_VIEW_ID, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
114         } catch (PartInitException ex) {
115             ExceptionHandler.handle(ex, SearchMessages.Search_Error_openResultView_title, SearchMessages.Search_Error_openResultView_message);
116         }
117     }
118
119     private ISearchResultViewPart findLRUSearchResultView(IWorkbenchPage page, boolean avoidPinnedViews) {
120         boolean viewFoundInPage= false;
121         for (Iterator JavaDoc iter= fLRUSearchViews.iterator(); iter.hasNext();) {
122             SearchView view= (SearchView) iter.next();
123             if (page.equals(view.getSite().getPage())) {
124                 if (!avoidPinnedViews || !view.isPinned()) {
125                     return view;
126                 }
127                 viewFoundInPage= true;
128             }
129         }
130         if (!viewFoundInPage) {
131             // find unresolved views
132
IViewReference[] viewReferences= page.getViewReferences();
133             for (int i= 0; i < viewReferences.length; i++) {
134                 IViewReference curr= viewReferences[i];
135                 if (NewSearchUI.SEARCH_VIEW_ID.equals(curr.getId()) && page.equals(curr.getPage())) {
136                     SearchView view= (SearchView) curr.getView(true);
137                     if (view != null && (!avoidPinnedViews || !view.isPinned())) {
138                         return view;
139                     }
140                     
141                 }
142             }
143         }
144         return null;
145     }
146     
147     
148     public void searchViewActivated(SearchView view) {
149         fLRUSearchViews.remove(view);
150         fLRUSearchViews.addFirst(view);
151     }
152
153     public void searchViewClosed(SearchView view) {
154         fLRUSearchViews.remove(view);
155     }
156
157     public ISearchResultViewPart getActiveSearchView() {
158         IWorkbenchPage activePage= SearchPlugin.getActivePage();
159         if (activePage != null) {
160             return findLRUSearchResultView(activePage, false);
161         }
162         return null;
163     }
164     
165     
166     
167 }
168
Popular Tags