1 11 package org.eclipse.ui.navigator; 12 13 import java.util.Arrays ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 54 public class CommonViewer extends TreeViewer { 55 56 private final NavigatorContentService contentService; 57 58 private ISelection cachedSelection; 59 60 80 public CommonViewer(String aViewerId, Composite aParent, int aStyle) { 81 super(aParent, aStyle); 82 contentService = new NavigatorContentService(aViewerId, this); 83 init(); 84 } 85 86 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 [] elements) { 106 super.remove(elements); 107 } 108 109 125 protected void initDragAndDrop() { 126 127 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 148 protected void createTreeItem(Widget parent, final Object element, int index) { 149 try { 150 super.createTreeItem(parent, element, index); 151 } catch (Exception ex) { 152 ex.printStackTrace(); 153 } catch (Error e) { 154 e.printStackTrace(); 155 } 156 157 } 158 159 162 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) { 163 164 Object [] changed = event.getElements(); 165 if (changed != null) { 166 List others = Arrays.asList(changed); 167 for (Iterator 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 191 public void dispose() { 192 if (contentService != null) { 193 contentService.dispose(); 194 } 195 clearSelectionCache(); 196 } 197 198 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 222 public INavigatorContentService getNavigatorContentService() { 223 return contentService; 224 } 225 226 232 public void add(Object parentElement, Object [] childElements) { 233 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 parent = (parentElement == getInput()) ? getInput() 245 : modification.getParent(); 246 247 super.add(parent, modification.getChildren().toArray()); 248 249 } 250 251 259 public void remove(Object [] elements) { 260 261 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 280 public void refresh(Object 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 292 if (pipeDream.interceptRefresh(update)) { 293 294 boolean toUpdateLabels = update.isUpdateLabels(); 295 for (Iterator 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 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 323 if (pipeDream.interceptRefresh(update)) { 324 325 super.setSelection(new StructuredSelection(update.getRefreshTargets().toArray()) , reveal); 326 } else { 327 super.setSelection(selection, reveal); 328 } 329 } 330 } 331 332 335 protected void setSelectionToWidget(List v, boolean reveal) { 336 clearSelectionCache(); 337 super.setSelectionToWidget(v, reveal); 338 } 339 340 343 protected void handleDoubleSelect(SelectionEvent event) { 344 clearSelectionCache(); 345 super.handleDoubleSelect(event); 346 } 347 348 351 protected void handleOpen(SelectionEvent event) { 352 clearSelectionCache(); 353 super.handleOpen(event); 354 } 355 356 359 protected void handlePostSelect(SelectionEvent e) { 360 clearSelectionCache(); 361 super.handlePostSelect(e); 362 } 363 364 367 protected void handleSelect(SelectionEvent event) { 368 clearSelectionCache(); 369 super.handleSelect(event); 370 } 371 372 375 private void clearSelectionCache() { 376 cachedSelection = null; 377 } 378 379 388 public ISelection getSelection() { 389 if (cachedSelection == null) { 390 cachedSelection = super.getSelection(); 391 } 392 return cachedSelection; 393 } 394 395 400 public void refresh(Object element) { 401 refresh(element, true); 402 } 403 404 410 public void update(Object element, String [] 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 421 if (pipeDream.interceptUpdate(update)) { 422 423 for (Iterator 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 440 public String toString() { 441 return contentService.toString() + " Viewer"; } 443 444 450 protected void internalRefresh(Object element, boolean updateLabels) { 451 if (element == null && getRoot() == null) { 452 return; 453 } 454 super.internalRefresh(element, updateLabels); 455 } 456 457 } 458 | Popular Tags |