KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ExtensionEventHandler


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
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 JavaDoc changeList = new ArrayList JavaDoc(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 JavaDoc appearList = new ArrayList JavaDoc(5);
68             ArrayList JavaDoc revokeList = new ArrayList JavaDoc(5);
69             String JavaDoc id = null;
70             int numPerspectives = 0;
71             int numActionSetPartAssoc = 0;
72
73             // push action sets and perspectives to the top because incoming
74
// actionSetPartAssociations and perspectiveExtensions may depend upon
75
// them for their bindings.
76
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 JavaDoc 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             // Suspend support for removing a plug-in until this is more stable
111
// iter = revokeList.iterator();
112
// while(iter.hasNext()) {
113
// extDelta = (IExtensionDelta) iter.next();
114
// extPt = extDelta.getExtensionPoint();
115
// ext = extDelta.getExtension();
116
// asyncRevoke(display, extPt, ext);
117
// }
118

119             resetCurrentPerspective(display);
120         } finally {
121             //ensure the list is cleared for the next pass through
122
changeList.clear();
123         }
124
125     }
126
127     private void asyncAppear(Display display, final IExtensionPoint extpt,
128             final IExtension ext) {
129         Runnable JavaDoc run = new Runnable JavaDoc() {
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 JavaDoc 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     /**
150      * @param ext
151      */

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 JavaDoc 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     //TODO: confirm
170
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc message = new StringBuffer JavaDoc(
205                 ExtensionEventHandlerMessages.ExtensionEventHandler_following_changes);
206
207         for (Iterator JavaDoc 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 JavaDoc() {
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