1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.ListenerList; 20 21 import org.eclipse.core.resources.IResource; 22 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.ISelectionProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.SelectionChangedEvent; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 30 import org.eclipse.jdt.core.IJavaElement; 31 32 33 34 43 public class ConvertingSelectionProvider implements ISelectionProvider { 44 45 private final ISelectionProvider fProvider; 46 private SelectionChangedListener fListener; 47 48 private class SelectionChangedListener implements ISelectionChangedListener { 49 50 ListenerList fListeners= new ListenerList(); 51 52 public void selectionChanged(SelectionChangedEvent event) { 53 ISelection selection= convertFrom(event.getSelection()); 54 SelectionChangedEvent newEvent= new SelectionChangedEvent(ConvertingSelectionProvider.this, selection); 55 Object [] listeners= fListeners.getListeners(); 56 for (int i= 0; i < listeners.length; i++) { 57 ((ISelectionChangedListener)listeners[i]).selectionChanged(newEvent); 58 } 59 } 60 public void addListener(ISelectionChangedListener listener) { 61 fListeners.add(listener); 62 } 63 public void removeListener(ISelectionChangedListener listener) { 64 fListeners.remove(listener); 65 } 66 public boolean isEmpty() { 67 return fListeners.isEmpty(); 68 } 69 } 70 71 77 public ConvertingSelectionProvider(ISelectionProvider provider) { 78 Assert.isNotNull(provider); 79 fProvider= provider; 80 } 81 82 92 public ISelection convertFrom(ISelection viewerSelection) { 93 return convertFromUsingDefaultMechanism(viewerSelection); 94 } 95 96 97 private ISelection convertFromUsingDefaultMechanism(ISelection viewerSelection) { 98 if (! (viewerSelection instanceof IStructuredSelection)) 99 return viewerSelection; 100 IStructuredSelection selection= (IStructuredSelection)viewerSelection; 101 List result= new ArrayList (selection.size()); 102 for (Iterator iter= selection.iterator(); iter.hasNext();) { 103 Object element= iter.next(); 104 if (element instanceof IResource || element instanceof IJavaElement) { 105 result.add(element); 106 } else if (element instanceof IAdaptable) { 107 IAdaptable adaptable= (IAdaptable)element; 108 IJavaElement jElement= (IJavaElement)adaptable.getAdapter(IJavaElement.class); 109 if (jElement != null) { 110 result.add(jElement); 111 } else { 112 IResource resource= (IResource)adaptable.getAdapter(IResource.class); 113 if (resource != null) { 114 result.add(resource); 115 } else { 116 result.add(element); 117 } 118 } 119 } else { 120 result.add(element); 121 } 122 } 123 return new StructuredSelection(result); 124 } 125 126 134 public ISelection convertTo(ISelection selection) { 135 return selection; 136 } 137 138 139 142 public final ISelection getSelection() { 143 return convertFrom(fProvider.getSelection()); 144 } 145 146 149 public final void setSelection(ISelection selection) { 150 fProvider.setSelection(convertTo(selection)); 151 } 152 153 156 public void addSelectionChangedListener(ISelectionChangedListener listener) { 157 if (fListener == null) { 158 fListener= new SelectionChangedListener(); 159 fProvider.addSelectionChangedListener(fListener); 160 } 161 fListener.addListener(listener); 162 } 163 164 167 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 168 if (fListener == null) 169 return; 170 fListener.removeListener(listener); 171 if (fListener.isEmpty()) { 172 fProvider.removeSelectionChangedListener(fListener); 173 fListener= null; 174 } 175 } 176 } 177 | Popular Tags |