| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2007 IBM Corporation and others. |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | ******************************************************************************/ |
| 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 | /** |
| 26 | * @since 3.3 |
| 27 | * |
| 28 | */ |
| 29 | public class Tweaklets { |
| 30 | |
| 31 | public static class TweakKey { |
| 32 | Class tweakClass; |
| 33 | |
| 34 | /** |
| 35 | * @param tweakClass |
| 36 | */ |
| 37 | public TweakKey(Class tweakClass) { |
| 38 | this.tweakClass = tweakClass; |
| 39 | } |
| 40 | |
| 41 | /* (non-Javadoc) |
| 42 | * @see java.lang.Object#hashCode() |
| 43 | */ |
| 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 | /* (non-Javadoc) |
| 53 | * @see java.lang.Object#equals(java.lang.Object) |
| 54 | */ |
| 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 | /** |
| 93 | * @param definition |
| 94 | * @return |
| 95 | */ |
| 96 | private static Object getDefault(TweakKey definition) { |
| 97 | return defaults.get(definition); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * @param definition |
| 102 | * @return |
| 103 | */ |
| 104 | private static Object createTweaklet(TweakKey definition) { |
| 105 | IConfigurationElement[] elements = Platform |
| 106 | .getExtensionRegistry() |
| 107 | .getConfigurationElementsFor("org.eclipse.ui.internalTweaklets"); //$NON-NLS-1$ |
| 108 | for (int i = 0; i < elements.length; i++) { |
| 109 | if (definition.tweakClass.getName().equals( |
| 110 | elements[i].getAttribute("definition"))) { //$NON-NLS-1$ |
| 111 | try { |
| 112 | Object tweaklet = elements[i].createExecutableExtension("implementation"); //$NON-NLS-1$ |
| 113 | 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), //$NON-NLS-1$ |
| 119 | StatusManager.LOG); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | } |