KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > tweaklets > Tweaklets


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 JavaDoc;
15 import java.util.Map JavaDoc;
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 JavaDoc tweakClass;
33
34         /**
35          * @param tweakClass
36          */

37         public TweakKey(Class JavaDoc 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 JavaDoc 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 JavaDoc defaults = new HashMap JavaDoc();
73     private static Map JavaDoc tweaklets = new HashMap JavaDoc();
74
75     public static void setDefault(TweakKey definition, Object JavaDoc implementation) {
76         defaults.put(definition, implementation);
77     }
78     
79     public static Object JavaDoc get(TweakKey definition) {
80         Object JavaDoc 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 JavaDoc getDefault(TweakKey definition) {
97         return defaults.get(definition);
98     }
99
100     /**
101      * @param definition
102      * @return
103      */

104     private static Object JavaDoc 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 JavaDoc 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 }
127
Popular Tags