1 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 51 public class TreeViewerAdvisor extends AbstractTreeViewerAdvisor { 52 53 private StatusLineContributionGroup statusLine; 55 56 59 public static final int CHECKBOX = 1; 60 61 private SynchronizeModelManager modelManager; 62 63 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 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 122 public TreeViewerAdvisor(Composite parent, ISynchronizePageConfiguration configuration) { 123 super(configuration); 124 125 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"); 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 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 163 public SynchronizeModelManager getModelManager() { 164 return modelManager; 165 } 166 167 170 public boolean validateViewer(StructuredViewer viewer) { 171 return viewer instanceof AbstractTreeViewer; 172 } 173 174 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 void updateStatusLine(IStructuredSelection selection) { 187 IWorkbenchSite ws = getConfiguration().getSite().getWorkbenchSite(); 188 if (ws != null && ws instanceof IViewSite) { 189 String msg = getStatusLineMessage(selection); 190 ((IViewSite)ws).getActionBars().getStatusLineManager().setMessage(msg); 191 } 192 } 193 194 private String getStatusLineMessage(IStructuredSelection selection) { 195 if (selection.size() == 1) { 196 Object 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 ""; } 212 213 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 () { 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 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 [] 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 [] 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 296 protected IStructuredContentProvider getContentProvider() { 297 return new BaseWorkbenchContentProvider(); 298 } 299 300 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 321 public void dispose() { 322 if (statusLine != null) { 323 statusLine.dispose(); 324 } 325 super.dispose(); 326 } 327 328 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 |