KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > CommonViewer


1 /*******************************************************************************
2  * Copyright (c) 2003, 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 package org.eclipse.ui.navigator;
12
13 import java.util.Arrays JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.viewers.DecoratingLabelProvider;
18 import org.eclipse.jface.viewers.IBaseLabelProvider;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.jface.viewers.ViewerSorter;
25 import org.eclipse.swt.dnd.DND;
26 import org.eclipse.swt.events.DisposeEvent;
27 import org.eclipse.swt.events.SelectionEvent;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Widget;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.internal.navigator.ContributorTrackingSet;
32 import org.eclipse.ui.internal.navigator.NavigatorContentService;
33 import org.eclipse.ui.internal.navigator.NavigatorPipelineService;
34
35 /**
36  *
37  * Provides the Tree Viewer for the Common Navigator. Content and labels are
38  * provided by an instance of {@link INavigatorContentService}  which uses
39  * the ID supplied in the constructor
40  * {@link CommonViewer#CommonViewer(String, Composite, int)} or through
41  * {@link NavigatorContentServiceFactory#createContentService(String, org.eclipse.jface.viewers.StructuredViewer)}.
42  *
43  * <p>
44  * Clients may extend this class.
45  * </p>
46  *
47  * <p>
48  * Note that as of 3.2.1 and 3.3, the common viewer caches its selection.
49  * Clients must not set the selection of the viewer's tree control directly.
50  * </p>
51  *
52  * @since 3.2
53  */

54 public class CommonViewer extends TreeViewer {
55
56     private final NavigatorContentService contentService;
57
58     private ISelection cachedSelection;
59     
60     /**
61      * <p>
62      * Constructs the Tree Viewer for the Common Navigator and the corresponding
63      * NavigatorContentService. The NavigatorContentService will provide the
64      * Content Provider and Label Provider -- these need not be supplied by
65      * clients.
66      * <p>
67      * For the valid bits to supply in the style mask (aStyle), see
68      * documentation provided by {@link TreeViewer}.
69      * </p>
70      *
71      * @param aViewerId
72      * An id tied to the extensions that is used to focus specific
73      * content to a particular instance of the Common Navigator
74      * @param aParent
75      * A Composite parent to contain the actual SWT widget
76      * @param aStyle
77      * A style mask that will be used to create the TreeViewer
78      * Composite.
79      */

80     public CommonViewer(String JavaDoc aViewerId, Composite aParent, int aStyle) {
81         super(aParent, aStyle);
82         contentService = new NavigatorContentService(aViewerId, this);
83         init();
84     }
85
86     /**
87      * <p>
88      * Initializes the content provider, label provider, and drag and drop
89      * support. Should not be called by clients -- this method is invoked when
90      * the constructor is invoked.
91      * </p>
92      */

93     protected void init() {
94         setUseHashlookup(true);
95         setContentProvider(contentService.createCommonContentProvider());
96         DecoratingLabelProvider decoratingProvider = new DecoratingLabelProvider(
97                 contentService.createCommonLabelProvider(), PlatformUI
98                         .getWorkbench().getDecoratorManager()
99                         .getLabelDecorator());
100         setLabelProvider(decoratingProvider);
101         initDragAndDrop();
102
103     }
104
105     protected void removeWithoutRefresh(Object JavaDoc[] elements) {
106         super.remove(elements);
107     }
108
109     /**
110      * <p>
111      * Adds DND support to the Navigator. Uses hooks into the extensible
112      * framework for DND.
113      * </p>
114      * <p>
115      * By default, the following Transfer types are supported:
116      * <ul>
117      * <li>LocalSelectionTransfer.getInstance(),
118      * <li>PluginTransfer.getInstance()
119      * </ul>
120      * </p>
121      *
122      * @see CommonDragAdapter
123      * @see CommonDropAdapter
124      */

125     protected void initDragAndDrop() {
126
127         /* Handle Drag and Drop */
128         int operations = DND.DROP_COPY | DND.DROP_MOVE;
129
130         CommonDragAdapter dragAdapter = new CommonDragAdapter(contentService,
131                 this);
132         addDragSupport(operations, dragAdapter.getSupportedDragTransfers(),
133                 dragAdapter);
134
135         CommonDropAdapter dropAdapter = new CommonDropAdapter(contentService,
136                 this);
137         addDropSupport(operations, dropAdapter.getSupportedDropTransfers(),
138                 dropAdapter);
139
140     }
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.eclipse.jface.viewers.AbstractTreeViewer#createTreeItem(org.eclipse.swt.widgets.Widget,
146      * java.lang.Object, int)
147      */

148     protected void createTreeItem(Widget parent, final Object JavaDoc element, int index) {
149         try {
150             super.createTreeItem(parent, element, index);
151         } catch (Exception JavaDoc ex) {
152             ex.printStackTrace();
153         } catch (Error JavaDoc e) {
154             e.printStackTrace();
155         }
156
157     }
158
159     /*
160      * @see ContentViewer#handleLabelProviderChanged(LabelProviderChangedEvent)
161      */

162     protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
163
164         Object JavaDoc[] changed = event.getElements();
165         if (changed != null) {
166             List JavaDoc others = Arrays.asList(changed);
167             for (Iterator JavaDoc iter = others.iterator(); iter.hasNext();) {
168                 if(iter.next() == null)
169                     iter.remove();
170             }
171             if (others.isEmpty()) {
172                 return;
173             }
174             event = new LabelProviderChangedEvent((IBaseLabelProvider) event
175                     .getSource(), others.toArray());
176         }
177         super.handleLabelProviderChanged(event);
178     }
179
180     protected void handleDispose(DisposeEvent event) {
181         dispose();
182         super.handleDispose(event);
183     }
184  
185     /**
186      * <p>
187      * Disposes of the NavigatorContentService, which will dispose the Content
188      * and Label providers.
189      * </p>
190      */

191     public void dispose() {
192         if (contentService != null) {
193             contentService.dispose();
194         }
195         clearSelectionCache();
196     }
197
198     /**
199      * Sets this viewer's sorter and triggers refiltering and resorting of this
200      * viewer's element. Passing <code>null</code> turns sorting off.
201      *
202      * @param sorter
203      * a viewer sorter, or <code>null</code> if none
204      */

205     public void setSorter(ViewerSorter sorter) {
206         if (sorter != null && sorter instanceof CommonViewerSorter) {
207             ((CommonViewerSorter) sorter).setContentService(contentService);
208         }
209
210         super.setSorter(sorter);
211     }
212
213     /**
214      * <p>
215      * The {@link INavigatorContentService}provides the hook into the framework
216      * to provide content from the various extensions.
217      * </p>
218      *
219      * @return The {@link INavigatorContentService}that was created when the
220      * viewer was created.
221      */

222     public INavigatorContentService getNavigatorContentService() {
223         return contentService;
224     }
225
226     /*
227      * (non-Javadoc)
228      *
229      * @see org.eclipse.jface.viewers.AbstractTreeViewer#add(java.lang.Object,
230      * java.lang.Object[])
231      */

232     public void add(Object JavaDoc parentElement, Object JavaDoc[] childElements) {
233         // TODO Intercept ADD for the pipeline service.
234

235         NavigatorPipelineService pipeDream = (NavigatorPipelineService) contentService
236                 .getPipelineService();
237
238         PipelinedShapeModification modification = new PipelinedShapeModification(
239                 parentElement, new ContributorTrackingSet(contentService,
240                         childElements));
241
242         pipeDream.interceptAdd(modification);
243
244         Object JavaDoc parent = (parentElement == getInput()) ? getInput()
245                 : modification.getParent();
246
247         super.add(parent, modification.getChildren().toArray());
248
249     }
250
251     /**
252      * <p>
253      * Removals are handled by refreshing the parents of each of the given
254      * elements. The parents are determined via calls ot the contentProvider.
255      * </p>
256      *
257      * @see org.eclipse.jface.viewers.AbstractTreeViewer#remove(java.lang.Object[])
258      */

259     public void remove(Object JavaDoc[] elements) {
260
261         // TODO Intercept REMOVE for the pipeline service.
262

263         NavigatorPipelineService pipeDream = (NavigatorPipelineService) contentService
264                 .getPipelineService();
265
266         PipelinedShapeModification modification = new PipelinedShapeModification(
267                 null, new ContributorTrackingSet(contentService, elements));
268
269         pipeDream.interceptRemove(modification);
270
271         super.remove(modification.getChildren().toArray());
272     }
273
274     /*
275      * (non-Javadoc)
276      *
277      * @see org.eclipse.jface.viewers.StructuredViewer#refresh(java.lang.Object,
278      * boolean)
279      */

280     public void refresh(Object JavaDoc element, boolean updateLabels) {
281
282         
283         if(element != getInput()) {
284         
285             INavigatorPipelineService pipeDream = contentService
286                     .getPipelineService();
287     
288             PipelinedViewerUpdate update = new PipelinedViewerUpdate();
289             update.getRefreshTargets().add(element);
290             update.setUpdateLabels(updateLabels);
291             /* if the update is modified */
292             if (pipeDream.interceptRefresh(update)) {
293                 /* intercept and apply the update */
294                 boolean toUpdateLabels = update.isUpdateLabels();
295                 for (Iterator JavaDoc iter = update.getRefreshTargets().iterator(); iter
296                         .hasNext();) {
297                     super.refresh(iter.next(), toUpdateLabels);
298                 }
299             } else {
300                 super.refresh(element, updateLabels);
301             }
302         } else {
303             super.refresh(element, updateLabels);
304         }
305     }
306     
307     /*
308      * (non-Javadoc)
309      * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
310      */

311     public void setSelection(ISelection selection, boolean reveal) {
312
313         if(selection instanceof IStructuredSelection) {
314             IStructuredSelection sSelection = (IStructuredSelection) selection;
315             
316             INavigatorPipelineService pipeDream = contentService
317                     .getPipelineService();
318
319             PipelinedViewerUpdate update = new PipelinedViewerUpdate();
320             update.getRefreshTargets().addAll(sSelection.toList());
321             update.setUpdateLabels(false);
322             /* if the update is modified */
323             if (pipeDream.interceptRefresh(update)) {
324                 /* intercept and apply the update */
325                 super.setSelection(new StructuredSelection(update.getRefreshTargets().toArray()) , reveal);
326             } else {
327                 super.setSelection(selection, reveal);
328             }
329         }
330     }
331     
332     /* (non-Javadoc)
333      * @see org.eclipse.jface.viewers.AbstractTreeViewer#setSelectionToWidget(java.util.List, boolean)
334      */

335     protected void setSelectionToWidget(List JavaDoc v, boolean reveal) {
336         clearSelectionCache();
337         super.setSelectionToWidget(v, reveal);
338     }
339     
340     /* (non-Javadoc)
341      * @see org.eclipse.jface.viewers.AbstractTreeViewer#handleDoubleSelect(org.eclipse.swt.events.SelectionEvent)
342      */

343     protected void handleDoubleSelect(SelectionEvent event) {
344         clearSelectionCache();
345         super.handleDoubleSelect(event);
346     }
347     
348     /* (non-Javadoc)
349      * @see org.eclipse.jface.viewers.StructuredViewer#handleOpen(org.eclipse.swt.events.SelectionEvent)
350      */

351     protected void handleOpen(SelectionEvent event) {
352         clearSelectionCache();
353         super.handleOpen(event);
354     }
355     
356     /* (non-Javadoc)
357      * @see org.eclipse.jface.viewers.StructuredViewer#handlePostSelect(org.eclipse.swt.events.SelectionEvent)
358      */

359     protected void handlePostSelect(SelectionEvent e) {
360         clearSelectionCache();
361         super.handlePostSelect(e);
362     }
363     
364     /* (non-Javadoc)
365      * @see org.eclipse.jface.viewers.StructuredViewer#handleSelect(org.eclipse.swt.events.SelectionEvent)
366      */

367     protected void handleSelect(SelectionEvent event) {
368         clearSelectionCache();
369         super.handleSelect(event);
370     }
371     
372     /**
373      * Clears the selection cache.
374      */

375     private void clearSelectionCache() {
376         cachedSelection = null;
377     }
378     
379     /**
380      * Returns the current selection.
381      * <p>
382      * Note that as of 3.2.1 and 3.3, the common viewer caches its selection.
383      * Clients must not set the selection of the viewer's tree control directly.
384      * </p>
385      *
386      * @see org.eclipse.jface.viewers.AbstractTreeViewer#getSelection()
387      */

388     public ISelection getSelection() {
389         if (cachedSelection == null) {
390             cachedSelection = super.getSelection();
391         }
392         return cachedSelection;
393     }
394
395     /*
396      * (non-Javadoc)
397      *
398      * @see org.eclipse.jface.viewers.StructuredViewer#refresh(java.lang.Object)
399      */

400     public void refresh(Object JavaDoc element) {
401         refresh(element, true);
402     }
403
404     /*
405      * (non-Javadoc)
406      *
407      * @see org.eclipse.jface.viewers.StructuredViewer#update(java.lang.Object,
408      * java.lang.String[])
409      */

410     public void update(Object JavaDoc element, String JavaDoc[] properties) {
411
412
413         if(element != getInput()) {
414             INavigatorPipelineService pipeDream = contentService
415                     .getPipelineService();
416     
417             PipelinedViewerUpdate update = new PipelinedViewerUpdate();
418             update.getRefreshTargets().add(element);
419             update.setUpdateLabels(true);
420             /* if the update is modified */
421             if (pipeDream.interceptUpdate(update)) {
422                 /* intercept and apply the update */
423                 for (Iterator JavaDoc iter = update.getRefreshTargets().iterator(); iter
424                         .hasNext();) {
425                     super.update(iter.next(), properties);
426                 }
427             } else {
428                 super.update(element, properties);
429             }
430         } else {
431             super.update(element, properties);
432         }
433     }
434
435     /*
436      * (non-Javadoc)
437      *
438      * @see java.lang.Object#toString()
439      */

440     public String JavaDoc toString() {
441         return contentService.toString() + " Viewer"; //$NON-NLS-1$
442
}
443
444     /*
445      * (non-Javadoc)
446      *
447      * @see org.eclipse.jface.viewers.AbstractTreeViewer#internalRefresh(java.lang.Object,
448      * boolean)
449      */

450     protected void internalRefresh(Object JavaDoc element, boolean updateLabels) {
451         if (element == null && getRoot() == null) {
452             return;
453         }
454         super.internalRefresh(element, updateLabels);
455     }
456
457 }
458
Popular Tags