KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > composite > views > TaskExplorerManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.cheatsheets.composite.views;
13
14 import java.io.IOException JavaDoc;
15 import java.lang.reflect.Constructor JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.FileLocator;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.swt.graphics.Image;
28 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
29 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
30 import org.eclipse.ui.internal.cheatsheets.Messages;
31 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
32 import org.eclipse.ui.internal.provisional.cheatsheets.TaskExplorer;
33 import org.osgi.framework.Bundle;
34
35 public class TaskExplorerManager {
36 private static TaskExplorerManager instance;
37
38     private Map JavaDoc images;
39     
40     private TaskExplorerManager() {
41     
42     }
43     
44     public static TaskExplorerManager getInstance() {
45         if (instance == null) {
46             instance = new TaskExplorerManager();
47         }
48         return instance;
49     }
50     
51     public TaskExplorer getExplorer(String JavaDoc explorerKind) {
52         CheatSheetRegistryReader.TaskExplorerNode explorerInfo =
53             CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind);
54         if (explorerInfo != null) {
55             TaskExplorer explorerInstance = null;
56             Class JavaDoc extClass = null;
57             String JavaDoc className = explorerInfo.getClassName();
58             try {
59                 Bundle bundle = Platform.getBundle(explorerInfo.getPluginId());
60                 extClass = bundle.loadClass(className);
61             } catch (Exception JavaDoc e) {
62                 String JavaDoc message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object JavaDoc[] {className}));
63                 Status status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
64                 CheatSheetPlugin.getPlugin().getLog().log(status);
65             }
66             try {
67                 if (extClass != null) {
68                     Constructor JavaDoc c = extClass.getConstructor(new Class JavaDoc[0]);
69                     Object JavaDoc[] parameters = new Object JavaDoc[0];
70                     explorerInstance = (TaskExplorer) c.newInstance(parameters);
71                 }
72             } catch (Exception JavaDoc e) {
73                 String JavaDoc message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object JavaDoc[] {className}));
74                 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e);
75                 CheatSheetPlugin.getPlugin().getLog().log(status);
76             }
77             
78             return explorerInstance;
79         }
80
81         return null;
82     }
83
84     private ImageDescriptor getImageDescriptor(String JavaDoc explorerKind) {
85         CheatSheetRegistryReader.TaskExplorerNode explorerInfo =
86             CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind);
87         if (explorerInfo == null) {
88             return null;
89         }
90         String JavaDoc iconPath = explorerInfo.getIconPath();
91         if (iconPath == null) {
92             return null;
93         }
94         Bundle bundle = Platform.getBundle(explorerInfo.getPluginId());
95         URL JavaDoc url = FileLocator.find(bundle, new Path(iconPath), null);
96         try {
97             url = FileLocator.resolve(url);
98             return ImageDescriptor.createFromURL(url);
99         } catch (IOException JavaDoc e) {
100             return null;
101         }
102     }
103     
104     private Map JavaDoc getImages() {
105         if (images == null) {
106             initImages();
107         }
108         return images;
109     }
110     
111     
112     private void initImages() {
113         if (images == null) {
114             images = new HashMap JavaDoc();
115             String JavaDoc[] ids = CheatSheetRegistryReader.getInstance().getExplorerIds();
116             for (int i = 0; i < ids.length; i++) {
117                 ImageDescriptor descriptor = getImageDescriptor(ids[i]);
118                 if (descriptor != null) {
119                     images.put(ids[i], descriptor.createImage());
120                 }
121             }
122         }
123     }
124
125     public String JavaDoc getName(String JavaDoc explorerKind) {
126         CheatSheetRegistryReader.TaskExplorerNode explorerInfo =
127             CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind);
128         if (explorerInfo != null) {
129             return explorerInfo.getName();
130         }
131         return null;
132     }
133
134     public Image getImage(String JavaDoc id) {
135         return (Image)getImages().get(id);
136     }
137
138 }
139
Popular Tags