1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.ArrayList ; 15 import java.util.Collection ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtension; 22 import org.eclipse.core.runtime.IExtensionDelta; 23 import org.eclipse.core.runtime.IExtensionPoint; 24 import org.eclipse.core.runtime.IRegistryChangeEvent; 25 import org.eclipse.core.runtime.IRegistryChangeListener; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 import org.eclipse.swt.widgets.Display; 28 import org.eclipse.swt.widgets.Shell; 29 import org.eclipse.ui.IWorkbenchPage; 30 import org.eclipse.ui.IWorkbenchWindow; 31 import org.eclipse.ui.PlatformUI; 32 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 33 import org.eclipse.ui.internal.themes.ColorDefinition; 34 import org.eclipse.ui.internal.themes.FontDefinition; 35 import org.eclipse.ui.internal.themes.ThemeElementHelper; 36 import org.eclipse.ui.internal.themes.ThemeRegistry; 37 import org.eclipse.ui.internal.themes.ThemeRegistryReader; 38 import org.eclipse.ui.internal.util.PrefUtil; 39 import org.eclipse.ui.themes.ITheme; 40 import org.eclipse.ui.themes.IThemeManager; 41 42 class ExtensionEventHandler implements IRegistryChangeListener { 43 44 private Workbench workbench; 45 46 private List changeList = new ArrayList (10); 47 48 public ExtensionEventHandler(Workbench workbench) { 49 this.workbench = workbench; 50 } 51 52 public void registryChanged(IRegistryChangeEvent event) { 53 try { 54 IExtensionDelta delta[] = event 55 .getExtensionDeltas(WorkbenchPlugin.PI_WORKBENCH); 56 IExtension ext; 57 IExtensionPoint extPt; 58 IWorkbenchWindow[] win = PlatformUI.getWorkbench() 59 .getWorkbenchWindows(); 60 if (win.length == 0) { 61 return; 62 } 63 Display display = win[0].getShell().getDisplay(); 64 if (display == null) { 65 return; 66 } 67 ArrayList appearList = new ArrayList (5); 68 ArrayList revokeList = new ArrayList (5); 69 String id = null; 70 int numPerspectives = 0; 71 int numActionSetPartAssoc = 0; 72 73 for (int i = 0; i < delta.length; i++) { 77 id = delta[i].getExtensionPoint().getSimpleIdentifier(); 78 if (delta[i].getKind() == IExtensionDelta.ADDED) { 79 if (id.equals(IWorkbenchRegistryConstants.PL_ACTION_SETS)) { 80 appearList.add(0, delta[i]); 81 } else if (!id.equals(IWorkbenchRegistryConstants.PL_PERSPECTIVES) 82 && !id.equals(IWorkbenchRegistryConstants.PL_VIEWS) 83 && !id.equals(IWorkbenchRegistryConstants.PL_ACTION_SETS)) { 84 appearList.add(appearList.size() - numPerspectives, 85 delta[i]); 86 } 87 } else { 88 if (delta[i].getKind() == IExtensionDelta.REMOVED) { 89 if (id 90 .equals(IWorkbenchRegistryConstants.PL_ACTION_SET_PART_ASSOCIATIONS)) { 91 revokeList.add(0, delta[i]); 92 numActionSetPartAssoc++; 93 } else if (id 94 .equals(IWorkbenchRegistryConstants.PL_PERSPECTIVES)) { 95 revokeList.add(numActionSetPartAssoc, delta[i]); 96 } else { 97 revokeList.add(delta[i]); 98 } 99 } 100 } 101 } 102 Iterator iter = appearList.iterator(); 103 IExtensionDelta extDelta = null; 104 while (iter.hasNext()) { 105 extDelta = (IExtensionDelta) iter.next(); 106 extPt = extDelta.getExtensionPoint(); 107 ext = extDelta.getExtension(); 108 asyncAppear(display, extPt, ext); 109 } 110 119 resetCurrentPerspective(display); 120 } finally { 121 changeList.clear(); 123 } 124 125 } 126 127 private void asyncAppear(Display display, final IExtensionPoint extpt, 128 final IExtension ext) { 129 Runnable run = new Runnable () { 130 public void run() { 131 appear(extpt, ext); 132 } 133 }; 134 display.syncExec(run); 135 } 136 137 private void appear(IExtensionPoint extPt, IExtension ext) { 138 String name = extPt.getSimpleIdentifier(); 139 if (name.equalsIgnoreCase(IWorkbenchRegistryConstants.PL_FONT_DEFINITIONS)) { 140 loadFontDefinitions(ext); 141 return; 142 } 143 if (name.equalsIgnoreCase(IWorkbenchRegistryConstants.PL_THEMES)) { 144 loadThemes(ext); 145 return; 146 } 147 } 148 149 152 private void loadFontDefinitions(IExtension ext) { 153 ThemeRegistryReader reader = new ThemeRegistryReader(); 154 reader.setRegistry((ThemeRegistry) WorkbenchPlugin.getDefault() 155 .getThemeRegistry()); 156 IConfigurationElement[] elements = ext.getConfigurationElements(); 157 for (int i = 0; i < elements.length; i++) { 158 reader.readElement(elements[i]); 159 } 160 161 Collection fonts = reader.getFontDefinitions(); 162 FontDefinition[] fontDefs = (FontDefinition[]) fonts 163 .toArray(new FontDefinition[fonts.size()]); 164 ThemeElementHelper.populateRegistry(workbench.getThemeManager() 165 .getTheme(IThemeManager.DEFAULT_THEME), fontDefs, PrefUtil 166 .getInternalPreferenceStore()); 167 } 168 169 private void loadThemes(IExtension ext) { 171 ThemeRegistryReader reader = new ThemeRegistryReader(); 172 ThemeRegistry registry = (ThemeRegistry) WorkbenchPlugin.getDefault() 173 .getThemeRegistry(); 174 reader.setRegistry(registry); 175 IConfigurationElement[] elements = ext.getConfigurationElements(); 176 for (int i = 0; i < elements.length; i++) { 177 reader.readElement(elements[i]); 178 } 179 180 Collection colors = reader.getColorDefinitions(); 181 ColorDefinition[] colorDefs = (ColorDefinition[]) colors 182 .toArray(new ColorDefinition[colors.size()]); 183 184 ITheme theme = workbench.getThemeManager().getTheme( 185 IThemeManager.DEFAULT_THEME); 186 ThemeElementHelper.populateRegistry(theme, colorDefs, PrefUtil 187 .getInternalPreferenceStore()); 188 189 Collection fonts = reader.getFontDefinitions(); 190 FontDefinition[] fontDefs = (FontDefinition[]) fonts 191 .toArray(new FontDefinition[fonts.size()]); 192 ThemeElementHelper.populateRegistry(theme, fontDefs, PrefUtil 193 .getInternalPreferenceStore()); 194 195 Map data = reader.getData(); 196 registry.addData(data); 197 } 198 199 private void resetCurrentPerspective(Display display) { 200 if (changeList.isEmpty()) { 201 return; 202 } 203 204 final StringBuffer message = new StringBuffer ( 205 ExtensionEventHandlerMessages.ExtensionEventHandler_following_changes); 206 207 for (Iterator i = changeList.iterator(); i.hasNext();) { 208 message.append(i.next()); 209 } 210 211 message.append(ExtensionEventHandlerMessages.ExtensionEventHandler_need_to_reset); 212 213 display.asyncExec(new Runnable () { 214 public void run() { 215 Shell parentShell = null; 216 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 217 if (window == null) { 218 if (workbench.getWorkbenchWindowCount() == 0) { 219 return; 220 } 221 window = workbench.getWorkbenchWindows()[0]; 222 } 223 224 parentShell = window.getShell(); 225 226 if (MessageDialog 227 .openQuestion( 228 parentShell, 229 ExtensionEventHandlerMessages.ExtensionEventHandler_reset_perspective, message.toString())) { 230 IWorkbenchPage page = window.getActivePage(); 231 if (page == null) { 232 return; 233 } 234 page.resetPerspective(); 235 } 236 } 237 }); 238 239 } 240 } 241 | Popular Tags |