1 11 package org.eclipse.ui.part; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.ListenerList; 15 import org.eclipse.core.runtime.SafeRunner; 16 import org.eclipse.jface.util.SafeRunnable; 17 import org.eclipse.jface.viewers.IPostSelectionProvider; 18 import org.eclipse.jface.viewers.ISelection; 19 import org.eclipse.jface.viewers.ISelectionChangedListener; 20 import org.eclipse.jface.viewers.ISelectionProvider; 21 import org.eclipse.jface.viewers.SelectionChangedEvent; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.ui.IEditorPart; 24 25 35 public class MultiPageSelectionProvider implements IPostSelectionProvider { 36 37 41 private ListenerList listeners = new ListenerList(); 42 43 46 private ListenerList postListeners = new ListenerList(); 47 48 51 private MultiPageEditorPart multiPageEditor; 52 53 58 public MultiPageSelectionProvider(MultiPageEditorPart multiPageEditor) { 59 Assert.isNotNull(multiPageEditor); 60 this.multiPageEditor = multiPageEditor; 61 } 62 63 66 public void addSelectionChangedListener(ISelectionChangedListener listener) { 67 listeners.add(listener); 68 } 69 70 76 public void addPostSelectionChangedListener(ISelectionChangedListener listener) { 77 postListeners.add(listener); 78 } 79 80 87 public void fireSelectionChanged(final SelectionChangedEvent event) { 88 Object [] listeners = this.listeners.getListeners(); 89 fireEventChange(event, listeners); 90 } 91 92 99 public void firePostSelectionChanged(final SelectionChangedEvent event) { 100 Object [] listeners = postListeners.getListeners(); 101 fireEventChange(event, listeners); 102 } 103 104 private void fireEventChange(final SelectionChangedEvent event, Object [] listeners) { 105 for (int i = 0; i < listeners.length; ++i) { 106 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; 107 SafeRunner.run(new SafeRunnable() { 108 public void run() { 109 l.selectionChanged(event); 110 } 111 }); 112 } 113 } 114 115 119 public MultiPageEditorPart getMultiPageEditor() { 120 return multiPageEditor; 121 } 122 123 126 public ISelection getSelection() { 127 IEditorPart activeEditor = multiPageEditor.getActiveEditor(); 128 if (activeEditor != null) { 129 ISelectionProvider selectionProvider = activeEditor.getSite() 130 .getSelectionProvider(); 131 if (selectionProvider != null) { 132 return selectionProvider.getSelection(); 133 } 134 } 135 return StructuredSelection.EMPTY; 136 } 137 138 141 public void removeSelectionChangedListener( 142 ISelectionChangedListener listener) { 143 listeners.remove(listener); 144 } 145 146 152 public void removePostSelectionChangedListener(ISelectionChangedListener listener) { 153 postListeners.remove(listener); 154 } 155 156 159 public void setSelection(ISelection selection) { 160 IEditorPart activeEditor = multiPageEditor.getActiveEditor(); 161 if (activeEditor != null) { 162 ISelectionProvider selectionProvider = activeEditor.getSite() 163 .getSelectionProvider(); 164 if (selectionProvider != null) { 165 selectionProvider.setSelection(selection); 166 } 167 } 168 } 169 } 170 | Popular Tags |