1 11 12 package org.eclipse.ui.internal.tweaklets; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IConfigurationElement; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.ui.internal.misc.StatusUtil; 23 import org.eclipse.ui.statushandlers.StatusManager; 24 25 29 public class Tweaklets { 30 31 public static class TweakKey { 32 Class tweakClass; 33 34 37 public TweakKey(Class tweakClass) { 38 this.tweakClass = tweakClass; 39 } 40 41 44 public int hashCode() { 45 final int prime = 31; 46 int result = 1; 47 result = prime * result 48 + ((tweakClass == null) ? 0 : tweakClass.hashCode()); 49 return result; 50 } 51 52 55 public boolean equals(Object obj) { 56 if (this == obj) 57 return true; 58 if (obj == null) 59 return false; 60 if (getClass() != obj.getClass()) 61 return false; 62 final TweakKey other = (TweakKey) obj; 63 if (tweakClass == null) { 64 if (other.tweakClass != null) 65 return false; 66 } else if (!tweakClass.equals(other.tweakClass)) 67 return false; 68 return true; 69 } 70 } 71 72 private static Map defaults = new HashMap (); 73 private static Map tweaklets = new HashMap (); 74 75 public static void setDefault(TweakKey definition, Object implementation) { 76 defaults.put(definition, implementation); 77 } 78 79 public static Object get(TweakKey definition) { 80 Object result = tweaklets.get(definition); 81 if (result == null) { 82 result = createTweaklet(definition); 83 if (result == null) { 84 result = getDefault(definition); 85 } 86 Assert.isNotNull(result); 87 tweaklets.put(definition, result); 88 } 89 return result; 90 } 91 92 96 private static Object getDefault(TweakKey definition) { 97 return defaults.get(definition); 98 } 99 100 104 private static Object createTweaklet(TweakKey definition) { 105 IConfigurationElement[] elements = Platform 106 .getExtensionRegistry() 107 .getConfigurationElementsFor("org.eclipse.ui.internalTweaklets"); for (int i = 0; i < elements.length; i++) { 109 if (definition.tweakClass.getName().equals( 110 elements[i].getAttribute("definition"))) { try { 112 Object tweaklet = elements[i].createExecutableExtension("implementation"); tweaklets.put(definition, tweaklet); 114 return tweaklet; 115 } catch (CoreException e) { 116 StatusManager.getManager().handle( 117 StatusUtil.newStatus(IStatus.ERROR, 118 "Error with extension " + elements[i], e), StatusManager.LOG); 120 } 121 } 122 } 123 return null; 124 } 125 126 } 127 | Popular Tags |