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: import java.util.HashMap;
016: import java.util.Map;
017:
018: import org.eclipse.core.runtime.FileLocator;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Path;
021: import org.eclipse.core.runtime.Platform;
022: import org.eclipse.core.runtime.Status;
023: import org.eclipse.jface.resource.ImageDescriptor;
024: import org.eclipse.osgi.util.NLS;
025: import org.eclipse.swt.graphics.Image;
026: import org.eclipse.ui.internal.cheatsheets.CheatSheetPlugin;
027: import org.eclipse.ui.internal.cheatsheets.ICheatSheetResource;
028: import org.eclipse.ui.internal.cheatsheets.Messages;
029: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
030: import org.eclipse.ui.internal.provisional.cheatsheets.TaskExplorer;
031: import org.osgi.framework.Bundle;
032:
033: public class TaskExplorerManager {
034: private static TaskExplorerManager instance;
035:
036: private Map images;
037:
038: private TaskExplorerManager() {
039:
040: }
041:
042: public static TaskExplorerManager getInstance() {
043: if (instance == null) {
044: instance = new TaskExplorerManager();
045: }
046: return instance;
047: }
048:
049: public TaskExplorer getExplorer(String explorerKind) {
050: CheatSheetRegistryReader.TaskExplorerNode explorerInfo = CheatSheetRegistryReader
051: .getInstance().findTaskExplorer(explorerKind);
052: if (explorerInfo != null) {
053: TaskExplorer explorerInstance = null;
054: Class extClass = null;
055: String className = explorerInfo.getClassName();
056: try {
057: Bundle bundle = Platform.getBundle(explorerInfo
058: .getPluginId());
059: extClass = bundle.loadClass(className);
060: } catch (Exception e) {
061: String message = NLS.bind(Messages.ERROR_LOADING_CLASS,
062: (new Object[] { className }));
063: Status status = new Status(IStatus.ERROR,
064: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
065: IStatus.OK, message, e);
066: CheatSheetPlugin.getPlugin().getLog().log(status);
067: }
068: try {
069: if (extClass != null) {
070: Constructor c = extClass
071: .getConstructor(new Class[0]);
072: Object[] parameters = new Object[0];
073: explorerInstance = (TaskExplorer) c
074: .newInstance(parameters);
075: }
076: } catch (Exception e) {
077: String message = NLS.bind(
078: Messages.ERROR_CREATING_CLASS,
079: (new Object[] { className }));
080: IStatus status = new Status(IStatus.ERROR,
081: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID,
082: IStatus.OK, message, e);
083: CheatSheetPlugin.getPlugin().getLog().log(status);
084: }
085:
086: return explorerInstance;
087: }
088:
089: return null;
090: }
091:
092: private ImageDescriptor getImageDescriptor(String explorerKind) {
093: CheatSheetRegistryReader.TaskExplorerNode explorerInfo = CheatSheetRegistryReader
094: .getInstance().findTaskExplorer(explorerKind);
095: if (explorerInfo == null) {
096: return null;
097: }
098: String iconPath = explorerInfo.getIconPath();
099: if (iconPath == null) {
100: return null;
101: }
102: Bundle bundle = Platform.getBundle(explorerInfo.getPluginId());
103: URL url = FileLocator.find(bundle, new Path(iconPath), null);
104: try {
105: url = FileLocator.resolve(url);
106: return ImageDescriptor.createFromURL(url);
107: } catch (IOException e) {
108: return null;
109: }
110: }
111:
112: private Map getImages() {
113: if (images == null) {
114: initImages();
115: }
116: return images;
117: }
118:
119: private void initImages() {
120: if (images == null) {
121: images = new HashMap();
122: String[] ids = CheatSheetRegistryReader.getInstance()
123: .getExplorerIds();
124: for (int i = 0; i < ids.length; i++) {
125: ImageDescriptor descriptor = getImageDescriptor(ids[i]);
126: if (descriptor != null) {
127: images.put(ids[i], descriptor.createImage());
128: }
129: }
130: }
131: }
132:
133: public String getName(String explorerKind) {
134: CheatSheetRegistryReader.TaskExplorerNode explorerInfo = CheatSheetRegistryReader
135: .getInstance().findTaskExplorer(explorerKind);
136: if (explorerInfo != null) {
137: return explorerInfo.getName();
138: }
139: return null;
140: }
141:
142: public Image getImage(String id) {
143: return (Image) getImages().get(id);
144: }
145:
146: }
|