KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > render > RendererRegistry


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer.render;
28
29 import org.apache.log4j.Logger;
30 import org.eclipse.core.runtime.IConfigurationElement;
31 import org.eclipse.core.runtime.IExtension;
32 import org.eclipse.core.runtime.Platform;
33 import org.eclipse.jface.resource.ImageDescriptor;
34 import org.osgi.framework.Bundle;
35
36 import org.nightlabs.base.extensionpoint.AbstractEPProcessor;
37 import org.nightlabs.base.extensionpoint.EPProcessorException;
38 import org.nightlabs.base.util.ImageUtil;
39 import org.nightlabs.editor2d.render.RenderModeDescriptor;
40 import org.nightlabs.editor2d.render.RenderModeManager;
41 import org.nightlabs.editor2d.render.Renderer;
42
43 public class RendererRegistry
44 extends AbstractEPProcessor
45 {
46     public static final String JavaDoc EXTENSION_POINT_ID = "org.nightlabs.editor2d.viewer.renderModeRegistry";
47     public static final Logger LOGGER = Logger.getLogger(RendererRegistry.class.getName());
48     
49     protected static RendererRegistry _sharedInstance;
50     public static RendererRegistry sharedInstance()
51     {
52         if (_sharedInstance == null) {
53             _sharedInstance = new RendererRegistry();
54 // _sharedInstance.process();
55
}
56         return _sharedInstance;
57     }
58     
59     protected RendererRegistry() {
60         super();
61     }
62
63     public String JavaDoc getExtensionPointID() {
64         return EXTENSION_POINT_ID;
65     }
66
67     public static final String JavaDoc ELEMENT_REGISTRY = "registry";
68     public static final String JavaDoc ATTRIBUTE_MODE = "mode";
69     public static final String JavaDoc ATTRIBUTE_RENDERER = "renderer";
70     public static final String JavaDoc ATTRIBUTE_DRAWCOMPONENT_CLASS = "drawComponentClass";
71     public static final String JavaDoc ATTRIBUTE_NAME = "name";
72     public static final String JavaDoc ATTRIBUTE_DESCRIPTION = "description";
73     public static final String JavaDoc ATTRIBUTE_ICON = "icon";
74     
75     public void processElement(IExtension extension, IConfigurationElement element)
76     throws EPProcessorException
77     {
78         if (renderModeManager == null)
79             renderModeManager = new RenderModeManager();
80         
81         if (element.getName().equalsIgnoreCase(ELEMENT_REGISTRY))
82         {
83             try {
84                 String JavaDoc mode = element.getAttribute(ATTRIBUTE_MODE);
85                 int renderMode = 1;
86                 try {
87                     renderMode = Integer.parseInt(mode);
88                 } catch (NumberFormatException JavaDoc e) {
89                     throw new IllegalArgumentException JavaDoc("Attribute mode must be an int");
90                 }
91         
92                 Object JavaDoc renderer = (Object JavaDoc) element.createExecutableExtension(ATTRIBUTE_RENDERER);
93                 if (!(renderer instanceof Renderer))
94                     throw new IllegalArgumentException JavaDoc("Attribute renderer must implement "+Renderer.class.getName()+" "+renderer.getClass().getName()+" does not!");
95                 Renderer r = (Renderer) renderer;
96                 
97                 Class JavaDoc dcClass = null;
98                 try {
99                     dcClass = Platform.getBundle(extension.getNamespace()).loadClass(element.getAttribute(ATTRIBUTE_DRAWCOMPONENT_CLASS));
100                 } catch (ClassNotFoundException JavaDoc e) {
101                     throw new EPProcessorException(e);
102                 }
103                 
104 // Object drawComponentClass = (Object) element.createExecutableExtension(ATTRIBUTE_DRAWCOMPONENT_CLASS);
105
// if (!(drawComponentClass instanceof DrawComponent))
106
// throw new IllegalArgumentException("Attribute renderer must implement "+DrawComponent.class.getName()+" "+drawComponentClass.getClass().getName()+" does not!");
107
// Class dcClass = (Class) drawComponentClass;
108

109                 String JavaDoc name = element.getAttribute(ATTRIBUTE_NAME);
110                 String JavaDoc description = element.getAttribute(ATTRIBUTE_DESCRIPTION);
111                 String JavaDoc icon = element.getAttribute(ATTRIBUTE_ICON);
112                 
113                 RenderModeDescriptor desc = null;
114                 if (checkString(name) && checkString(description) && checkString(icon))
115                 {
116                     Bundle bundle = Platform.getBundle(extension.getNamespace());
117                     ImageDescriptor imgDesc = ImageDescriptor.createFromURL(bundle.getEntry(icon));
118                     
119                     desc = new RenderModeDescriptor(renderMode, name, description,
120                             ImageUtil.convertToAWT(imgDesc.getImageData()));
121                 }
122                 else if (checkString(name) && checkString(description)) {
123                     desc = new RenderModeDescriptor(renderMode, name, description);
124                 }
125                 else if (checkString(name)) {
126                     desc = new RenderModeDescriptor(renderMode, name);
127                 }
128                 
129                 if (desc != null) {
130                     renderModeManager.addRenderer(desc, dcClass, r);
131                 }
132                 else {
133                     renderModeManager.addRenderer(renderMode, dcClass, r);
134                 }
135                 
136 // LOGGER.debug("mode = " + mode);
137
// LOGGER.debug("renderer = " + renderer);
138
// LOGGER.debug("drawComponentClass = " + dcClass.getName());
139

140             } catch (Exception JavaDoc e) {
141                 throw new EPProcessorException(e);
142             }
143         }
144     }
145
146     protected RenderModeManager renderModeManager;
147     public RenderModeManager getRenderModeManager()
148     {
149         checkProcessing();
150         if (renderModeManager == null)
151             renderModeManager = new RenderModeManager();
152         
153         return renderModeManager;
154     }
155     
156 }
157
Popular Tags