KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > IntroRegistry


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.intro;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExtension;
18 import org.eclipse.core.runtime.IExtensionPoint;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.ui.PlatformUI;
23 import org.eclipse.ui.internal.WorkbenchPlugin;
24 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
25 import org.eclipse.ui.internal.registry.RegistryReader;
26
27 /**
28  * Registry for introduction elements.
29  *
30  * @since 3.0
31  */

32 public class IntroRegistry implements IIntroRegistry {
33     private static final String JavaDoc TAG_INTRO = "intro";//$NON-NLS-1$
34

35     private static final String JavaDoc TAG_INTROPRODUCTBINDING = "introProductBinding";//$NON-NLS-1$
36

37     private static final String JavaDoc ATT_INTROID = "introId"; //$NON-NLS-1$
38

39     private static final String JavaDoc ATT_PRODUCTID = "productId"; //$NON-NLS-1$
40

41     /*
42      * (non-Javadoc)
43      *
44      * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntroCount()
45      */

46     public int getIntroCount() {
47         return getIntros().length;
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntros()
54      */

55     public IIntroDescriptor[] getIntros() {
56         IExtensionPoint point = Platform.getExtensionRegistry()
57                 .getExtensionPoint(PlatformUI.PLUGIN_ID,
58                         IWorkbenchRegistryConstants.PL_INTRO);
59         if (point == null) {
60             return new IIntroDescriptor[0];
61         }
62
63         IExtension[] extensions = point.getExtensions();
64         extensions = RegistryReader.orderExtensions(extensions);
65
66         ArrayList JavaDoc list = new ArrayList JavaDoc(extensions.length);
67         for (int i = 0; i < extensions.length; i++) {
68             IConfigurationElement[] elements = extensions[i]
69                     .getConfigurationElements();
70             for (int j = 0; j < elements.length; j++) {
71                 if (elements[j].getName().equals(TAG_INTRO)) {
72                     try {
73                         IIntroDescriptor descriptor = new IntroDescriptor(
74                                 elements[j]);
75                         list.add(descriptor);
76                     } catch (CoreException e) {
77                         // log an error since its not safe to open a dialog here
78
WorkbenchPlugin
79                                 .log(
80                                         IntroMessages.Intro_could_not_create_descriptor, e.getStatus());
81                     }
82                 }
83             }
84         }
85
86         return (IIntroDescriptor[]) list.toArray(new IIntroDescriptor[list
87                 .size()]);
88     }
89
90     /*
91      * (non-Javadoc)
92      *
93      * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntroForProduct(java.lang.String)
94      */

95     public IIntroDescriptor getIntroForProduct(String JavaDoc targetProductId) {
96         IExtensionPoint point = Platform.getExtensionRegistry()
97                 .getExtensionPoint(PlatformUI.PLUGIN_ID,
98                         IWorkbenchRegistryConstants.PL_INTRO);
99         if (point == null) {
100             return null;
101         }
102
103         IExtension[] extensions = point.getExtensions();
104         extensions = RegistryReader.orderExtensions(extensions);
105
106         String JavaDoc targetIntroId = getIntroForProduct(targetProductId, extensions);
107         if (targetIntroId == null) {
108             return null;
109         }
110
111         IIntroDescriptor descriptor = null;
112
113         IIntroDescriptor[] intros = getIntros();
114         for (int i = 0; i < intros.length; i++) {
115             if (intros[i].getId().equals(targetIntroId)) {
116                 descriptor = intros[i];
117                 break;
118             }
119         }
120
121         return descriptor;
122     }
123
124     /**
125      * @param targetProductId
126      * @param extensions
127      * @return
128      */

129     private String JavaDoc getIntroForProduct(String JavaDoc targetProductId,
130             IExtension[] extensions) {
131         for (int i = 0; i < extensions.length; i++) {
132             IConfigurationElement[] elements = extensions[i]
133                     .getConfigurationElements();
134             for (int j = 0; j < elements.length; j++) {
135                 if (elements[j].getName().equals(TAG_INTROPRODUCTBINDING)) {
136                     String JavaDoc introId = elements[j].getAttribute(ATT_INTROID);
137                     String JavaDoc productId = elements[j].getAttribute(ATT_PRODUCTID);
138
139                     if (introId == null || productId == null) {
140                         IStatus status = new Status(
141                                 IStatus.ERROR,
142                                 elements[j].getDeclaringExtension()
143                                         .getNamespace(),
144                                 IStatus.ERROR,
145                                 "introId and productId must be defined.", new IllegalArgumentException JavaDoc()); //$NON-NLS-1$
146
WorkbenchPlugin.log("Invalid intro binding", status); //$NON-NLS-1$
147
continue;
148                     }
149
150                     if (targetProductId.equals(productId)) {
151                         return introId;
152                     }
153                 }
154             }
155         }
156         return null;
157     }
158
159     /*
160      * (non-Javadoc)
161      *
162      * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntro(java.lang.String)
163      */

164     public IIntroDescriptor getIntro(String JavaDoc id) {
165         IIntroDescriptor[] intros = getIntros();
166         for (int i = 0; i < intros.length; i++) {
167             IIntroDescriptor desc = intros[i];
168             if (desc.getId().equals(id)) {
169                 return desc;
170             }
171         }
172         return null;
173     }
174 }
175
Popular Tags