1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.ui.AbstractSourceProvider; 18 import org.eclipse.ui.IPartListener; 19 import org.eclipse.ui.ISources; 20 import org.eclipse.ui.IWindowListener; 21 import org.eclipse.ui.IWorkbench; 22 import org.eclipse.ui.IWorkbenchPage; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.IWorkbenchPartSite; 25 import org.eclipse.ui.IWorkbenchWindow; 26 import org.eclipse.ui.internal.util.Util; 27 28 33 public class ActivePartSourceProvider extends AbstractSourceProvider { 34 35 39 private String lastActivePartId = null; 40 41 45 private IWorkbenchPartSite lastActivePartSite = null; 46 47 private final IPartListener partListener = new IPartListener() { 48 49 public final void partActivated(final IWorkbenchPart part) { 50 checkActivePart(); 51 } 52 53 public final void partBroughtToTop(final IWorkbenchPart part) { 54 checkActivePart(); 55 } 56 57 public final void partClosed(final IWorkbenchPart part) { 58 checkActivePart(); 59 } 60 61 public final void partDeactivated(final IWorkbenchPart part) { 62 checkActivePart(); 63 } 64 65 public final void partOpened(final IWorkbenchPart part) { 66 checkActivePart(); 67 } 68 69 }; 70 71 private final IWindowListener windowListener = new IWindowListener() { 72 73 public final void windowActivated(final IWorkbenchWindow window) { 74 if (window != null) { 75 window.getPartService().addPartListener(partListener); 76 } 77 checkActivePart(); 78 } 79 80 public final void windowClosed(final IWorkbenchWindow window) { 81 if (window != null) { 82 window.getPartService().removePartListener(partListener); 83 } 84 checkActivePart(); 85 } 86 87 public final void windowDeactivated(final IWorkbenchWindow window) { 88 if (window != null) { 89 window.getPartService().removePartListener(partListener); 90 } 91 checkActivePart(); 92 } 93 94 public final void windowOpened(final IWorkbenchWindow window) { 95 if (window != null) { 96 window.getPartService().addPartListener(partListener); 97 } 98 checkActivePart(); 99 } 100 101 }; 102 103 106 private final IWorkbench workbench; 107 108 115 public ActivePartSourceProvider(final IWorkbench workbench) { 116 this.workbench = workbench; 117 workbench.addWindowListener(windowListener); 118 } 119 120 private final void checkActivePart() { 121 final Map currentState = getCurrentState(); 122 int sources = 0; 123 final Object newActivePartId = currentState 124 .get(ISources.ACTIVE_PART_NAME); 125 if (!Util.equals(newActivePartId, lastActivePartId)) { 126 sources |= ISources.ACTIVE_PART; 127 lastActivePartId = (String ) newActivePartId; 128 } 129 final Object newActivePartSite = currentState 130 .get(ISources.ACTIVE_SITE_NAME); 131 if (!Util.equals(newActivePartSite, lastActivePartSite)) { 132 sources |= ISources.ACTIVE_SITE; 133 lastActivePartSite = (IWorkbenchPartSite) newActivePartSite; 134 } 135 136 if (sources != 0) { 137 fireSourceChanged(sources, currentState); 138 } 139 } 140 141 public final void dispose() { 142 workbench.removeWindowListener(windowListener); 143 } 144 145 public final Map getCurrentState() { 146 final Map currentState = new HashMap (4); 147 currentState.put(ISources.ACTIVE_SITE_NAME, null); 148 currentState.put(ISources.ACTIVE_PART_NAME, null); 149 150 final IWorkbenchWindow activeWorkbenchWindow = workbench 151 .getActiveWorkbenchWindow(); 152 if (activeWorkbenchWindow != null) { 153 final IWorkbenchPage activeWorkbenchPage = activeWorkbenchWindow 154 .getActivePage(); 155 if (activeWorkbenchPage != null) { 156 final IWorkbenchPart activeWorkbenchPart = activeWorkbenchPage 157 .getActivePart(); 158 if (activeWorkbenchPart != null) { 159 final IWorkbenchPartSite activeWorkbenchPartSite = activeWorkbenchPart 160 .getSite(); 161 currentState.put(ISources.ACTIVE_SITE_NAME, 162 activeWorkbenchPartSite); 163 if (activeWorkbenchPartSite != null) { 164 final String newActivePartId = activeWorkbenchPartSite 165 .getId(); 166 currentState.put(ISources.ACTIVE_PART_NAME, 167 newActivePartId); 168 } 169 } 170 } 171 } 172 173 return currentState; 174 } 175 176 } 177 | Popular Tags |