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.wizards;
011:
012: import org.eclipse.core.runtime.IConfigurationElement;
013: import org.eclipse.core.runtime.IExtension;
014: import org.eclipse.core.runtime.IExtensionPoint;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
017: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
018: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
019: import org.eclipse.ui.PlatformUI;
020: import org.eclipse.ui.internal.dialogs.WizardCollectionElement;
021: import org.eclipse.ui.internal.dialogs.WorkbenchWizardElement;
022: import org.eclipse.ui.internal.registry.WizardsRegistryReader;
023: import org.eclipse.ui.internal.util.Util;
024: import org.eclipse.ui.wizards.IWizardDescriptor;
025:
026: /**
027: * Abstract baseclass for wizard registries that listen to extension changes.
028: *
029: * @since 3.1
030: */
031: public abstract class AbstractExtensionWizardRegistry extends
032: AbstractWizardRegistry implements IExtensionChangeHandler {
033:
034: /**
035: * Create a new instance of this class.
036: */
037: public AbstractExtensionWizardRegistry() {
038: super ();
039: }
040:
041: /* (non-Javadoc)
042: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
043: */
044: public void addExtension(IExtensionTracker tracker,
045: IExtension extension) {
046: WizardsRegistryReader reader = new WizardsRegistryReader(
047: getPlugin(), getExtensionPoint());
048: reader.setInitialCollection(getWizardElements());
049: IConfigurationElement[] configurationElements = extension
050: .getConfigurationElements();
051: for (int i = 0; i < configurationElements.length; i++) {
052: reader.readElement(configurationElements[i]);
053: }
054: // no need to reset the wizard elements - getWizardElements will parse
055: // the
056: // results of the registry reading
057: setWizardElements(reader.getWizardElements());
058: // reregister all object handles - it'd be better to process the deltas
059: // in this case
060: registerWizards(getWizardElements());
061:
062: // handle the primary wizards
063: WorkbenchWizardElement[] additionalPrimary = reader
064: .getPrimaryWizards();
065: if (additionalPrimary.length == 0) {
066: return;
067: }
068: IWizardDescriptor[] localPrimaryWizards = getPrimaryWizards();
069: WorkbenchWizardElement[] newPrimary = new WorkbenchWizardElement[additionalPrimary.length
070: + localPrimaryWizards.length];
071: System.arraycopy(localPrimaryWizards, 0, newPrimary, 0,
072: localPrimaryWizards.length);
073: System.arraycopy(additionalPrimary, 0, newPrimary,
074: localPrimaryWizards.length, additionalPrimary.length);
075: setPrimaryWizards(newPrimary);
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.ui.internal.wizards.AbstractWizardRegistry#dispose()
080: */
081: public void dispose() {
082: super .dispose();
083: PlatformUI.getWorkbench().getExtensionTracker()
084: .unregisterHandler(this );
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.eclipse.ui.internal.wizards.AbstractWizardRegistry#doInitialize()
091: */
092: protected void doInitialize() {
093:
094: PlatformUI
095: .getWorkbench()
096: .getExtensionTracker()
097: .registerHandler(
098: this ,
099: ExtensionTracker
100: .createExtensionPointFilter(getExtensionPointFilter()));
101:
102: WizardsRegistryReader reader = new WizardsRegistryReader(
103: getPlugin(), getExtensionPoint());
104: setWizardElements(reader.getWizardElements());
105: setPrimaryWizards(reader.getPrimaryWizards());
106: registerWizards(getWizardElements());
107: }
108:
109: /**
110: * Return the extension point id that should be used for extension registry
111: * queries.
112: *
113: * @return the extension point id
114: */
115: protected abstract String getExtensionPoint();
116:
117: private IExtensionPoint getExtensionPointFilter() {
118: return Platform.getExtensionRegistry().getExtensionPoint(
119: getPlugin(), getExtensionPoint());
120: }
121:
122: /**
123: * Return the plugin id that should be used for extension registry queries.
124: *
125: * @return the plugin id
126: */
127: protected abstract String getPlugin();
128:
129: /**
130: * Register the object with the workbench tracker.
131: *
132: * @param extension
133: * the originating extension
134: * @param object
135: * the object to track
136: */
137: private void register(IExtension extension, Object object) {
138: PlatformUI.getWorkbench().getExtensionTracker().registerObject(
139: extension, object, IExtensionTracker.REF_WEAK);
140: }
141:
142: /**
143: * Register all wizards in the given collection with the extension tracker.
144: *
145: * @param collection
146: * the collection to register
147: */
148: private void registerWizards(WizardCollectionElement collection) {
149: registerWizards(collection.getWorkbenchWizardElements());
150:
151: WizardCollectionElement[] collections = collection
152: .getCollectionElements();
153: for (int i = 0; i < collections.length; i++) {
154: IConfigurationElement configurationElement = collections[i]
155: .getConfigurationElement();
156: if (configurationElement != null) {
157: register(configurationElement.getDeclaringExtension(),
158: collections[i]);
159: }
160: registerWizards(collections[i]);
161: }
162: }
163:
164: /**
165: * Register all wizards in the given array.
166: *
167: * @param wizards
168: * the wizards to register
169: */
170: private void registerWizards(WorkbenchWizardElement[] wizards) {
171: for (int i = 0; i < wizards.length; i++) {
172: register(wizards[i].getConfigurationElement()
173: .getDeclaringExtension(), wizards[i]);
174: }
175: }
176:
177: /* (non-Javadoc)
178: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
179: */
180: public void removeExtension(IExtension extension, Object[] objects) {
181: if (!extension.getExtensionPointUniqueIdentifier().equals(
182: getExtensionPointFilter().getUniqueIdentifier())) {
183: return;
184: }
185: for (int i = 0; i < objects.length; i++) {
186: Object object = objects[i];
187: if (object instanceof WizardCollectionElement) {
188: // TODO: should we move child wizards to the "other" node?
189: WizardCollectionElement collection = (WizardCollectionElement) object;
190: collection.getParentCollection().remove(collection);
191: } else if (object instanceof WorkbenchWizardElement) {
192: WorkbenchWizardElement wizard = (WorkbenchWizardElement) object;
193: WizardCollectionElement parent = wizard
194: .getCollectionElement();
195: if (parent != null) {
196: parent.remove(wizard);
197: }
198: IWizardDescriptor[] primaryWizards = getPrimaryWizards();
199: for (int j = 0; j < primaryWizards.length; j++) {
200: if (primaryWizards[j] == wizard) {
201: WorkbenchWizardElement[] newPrimary = new WorkbenchWizardElement[primaryWizards.length - 1];
202: Util.arrayCopyWithRemoval(primaryWizards,
203: newPrimary, j);
204: primaryWizards = newPrimary;
205: break;
206: }
207: }
208: setPrimaryWizards((WorkbenchWizardElement[]) primaryWizards);
209: }
210: }
211: }
212: }
|