1 25 26 package org.snipsnap.snip.label; 27 28 import org.radeox.util.logging.Logger; 29 import org.radeox.util.Service; 30 import org.snipsnap.container.Components; 31 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.Map ; 35 import java.util.Set ; 36 37 43 public class LabelManager { 44 private Map typeMap; 45 private String defaultName; 46 private static LabelManager instance = null; 47 private static String labelClassName = "org.snipsnap.snip.label.Label"; 48 49 public LabelManager() { 50 typeMap = new HashMap (); 51 defaultName = "DefaultLabel"; 52 try { 53 Iterator labelTypes = Service.providers(Class.forName(labelClassName)); 54 while (labelTypes.hasNext()) { 55 Label label = (Label) labelTypes.next(); 56 addLabelType(label.getType(), label.getClass()); 57 } 58 } catch (ClassNotFoundException e) { 59 System.err.println("LabelManager: base label type " + labelClassName + " not found. Label types have not been registered."+e); 60 } 61 } 62 63 private void addLabelType(String name, String className) { 64 try { 68 Class labelClass = Class.forName(className); 69 addLabelType(name, labelClass); 70 } catch (ClassNotFoundException e) { 71 Logger.warn("LabelManager: label class " + className + " not found and therefore not registered.", e); 72 } 73 } 74 75 private void addLabelType(String name, Class labelClass) { 76 typeMap.put(name, labelClass); 80 } 81 82 public Label getLabel(String type) { 83 if (null == type) { 84 return null; 85 } 86 Class labelClass = (Class ) typeMap.get(type); 87 if (null == labelClass) { 88 return null; 89 } 90 Label label = null; 91 try { 92 label = (Label) labelClass.newInstance(); 93 label.create(); 94 } catch (Exception e) { 95 Logger.warn("LabelManager: label class of type " + type + " could not be instantiated.", e); 96 } 97 return label; 98 } 99 100 public Label getDefaultLabel() { 101 return getLabel(defaultName); 102 } 103 104 public Set getTypes() { 105 return typeMap.keySet(); 106 } 107 } 108 | Popular Tags |