KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > rulers > ExtensionPointHelper


1 /*******************************************************************************
2  * Copyright (c) 2006 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.texteditor.rulers;
12
13 import java.net.URL JavaDoc;
14
15 import org.osgi.framework.Bundle;
16
17 import com.ibm.icu.text.MessageFormat;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.FileLocator;
21 import org.eclipse.core.runtime.IConfigurationElement;
22 import org.eclipse.core.runtime.IExtension;
23 import org.eclipse.core.runtime.ILog;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.InvalidRegistryObjectException;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.Status;
29
30 import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
31
32 public final class ExtensionPointHelper {
33     
34     private final IConfigurationElement fElement;
35     private final String JavaDoc fName;
36     private final ILog fLog;
37
38     public ExtensionPointHelper(IConfigurationElement element, ILog log) throws InvalidRegistryObjectException {
39         Assert.isLegal(element != null);
40         Assert.isLegal(log != null);
41         fLog= log;
42         fElement= element;
43         fName= element.getName();
44         // see if we have a conventional 'id' attribute
45
}
46     
47     public String JavaDoc getDefaultAttribute(String JavaDoc attribute, String JavaDoc dflt) throws InvalidRegistryObjectException {
48         String JavaDoc value= fElement.getAttribute(attribute);
49         return value == null ? dflt : value;
50     }
51
52     public String JavaDoc getNonNullAttribute(String JavaDoc attribute) throws InvalidRegistryObjectException {
53         String JavaDoc value= fElement.getAttribute(attribute);
54         if (value == null)
55             fail(MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_missing_attribute_msg, new Object JavaDoc[] {fName, attribute}));
56         return value;
57     }
58     
59     public float getDefaultAttribute(String JavaDoc attribute, float dflt) {
60         String JavaDoc value= getDefaultAttribute(attribute, null);
61         if (value == null)
62             return dflt;
63         
64         try {
65             return Float.valueOf(value).floatValue();
66         } catch (NumberFormatException JavaDoc x) {
67             fail(MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_invalid_number_attribute_msg, new Object JavaDoc[] {attribute, fName}));
68             return dflt;
69         }
70     }
71
72     public boolean getDefaultAttribute(String JavaDoc attribute, boolean dflt) {
73         String JavaDoc value= getDefaultAttribute(attribute, null);
74         if (value == null)
75             return dflt;
76         
77         try {
78             return Boolean.valueOf(value).booleanValue();
79         } catch (NumberFormatException JavaDoc x) {
80             fail(MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_invalid_number_attribute_msg, new Object JavaDoc[] {fName, attribute}));
81             return dflt;
82         }
83     }
84     
85     public void fail(String JavaDoc message) throws InvalidRegistryObjectException {
86         String JavaDoc id= findId(fElement);
87         String JavaDoc extensionPointId= fElement.getDeclaringExtension().getExtensionPointUniqueIdentifier();
88         Object JavaDoc[] args= { fElement.getContributor().getName(), id, extensionPointId };
89         String JavaDoc blame= MessageFormat.format(RulerColumnMessages.ExtensionPointHelper_invalid_contribution_msg, args);
90
91         IStatus status= new Status(IStatus.WARNING, TextEditorPlugin.PLUGIN_ID, IStatus.OK, blame + message, null);
92         fLog.log(status);
93         throw new InvalidRegistryObjectException();
94     }
95     
96     public static String JavaDoc findId(IConfigurationElement element) {
97         String JavaDoc id= null;
98         while (element != null && id == null) {
99             id= element.getAttribute("id"); //$NON-NLS-1$
100
if (id != null)
101                 break;
102             Object JavaDoc parent= element.getParent();
103             if (parent instanceof IExtension) {
104                 id= ((IExtension) parent).getUniqueIdentifier();
105                 break;
106             } else if (parent instanceof IConfigurationElement) {
107                 element= (IConfigurationElement) parent;
108             } else {
109                 break;
110             }
111         }
112         return id == null ? "<unknown>" : id; //$NON-NLS-1$
113
}
114
115     public URL JavaDoc getDefaultResourceURL(String JavaDoc attribute, URL JavaDoc dflt) {
116         String JavaDoc value= getDefaultAttribute(attribute, null);
117         if (value == null)
118             return dflt;
119
120         Bundle bundle= getBundle();
121         if (bundle == null)
122             return dflt;
123
124         Path path= new Path(value);
125         return FileLocator.find(bundle, path, null);
126     }
127     
128     private Bundle getBundle() {
129         String JavaDoc namespace= fElement.getDeclaringExtension().getContributor().getName();
130         Bundle bundle= Platform.getBundle(namespace);
131         return bundle;
132     }
133 }
134
Popular Tags