KickJava   Java API By Example, From Geeks To Geeks.

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


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
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 JavaDoc editorKind) {
47         CheatSheetRegistryReader.TaskEditorNode editorInfo =
48             CheatSheetRegistryReader.getInstance().findTaskEditor(editorKind);
49         if (editorInfo != null) {
50             TaskEditor editorInstance = null;
51             Class JavaDoc extClass = null;
52             String JavaDoc className = editorInfo.getClassName();
53             try {
54                 Bundle bundle = Platform.getBundle(editorInfo.getPluginId());
55                 extClass = bundle.loadClass(className);
56             } catch (Exception JavaDoc e) {
57                 String JavaDoc message = NLS.bind(Messages.ERROR_LOADING_CLASS, (new Object JavaDoc[] {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 JavaDoc c = extClass.getConstructor(new Class JavaDoc[0]);
64                     Object JavaDoc[] parameters = new Object JavaDoc[0];
65                     editorInstance = (TaskEditor) c.newInstance(parameters);
66                 }
67             } catch (Exception JavaDoc e) {
68                 String JavaDoc message = NLS.bind(Messages.ERROR_CREATING_CLASS, (new Object JavaDoc[] {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 JavaDoc editorKind) {
80         CheatSheetRegistryReader.TaskEditorNode editorInfo =
81             CheatSheetRegistryReader.getInstance().findTaskEditor(editorKind);
82         if (editorInfo != null) {
83             Bundle bundle = Platform.getBundle(editorInfo.getPluginId());
84             URL JavaDoc 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 JavaDoc e) {
90                     return null;
91                 }
92             }
93         }
94         return null;
95     }
96
97 }
98
Popular Tags