001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 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.activities.ws;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IExtension;
016: import org.eclipse.core.runtime.IExtensionPoint;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Platform;
019: import org.eclipse.core.runtime.Status;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.WorkbenchPlugin;
022: import org.eclipse.ui.internal.misc.StatusUtil;
023: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
024: import org.eclipse.ui.internal.registry.RegistryReader;
025:
026: /**
027: * @since 3.1
028: */
029: public class TriggerPointAdvisorRegistry {
030:
031: private static TriggerPointAdvisorRegistry instance;
032:
033: /**
034: *
035: */
036: private TriggerPointAdvisorRegistry() {
037: }
038:
039: /**
040: * Return the instance of this registry.
041: *
042: * @return the instance of this registry
043: */
044: public static TriggerPointAdvisorRegistry getInstance() {
045: if (instance == null) {
046: instance = new TriggerPointAdvisorRegistry();
047: }
048:
049: return instance;
050: }
051:
052: /**
053: * Return the trigger point advisors.
054: *
055: * @return the advisors
056: */
057: public TriggerPointAdvisorDescriptor[] getAdvisors() {
058: IExtensionPoint point = Platform.getExtensionRegistry()
059: .getExtensionPoint(PlatformUI.PLUGIN_ID,
060: IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT);
061: if (point == null) {
062: return new TriggerPointAdvisorDescriptor[0];
063: }
064:
065: IExtension[] extensions = point.getExtensions();
066: extensions = RegistryReader.orderExtensions(extensions);
067:
068: ArrayList list = new ArrayList(extensions.length);
069: for (int i = 0; i < extensions.length; i++) {
070: IConfigurationElement[] elements = extensions[i]
071: .getConfigurationElements();
072: for (int j = 0; j < elements.length; j++) {
073: if (elements[j]
074: .getName()
075: .equals(
076: IWorkbenchRegistryConstants.TAG_TRIGGERPOINTADVISOR)) {
077: try {
078: TriggerPointAdvisorDescriptor descriptor = new TriggerPointAdvisorDescriptor(
079: elements[j]);
080: list.add(descriptor);
081: } catch (IllegalArgumentException e) {
082: // log an error since its not safe to open a dialog here
083: WorkbenchPlugin
084: .log(
085: "invalid trigger point advisor extension", //$NON-NLS-1$
086: StatusUtil.newStatus(
087: IStatus.ERROR, e
088: .getMessage(),
089: e));
090: }
091: }
092: }
093: }
094:
095: return (TriggerPointAdvisorDescriptor[]) list
096: .toArray(new TriggerPointAdvisorDescriptor[list.size()]);
097: }
098:
099: /**
100: * Return the trigger point advisor bound to a given product.
101: *
102: * @param productId
103: * the product id
104: * @return the advisor
105: */
106: public TriggerPointAdvisorDescriptor getAdvisorForProduct(
107: String productId) {
108: IExtensionPoint point = Platform.getExtensionRegistry()
109: .getExtensionPoint(PlatformUI.PLUGIN_ID,
110: IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT);
111: if (point == null) {
112: return null;
113: }
114:
115: IExtension[] extensions = point.getExtensions();
116: extensions = RegistryReader.orderExtensions(extensions);
117:
118: String targetIntroId = getAdvisorForProduct(productId,
119: extensions);
120: if (targetIntroId == null) {
121: return null;
122: }
123:
124: TriggerPointAdvisorDescriptor[] advisors = getAdvisors();
125: for (int i = 0; i < advisors.length; i++) {
126: if (advisors[i].getId().equals(targetIntroId)) {
127: return advisors[i];
128: }
129: }
130:
131: return null;
132: }
133:
134: /**
135: * @param targetProductId
136: * @param extensions
137: * @return the advisor id
138: */
139: private String getAdvisorForProduct(String targetProductId,
140: IExtension[] extensions) {
141: for (int i = 0; i < extensions.length; i++) {
142: IConfigurationElement[] elements = extensions[i]
143: .getConfigurationElements();
144: for (int j = 0; j < elements.length; j++) {
145: if (elements[j]
146: .getName()
147: .equals(
148: IWorkbenchRegistryConstants.TAG_ADVISORPRODUCTBINDING)) {
149: String advisorId = elements[j]
150: .getAttribute(IWorkbenchRegistryConstants.ATT_ADVISORID);
151: String productId = elements[j]
152: .getAttribute(IWorkbenchRegistryConstants.ATT_PRODUCTID);
153:
154: if (advisorId == null || productId == null) {
155: IStatus status = new Status(
156: IStatus.ERROR,
157: elements[j].getDeclaringExtension()
158: .getNamespace(),
159: IStatus.ERROR,
160: "triggerPointAdvisorId and productId must be defined.", new IllegalArgumentException()); //$NON-NLS-1$
161: WorkbenchPlugin
162: .log(
163: "Invalid trigger point advisor binding", status); //$NON-NLS-1$
164: continue;
165: }
166:
167: if (targetProductId.equals(productId)) {
168: return advisorId;
169: }
170: }
171: }
172: }
173: return null;
174: }
175:
176: }
|