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 18 import org.eclipse.core.runtime.FileLocator; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Path; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin; 26 import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource; 27 import org.eclipse.ui.internal.cheatsheets.Messages; 28 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader; 29 import org.eclipse.ui.internal.provisional.cheatsheets.TaskEditor; 30 import org.osgi.framework.Bundle; 31 32 public class TaskEditorManager { 33 34 private static TaskEditorManager instance; 35 36 private TaskEditorManager() { 37 } 38 39 public static TaskEditorManager getInstance() { 40 if (instance == null) { 41 instance = new TaskEditorManager(); 42 } 43 return instance; 44 } 45 46 public TaskEditor getEditor(String editorKind) { 47 CheatSheetRegistryReader.TaskEditorNode editorInfo = 48 CheatSheetRegistryReader.getInstance().findTaskEditor(editorKind); 49 if (editorInfo != null) { 50 TaskEditor editorInstance = null; 51 Class extClass = null; 52 String className = editorInfo.getClassName(); 53 try { 54 Bundle bundle = Platform.getBundle(editorInfo.getPluginId()); 55 extClass = bundle.loadClass(className); 56 } catch (Exception e) { 57 String message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object [] {className})); 58 Status status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e); 59 CheatSheetPlugin.getPlugin().getLog().log(status); 60 } 61 try { 62 if (extClass != null) { 63 Constructor c = extClass.getConstructor(new Class [0]); 64 Object [] parameters = new Object [0]; 65 editorInstance = (TaskEditor) c.newInstance(parameters); 66 } 67 } catch (Exception e) { 68 String message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object [] {className})); 69 IStatus status = new Status(IStatus.ERROR, ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID, IStatus.OK, message, e); 70 CheatSheetPlugin.getPlugin().getLog().log(status); 71 } 72 73 return editorInstance; 74 } 75 76 return null; 77 } 78 79 public ImageDescriptor getImageDescriptor(String editorKind) { 80 CheatSheetRegistryReader.TaskEditorNode editorInfo = 81 CheatSheetRegistryReader.getInstance().findTaskEditor(editorKind); 82 if (editorInfo != null) { 83 Bundle bundle = Platform.getBundle(editorInfo.getPluginId()); 84 URL url = FileLocator.find(bundle, new Path(editorInfo.getIconPath()), null); 85 if (url != null) { 86 try { 87 url = FileLocator.resolve(url); 88 return ImageDescriptor.createFromURL(url); 89 } catch (IOException e) { 90 return null; 91 } 92 } 93 } 94 return null; 95 } 96 97 } 98 | Popular Tags |