refactory: added UI removed from core plugin.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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 net.sourceforge.phpdt.ui.text.folding.IJavaFoldingPreferenceBlock;
14 import net.sourceforge.phpdt.ui.text.folding.IJavaFoldingStructureProvider;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.jface.text.Assert;
19
20 /**
21  * Describes a contribution to the folding provider extension point.
22  * 
23  * @since 3.0
24  */
25 public final class JavaFoldingStructureProviderDescriptor {
26
27         /* extension point attribute names */
28
29         private static final String PREFERENCES_CLASS = "preferencesClass"; //$NON-NLS-1$
30
31         private static final String CLASS = "class"; //$NON-NLS-1$
32
33         private static final String NAME = "name"; //$NON-NLS-1$
34
35         private static final String ID = "id"; //$NON-NLS-1$
36
37         /** The identifier of the extension. */
38         private String fId;
39
40         /** The name of the extension. */
41         private String fName;
42
43         /** The class name of the provided <code>IJavaFoldingStructureProvider</code>. */
44         private String fClass;
45
46         /**
47          * <code>true</code> if the extension specifies a custom
48          * <code>IJavaFoldingPreferenceBlock</code>.
49          */
50         private boolean fHasPreferences;
51
52         /** The configuration element of this extension. */
53         private IConfigurationElement fElement;
54
55         /**
56          * Creates a new descriptor.
57          * 
58          * @param element
59          *            the configuration element to read
60          */
61         JavaFoldingStructureProviderDescriptor(IConfigurationElement element) {
62                 fElement = element;
63                 fId = element.getAttributeAsIs(ID);
64                 Assert.isLegal(fId != null);
65
66                 fName = element.getAttribute(NAME);
67                 if (fName == null)
68                         fName = fId;
69
70                 fClass = element.getAttributeAsIs(CLASS);
71                 Assert.isLegal(fClass != null);
72
73                 if (element.getAttributeAsIs(PREFERENCES_CLASS) == null)
74                         fHasPreferences = false;
75                 else
76                         fHasPreferences = true;
77         }
78
79         /**
80          * Creates a folding provider as described in the extension's xml.
81          * 
82          * @return a new instance of the folding provider described by this
83          *         descriptor
84          * @throws CoreException
85          *             if creation fails
86          */
87         public IJavaFoldingStructureProvider createProvider() throws CoreException {
88                 IJavaFoldingStructureProvider prov = (IJavaFoldingStructureProvider) fElement
89                                 .createExecutableExtension(CLASS);
90                 return prov;
91         }
92
93         /**
94          * Creates a preferences object as described in the extension's xml.
95          * 
96          * @return a new instance of the reference provider described by this
97          *         descriptor
98          * @throws CoreException
99          *             if creation fails
100          */
101         public IJavaFoldingPreferenceBlock createPreferences() throws CoreException {
102                 if (fHasPreferences) {
103                         IJavaFoldingPreferenceBlock prefs = (IJavaFoldingPreferenceBlock) fElement
104                                         .createExecutableExtension(PREFERENCES_CLASS);
105                         return prefs;
106                 } else {
107                         return new EmptyJavaFoldingPreferenceBlock();
108                 }
109         }
110
111         /**
112          * Returns the identifier of the described extension.
113          * 
114          * @return Returns the id
115          */
116         public String getId() {
117                 return fId;
118         }
119
120         /**
121          * Returns the name of the described extension.
122          * 
123          * @return Returns the name
124          */
125         public String getName() {
126                 return fName;
127         }
128 }