1 11 package org.eclipse.jdt.internal.ui.viewsupport; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.ListenerList; 15 16 import org.eclipse.swt.events.FocusEvent; 17 import org.eclipse.swt.events.FocusListener; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Widget; 20 21 import org.eclipse.jface.viewers.IPostSelectionProvider; 22 import org.eclipse.jface.viewers.ISelection; 23 import org.eclipse.jface.viewers.ISelectionChangedListener; 24 import org.eclipse.jface.viewers.ISelectionProvider; 25 import org.eclipse.jface.viewers.SelectionChangedEvent; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 import org.eclipse.jface.viewers.StructuredViewer; 28 29 33 public class SelectionProviderMediator implements IPostSelectionProvider { 34 35 private class InternalListener implements ISelectionChangedListener, FocusListener { 36 39 public void selectionChanged(SelectionChangedEvent event) { 40 doSelectionChanged(event); 41 } 42 43 46 public void focusGained(FocusEvent e) { 47 doFocusChanged(e.widget); 48 } 49 50 53 public void focusLost(FocusEvent e) { 54 } 57 } 58 59 private class InternalPostSelectionListener implements ISelectionChangedListener { 60 public void selectionChanged(SelectionChangedEvent event) { 61 doPostSelectionChanged(event); 62 } 63 64 } 65 66 private StructuredViewer[] fViewers; 67 68 private StructuredViewer fViewerInFocus; 69 private ListenerList fSelectionChangedListeners; 70 private ListenerList fPostSelectionChangedListeners; 71 72 76 public SelectionProviderMediator(StructuredViewer[] viewers, StructuredViewer viewerInFocus) { 77 Assert.isNotNull(viewers); 78 fViewers= viewers; 79 InternalListener listener= new InternalListener(); 80 fSelectionChangedListeners= new ListenerList(); 81 fPostSelectionChangedListeners= new ListenerList(); 82 fViewerInFocus= viewerInFocus; 83 84 for (int i= 0; i < fViewers.length; i++) { 85 StructuredViewer viewer= fViewers[i]; 86 viewer.addSelectionChangedListener(listener); 87 viewer.addPostSelectionChangedListener(new InternalPostSelectionListener()); 88 Control control= viewer.getControl(); 89 control.addFocusListener(listener); 90 } 91 } 92 93 private void doFocusChanged(Widget control) { 94 for (int i= 0; i < fViewers.length; i++) { 95 if (fViewers[i].getControl() == control) { 96 propagateFocusChanged(fViewers[i]); 97 return; 98 } 99 } 100 } 101 102 final void doPostSelectionChanged(SelectionChangedEvent event) { 103 ISelectionProvider provider= event.getSelectionProvider(); 104 if (provider == fViewerInFocus) { 105 firePostSelectionChanged(); 106 } 107 } 108 109 final void doSelectionChanged(SelectionChangedEvent event) { 110 ISelectionProvider provider= event.getSelectionProvider(); 111 if (provider == fViewerInFocus) { 112 fireSelectionChanged(); 113 } 114 } 115 116 final void propagateFocusChanged(StructuredViewer viewer) { 117 if (viewer != fViewerInFocus) { fViewerInFocus= viewer; 119 fireSelectionChanged(); 120 firePostSelectionChanged(); 121 } 122 } 123 124 private void fireSelectionChanged() { 125 if (fSelectionChangedListeners != null) { 126 SelectionChangedEvent event= new SelectionChangedEvent(this, getSelection()); 127 128 Object [] listeners= fSelectionChangedListeners.getListeners(); 129 for (int i= 0; i < listeners.length; i++) { 130 ISelectionChangedListener listener= (ISelectionChangedListener) listeners[i]; 131 listener.selectionChanged(event); 132 } 133 } 134 } 135 136 private void firePostSelectionChanged() { 137 if (fPostSelectionChangedListeners != null) { 138 SelectionChangedEvent event= new SelectionChangedEvent(this, getSelection()); 139 140 Object [] listeners= fPostSelectionChangedListeners.getListeners(); 141 for (int i= 0; i < listeners.length; i++) { 142 ISelectionChangedListener listener= (ISelectionChangedListener) listeners[i]; 143 listener.selectionChanged(event); 144 } 145 } 146 } 147 148 151 public void addSelectionChangedListener(ISelectionChangedListener listener) { 152 fSelectionChangedListeners.add(listener); 153 } 154 155 158 public void removeSelectionChangedListener(ISelectionChangedListener listener) { 159 fSelectionChangedListeners.remove(listener); 160 } 161 162 165 public void addPostSelectionChangedListener(ISelectionChangedListener listener) { 166 fPostSelectionChangedListeners.add(listener); 167 } 168 169 170 173 public void removePostSelectionChangedListener(ISelectionChangedListener listener) { 174 fPostSelectionChangedListeners.remove(listener); 175 } 176 177 180 public ISelection getSelection() { 181 if (fViewerInFocus != null) { 182 return fViewerInFocus.getSelection(); 183 } 184 return StructuredSelection.EMPTY; 185 } 186 187 190 public void setSelection(ISelection selection) { 191 if (fViewerInFocus != null) { 192 fViewerInFocus.setSelection(selection); 193 } 194 } 195 196 public void setSelection(ISelection selection, boolean reveal) { 197 if (fViewerInFocus != null) { 198 fViewerInFocus.setSelection(selection, reveal); 199 } 200 } 201 202 205 public StructuredViewer getViewerInFocus() { 206 return fViewerInFocus; 207 } 208 } 209 | Popular Tags |