KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > rcp > action > NewFileRegistry


1 /**
2  * <p> Project: com.nightlabs.base </p>
3  * <p> Copyright: Copyright (c) 2005 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 01.07.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.rcp.action;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Map JavaDoc;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.jface.resource.ImageDescriptor;
16
17 import com.nightlabs.base.NLBasePlugin;
18 import com.nightlabs.rcp.extensionpoint.AbstractEPProcessor;
19 import com.nightlabs.rcp.extensionpoint.EPProcessorException;
20
21 public class NewFileRegistry
22 extends AbstractEPProcessor
23 {
24     public static final String JavaDoc EXTENSION_POINT_ID = "com.nightlabs.base.newfileaction";
25     public static final String JavaDoc DEFAULT_CATEGORY = "Default category";
26     
27     private static NewFileRegistry sharedInstance;
28     public static NewFileRegistry getSharedInstance() {
29         if (sharedInstance == null)
30             sharedInstance = new NewFileRegistry();
31         return sharedInstance;
32     }
33     
34     protected Map JavaDoc category2Actions = new HashMap JavaDoc();
35     public Map JavaDoc getCategory2Actions() {
36         checkProcessing();
37         return category2Actions;
38     }
39     
40     public String JavaDoc getCategoryName(String JavaDoc categoryID) {
41         return categoryRegistry.getCategoryName(categoryID);
42     }
43     
44     protected CategoryRegistry categoryRegistry = new CategoryRegistry();
45     
46     public String JavaDoc getExtensionPointID() {
47         return EXTENSION_POINT_ID;
48     }
49
50     public void processElement(IConfigurationElement element)
51     throws EPProcessorException
52     {
53         if (element.getName().equalsIgnoreCase("action"))
54         {
55             String JavaDoc className = element.getAttribute("class");
56             if (!checkString(className))
57                 throw new EPProcessorException("Element action has to define attribute class.");
58                         
59             String JavaDoc categoryID = element.getAttribute("categoryID");
60             if (!checkString(categoryID) || categoryRegistry.getCategoryName(categoryID) == null)
61                 categoryID = DEFAULT_CATEGORY;
62                 
63             String JavaDoc fileExtension = element.getAttribute("fileExtension");
64             if (!checkString(fileExtension))
65                 throw new EPProcessorException("Element action has to define attribute fileExtension.");
66             
67             String JavaDoc title = element.getAttribute("title");
68             if (!checkString(title))
69                 throw new EPProcessorException("Element action has to define attribute title.");
70
71             String JavaDoc tooltip = element.getAttribute("tooltip");
72             String JavaDoc iconName = element.getAttribute("icon");
73             
74 // try {
75
// Class actionClass = Class.forName(className);
76
// Object o = actionClass.newInstance();
77
// if (o instanceof IAction) {
78
// IAction action = (IAction) o;
79
// category2Actions.put(categoryID, action);
80
// }
81
// } catch (ClassNotFoundException e) {
82
// throw new EPProcessorException("Class "+className+" not found!");
83
// } catch (InstantiationException e) {
84
// throw new EPProcessorException("Class "+className+" can not be instantiated!");
85
// } catch (IllegalAccessException e) {
86
// throw new EPProcessorException("Class "+className+" can not be accessed!");
87
// }
88

89             Object JavaDoc o;
90             try {
91                 o = element.createExecutableExtension("class");
92                 if (o instanceof INewFileAction) {
93                     INewFileAction action = (INewFileAction) o;
94                     action.setFileExtension(fileExtension);
95                     action.setText(title);
96                     if (checkString(tooltip))
97                         action.setToolTipText(tooltip);
98                     if (checkString(iconName))
99                         action.setImageDescriptor(getImageDescriptor(iconName));
100                     
101                     category2Actions.put(categoryID, action);
102                 }
103             } catch (CoreException e) {
104                 throw new EPProcessorException(e);
105             }
106         }
107     }
108
109     protected ImageDescriptor getImageDescriptor(String JavaDoc iconName)
110     {
111         if (iconName != null) {
112             return ImageDescriptor.createFromURL(NLBasePlugin.getDefault().getBundle().getEntry(iconName));
113         }
114         return null;
115     }
116     
117     protected boolean checkString(String JavaDoc s)
118     {
119         if (s == null || "".equals(s))
120             return false;
121         
122         return true;
123     }
124     
125     protected class CategoryRegistry
126     extends AbstractEPProcessor
127     {
128         protected Map JavaDoc categoryID2name = new HashMap JavaDoc();
129         
130         public String JavaDoc getExtensionPointID() {
131             return EXTENSION_POINT_ID;
132         }
133         
134         // TODO: implement this
135
public String JavaDoc getCategoryName(String JavaDoc categoryID)
136         {
137             checkProcessing();
138             return (String JavaDoc) categoryID2name.get(categoryID);
139         }
140
141         public void processElement(IConfigurationElement element)
142         throws EPProcessorException
143         {
144             if (element.getName().equalsIgnoreCase("category"))
145             {
146                 String JavaDoc id = element.getAttribute("id");
147                 if (!checkString(id))
148                     throw new EPProcessorException("Element category has to define attribute id.");
149                 
150                 String JavaDoc name = element.getAttribute("name");
151                 if (!checkString(name))
152                     throw new EPProcessorException("Element category has to define attribute name.");
153                 
154                 categoryID2name.put(id, name);
155             }
156         }
157                 
158     }
159     
160 }
Popular Tags