KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > loader > ContentProviderManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.intro.impl.model.loader;
13
14 import java.util.Enumeration JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroPage;
19 import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider;
20 import org.eclipse.ui.internal.intro.impl.util.Log;
21 import org.eclipse.ui.intro.config.IIntroContentProvider;
22 import org.eclipse.ui.intro.config.IIntroContentProviderSite;
23
24 /**
25  * Class for handling/caching all the loaded Intro Content providers, from all loaded models. <br>
26  * <br />
27  * Design notes:
28  * <ul>
29  * <li>content providers are only ever created once. The init method is only called once, and so
30  * this is why they need to be cached.</li>
31  * <li>Content provider ids are used as keys in the hashtable, and their corresponding wrapper
32  * classes as values.</li>
33  * <li>In the case of HTML presentation, html is cached and so model is not consuloted when we need
34  * to redisplay a page. When content provider asks for a reflow, the page is removed from HTML cache
35  * and intro model is consulted again. This is when the calls happen to this class.</li>
36  * <li>In the case of SWT presentation, same design. SWT pages are cached in a page book. When a
37  * content provider needs to refresh a page, the page is removed from the page book and recreated
38  * from intro model.</li>
39  * </ul>
40  */

41
42 public class ContentProviderManager {
43
44     // singleton instance. Can be retrieved from here or from the Intro Plugin.
45
private static ContentProviderManager inst = new ContentProviderManager();
46
47
48     // Holds all created content providers, to prevent the need to recreate the
49
// class on each navigation. Key is the contentProvider id, value
50
// is a wrapper class to hold the actual Intro content provider instance and
51
// the intro page that holds it.
52
private Hashtable JavaDoc contentProviders = new Hashtable JavaDoc();
53
54
55     class ContentProviderWrapper {
56
57         IIntroContentProvider provider;
58         AbstractIntroPage parentPage;
59
60         ContentProviderWrapper(IIntroContentProvider provider, AbstractIntroPage parentPage) {
61             this.provider = provider;
62             this.parentPage = parentPage;
63         }
64
65         IIntroContentProvider getIIntroContentProvider() {
66             return provider;
67         }
68
69         AbstractIntroPage getParentPage() {
70             return parentPage;
71         }
72     }
73
74
75
76     /*
77      * Prevent creation.
78      */

79     protected ContentProviderManager() {
80         // do nothing
81
}
82
83     /**
84      * @return Returns the inst.
85      */

86     public static ContentProviderManager getInst() {
87         return inst;
88     }
89
90     /**
91      * Retrieve an existing content provider class, or null if never created before.
92      *
93      * @param provider
94      * @return
95      */

96     public IIntroContentProvider getContentProvider(IntroContentProvider provider) {
97         // safe to cast since we know the object class in table.
98
ContentProviderWrapper providerWrapper = (ContentProviderWrapper) contentProviders.get(provider
99                 .getId());
100         if (providerWrapper == null)
101             // return null if provider has not been created yet.
102
return null;
103         IIntroContentProvider providerClass = providerWrapper.getIIntroContentProvider();
104         return providerClass;
105     }
106
107     /**
108      * Tries to create an intro content provider class. may return null if creation fails. This will
109      * be logged.
110      *
111      * @param provider
112      * @param site
113      * @return
114      */

115     public IIntroContentProvider createContentProvider(IntroContentProvider provider,
116             IIntroContentProviderSite site) {
117
118         // the content provider has never been created before. Create and cache
119
// one.
120
String JavaDoc pluginId = (provider.getPluginId() != null) ? provider.getPluginId() : provider.getBundle()
121                 .getSymbolicName();
122         Object JavaDoc aClass = ModelLoaderUtil.createClassInstance(pluginId, provider.getClassName());
123         IIntroContentProvider providerClass = null;
124         if (aClass != null && aClass instanceof IIntroContentProvider) {
125             providerClass = ((IIntroContentProvider) aClass);
126             providerClass.init(site);
127             if (provider.getId() != null) {
128                 // cache only when an id is defined.
129
ContentProviderWrapper wrapper = new ContentProviderWrapper(providerClass, provider
130                         .getParentPage());
131                 contentProviders.put(provider.getId(), wrapper);
132             }
133         } else
134             Log.warning("Failed to create Intro model content provider: " //$NON-NLS-1$
135
+ provider.getClassName());
136         return providerClass;
137     }
138
139
140     public AbstractIntroPage getContentProviderParentPage(IIntroContentProvider provider) {
141         Enumeration JavaDoc keys = contentProviders.keys();
142         while (keys.hasMoreElements()) {
143             String JavaDoc key = (String JavaDoc) keys.nextElement();
144             ContentProviderWrapper wrapper = (ContentProviderWrapper) contentProviders.get(key);
145             boolean foundKey = wrapper.getIIntroContentProvider().equals(provider) ? true : false;
146             if (foundKey) {
147                 return wrapper.getParentPage();
148             }
149         }
150         return null;
151     }
152
153     public void clear() {
154         for (Iterator JavaDoc it = contentProviders.values().iterator(); it.hasNext();) {
155             ContentProviderWrapper providerWrapper = (ContentProviderWrapper) it.next();
156             IIntroContentProvider provider = providerWrapper.getIIntroContentProvider();
157             provider.dispose();
158         }
159         contentProviders.clear();
160         if (Log.logInfo)
161             Log.info("Cleared Intro model content providers"); //$NON-NLS-1$
162
}
163
164
165 }
166
Popular Tags