KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > ws > TriggerPointAdvisorRegistry


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.activities.ws;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IExtension;
17 import org.eclipse.core.runtime.IExtensionPoint;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.WorkbenchPlugin;
23 import org.eclipse.ui.internal.misc.StatusUtil;
24 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
25 import org.eclipse.ui.internal.registry.RegistryReader;
26
27 /**
28  * @since 3.1
29  */

30 public class TriggerPointAdvisorRegistry {
31
32     private static TriggerPointAdvisorRegistry instance;
33
34     /**
35      *
36      */

37     private TriggerPointAdvisorRegistry() {
38     }
39
40     /**
41      * Return the instance of this registry.
42      *
43      * @return the instance of this registry
44      */

45     public static TriggerPointAdvisorRegistry getInstance() {
46         if (instance == null) {
47             instance = new TriggerPointAdvisorRegistry();
48         }
49
50         return instance;
51     }
52
53     /**
54      * Return the trigger point advisors.
55      *
56      * @return the advisors
57      */

58     public TriggerPointAdvisorDescriptor[] getAdvisors() {
59         IExtensionPoint point = Platform.getExtensionRegistry()
60                 .getExtensionPoint(PlatformUI.PLUGIN_ID,
61                         IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT);
62         if (point == null) {
63             return new TriggerPointAdvisorDescriptor[0];
64         }
65
66         IExtension[] extensions = point.getExtensions();
67         extensions = RegistryReader.orderExtensions(extensions);
68
69         ArrayList JavaDoc list = new ArrayList JavaDoc(extensions.length);
70         for (int i = 0; i < extensions.length; i++) {
71             IConfigurationElement[] elements = extensions[i]
72                     .getConfigurationElements();
73             for (int j = 0; j < elements.length; j++) {
74                 if (elements[j].getName().equals(
75                         IWorkbenchRegistryConstants.TAG_TRIGGERPOINTADVISOR)) {
76                     try {
77                         TriggerPointAdvisorDescriptor descriptor = new TriggerPointAdvisorDescriptor(
78                                 elements[j]);
79                         list.add(descriptor);
80                     } catch (IllegalArgumentException JavaDoc e) {
81                         // log an error since its not safe to open a dialog here
82
WorkbenchPlugin.log(
83                                 "invalid trigger point advisor extension", //$NON-NLS-1$
84
StatusUtil.newStatus(IStatus.ERROR, e
85                                         .getMessage(), e));
86                     }
87                 }
88             }
89         }
90
91         return (TriggerPointAdvisorDescriptor[]) list
92                 .toArray(new TriggerPointAdvisorDescriptor[list.size()]);
93     }
94
95     /**
96      * Return the trigger point advisor bound to a given product.
97      *
98      * @param productId
99      * the product id
100      * @return the advisor
101      */

102     public TriggerPointAdvisorDescriptor getAdvisorForProduct(String JavaDoc productId) {
103         IExtensionPoint point = Platform.getExtensionRegistry()
104                 .getExtensionPoint(PlatformUI.PLUGIN_ID,
105                         IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT);
106         if (point == null) {
107             return null;
108         }
109
110         IExtension[] extensions = point.getExtensions();
111         extensions = RegistryReader.orderExtensions(extensions);
112
113         String JavaDoc targetIntroId = getAdvisorForProduct(productId, extensions);
114         if (targetIntroId == null) {
115             return null;
116         }
117
118         TriggerPointAdvisorDescriptor[] advisors = getAdvisors();
119         for (int i = 0; i < advisors.length; i++) {
120             if (advisors[i].getId().equals(targetIntroId)) {
121                 return advisors[i];
122             }
123         }
124
125         return null;
126     }
127
128     /**
129      * @param targetProductId
130      * @param extensions
131      * @return the advisor id
132      */

133     private String JavaDoc getAdvisorForProduct(String JavaDoc targetProductId,
134             IExtension[] extensions) {
135         for (int i = 0; i < extensions.length; i++) {
136             IConfigurationElement[] elements = extensions[i]
137                     .getConfigurationElements();
138             for (int j = 0; j < elements.length; j++) {
139                 if (elements[j].getName().equals(
140                         IWorkbenchRegistryConstants.TAG_ADVISORPRODUCTBINDING)) {
141                     String JavaDoc advisorId = elements[j]
142                             .getAttribute(IWorkbenchRegistryConstants.ATT_ADVISORID);
143                     String JavaDoc productId = elements[j]
144                             .getAttribute(IWorkbenchRegistryConstants.ATT_PRODUCTID);
145
146                     if (advisorId == null || productId == null) {
147                         IStatus status = new Status(
148                                 IStatus.ERROR,
149                                 elements[j].getDeclaringExtension()
150                                         .getNamespace(),
151                                 IStatus.ERROR,
152                                 "triggerPointAdvisorId and productId must be defined.", new IllegalArgumentException JavaDoc()); //$NON-NLS-1$
153
WorkbenchPlugin
154                                 .log(
155                                         "Invalid trigger point advisor binding", status); //$NON-NLS-1$
156
continue;
157                     }
158
159                     if (targetProductId.equals(productId)) {
160                         return advisorId;
161                     }
162                 }
163             }
164         }
165         return null;
166     }
167
168 }
169
Popular Tags