KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > ipanema > config > ConfigPreferencePageRegistry


1 /*
2  * Created on May 31, 2005
3  * by alex
4  *
5  */

6 package com.nightlabs.ipanema.config;
7
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.eclipse.core.runtime.CoreException;
13 import org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.ui.IWorkbenchPreferencePage;
15
16 import com.nightlabs.rcp.extensionpoint.AbstractEPProcessor;
17 import com.nightlabs.rcp.extensionpoint.EPProcessorException;
18
19 /**
20  * @author Alexander Bieber <alex[AT]nightlabs[DOT]de>
21  *
22  */

23 public class ConfigPreferencePageRegistry extends AbstractEPProcessor {
24
25     
26     private ConfigPreferenceNode preferencesRootNode = new ConfigPreferenceNode(
27             "",
28             "",
29             "",
30             null,
31             null
32     );
33     
34     /**
35      * key: String id<br/>
36      * value: ConfigPreferenceNode preferenceNode
37      */

38     private Map JavaDoc preferencePagesByIDs = new HashMap JavaDoc();
39     
40     /**
41      *
42      */

43     public ConfigPreferencePageRegistry() {
44         super();
45     }
46
47     /**
48      * @see com.nightlabs.rcp.extensionpoint.AbstractEPProcessor#getExtensionPointID()
49      */

50     public String JavaDoc getExtensionPointID() {
51         // TODO: find static final
52
return "org.eclipse.ui.preferencePages";
53     }
54     
55     /**
56      * Returns the node to wich all registered
57      * {@link AbstractConfigModulePreferencePage}s
58      * are children.
59      *
60      * @return The root ConfigPreferenceNode
61      */

62     public ConfigPreferenceNode getPreferencesRootNode() {
63         if (!isProcessed())
64             try {
65                 process();
66             } catch (EPProcessorException e) {
67                 throw new RuntimeException JavaDoc(e);
68             }
69         return preferencesRootNode;
70     }
71
72     /**
73      * @see com.nightlabs.rcp.extensionpoint.AbstractEPProcessor#processElement(org.eclipse.core.runtime.IConfigurationElement)
74      */

75     public void processElement(IConfigurationElement element)
76             throws EPProcessorException {
77         if (element.getName().equals("page")) {
78             String JavaDoc id = element.getAttribute("id");
79             String JavaDoc category = element.getAttribute("category");
80             if (id == null || "".equals(id))
81                 throw new EPProcessorException("Element page has to define an attribute id.");
82             String JavaDoc name = element.getAttribute("name");
83             IWorkbenchPreferencePage page = null;
84             try {
85                 page = (IWorkbenchPreferencePage)element.createExecutableExtension("class");
86             } catch (CoreException e) {
87                 throw new EPProcessorException(e);
88             }
89             if (!(page instanceof AbstractConfigModulePreferencePage))
90                 return;
91             
92             ConfigPreferenceNode preferenceNode = new ConfigPreferenceNode(id, name, category, null, (AbstractConfigModulePreferencePage)page);
93             preferencePagesByIDs.put(id, preferenceNode);
94         }
95     }
96
97     public synchronized void process() throws EPProcessorException {
98         super.process();
99         for (Iterator JavaDoc iter = preferencePagesByIDs.values().iterator(); iter.hasNext();) {
100             ConfigPreferenceNode node = (ConfigPreferenceNode) iter.next();
101             ConfigPreferenceNode parentNode = (ConfigPreferenceNode)preferencePagesByIDs.get(node.getCategoryID());
102             if (parentNode != null)
103                 parentNode.addChild(node);
104             else
105                 preferencesRootNode.addChild(node);
106         }
107     }
108     
109     private static ConfigPreferencePageRegistry sharedInstance;
110     
111     public static ConfigPreferencePageRegistry getSharedInstance() {
112         if (sharedInstance == null)
113             sharedInstance = new ConfigPreferencePageRegistry();
114         return sharedInstance;
115     }
116
117 }
118
Popular Tags