001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017:
018: import org.eclipse.core.runtime.IConfigurationElement;
019: import org.eclipse.core.runtime.IExtension;
020: import org.eclipse.core.runtime.IExtensionDelta;
021: import org.eclipse.core.runtime.IExtensionPoint;
022: import org.eclipse.core.runtime.IRegistryChangeEvent;
023: import org.eclipse.core.runtime.IRegistryChangeListener;
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.swt.widgets.Display;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.ui.IWorkbenchPage;
028: import org.eclipse.ui.IWorkbenchWindow;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
031: import org.eclipse.ui.internal.themes.ColorDefinition;
032: import org.eclipse.ui.internal.themes.FontDefinition;
033: import org.eclipse.ui.internal.themes.ThemeElementHelper;
034: import org.eclipse.ui.internal.themes.ThemeRegistry;
035: import org.eclipse.ui.internal.themes.ThemeRegistryReader;
036: import org.eclipse.ui.internal.util.PrefUtil;
037: import org.eclipse.ui.themes.ITheme;
038: import org.eclipse.ui.themes.IThemeManager;
039:
040: class ExtensionEventHandler implements IRegistryChangeListener {
041:
042: private Workbench workbench;
043:
044: private List changeList = new ArrayList(10);
045:
046: public ExtensionEventHandler(Workbench workbench) {
047: this .workbench = workbench;
048: }
049:
050: public void registryChanged(IRegistryChangeEvent event) {
051: try {
052: IExtensionDelta delta[] = event
053: .getExtensionDeltas(WorkbenchPlugin.PI_WORKBENCH);
054: IExtension ext;
055: IExtensionPoint extPt;
056: IWorkbenchWindow[] win = PlatformUI.getWorkbench()
057: .getWorkbenchWindows();
058: if (win.length == 0) {
059: return;
060: }
061: Display display = win[0].getShell().getDisplay();
062: if (display == null) {
063: return;
064: }
065: ArrayList appearList = new ArrayList(5);
066: ArrayList revokeList = new ArrayList(5);
067: String id = null;
068: int numPerspectives = 0;
069: int numActionSetPartAssoc = 0;
070:
071: // push action sets and perspectives to the top because incoming
072: // actionSetPartAssociations and perspectiveExtensions may depend upon
073: // them for their bindings.
074: for (int i = 0; i < delta.length; i++) {
075: id = delta[i].getExtensionPoint().getSimpleIdentifier();
076: if (delta[i].getKind() == IExtensionDelta.ADDED) {
077: if (id
078: .equals(IWorkbenchRegistryConstants.PL_ACTION_SETS)) {
079: appearList.add(0, delta[i]);
080: } else if (!id
081: .equals(IWorkbenchRegistryConstants.PL_PERSPECTIVES)
082: && !id
083: .equals(IWorkbenchRegistryConstants.PL_VIEWS)
084: && !id
085: .equals(IWorkbenchRegistryConstants.PL_ACTION_SETS)) {
086: appearList.add(appearList.size()
087: - numPerspectives, delta[i]);
088: }
089: } else {
090: if (delta[i].getKind() == IExtensionDelta.REMOVED) {
091: if (id
092: .equals(IWorkbenchRegistryConstants.PL_ACTION_SET_PART_ASSOCIATIONS)) {
093: revokeList.add(0, delta[i]);
094: numActionSetPartAssoc++;
095: } else if (id
096: .equals(IWorkbenchRegistryConstants.PL_PERSPECTIVES)) {
097: revokeList.add(numActionSetPartAssoc,
098: delta[i]);
099: } else {
100: revokeList.add(delta[i]);
101: }
102: }
103: }
104: }
105: Iterator iter = appearList.iterator();
106: IExtensionDelta extDelta = null;
107: while (iter.hasNext()) {
108: extDelta = (IExtensionDelta) iter.next();
109: extPt = extDelta.getExtensionPoint();
110: ext = extDelta.getExtension();
111: asyncAppear(display, extPt, ext);
112: }
113: // Suspend support for removing a plug-in until this is more stable
114: // iter = revokeList.iterator();
115: // while(iter.hasNext()) {
116: // extDelta = (IExtensionDelta) iter.next();
117: // extPt = extDelta.getExtensionPoint();
118: // ext = extDelta.getExtension();
119: // asyncRevoke(display, extPt, ext);
120: // }
121:
122: resetCurrentPerspective(display);
123: } finally {
124: //ensure the list is cleared for the next pass through
125: changeList.clear();
126: }
127:
128: }
129:
130: private void asyncAppear(Display display,
131: final IExtensionPoint extpt, final IExtension ext) {
132: Runnable run = new Runnable() {
133: public void run() {
134: appear(extpt, ext);
135: }
136: };
137: display.syncExec(run);
138: }
139:
140: private void appear(IExtensionPoint extPt, IExtension ext) {
141: String name = extPt.getSimpleIdentifier();
142: if (name
143: .equalsIgnoreCase(IWorkbenchRegistryConstants.PL_FONT_DEFINITIONS)) {
144: loadFontDefinitions(ext);
145: return;
146: }
147: if (name
148: .equalsIgnoreCase(IWorkbenchRegistryConstants.PL_THEMES)) {
149: loadThemes(ext);
150: return;
151: }
152: }
153:
154: /**
155: * @param ext
156: */
157: private void loadFontDefinitions(IExtension ext) {
158: ThemeRegistryReader reader = new ThemeRegistryReader();
159: reader.setRegistry((ThemeRegistry) WorkbenchPlugin.getDefault()
160: .getThemeRegistry());
161: IConfigurationElement[] elements = ext
162: .getConfigurationElements();
163: for (int i = 0; i < elements.length; i++) {
164: reader.readElement(elements[i]);
165: }
166:
167: Collection fonts = reader.getFontDefinitions();
168: FontDefinition[] fontDefs = (FontDefinition[]) fonts
169: .toArray(new FontDefinition[fonts.size()]);
170: ThemeElementHelper.populateRegistry(workbench.getThemeManager()
171: .getTheme(IThemeManager.DEFAULT_THEME), fontDefs,
172: PrefUtil.getInternalPreferenceStore());
173: }
174:
175: //TODO: confirm
176: private void loadThemes(IExtension ext) {
177: ThemeRegistryReader reader = new ThemeRegistryReader();
178: ThemeRegistry registry = (ThemeRegistry) WorkbenchPlugin
179: .getDefault().getThemeRegistry();
180: reader.setRegistry(registry);
181: IConfigurationElement[] elements = ext
182: .getConfigurationElements();
183: for (int i = 0; i < elements.length; i++) {
184: reader.readElement(elements[i]);
185: }
186:
187: Collection colors = reader.getColorDefinitions();
188: ColorDefinition[] colorDefs = (ColorDefinition[]) colors
189: .toArray(new ColorDefinition[colors.size()]);
190:
191: ITheme theme = workbench.getThemeManager().getTheme(
192: IThemeManager.DEFAULT_THEME);
193: ThemeElementHelper.populateRegistry(theme, colorDefs, PrefUtil
194: .getInternalPreferenceStore());
195:
196: Collection fonts = reader.getFontDefinitions();
197: FontDefinition[] fontDefs = (FontDefinition[]) fonts
198: .toArray(new FontDefinition[fonts.size()]);
199: ThemeElementHelper.populateRegistry(theme, fontDefs, PrefUtil
200: .getInternalPreferenceStore());
201:
202: Map data = reader.getData();
203: registry.addData(data);
204: }
205:
206: private void resetCurrentPerspective(Display display) {
207: if (changeList.isEmpty()) {
208: return;
209: }
210:
211: final StringBuffer message = new StringBuffer(
212: ExtensionEventHandlerMessages.ExtensionEventHandler_following_changes);
213:
214: for (Iterator i = changeList.iterator(); i.hasNext();) {
215: message.append(i.next());
216: }
217:
218: message
219: .append(ExtensionEventHandlerMessages.ExtensionEventHandler_need_to_reset);
220:
221: display.asyncExec(new Runnable() {
222: public void run() {
223: Shell parentShell = null;
224: IWorkbenchWindow window = workbench
225: .getActiveWorkbenchWindow();
226: if (window == null) {
227: if (workbench.getWorkbenchWindowCount() == 0) {
228: return;
229: }
230: window = workbench.getWorkbenchWindows()[0];
231: }
232:
233: parentShell = window.getShell();
234:
235: if (MessageDialog
236: .openQuestion(
237: parentShell,
238: ExtensionEventHandlerMessages.ExtensionEventHandler_reset_perspective,
239: message.toString())) {
240: IWorkbenchPage page = window.getActivePage();
241: if (page == null) {
242: return;
243: }
244: page.resetPerspective();
245: }
246: }
247: });
248:
249: }
250: }
|