Added
Link Here
|
1 |
/*********************************************************************** |
2 |
* Copyright (c) 2008 Anyware Technologies |
3 |
* |
4 |
* All rights reserved. This program and the accompanying materials |
5 |
* are made available under the terms of the Eclipse Public License v1.0 |
6 |
* which accompanies this distribution, and is available at |
7 |
* http://www.eclipse.org/legal/epl-v10.html |
8 |
* |
9 |
* Contributors: |
10 |
* Anyware Technologies - initial API and implementation |
11 |
* |
12 |
* $Id$ |
13 |
**********************************************************************/ |
14 |
|
15 |
package org.eclipse.ui.navigator; |
16 |
|
17 |
import org.eclipse.jface.viewers.DoubleClickEvent; |
18 |
import org.eclipse.jface.viewers.IDoubleClickListener; |
19 |
import org.eclipse.jface.viewers.ISelection; |
20 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
21 |
import org.eclipse.jface.viewers.IStructuredSelection; |
22 |
import org.eclipse.jface.viewers.TreeViewer; |
23 |
import org.eclipse.jface.viewers.ViewerFilter; |
24 |
import org.eclipse.swt.SWT; |
25 |
import org.eclipse.swt.widgets.Composite; |
26 |
import org.eclipse.swt.widgets.Control; |
27 |
import org.eclipse.ui.IActionBars; |
28 |
import org.eclipse.ui.IMemento; |
29 |
import org.eclipse.ui.IWorkbenchSite; |
30 |
import org.eclipse.ui.actions.ActionGroup; |
31 |
import org.eclipse.ui.internal.navigator.CommonNavigatorActionGroup; |
32 |
import org.eclipse.ui.internal.navigator.CommonNavigatorManager; |
33 |
import org.eclipse.ui.internal.navigator.CommonNavigatorMessages; |
34 |
import org.eclipse.ui.part.Page; |
35 |
import org.eclipse.ui.views.contentoutline.IContentOutlinePage; |
36 |
|
37 |
/** |
38 |
* <p> |
39 |
* This class provides an implementation of Common Navigator hosted in an |
40 |
* IContentOutlinePage. |
41 |
* |
42 |
* </p> |
43 |
* Clients that wish to extend the view menu provided via the |
44 |
* <b>org.eclipse.ui.popupMenu</b>s extension may specify the the |
45 |
* <i>popupMenuId</i> specified by <b>org.eclipse.ui.navigator.viewer</b> (or a |
46 |
* nested <b>popupMenu</b> element) of their target viewer as their target menu |
47 |
* id. |
48 |
* |
49 |
* @since 3.5 |
50 |
*/ |
51 |
public abstract class CommonNavigatorOutline extends Page implements |
52 |
IContentOutlinePage, ICommonNavigator { |
53 |
|
54 |
private static final Class INAVIGATOR_CONTENT_SERVICE = INavigatorContentService.class; |
55 |
private static final Class COMMON_VIEWER_CLASS = CommonViewer.class; |
56 |
|
57 |
private CommonViewer commonViewer; |
58 |
|
59 |
private CommonNavigatorManager commonManager; |
60 |
|
61 |
private ActionGroup commonActionGroup; |
62 |
|
63 |
private IMemento memento; |
64 |
|
65 |
/** |
66 |
* |
67 |
*/ |
68 |
public CommonNavigatorOutline() { |
69 |
super(); |
70 |
} |
71 |
|
72 |
/** |
73 |
* <p> |
74 |
* Returns The id used to initialize the Common Viewer from extensions. It |
75 |
* is recommended to use the same id as the editor providing this Outline. |
76 |
* </p> |
77 |
* |
78 |
* @return The Id of the Common Viewer |
79 |
*/ |
80 |
protected abstract String getCommonViewerId(); |
81 |
|
82 |
/** |
83 |
* <p> |
84 |
* Constructs and returns an instance of {@link CommonViewer}. The ID from |
85 |
* <code>getCommonViewer()</code> will be used to create the viewer. The ID |
86 |
* is important as some extensions indicate they should only be used with a |
87 |
* particular viewer ID. |
88 |
* <p> |
89 |
* |
90 |
* @param aParent |
91 |
* A composite parent to contain the Common Viewer |
92 |
* @return An initialized instance of CommonViewer |
93 |
*/ |
94 |
protected CommonViewer createCommonViewer(Composite aParent) { |
95 |
CommonViewer aViewer = new CommonViewer(getCommonViewerId(), aParent, |
96 |
SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
97 |
initListeners(aViewer); |
98 |
aViewer.getNavigatorContentService().restoreState(memento); |
99 |
return aViewer; |
100 |
} |
101 |
|
102 |
/** |
103 |
* <p> |
104 |
* Adds the listeners to the Common Viewer. |
105 |
* </p> |
106 |
* |
107 |
* @param viewer |
108 |
* The viewer |
109 |
* @since 2.0 |
110 |
*/ |
111 |
protected void initListeners(TreeViewer viewer) { |
112 |
|
113 |
viewer.addDoubleClickListener(new IDoubleClickListener() { |
114 |
|
115 |
public void doubleClick(DoubleClickEvent event) { |
116 |
try { |
117 |
handleDoubleClick(event); |
118 |
} catch (RuntimeException re) { |
119 |
re.printStackTrace(); |
120 |
} |
121 |
} |
122 |
}); |
123 |
} |
124 |
|
125 |
/** |
126 |
* <p> |
127 |
* This method can be overridden to customize the behavior of the double |
128 |
* click. |
129 |
* </p> |
130 |
* |
131 |
* @param anEvent |
132 |
* Supplied by the DoubleClick listener. |
133 |
*/ |
134 |
protected void handleDoubleClick(DoubleClickEvent anEvent) { |
135 |
|
136 |
IStructuredSelection selection = (IStructuredSelection) anEvent |
137 |
.getSelection(); |
138 |
Object element = selection.getFirstElement(); |
139 |
|
140 |
TreeViewer viewer = getCommonViewer(); |
141 |
if (viewer.isExpandable(element)) { |
142 |
viewer.setExpandedState(element, !viewer.getExpandedState(element)); |
143 |
} |
144 |
} |
145 |
|
146 |
/** |
147 |
* <p> |
148 |
* The Common Navigator Manager handles the setup of the Common Navigator |
149 |
* Menu, manages updates to the ActionBars from {@link CommonActionProvider} |
150 |
* extensions as the user's selection changes, and also updates the |
151 |
* status bar based on the current selection. |
152 |
* |
153 |
* @return The Common Navigator Manager class which handles menu population |
154 |
* and ActionBars |
155 |
*/ |
156 |
protected CommonNavigatorManager createCommonManager() { |
157 |
return new CommonNavigatorManager(this, memento); |
158 |
} |
159 |
|
160 |
/** |
161 |
* <p> |
162 |
* The ActionGroup is used to populate the ActionBars of Common Navigator , |
163 |
* and the returned implementation will have an opportunity to fill the |
164 |
* ActionBars of the outline as soon as it is created. ( |
165 |
* {@link ActionGroup#fillActionBars(org.eclipse.ui.IActionBars)}. |
166 |
* </p> |
167 |
* <p> |
168 |
* The default implementation returns an action group which will add the |
169 |
* following actions: |
170 |
* <ul> |
171 |
* <li> |
172 |
* <li> |
173 |
* <p> |
174 |
* Collapse all. Collapses all expanded nodes. |
175 |
* </p> |
176 |
* <li> |
177 |
* <p> |
178 |
* Select Filters. Provides access to the "Select Filters" dialog that |
179 |
* allows users to enable/disable filters and also the Content Extension |
180 |
* activations. |
181 |
* </p> |
182 |
* </ul> |
183 |
* |
184 |
* @return The Action Group to be associated with the Common Navigator View |
185 |
* Part. |
186 |
*/ |
187 |
protected ActionGroup createCommonActionGroup() { |
188 |
return new CommonNavigatorActionGroup(this, commonViewer); |
189 |
} |
190 |
|
191 |
/** |
192 |
* @see org.eclipse.ui.part.Page#createControl(org.eclipse.swt.widgets.Composite) |
193 |
*/ |
194 |
public void createControl(Composite parent) { |
195 |
commonViewer = createCommonViewer(parent); |
196 |
|
197 |
try { |
198 |
commonViewer.getControl().setRedraw(false); |
199 |
|
200 |
INavigatorFilterService filterService = commonViewer |
201 |
.getNavigatorContentService().getFilterService(); |
202 |
ViewerFilter[] visibleFilters = filterService |
203 |
.getVisibleFilters(true); |
204 |
for (int i = 0; i < visibleFilters.length; i++) { |
205 |
commonViewer.addFilter(visibleFilters[i]); |
206 |
} |
207 |
|
208 |
commonViewer.setSorter(new CommonViewerSorter()); |
209 |
|
210 |
/* |
211 |
* make sure input is set after sorters and filters to avoid |
212 |
* unnecessary refreshes |
213 |
*/ |
214 |
commonViewer.setInput(getInitialInput()); |
215 |
|
216 |
getSite().setSelectionProvider(commonViewer); |
217 |
|
218 |
} finally { |
219 |
commonViewer.getControl().setRedraw(true); |
220 |
} |
221 |
|
222 |
/* |
223 |
* Create the CommonNavigatorManager last because information about the |
224 |
* state of the CommonNavigator is required for the initialization of |
225 |
* the CommonNavigatorManager |
226 |
*/ |
227 |
commonManager = createCommonManager(); |
228 |
if (memento != null) { |
229 |
commonViewer.getNavigatorContentService().restoreState(memento); |
230 |
} |
231 |
|
232 |
commonActionGroup = createCommonActionGroup(); |
233 |
commonActionGroup.fillActionBars(getActionBars()); |
234 |
|
235 |
} |
236 |
|
237 |
/** |
238 |
* <p> |
239 |
* Note: This method is for internal use only. Clients should not call this |
240 |
* method. |
241 |
* </p> |
242 |
* <p> |
243 |
* This method will be invoked when the DisposeListener is notified of the |
244 |
* disposal of the Eclipse view part. |
245 |
* </p> |
246 |
* |
247 |
* @see org.eclipse.ui.part.WorkbenchPart#dispose() |
248 |
*/ |
249 |
public void dispose() { |
250 |
if (commonManager != null) { |
251 |
commonManager.dispose(); |
252 |
} |
253 |
if (commonActionGroup != null) { |
254 |
commonActionGroup.dispose(); |
255 |
} |
256 |
super.dispose(); |
257 |
} |
258 |
|
259 |
/** |
260 |
* @see org.eclipse.ui.part.Page#getControl() |
261 |
*/ |
262 |
public Control getControl() { |
263 |
return commonViewer.getControl(); |
264 |
} |
265 |
|
266 |
/** |
267 |
* Returns an object which is an instance of the given class associated with |
268 |
* this object. Returns <code>null</code> if no such object can be found. |
269 |
* |
270 |
* @param adapter |
271 |
* the adapter class to look up |
272 |
* @return a object castable to the given class, or <code>null</code> if |
273 |
* this object does not have an adapter for the given class |
274 |
*/ |
275 |
public Object getAdapter(Class adapter) { |
276 |
if (adapter == COMMON_VIEWER_CLASS) { |
277 |
return getCommonViewer(); |
278 |
} else if (adapter == INAVIGATOR_CONTENT_SERVICE) { |
279 |
return getCommonViewer().getNavigatorContentService(); |
280 |
} |
281 |
return null; |
282 |
} |
283 |
|
284 |
/** |
285 |
* @see org.eclipse.ui.part.Page#setFocus() |
286 |
*/ |
287 |
public void setFocus() { |
288 |
if (commonViewer != null) { |
289 |
commonViewer.getTree().setFocus(); |
290 |
} |
291 |
} |
292 |
|
293 |
/** |
294 |
* <p> |
295 |
* Subclasses should override this method to define the initial input of the |
296 |
* Common Navigator Outline. |
297 |
* </p> |
298 |
* |
299 |
* @return The initial input for the viewer. Defaults to |
300 |
* getSite().getPage().getInput() |
301 |
*/ |
302 |
protected Object getInitialInput() { |
303 |
return getSite().getPage().getInput(); |
304 |
} |
305 |
|
306 |
/** |
307 |
* @see org.eclipse.jface.viewers.ISelectionProvider#addSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) |
308 |
*/ |
309 |
public void addSelectionChangedListener(ISelectionChangedListener listener) { |
310 |
getCommonViewer().addSelectionChangedListener(listener); |
311 |
} |
312 |
|
313 |
/** |
314 |
* @see org.eclipse.jface.viewers.ISelectionProvider#getSelection() |
315 |
*/ |
316 |
public ISelection getSelection() { |
317 |
return getCommonViewer().getSelection(); |
318 |
} |
319 |
|
320 |
/** |
321 |
* @see org.eclipse.jface.viewers.ISelectionProvider#removeSelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) |
322 |
*/ |
323 |
public void removeSelectionChangedListener( |
324 |
ISelectionChangedListener listener) { |
325 |
getCommonViewer().removeSelectionChangedListener(listener); |
326 |
} |
327 |
|
328 |
/** |
329 |
* @see org.eclipse.jface.viewers.ISelectionProvider#setSelection(org.eclipse.jface.viewers.ISelection) |
330 |
*/ |
331 |
public void setSelection(ISelection selection) { |
332 |
getCommonViewer().setSelection(selection); |
333 |
} |
334 |
|
335 |
/** |
336 |
* @see org.eclipse.ui.navigator.ICommonNavigator#getActionBars() |
337 |
*/ |
338 |
public IActionBars getActionBars() { |
339 |
return getSite().getActionBars(); |
340 |
} |
341 |
|
342 |
/** |
343 |
* @see org.eclipse.ui.navigator.ICommonNavigator#getCommonViewer() |
344 |
*/ |
345 |
public CommonViewer getCommonViewer() { |
346 |
return commonViewer; |
347 |
} |
348 |
|
349 |
/** |
350 |
* @see org.eclipse.ui.navigator.ICommonNavigator#getCommonViewerSite() |
351 |
*/ |
352 |
public ICommonViewerSite getCommonViewerSite() { |
353 |
return CommonViewerSiteFactory.createCommonViewerSite( |
354 |
getCommonViewerId(), getSite()); |
355 |
} |
356 |
|
357 |
/** |
358 |
* @return The Navigator Content Service which populates this instance of |
359 |
* Common Navigator |
360 |
*/ |
361 |
public INavigatorContentService getNavigatorContentService() { |
362 |
return getCommonViewer().getNavigatorContentService(); |
363 |
} |
364 |
|
365 |
/** |
366 |
* @see org.eclipse.ui.navigator.ICommonNavigator#getTitle() |
367 |
*/ |
368 |
public String getTitle() { |
369 |
return CommonNavigatorMessages.CommonNavigatorOutline_DefaultTitle; |
370 |
} |
371 |
|
372 |
/** |
373 |
* @see org.eclipse.ui.navigator.ICommonNavigator#getWorkbenchSite() |
374 |
*/ |
375 |
public IWorkbenchSite getWorkbenchSite() { |
376 |
return getSite(); |
377 |
} |
378 |
|
379 |
/** |
380 |
* <p> |
381 |
* Set the selection to the Common Navigator tree, and expand nodes if |
382 |
* necessary. Use caution when invoking this method as it can cause |
383 |
* Navigator Content Extensions to load, thus causing plugin activation. |
384 |
* </p> |
385 |
* |
386 |
* @see org.eclipse.ui.part.ISetSelectionTarget#selectReveal(org.eclipse.jface.viewers.ISelection) |
387 |
*/ |
388 |
public void selectReveal(ISelection selection) { |
389 |
if (commonViewer != null) { |
390 |
if (selection instanceof IStructuredSelection) { |
391 |
Object[] newSelection = ((IStructuredSelection) selection) |
392 |
.toArray(); |
393 |
Object[] expandedElements = commonViewer.getExpandedElements(); |
394 |
Object[] newExpandedElements = new Object[newSelection.length |
395 |
+ expandedElements.length]; |
396 |
System.arraycopy(expandedElements, 0, newExpandedElements, 0, |
397 |
expandedElements.length); |
398 |
System.arraycopy(newSelection, 0, newExpandedElements, |
399 |
expandedElements.length, newSelection.length); |
400 |
commonViewer.setExpandedElements(newExpandedElements); |
401 |
} |
402 |
commonViewer.setSelection(selection, true); |
403 |
} |
404 |
} |
405 |
|
406 |
} |