1 11 12 package org.eclipse.ui.internal.cheatsheets.composite.views; 13 14 import java.io.IOException ; 15 import java.lang.reflect.Constructor ; 16 import java.net.URL ; 17 import java.util.HashMap ; 18 import java.util.Map ; 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 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 explorerKind) { 52 CheatSheetRegistryReader.TaskExplorerNode explorerInfo = 53 CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind); 54 if (explorerInfo != null) { 55 TaskExplorer explorerInstance = null; 56 Class extClass = null; 57 String className = explorerInfo.getClassName(); 58 try { 59 Bundle bundle = Platform.getBundle(explorerInfo.getPluginId()); 60 extClass = bundle.loadClass(className); 61 } catch (Exception e) { 62 String message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object [] {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 c = extClass.getConstructor(new Class [0]); 69 Object [] parameters = new Object [0]; 70 explorerInstance = (TaskExplorer) c.newInstance(parameters); 71 } 72 } catch (Exception e) { 73 String message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object [] {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 explorerKind) { 85 CheatSheetRegistryReader.TaskExplorerNode explorerInfo = 86 CheatSheetRegistryReader.getInstance().findTaskExplorer(explorerKind); 87 if (explorerInfo == null) { 88 return null; 89 } 90 String iconPath = explorerInfo.getIconPath(); 91 if (iconPath == null) { 92 return null; 93 } 94 Bundle bundle = Platform.getBundle(explorerInfo.getPluginId()); 95 URL url = FileLocator.find(bundle, new Path(iconPath), null); 96 try { 97 url = FileLocator.resolve(url); 98 return ImageDescriptor.createFromURL(url); 99 } catch (IOException e) { 100 return null; 101 } 102 } 103 104 private Map 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 (); 115 String [] 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 getName(String 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 id) { 135 return (Image)getImages().get(id); 136 } 137 138 } 139 | Popular Tags |