1 11 12 package org.eclipse.ui.internal.services; 13 14 import java.util.Map ; 15 import java.util.TreeMap ; 16 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.ui.AbstractSourceProvider; 19 import org.eclipse.ui.INullSelectionListener; 20 import org.eclipse.ui.ISelectionService; 21 import org.eclipse.ui.ISources; 22 import org.eclipse.ui.IWindowListener; 23 import org.eclipse.ui.IWorkbench; 24 import org.eclipse.ui.IWorkbenchPart; 25 import org.eclipse.ui.IWorkbenchWindow; 26 27 40 public final class CurrentSelectionSourceProvider extends 41 AbstractSourceProvider implements INullSelectionListener { 42 43 46 private static final String [] PROVIDED_SOURCE_NAMES = new String [] { ISources.ACTIVE_CURRENT_SELECTION_NAME }; 47 48 52 private final IWindowListener windowListener = new IWindowListener() { 53 public final void windowActivated(final IWorkbenchWindow window) { 54 swapListeners(window, false); 55 } 56 57 public final void windowClosed(final IWorkbenchWindow window) { 58 swapListeners(window, true); 59 } 60 61 public final void windowDeactivated(final IWorkbenchWindow window) { 62 swapListeners(window, true); 63 } 64 65 public final void windowOpened(final IWorkbenchWindow window) { 66 swapListeners(window, false); 67 } 68 }; 69 70 74 private final IWorkbench workbench; 75 76 83 public CurrentSelectionSourceProvider(final IWorkbench workbench) { 84 this.workbench = workbench; 85 workbench.addWindowListener(windowListener); 86 } 87 88 public final void dispose() { 89 workbench.removeWindowListener(windowListener); 90 } 91 92 public final Map getCurrentState() { 93 final Map currentState = new TreeMap (); 94 final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 95 if (window != null) { 96 final ISelectionService service = window.getSelectionService(); 97 final ISelection selection = service.getSelection(); 98 currentState.put(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection); 99 } else { 100 currentState.put(ISources.ACTIVE_CURRENT_SELECTION_NAME, null); 101 } 102 return currentState; 103 } 104 105 public final String [] getProvidedSourceNames() { 106 return PROVIDED_SOURCE_NAMES; 107 } 108 109 public final void selectionChanged(final IWorkbenchPart part, 110 final ISelection selection) { 111 if (DEBUG) { 112 logDebuggingInfo("Selection changed to " + selection); } 114 115 fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION, 116 ISources.ACTIVE_CURRENT_SELECTION_NAME, selection); 117 } 118 119 131 private final void swapListeners(final IWorkbenchWindow window, 132 final boolean remove) { 133 final ISelectionService selectionService = window.getSelectionService(); 134 if (remove) { 135 window.getSelectionService().removeSelectionListener( 136 CurrentSelectionSourceProvider.this); 137 selectionChanged(null, null); 138 } else { 139 window.getSelectionService().addSelectionListener( 140 CurrentSelectionSourceProvider.this); 141 selectionChanged(null, selectionService.getSelection()); 142 } 143 } 144 } 145 | Popular Tags |