KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > SearchViewSiteAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.search;
12
13 import org.eclipse.core.resources.IMarker;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.PlatformObject;
16 import org.eclipse.jdt.core.IJavaElement;
17 import org.eclipse.jdt.core.JavaCore;
18 import org.eclipse.jdt.internal.ui.JavaPlugin;
19 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
20 import org.eclipse.jdt.internal.ui.util.SelectionUtil;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.util.Assert;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.ISelectionProvider;
26 import org.eclipse.jface.viewers.SelectionChangedEvent;
27 import org.eclipse.jface.viewers.StructuredSelection;
28 import org.eclipse.search.ui.ISearchResultViewEntry;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IKeyBindingService;
31 import org.eclipse.ui.IWorkbenchPage;
32 import org.eclipse.ui.IWorkbenchPartSite;
33 import org.eclipse.ui.IWorkbenchSite;
34 import org.eclipse.ui.IWorkbenchWindow;
35
36 /**
37  * This class adapts a Search view site.
38  * It converts selection of Search view entries to
39  * be a selection of Java elements.
40  *
41  * @since 2.0
42  */

43 class SearchViewSiteAdapter extends PlatformObject implements IWorkbenchPartSite {
44     
45     private ISelectionProvider fProvider;
46     private IWorkbenchSite fSite;
47     
48     private ISelectionChangedListener fListener;
49
50     public SearchViewSiteAdapter(IWorkbenchSite site){
51         fSite= site;
52         setSelectionProvider(site.getSelectionProvider());
53     }
54     
55     public IWorkbenchPage getPage() {
56         return fSite.getPage();
57     }
58
59     public ISelectionProvider getSelectionProvider() {
60         return fProvider;
61     }
62
63     public Shell getShell() {
64         return JavaPlugin.getActiveWorkbenchShell();
65     }
66
67     public IWorkbenchWindow getWorkbenchWindow() {
68         return fSite.getWorkbenchWindow();
69     }
70     
71     public void setSelectionProvider(final ISelectionProvider provider) {
72         Assert.isNotNull(provider);
73         fProvider=
74             new ISelectionProvider() {
75                 public void addSelectionChangedListener(final ISelectionChangedListener listener) {
76                     fListener=
77                         new ISelectionChangedListener() {
78                             public void selectionChanged(SelectionChangedEvent event) {
79                                 listener.selectionChanged(new SelectionChangedEvent(fProvider, convertSelection(event.getSelection())));
80                             }
81                         };
82                     provider.addSelectionChangedListener(fListener);
83                 }
84                 public ISelection getSelection() {
85                     return convertSelection(provider.getSelection());
86                 }
87                 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
88                     provider.removeSelectionChangedListener(fListener);
89                 }
90                 public void setSelection(ISelection selection) {
91                 }
92             };
93     }
94
95     private ISelection convertSelection(ISelection selection) {
96         Object JavaDoc element= SelectionUtil.getSingleElement(selection);
97         if (element instanceof ISearchResultViewEntry) {
98             IMarker marker= ((ISearchResultViewEntry)element).getSelectedMarker();
99             if (marker != null && marker.exists())
100                 try {
101                     IJavaElement je= JavaCore.create((String JavaDoc)marker.getAttribute(IJavaSearchUIConstants.ATT_JE_HANDLE_ID));
102                     if (je != null)
103                         return new StructuredSelection(je);
104                 } catch (CoreException ex) {
105                     ExceptionHandler.log(ex, SearchMessages.getString("Search.Error.createJavaElement.message")); //$NON-NLS-1$
106
}
107         }
108         return StructuredSelection.EMPTY;
109     }
110     
111     // --------- only empty stubs below ---------
112

113     /*
114      * @see org.eclipse.ui.IWorkbenchPartSite#getId()
115      */

116     public String JavaDoc getId() {
117         return null;
118     }
119
120     /*
121      * @see org.eclipse.ui.IWorkbenchPartSite#getKeyBindingService()
122      */

123     public IKeyBindingService getKeyBindingService() {
124         return null;
125     }
126
127     /*
128      * @see org.eclipse.ui.IWorkbenchPartSite#getPluginId()
129      */

130     public String JavaDoc getPluginId() {
131         return null;
132     }
133
134     /*
135      * @see org.eclipse.ui.IWorkbenchPartSite#getRegisteredName()
136      */

137     public String JavaDoc getRegisteredName() {
138         return null;
139     }
140
141     /*
142      * @see org.eclipse.ui.IWorkbenchPartSite#registerContextMenu(MenuManager, ISelectionProvider)
143      */

144     public void registerContextMenu(MenuManager menuManager, ISelectionProvider selectionProvider) {
145     }
146
147     /*
148      * @see org.eclipse.ui.IWorkbenchPartSite#registerContextMenu(String, MenuManager, ISelectionProvider)
149      */

150     public void registerContextMenu(String JavaDoc menuId, MenuManager menuManager, ISelectionProvider selectionProvider) {
151     }
152 }
153
Popular Tags