1 8 package com.nightlabs.rcp.action; 9 10 import java.util.HashMap ; 11 import java.util.Map ; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 17 import com.nightlabs.base.NLBasePlugin; 18 import com.nightlabs.rcp.extensionpoint.AbstractEPProcessor; 19 import com.nightlabs.rcp.extensionpoint.EPProcessorException; 20 21 public class NewFileRegistry 22 extends AbstractEPProcessor 23 { 24 public static final String EXTENSION_POINT_ID = "com.nightlabs.base.newfileaction"; 25 public static final String DEFAULT_CATEGORY = "Default category"; 26 27 private static NewFileRegistry sharedInstance; 28 public static NewFileRegistry getSharedInstance() { 29 if (sharedInstance == null) 30 sharedInstance = new NewFileRegistry(); 31 return sharedInstance; 32 } 33 34 protected Map category2Actions = new HashMap (); 35 public Map getCategory2Actions() { 36 checkProcessing(); 37 return category2Actions; 38 } 39 40 public String getCategoryName(String categoryID) { 41 return categoryRegistry.getCategoryName(categoryID); 42 } 43 44 protected CategoryRegistry categoryRegistry = new CategoryRegistry(); 45 46 public String getExtensionPointID() { 47 return EXTENSION_POINT_ID; 48 } 49 50 public void processElement(IConfigurationElement element) 51 throws EPProcessorException 52 { 53 if (element.getName().equalsIgnoreCase("action")) 54 { 55 String className = element.getAttribute("class"); 56 if (!checkString(className)) 57 throw new EPProcessorException("Element action has to define attribute class."); 58 59 String categoryID = element.getAttribute("categoryID"); 60 if (!checkString(categoryID) || categoryRegistry.getCategoryName(categoryID) == null) 61 categoryID = DEFAULT_CATEGORY; 62 63 String fileExtension = element.getAttribute("fileExtension"); 64 if (!checkString(fileExtension)) 65 throw new EPProcessorException("Element action has to define attribute fileExtension."); 66 67 String title = element.getAttribute("title"); 68 if (!checkString(title)) 69 throw new EPProcessorException("Element action has to define attribute title."); 70 71 String tooltip = element.getAttribute("tooltip"); 72 String iconName = element.getAttribute("icon"); 73 74 89 Object o; 90 try { 91 o = element.createExecutableExtension("class"); 92 if (o instanceof INewFileAction) { 93 INewFileAction action = (INewFileAction) o; 94 action.setFileExtension(fileExtension); 95 action.setText(title); 96 if (checkString(tooltip)) 97 action.setToolTipText(tooltip); 98 if (checkString(iconName)) 99 action.setImageDescriptor(getImageDescriptor(iconName)); 100 101 category2Actions.put(categoryID, action); 102 } 103 } catch (CoreException e) { 104 throw new EPProcessorException(e); 105 } 106 } 107 } 108 109 protected ImageDescriptor getImageDescriptor(String iconName) 110 { 111 if (iconName != null) { 112 return ImageDescriptor.createFromURL(NLBasePlugin.getDefault().getBundle().getEntry(iconName)); 113 } 114 return null; 115 } 116 117 protected boolean checkString(String s) 118 { 119 if (s == null || "".equals(s)) 120 return false; 121 122 return true; 123 } 124 125 protected class CategoryRegistry 126 extends AbstractEPProcessor 127 { 128 protected Map categoryID2name = new HashMap (); 129 130 public String getExtensionPointID() { 131 return EXTENSION_POINT_ID; 132 } 133 134 public String getCategoryName(String categoryID) 136 { 137 checkProcessing(); 138 return (String ) categoryID2name.get(categoryID); 139 } 140 141 public void processElement(IConfigurationElement element) 142 throws EPProcessorException 143 { 144 if (element.getName().equalsIgnoreCase("category")) 145 { 146 String id = element.getAttribute("id"); 147 if (!checkString(id)) 148 throw new EPProcessorException("Element category has to define attribute id."); 149 150 String name = element.getAttribute("name"); 151 if (!checkString(name)) 152 throw new EPProcessorException("Element category has to define attribute name."); 153 154 categoryID2name.put(id, name); 155 } 156 } 157 158 } 159 160 } | Popular Tags |