001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.tweaklets;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.Assert;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.ui.internal.misc.StatusUtil;
021: import org.eclipse.ui.statushandlers.StatusManager;
022:
023: /**
024: * @since 3.3
025: *
026: */
027: public class Tweaklets {
028:
029: public static class TweakKey {
030: Class tweakClass;
031:
032: /**
033: * @param tweakClass
034: */
035: public TweakKey(Class tweakClass) {
036: this .tweakClass = tweakClass;
037: }
038:
039: /* (non-Javadoc)
040: * @see java.lang.Object#hashCode()
041: */
042: public int hashCode() {
043: final int prime = 31;
044: int result = 1;
045: result = prime
046: * result
047: + ((tweakClass == null) ? 0 : tweakClass.hashCode());
048: return result;
049: }
050:
051: /* (non-Javadoc)
052: * @see java.lang.Object#equals(java.lang.Object)
053: */
054: public boolean equals(Object obj) {
055: if (this == obj)
056: return true;
057: if (obj == null)
058: return false;
059: if (getClass() != obj.getClass())
060: return false;
061: final TweakKey other = (TweakKey) obj;
062: if (tweakClass == null) {
063: if (other.tweakClass != null)
064: return false;
065: } else if (!tweakClass.equals(other.tweakClass))
066: return false;
067: return true;
068: }
069: }
070:
071: private static Map defaults = new HashMap();
072: private static Map tweaklets = new HashMap();
073:
074: public static void setDefault(TweakKey definition,
075: Object implementation) {
076: defaults.put(definition, implementation);
077: }
078:
079: public static Object get(TweakKey definition) {
080: Object result = tweaklets.get(definition);
081: if (result == null) {
082: result = createTweaklet(definition);
083: if (result == null) {
084: result = getDefault(definition);
085: }
086: Assert.isNotNull(result);
087: tweaklets.put(definition, result);
088: }
089: return result;
090: }
091:
092: /**
093: * @param definition
094: * @return
095: */
096: private static Object getDefault(TweakKey definition) {
097: return defaults.get(definition);
098: }
099:
100: /**
101: * @param definition
102: * @return
103: */
104: private static Object createTweaklet(TweakKey definition) {
105: IConfigurationElement[] elements = Platform
106: .getExtensionRegistry().getConfigurationElementsFor(
107: "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]
113: .createExecutableExtension("implementation"); //$NON-NLS-1$
114: tweaklets.put(definition, tweaklet);
115: return tweaklet;
116: } catch (CoreException e) {
117: StatusManager
118: .getManager()
119: .handle(
120: StatusUtil
121: .newStatus(
122: IStatus.ERROR,
123: "Error with extension " + elements[i], e), //$NON-NLS-1$
124: StatusManager.LOG);
125: }
126: }
127: }
128: return null;
129: }
130:
131: }
|