KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > TreeViewerAdvisor


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 package org.eclipse.team.internal.ui.synchronize;
12
13 import org.eclipse.compare.structuremergeviewer.ICompareInput;
14 import org.eclipse.compare.structuremergeviewer.ICompareInputChangeListener;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.jface.action.IStatusLineManager;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.dnd.*;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.TreeItem;
26 import org.eclipse.team.internal.ui.TeamUIMessages;
27 import org.eclipse.team.internal.ui.Utils;
28 import org.eclipse.team.internal.ui.synchronize.actions.StatusLineContributionGroup;
29 import org.eclipse.team.internal.ui.synchronize.actions.SyncInfoSetStatusLineContributionGroup;
30 import org.eclipse.team.ui.synchronize.*;
31 import org.eclipse.ui.*;
32 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
33 import org.eclipse.ui.model.BaseWorkbenchContentProvider;
34 import org.eclipse.ui.part.ResourceTransfer;
35
36 /**
37  * A <code>TreeViewerAdvisor</code> that works with TreeViewers. Two default
38  * tree viewers are provided that support navigation: <code>NavigableTreeViewer</code>
39  * and <code>NavigableCheckboxTreeViewer</code>.
40  * <p>
41  * Note that this advisor can be used with any tree viewer. By default it provides an
42  * expand all action, double click behavior on containers, and navigation support for
43  * tree viewers.
44  * </p><p>
45  * By default this advisor supports hierarchical models and honour the compressed
46  * folder Team preference for showing the sync set as compressed folders. Subclasses
47  * can provide their own presentation models.
48  * <p>
49  * @since 3.0
50  */

51 public class TreeViewerAdvisor extends AbstractTreeViewerAdvisor {
52     
53     // Special actions that could not be contributed using an ActionGroup
54
private StatusLineContributionGroup statusLine;
55     
56     /**
57      * Style bit that indicates that a checkbox viewer is desired.
58      */

59     public static final int CHECKBOX = 1;
60     
61     private SynchronizeModelManager modelManager;
62     
63     /**
64      * A navigable checkbox tree viewer that will work with the <code>navigate</code> method of
65      * this advisor.
66      */

67     public static class NavigableCheckboxTreeViewer extends ContainerCheckedTreeViewer implements ITreeViewerAccessor {
68         public NavigableCheckboxTreeViewer(Composite parent, int style) {
69             super(parent, style);
70             setUseHashlookup(true);
71         }
72
73         public void createChildren(TreeItem item) {
74             super.createChildren(item);
75         }
76
77         public void openSelection() {
78             fireOpen(new OpenEvent(this, getSelection()));
79         }
80     }
81     
82     /**
83      * A navigable tree viewer that will work with the <code>navigate</code> method of
84      * this advisor.
85      */

86     public static class NavigableTreeViewer extends TreeViewer implements ITreeViewerAccessor {
87         public NavigableTreeViewer(Composite parent, int style) {
88             super(parent, style);
89             setUseHashlookup(true);
90         }
91
92         public void createChildren(TreeItem item) {
93             super.createChildren(item);
94         }
95
96         public void openSelection() {
97             fireOpen(new OpenEvent(this, getSelection()));
98         }
99     }
100     
101     public static StructuredViewer createViewer(Composite parent, ISynchronizePageConfiguration configuration) {
102         int style = ((SynchronizePageConfiguration)configuration).getViewerStyle();
103         if ((style & CHECKBOX) > 0) {
104             NavigableCheckboxTreeViewer v = new TreeViewerAdvisor.NavigableCheckboxTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
105             configuration.getSite().setSelectionProvider(v);
106             return v;
107         } else {
108             NavigableTreeViewer v = new TreeViewerAdvisor.NavigableTreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
109             configuration.getSite().setSelectionProvider(v);
110             return v;
111         }
112     }
113
114     /**
115      * Create an advisor that will allow viewer contributions with the given <code>targetID</code>. This
116      * advisor will provide a presentation model based on the given sync info set. Note that it's important
117      * to call {@link #dispose()} when finished with an advisor.
118      *
119      * @param parent
120      * @param configuration
121      */

122     public TreeViewerAdvisor(Composite parent, ISynchronizePageConfiguration configuration) {
123         super(configuration);
124         
125         // Allow the configuration to provide it's own model manager but if one isn't initialized, then
126
// simply use the default provided by the advisor.
127
modelManager = (SynchronizeModelManager)configuration.getProperty(SynchronizePageConfiguration.P_MODEL_MANAGER);
128         if(modelManager == null) {
129             modelManager = createModelManager(configuration);
130             configuration.setProperty(SynchronizePageConfiguration.P_MODEL_MANAGER, modelManager);
131         }
132         Assert.isNotNull(modelManager, "model manager must be set"); //$NON-NLS-1$
133
modelManager.setViewerAdvisor(this);
134         
135         StructuredViewer viewer = TreeViewerAdvisor.createViewer(parent, configuration);
136         GridData data = new GridData(GridData.FILL_BOTH);
137         viewer.getControl().setLayoutData(data);
138         initializeViewer(viewer);
139     }
140
141     /**
142      * Create the model manager to be used by this advisor
143      * @param configuration
144      */

145     protected SynchronizeModelManager createModelManager(ISynchronizePageConfiguration configuration) {
146         ISynchronizeParticipant participant = configuration.getParticipant();
147         if (participant instanceof IChangeSetProvider) {
148             IChangeSetProvider provider = (IChangeSetProvider) participant;
149             ChangeSetCapability changeSetCapability = provider.getChangeSetCapability();
150             if (changeSetCapability != null) {
151                 if (changeSetCapability.supportsActiveChangeSets() || changeSetCapability.supportsCheckedInChangeSets()) {
152                     return new ChangeSetModelManager(configuration);
153                 }
154             }
155         }
156         return new HierarchicalModelManager(configuration);
157     }
158     
159     /*
160      * For use by test cases only
161      * @return Returns the modelManager.
162      */

163     public SynchronizeModelManager getModelManager() {
164         return modelManager;
165     }
166     
167     /* (non-Javadoc)
168      * @see org.eclipse.team.ui.synchronize.viewers.StructuredViewerAdvisor#initializeViewer(org.eclipse.jface.viewers.StructuredViewer)
169      */

170     public boolean validateViewer(StructuredViewer viewer) {
171         return viewer instanceof AbstractTreeViewer;
172     }
173     
174     /* (non-Javadoc)
175      * @see org.eclipse.team.ui.synchronize.viewers.StructuredViewerAdvisor#initializeListeners(org.eclipse.jface.viewers.StructuredViewer)
176      */

177     protected void initializeListeners(final StructuredViewer viewer) {
178         super.initializeListeners(viewer);
179         viewer.addSelectionChangedListener(new ISelectionChangedListener() {
180             public void selectionChanged(SelectionChangedEvent event) {
181                 updateStatusLine((IStructuredSelection) event.getSelection());
182             }
183         });
184     }
185     
186     /* private */ void updateStatusLine(IStructuredSelection selection) {
187         IWorkbenchSite ws = getConfiguration().getSite().getWorkbenchSite();
188         if (ws != null && ws instanceof IViewSite) {
189             String JavaDoc msg = getStatusLineMessage(selection);
190             ((IViewSite)ws).getActionBars().getStatusLineManager().setMessage(msg);
191         }
192     }
193     
194     private String JavaDoc getStatusLineMessage(IStructuredSelection selection) {
195         if (selection.size() == 1) {
196             Object JavaDoc first = selection.getFirstElement();
197             if (first instanceof SyncInfoModelElement) {
198                 SyncInfoModelElement node = (SyncInfoModelElement) first;
199                 IResource resource = node.getResource();
200                 if (resource == null) {
201                     return node.getName();
202                 } else {
203                     return resource.getFullPath().makeRelative().toString();
204                 }
205             }
206         }
207         if (selection.size() > 1) {
208             return selection.size() + TeamUIMessages.SynchronizeView_13;
209         }
210         return ""; //$NON-NLS-1$
211
}
212     
213     /**
214      * Called to set the input to a viewer. The input to a viewer is always the model created
215      * by the model provider.
216      *
217      * @param modelProvider
218      */

219     public final void setInput(final ISynchronizeModelProvider modelProvider) {
220         final ISynchronizeModelElement modelRoot = modelProvider.getModelRoot();
221         getActionGroup().modelChanged(modelRoot);
222         modelRoot.addCompareInputChangeListener(new ICompareInputChangeListener() {
223             public void compareInputChanged(ICompareInput source) {
224                 getActionGroup().modelChanged(modelRoot);
225             }
226         });
227         final StructuredViewer viewer = getViewer();
228         if (viewer != null) {
229             viewer.setSorter(modelProvider.getViewerSorter());
230             viewer.setInput(modelRoot);
231             modelProvider.addPropertyChangeListener(new IPropertyChangeListener() {
232                 public void propertyChange(PropertyChangeEvent event) {
233                     if (event.getProperty() == ISynchronizeModelProvider.P_VIEWER_SORTER) {
234                         if (viewer != null && !viewer.getControl().isDisposed()) {
235                             viewer.getControl().getDisplay().syncExec(new Runnable JavaDoc() {
236                                 public void run() {
237                                     if (viewer != null && !viewer.getControl().isDisposed()) {
238                                         ViewerSorter newSorter = modelProvider.getViewerSorter();
239                                         ViewerSorter oldSorter = viewer.getSorter();
240                                         if (newSorter == oldSorter) {
241                                             viewer.refresh();
242                                         } else {
243                                             viewer.setSorter(newSorter);
244                                         }
245                                     }
246                                 }
247                             });
248                         }
249                     }
250                 }
251             });
252         }
253     }
254     
255     /**
256      * Install a viewer to be configured with this advisor. An advisor can only be installed with
257      * one viewer at a time. When this method completes the viewer is considered initialized and
258      * can be shown to the user.
259      * @param viewer the viewer being installed
260      */

261     public final void initializeViewer(final StructuredViewer viewer) {
262         super.initializeViewer(viewer);
263         
264         final DragSourceListener listener = new DragSourceListener() {
265
266             public void dragStart(DragSourceEvent event) {
267                 final IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
268                 final Object JavaDoc [] array= selection.toArray();
269                 event.doit= Utils.getResources(array).length > 0;
270             }
271
272             public void dragSetData(DragSourceEvent event) {
273                 
274                 if (ResourceTransfer.getInstance().isSupportedType(event.dataType)) {
275                     final IStructuredSelection selection= (IStructuredSelection)viewer.getSelection();
276                     final Object JavaDoc [] array= selection.toArray();
277                     event.data= Utils.getResources(array);
278                 }
279             }
280
281             public void dragFinished(DragSourceEvent event) {}
282         };
283         
284         final int ops = DND.DROP_COPY | DND.DROP_LINK;
285         viewer.addDragSupport(ops, new Transfer[] { ResourceTransfer.getInstance() }, listener);
286     
287         viewer.setLabelProvider(getLabelProvider());
288         viewer.setContentProvider(getContentProvider());
289     }
290     
291     /**
292      * Returns the content provider for the viewer.
293      *
294      * @return the content provider for the viewer.
295      */

296     protected IStructuredContentProvider getContentProvider() {
297         return new BaseWorkbenchContentProvider();
298     }
299     
300     /**
301      * Get the label provider that will be assigned to the viewer initialized
302      * by this configuration. Subclass may override but should either wrap the
303      * default one provided by this method or subclass <code>TeamSubscriberParticipantLabelProvider</code>.
304      * In the later case, the logical label provider should still be assigned
305      * to the subclass of <code>TeamSubscriberParticipantLabelProvider</code>.
306      * @return a label provider
307      * @see SynchronizeModelElementLabelProvider
308      */

309     protected ILabelProvider getLabelProvider() {
310         ILabelProvider provider = new SynchronizeModelElementLabelProvider();
311         ILabelDecorator[] decorators = (ILabelDecorator[])getConfiguration().getProperty(ISynchronizePageConfiguration.P_LABEL_DECORATORS);
312         if (decorators == null) {
313             return provider;
314         }
315         return new DecoratingColorLabelProvider(provider, decorators);
316     }
317     
318     /* (non-Javadoc)
319      * @see org.eclipse.team.internal.ui.synchronize.StructuredViewerAdvisor#dispose()
320      */

321     public void dispose() {
322         if (statusLine != null) {
323             statusLine.dispose();
324         }
325         super.dispose();
326     }
327     
328     /* (non-Javadoc)
329      * @see org.eclipse.team.internal.ui.synchronize.StructuredViewerAdvisor#initializeStatusLine(org.eclipse.ui.IActionBars)
330      */

331     protected void initializeStatusLine(IActionBars actionBars) {
332         statusLine = new SyncInfoSetStatusLineContributionGroup(
333                 getConfiguration().getSite().getShell(),
334                 getConfiguration());
335         IStatusLineManager statusLineMgr = actionBars.getStatusLineManager();
336         if (statusLineMgr != null && statusLine != null) {
337             statusLine.fillActionBars(actionBars);
338         }
339     }
340 }
341
Popular Tags