001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets.composite.views;
011:
012: import java.io.IOException;
013: import java.lang.reflect.Constructor;
014: import java.net.URL;
015:
016: import org.eclipse.core.runtime.FileLocator;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.jface.resource.ImageDescriptor;
022: import org.eclipse.osgi.util.NLS;
023: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
024: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
025: import org.eclipse.ui.internal.cheatsheets.Messages;
026: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
027: import org.eclipse.ui.internal.provisional.cheatsheets.TaskEditor;
028: import org.osgi.framework.Bundle;
029:
030: public class TaskEditorManager {
031:
032: private static TaskEditorManager instance;
033:
034: private TaskEditorManager() {
035: }
036:
037: public static TaskEditorManager getInstance() {
038: if (instance == null) {
039: instance = new TaskEditorManager();
040: }
041: return instance;
042: }
043:
044: public TaskEditor getEditor(String editorKind) {
045: CheatSheetRegistryReader.TaskEditorNode editorInfo = CheatSheetRegistryReader
046: .getInstance().findTaskEditor(editorKind);
047: if (editorInfo != null) {
048: TaskEditor editorInstance = null;
049: Class extClass = null;
050: String className = editorInfo.getClassName();
051: try {
052: Bundle bundle = Platform.getBundle(editorInfo
053: .getPluginId());
054: extClass = bundle.loadClass(className);
055: } catch (Exception e) {
056: String message = NLS.bind(Messages.ERROR_LOADING_CLASS,
057: (new Object[] { className }));
058: Status status = new Status(IStatus.ERROR,
059: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
060: IStatus.OK, message, e);
061: CheatSheetPlugin.getPlugin().getLog().log(status);
062: }
063: try {
064: if (extClass != null) {
065: Constructor c = extClass
066: .getConstructor(new Class[0]);
067: Object[] parameters = new Object[0];
068: editorInstance = (TaskEditor) c
069: .newInstance(parameters);
070: }
071: } catch (Exception e) {
072: String message = NLS.bind(
073: Messages.ERROR_CREATING_CLASS,
074: (new Object[] { className }));
075: IStatus status = new Status(IStatus.ERROR,
076: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
077: IStatus.OK, message, e);
078: CheatSheetPlugin.getPlugin().getLog().log(status);
079: }
080:
081: return editorInstance;
082: }
083:
084: return null;
085: }
086:
087: public ImageDescriptor getImageDescriptor(String editorKind) {
088: CheatSheetRegistryReader.TaskEditorNode editorInfo = CheatSheetRegistryReader
089: .getInstance().findTaskEditor(editorKind);
090: if (editorInfo != null) {
091: Bundle bundle = Platform
092: .getBundle(editorInfo.getPluginId());
093: URL url = FileLocator.find(bundle, new Path(editorInfo
094: .getIconPath()), null);
095: if (url != null) {
096: try {
097: url = FileLocator.resolve(url);
098: return ImageDescriptor.createFromURL(url);
099: } catch (IOException e) {
100: return null;
101: }
102: }
103: }
104: return null;
105: }
106:
107: }
|