KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > AbstractExtensionWizardRegistry


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 package org.eclipse.ui.internal.wizards;
12
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.core.runtime.IExtension;
15 import org.eclipse.core.runtime.IExtensionPoint;
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
18 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
19 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.internal.dialogs.WizardCollectionElement;
22 import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
23 import org.eclipse.ui.internal.registry.WizardsRegistryReader;
24 import org.eclipse.ui.internal.util.Util;
25 import org.eclipse.ui.wizards.IWizardDescriptor;
26
27 /**
28  * Abstract baseclass for wizard registries that listen to extension changes.
29  *
30  * @since 3.1
31  */

32 public abstract class AbstractExtensionWizardRegistry extends
33         AbstractWizardRegistry implements IExtensionChangeHandler{
34
35     /**
36      * Create a new instance of this class.
37      */

38     public AbstractExtensionWizardRegistry() {
39         super();
40     }
41
42     /* (non-Javadoc)
43      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
44      */

45     public void addExtension(IExtensionTracker tracker, IExtension extension) {
46         WizardsRegistryReader reader = new WizardsRegistryReader(getPlugin(),
47                 getExtensionPoint());
48         reader.setInitialCollection(getWizardElements());
49         IConfigurationElement[] configurationElements = extension
50                 .getConfigurationElements();
51         for (int i = 0; i < configurationElements.length; i++) {
52             reader.readElement(configurationElements[i]);
53         }
54         // no need to reset the wizard elements - getWizardElements will parse
55
// the
56
// results of the registry reading
57
setWizardElements(reader.getWizardElements());
58         // reregister all object handles - it'd be better to process the deltas
59
// in this case
60
registerWizards(getWizardElements());
61
62         // handle the primary wizards
63
WorkbenchWizardElement[] additionalPrimary = reader.getPrimaryWizards();
64         if (additionalPrimary.length == 0) {
65             return;
66         }
67         IWizardDescriptor[] localPrimaryWizards = getPrimaryWizards();
68         WorkbenchWizardElement[] newPrimary = new WorkbenchWizardElement[additionalPrimary.length
69                 + localPrimaryWizards.length];
70         System.arraycopy(localPrimaryWizards, 0, newPrimary, 0,
71                 localPrimaryWizards.length);
72         System.arraycopy(additionalPrimary, 0, newPrimary,
73                 localPrimaryWizards.length, additionalPrimary.length);
74         setPrimaryWizards(newPrimary);
75     }
76     
77     /* (non-Javadoc)
78      * @see org.eclipse.ui.internal.wizards.AbstractWizardRegistry#dispose()
79      */

80     public void dispose() {
81         super.dispose();
82         PlatformUI.getWorkbench().getExtensionTracker()
83                 .unregisterHandler(this);
84     }
85
86     /*
87      * (non-Javadoc)
88      *
89      * @see org.eclipse.ui.internal.wizards.AbstractWizardRegistry#doInitialize()
90      */

91     protected void doInitialize() {
92         
93         PlatformUI.getWorkbench().getExtensionTracker().registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter()));
94
95         WizardsRegistryReader reader = new WizardsRegistryReader(getPlugin(),
96                 getExtensionPoint());
97         setWizardElements(reader.getWizardElements());
98         setPrimaryWizards(reader.getPrimaryWizards());
99         registerWizards(getWizardElements());
100     }
101
102     /**
103      * Return the extension point id that should be used for extension registry
104      * queries.
105      *
106      * @return the extension point id
107      */

108     protected abstract String JavaDoc getExtensionPoint();
109
110     private IExtensionPoint getExtensionPointFilter() {
111         return Platform.getExtensionRegistry().getExtensionPoint(getPlugin(),
112                 getExtensionPoint());
113     }
114
115     /**
116      * Return the plugin id that should be used for extension registry queries.
117      *
118      * @return the plugin id
119      */

120     protected abstract String JavaDoc getPlugin();
121
122     /**
123      * Register the object with the workbench tracker.
124      *
125      * @param extension
126      * the originating extension
127      * @param object
128      * the object to track
129      */

130     private void register(IExtension extension, Object JavaDoc object) {
131         PlatformUI.getWorkbench().getExtensionTracker().registerObject(
132                 extension, object, IExtensionTracker.REF_WEAK);
133     }
134
135     /**
136      * Register all wizards in the given collection with the extension tracker.
137      *
138      * @param collection
139      * the collection to register
140      */

141     private void registerWizards(WizardCollectionElement collection) {
142         registerWizards(collection.getWorkbenchWizardElements());
143
144         WizardCollectionElement[] collections = collection
145                 .getCollectionElements();
146         for (int i = 0; i < collections.length; i++) {
147             IConfigurationElement configurationElement = collections[i]
148                     .getConfigurationElement();
149             if (configurationElement != null) {
150                 register(configurationElement.getDeclaringExtension(),
151                         collections[i]);
152             }
153             registerWizards(collections[i]);
154         }
155     }
156
157     /**
158      * Register all wizards in the given array.
159      *
160      * @param wizards
161      * the wizards to register
162      */

163     private void registerWizards(WorkbenchWizardElement[] wizards) {
164         for (int i = 0; i < wizards.length; i++) {
165             register(wizards[i].getConfigurationElement()
166                     .getDeclaringExtension(), wizards[i]);
167         }
168     }
169
170     /* (non-Javadoc)
171      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
172      */

173     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
174         if (!extension.getExtensionPointUniqueIdentifier().equals(
175                 getExtensionPointFilter().getUniqueIdentifier())) {
176             return;
177         }
178         for (int i = 0; i < objects.length; i++) {
179             Object JavaDoc object = objects[i];
180             if (object instanceof WizardCollectionElement) {
181                 // TODO: should we move child wizards to the "other" node?
182
WizardCollectionElement collection = (WizardCollectionElement) object;
183                 collection.getParentCollection().remove(collection);
184             } else if (object instanceof WorkbenchWizardElement) {
185                 WorkbenchWizardElement wizard = (WorkbenchWizardElement) object;
186                 WizardCollectionElement parent = wizard.getCollectionElement();
187                 if (parent != null) {
188                     parent.remove(wizard);
189                 }
190                 IWizardDescriptor[] primaryWizards = getPrimaryWizards();
191                 for (int j = 0; j < primaryWizards.length; j++) {
192                     if (primaryWizards[j] == wizard) {
193                         WorkbenchWizardElement[] newPrimary = new WorkbenchWizardElement[primaryWizards.length - 1];
194                         Util
195                                 .arrayCopyWithRemoval(primaryWizards,
196                                         newPrimary, j);
197                         primaryWizards = newPrimary;
198                         break;
199                     }
200                 }
201                 setPrimaryWizards((WorkbenchWizardElement[]) primaryWizards);
202             }
203         }
204     }
205 }
206
Popular Tags