1 6 package com.nightlabs.ipanema.config; 7 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.Map ; 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 23 public class ConfigPreferencePageRegistry extends AbstractEPProcessor { 24 25 26 private ConfigPreferenceNode preferencesRootNode = new ConfigPreferenceNode( 27 "", 28 "", 29 "", 30 null, 31 null 32 ); 33 34 38 private Map preferencePagesByIDs = new HashMap (); 39 40 43 public ConfigPreferencePageRegistry() { 44 super(); 45 } 46 47 50 public String getExtensionPointID() { 51 return "org.eclipse.ui.preferencePages"; 53 } 54 55 62 public ConfigPreferenceNode getPreferencesRootNode() { 63 if (!isProcessed()) 64 try { 65 process(); 66 } catch (EPProcessorException e) { 67 throw new RuntimeException (e); 68 } 69 return preferencesRootNode; 70 } 71 72 75 public void processElement(IConfigurationElement element) 76 throws EPProcessorException { 77 if (element.getName().equals("page")) { 78 String id = element.getAttribute("id"); 79 String category = element.getAttribute("category"); 80 if (id == null || "".equals(id)) 81 throw new EPProcessorException("Element page has to define an attribute id."); 82 String 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 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 |