7377f614af75d9188297e809c20ed33f71ee00ca
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / folding / JavaFoldingStructureProviderDescriptor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.text.folding;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15
16 import org.eclipse.jface.text.Assert;
17
18 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingPreferenceBlock;
19 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingStructureProvider;
20
21 /**
22  * Describes a contribution to the folding provider extension point.
23  * 
24  * @since 3.0
25  */
26 public final class JavaFoldingStructureProviderDescriptor {
27
28         /* extension point attribute names */
29         
30         private static final String PREFERENCES_CLASS= "preferencesClass"; //$NON-NLS-1$
31         private static final String CLASS= "class"; //$NON-NLS-1$
32         private static final String NAME= "name"; //$NON-NLS-1$
33         private static final String ID= "id"; //$NON-NLS-1$
34         
35         /** The identifier of the extension. */
36         private String fId;
37         /** The name of the extension. */
38         private String fName;
39         /** The class name of the provided <code>IJavaFoldingStructureProvider</code>. */
40         private String fClass;
41         /**
42          * <code>true</code> if the extension specifies a custom
43          * <code>IJavaFoldingPreferenceBlock</code>.
44          */
45         private boolean fHasPreferences;
46         /** The configuration element of this extension. */
47         private IConfigurationElement fElement;
48         
49         /**
50          * Creates a new descriptor.
51          * 
52          * @param element the configuration element to read
53          */
54         JavaFoldingStructureProviderDescriptor(IConfigurationElement element) {
55                 fElement= element;
56                 fId= element.getAttributeAsIs(ID);
57                 Assert.isLegal(fId != null);
58                 
59                 fName= element.getAttribute(NAME);
60                 if (fName == null)
61                         fName= fId;
62                 
63                 fClass= element.getAttributeAsIs(CLASS);
64                 Assert.isLegal(fClass != null);
65                 
66                 if (element.getAttributeAsIs(PREFERENCES_CLASS) == null)
67                         fHasPreferences= false;
68                 else
69                         fHasPreferences= true;
70         }
71         
72         /**
73          * Creates a folding provider as described in the extension's xml.
74          * 
75          * @return a new instance of the folding provider described by this
76          *         descriptor
77          * @throws CoreException if creation fails
78          */
79         public IJavaFoldingStructureProvider createProvider() throws CoreException {
80                 IJavaFoldingStructureProvider prov= (IJavaFoldingStructureProvider) fElement.createExecutableExtension(CLASS);
81                 return prov;
82         }
83
84         /**
85          * Creates a preferences object as described in the extension's xml.
86          * 
87          * @return a new instance of the reference provider described by this
88          *         descriptor
89          * @throws CoreException if creation fails
90          */
91         public IJavaFoldingPreferenceBlock createPreferences() throws CoreException {
92                 if (fHasPreferences) {
93                         IJavaFoldingPreferenceBlock prefs= (IJavaFoldingPreferenceBlock) fElement.createExecutableExtension(PREFERENCES_CLASS);
94                         return prefs;
95                 } else {
96                         return new EmptyJavaFoldingPreferenceBlock();
97                 }
98         }
99         
100         /**
101          * Returns the identifier of the described extension.
102          * 
103          * @return Returns the id
104          */
105         public String getId() {
106                 return fId;
107         }
108         
109         /**
110          * Returns the name of the described extension.
111          * 
112          * @return Returns the name
113          */
114         public String getName() {
115                 return fName;
116         }
117 }