1 11 12 package org.eclipse.ui.internal.services; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.ui.AbstractSourceProvider; 18 import org.eclipse.ui.IEditorPart; 19 import org.eclipse.ui.IEditorSite; 20 import org.eclipse.ui.IPartListener; 21 import org.eclipse.ui.ISources; 22 import org.eclipse.ui.IWindowListener; 23 import org.eclipse.ui.IWorkbench; 24 import org.eclipse.ui.IWorkbenchPage; 25 import org.eclipse.ui.IWorkbenchPart; 26 import org.eclipse.ui.IWorkbenchPartSite; 27 import org.eclipse.ui.IWorkbenchWindow; 28 import org.eclipse.ui.internal.util.Util; 29 30 35 public class ActivePartSourceProvider extends AbstractSourceProvider { 36 37 40 private static final String [] PROVIDED_SOURCE_NAMES = new String [] { 41 ISources.ACTIVE_EDITOR_ID_NAME, ISources.ACTIVE_EDITOR_NAME, 42 ISources.ACTIVE_PART_ID_NAME, ISources.ACTIVE_PART_NAME, 43 ISources.ACTIVE_SITE_NAME }; 44 45 49 private IEditorPart lastActiveEditor = null; 50 51 55 private String lastActiveEditorId = null; 56 57 61 private IWorkbenchPart lastActivePart = null; 62 63 67 private String lastActivePartId = null; 68 69 73 private IWorkbenchPartSite lastActivePartSite = null; 74 75 private final IPartListener partListener = new IPartListener() { 76 77 public final void partActivated(final IWorkbenchPart part) { 78 checkActivePart(); 79 } 80 81 public final void partBroughtToTop(final IWorkbenchPart part) { 82 checkActivePart(); 83 } 84 85 public final void partClosed(final IWorkbenchPart part) { 86 checkActivePart(); 87 } 88 89 public final void partDeactivated(final IWorkbenchPart part) { 90 checkActivePart(); 91 } 92 93 public final void partOpened(final IWorkbenchPart part) { 94 checkActivePart(); 95 } 96 97 }; 98 99 private final IWindowListener windowListener = new IWindowListener() { 100 101 public final void windowActivated(final IWorkbenchWindow window) { 102 checkActivePart(); 103 } 104 105 public final void windowClosed(final IWorkbenchWindow window) { 106 if (window != null) { 107 window.getPartService().removePartListener(partListener); 108 } 109 checkActivePart(); 110 } 111 112 public final void windowDeactivated(final IWorkbenchWindow window) { 113 checkActivePart(); 114 } 115 116 public final void windowOpened(final IWorkbenchWindow window) { 117 if (window != null) { 118 window.getPartService().addPartListener(partListener); 119 } 120 checkActivePart(); 121 } 122 123 }; 124 125 128 private final IWorkbench workbench; 129 130 137 public ActivePartSourceProvider(final IWorkbench workbench) { 138 this.workbench = workbench; 139 workbench.addWindowListener(windowListener); 140 } 141 142 private final void checkActivePart() { 143 final Map currentState = getCurrentState(); 144 int sources = 0; 145 146 final Object newActivePart = currentState 148 .get(ISources.ACTIVE_PART_NAME); 149 if (!Util.equals(newActivePart, lastActivePart)) { 150 sources |= ISources.ACTIVE_PART; 151 lastActivePart = (IWorkbenchPart) newActivePart; 152 } 153 final Object newActivePartId = currentState 154 .get(ISources.ACTIVE_PART_ID_NAME); 155 if (!Util.equals(newActivePartId, lastActivePartId)) { 156 sources |= ISources.ACTIVE_PART_ID; 157 lastActivePartId = (String ) newActivePartId; 158 } 159 final Object newActivePartSite = currentState 160 .get(ISources.ACTIVE_SITE_NAME); 161 if (!Util.equals(newActivePartSite, lastActivePartSite)) { 162 sources |= ISources.ACTIVE_SITE; 163 lastActivePartSite = (IWorkbenchPartSite) newActivePartSite; 164 } 165 final Object newActiveEditor = currentState 166 .get(ISources.ACTIVE_EDITOR_NAME); 167 if (!Util.equals(newActiveEditor, lastActiveEditor)) { 168 sources |= ISources.ACTIVE_EDITOR; 169 lastActiveEditor = (IEditorPart) newActiveEditor; 170 } 171 final Object newActiveEditorId = currentState 172 .get(ISources.ACTIVE_EDITOR_ID_NAME); 173 if (!Util.equals(newActiveEditorId, lastActiveEditorId)) { 174 sources |= ISources.ACTIVE_EDITOR_ID; 175 lastActiveEditorId = (String ) newActiveEditorId; 176 } 177 178 if (sources != 0) { 180 if (DEBUG) { 181 if ((sources & ISources.ACTIVE_PART) != 0) { 182 logDebuggingInfo("Active part changed to " + lastActivePart); 184 } 185 if ((sources & ISources.ACTIVE_PART_ID) != 0) { 186 logDebuggingInfo("Active part id changed to " + lastActivePartId); 188 } 189 if ((sources & ISources.ACTIVE_SITE) != 0) { 190 logDebuggingInfo("Active site changed to " + lastActivePartSite); 192 } 193 if ((sources & ISources.ACTIVE_EDITOR) != 0) { 194 logDebuggingInfo("Active editor changed to " + lastActiveEditor); 196 } 197 if ((sources & ISources.ACTIVE_EDITOR_ID) != 0) { 198 logDebuggingInfo("Active editor id changed to " + lastActiveEditorId); 200 } 201 } 202 fireSourceChanged(sources, currentState); 203 } 204 } 205 206 public final void dispose() { 207 workbench.removeWindowListener(windowListener); 208 } 209 210 public final Map getCurrentState() { 211 final Map currentState = new HashMap (7); 212 currentState.put(ISources.ACTIVE_SITE_NAME, null); 213 currentState.put(ISources.ACTIVE_PART_NAME, null); 214 currentState.put(ISources.ACTIVE_PART_ID_NAME, null); 215 currentState.put(ISources.ACTIVE_EDITOR_NAME, null); 216 currentState.put(ISources.ACTIVE_EDITOR_ID_NAME, null); 217 218 final IWorkbenchWindow activeWorkbenchWindow = workbench 219 .getActiveWorkbenchWindow(); 220 if (activeWorkbenchWindow != null) { 221 final IWorkbenchPage activeWorkbenchPage = activeWorkbenchWindow 222 .getActivePage(); 223 if (activeWorkbenchPage != null) { 224 final IWorkbenchPart newActivePart = activeWorkbenchPage 226 .getActivePart(); 227 currentState.put(ISources.ACTIVE_PART_NAME, newActivePart); 228 if (newActivePart != null) { 229 final IWorkbenchPartSite activeWorkbenchPartSite = newActivePart 230 .getSite(); 231 currentState.put(ISources.ACTIVE_SITE_NAME, 232 activeWorkbenchPartSite); 233 if (activeWorkbenchPartSite != null) { 234 final String newActivePartId = activeWorkbenchPartSite 235 .getId(); 236 currentState.put(ISources.ACTIVE_PART_ID_NAME, 237 newActivePartId); 238 } 239 } 240 241 final IEditorPart newActiveEditor = activeWorkbenchPage 243 .getActiveEditor(); 244 currentState.put(ISources.ACTIVE_EDITOR_NAME, newActiveEditor); 245 if (newActiveEditor != null) { 246 final IEditorSite activeEditorSite = newActiveEditor 247 .getEditorSite(); 248 if (activeEditorSite != null) { 249 final String newActiveEditorId = activeEditorSite 250 .getId(); 251 currentState.put(ISources.ACTIVE_EDITOR_ID_NAME, 252 newActiveEditorId); 253 } 254 } 255 } 256 } 257 258 return currentState; 259 } 260 261 public final String [] getProvidedSourceNames() { 262 return PROVIDED_SOURCE_NAMES; 263 } 264 265 } 266 | Popular Tags |