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.intro;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.core.runtime.IExtensionPoint;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.ui.PlatformUI;
022: import org.eclipse.ui.internal.WorkbenchPlugin;
023: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
024: import org.eclipse.ui.internal.registry.RegistryReader;
025:
026: /**
027: * Registry for introduction elements.
028: *
029: * @since 3.0
030: */
031: public class IntroRegistry implements IIntroRegistry {
032: private static final String TAG_INTRO = "intro";//$NON-NLS-1$
033:
034: private static final String TAG_INTROPRODUCTBINDING = "introProductBinding";//$NON-NLS-1$
035:
036: private static final String ATT_INTROID = "introId"; //$NON-NLS-1$
037:
038: private static final String ATT_PRODUCTID = "productId"; //$NON-NLS-1$
039:
040: /*
041: * (non-Javadoc)
042: *
043: * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntroCount()
044: */
045: public int getIntroCount() {
046: return getIntros().length;
047: }
048:
049: /*
050: * (non-Javadoc)
051: *
052: * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntros()
053: */
054: public IIntroDescriptor[] getIntros() {
055: IExtensionPoint point = Platform.getExtensionRegistry()
056: .getExtensionPoint(PlatformUI.PLUGIN_ID,
057: IWorkbenchRegistryConstants.PL_INTRO);
058: if (point == null) {
059: return new IIntroDescriptor[0];
060: }
061:
062: IExtension[] extensions = point.getExtensions();
063: extensions = RegistryReader.orderExtensions(extensions);
064:
065: ArrayList list = new ArrayList(extensions.length);
066: for (int i = 0; i < extensions.length; i++) {
067: IConfigurationElement[] elements = extensions[i]
068: .getConfigurationElements();
069: for (int j = 0; j < elements.length; j++) {
070: if (elements[j].getName().equals(TAG_INTRO)) {
071: try {
072: IIntroDescriptor descriptor = new IntroDescriptor(
073: elements[j]);
074: list.add(descriptor);
075: } catch (CoreException e) {
076: // log an error since its not safe to open a dialog here
077: WorkbenchPlugin
078: .log(
079: IntroMessages.Intro_could_not_create_descriptor,
080: e.getStatus());
081: }
082: }
083: }
084: }
085:
086: return (IIntroDescriptor[]) list
087: .toArray(new IIntroDescriptor[list.size()]);
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntroForProduct(java.lang.String)
094: */
095: public IIntroDescriptor getIntroForProduct(String targetProductId) {
096: IExtensionPoint point = Platform.getExtensionRegistry()
097: .getExtensionPoint(PlatformUI.PLUGIN_ID,
098: IWorkbenchRegistryConstants.PL_INTRO);
099: if (point == null) {
100: return null;
101: }
102:
103: IExtension[] extensions = point.getExtensions();
104: extensions = RegistryReader.orderExtensions(extensions);
105:
106: String targetIntroId = getIntroForProduct(targetProductId,
107: extensions);
108: if (targetIntroId == null) {
109: return null;
110: }
111:
112: IIntroDescriptor descriptor = null;
113:
114: IIntroDescriptor[] intros = getIntros();
115: for (int i = 0; i < intros.length; i++) {
116: if (intros[i].getId().equals(targetIntroId)) {
117: descriptor = intros[i];
118: break;
119: }
120: }
121:
122: return descriptor;
123: }
124:
125: /**
126: * @param targetProductId
127: * @param extensions
128: * @return
129: */
130: private String getIntroForProduct(String targetProductId,
131: IExtension[] extensions) {
132: for (int i = 0; i < extensions.length; i++) {
133: IConfigurationElement[] elements = extensions[i]
134: .getConfigurationElements();
135: for (int j = 0; j < elements.length; j++) {
136: if (elements[j].getName().equals(
137: TAG_INTROPRODUCTBINDING)) {
138: String introId = elements[j]
139: .getAttribute(ATT_INTROID);
140: String productId = elements[j]
141: .getAttribute(ATT_PRODUCTID);
142:
143: if (introId == null || productId == null) {
144: IStatus status = new Status(
145: IStatus.ERROR,
146: elements[j].getDeclaringExtension()
147: .getNamespace(),
148: IStatus.ERROR,
149: "introId and productId must be defined.", new IllegalArgumentException()); //$NON-NLS-1$
150: WorkbenchPlugin.log(
151: "Invalid intro binding", status); //$NON-NLS-1$
152: continue;
153: }
154:
155: if (targetProductId.equals(productId)) {
156: return introId;
157: }
158: }
159: }
160: }
161: return null;
162: }
163:
164: /*
165: * (non-Javadoc)
166: *
167: * @see org.eclipse.ui.internal.intro.IIntroRegistry#getIntro(java.lang.String)
168: */
169: public IIntroDescriptor getIntro(String id) {
170: IIntroDescriptor[] intros = getIntros();
171: for (int i = 0; i < intros.length; i++) {
172: IIntroDescriptor desc = intros[i];
173: if (desc.getId().equals(id)) {
174: return desc;
175: }
176: }
177: return null;
178: }
179: }
|