001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/scenegrapheditor/sourcecontrol/Delegator.java,v 1.1 2005/04/20 22:21:20 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Jan Becicka.
012: * Portions created by Jan Becicka are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Jan Becicka.
016: *
017: **/
018: package org.jdesktop.j3dedit.scenegrapheditor.sourcecontrol;
019:
020: import java.util.Hashtable;
021: import java.beans.*;
022: import java.io.BufferedOutputStream;
023: import java.io.FileOutputStream;
024: import java.lang.reflect.Constructor;
025: import java.lang.reflect.InvocationTargetException;
026: import java.util.Enumeration;
027: import org.jdesktop.j3dedit.J3dEditContext;
028:
029: /**
030: *
031: * @author Jan Becicka
032: * @version
033: */
034: public class Delegator {
035:
036: private Hashtable table;
037: private NamePool namePool;
038:
039: /** Creates new Delegator */
040: public Delegator(J3dEditContext context, NamePool np) {
041: namePool = np;
042: setTable(context.getConfigLoader().getGeneratorTable());
043: }
044:
045: private void setTable(Hashtable t) {
046: Enumeration e = t.keys();
047: String key;
048: table = new Hashtable(t.size());
049: while (e.hasMoreElements()) {
050: key = (String) e.nextElement();
051: try {
052: Class keyClass = Class.forName(key);
053: Class genClass = Class.forName((String) t.get(key));
054: table.put(keyClass, genClass);
055: } catch (ClassNotFoundException ex) {
056: System.out.println("Could not find " + key + " or "
057: + t.get(key) + " in classpath");
058: }
059: }
060: }
061:
062: public BeanCodeGenerator getBeanCodeGenerator(Object bean) {
063: //is the generator in configuration file?
064: Class generatorClass = (Class) table.get(bean.getClass());
065: BeanCodeGenerator g = null;
066: if (generatorClass == null) {
067: //is the generator in spec package?
068: generatorClass = findClass(bean.getClass());
069: }
070:
071: if (generatorClass == null) {
072: //use default
073: if (bean.getClass().isArray()) {
074: generatorClass = ArrayGenerator.class;
075: //generatorClass = BeanCodeGenerator.class;
076: } else {
077: generatorClass = BeanCodeGenerator.class;
078: }
079: }
080:
081: try {
082: Constructor constr = generatorClass
083: .getConstructor(new Class[] { Object.class,
084: NamePool.class });
085: g = (BeanCodeGenerator) constr.newInstance(new Object[] {
086: bean, namePool });
087: } catch (NoSuchMethodException e) {
088: e.printStackTrace();
089: } catch (InstantiationException e) {
090: e.printStackTrace();
091: } catch (IllegalAccessException e) {
092: e.printStackTrace();
093: } catch (InvocationTargetException e) {
094: e.printStackTrace();
095: }
096: return g;
097: }
098:
099: private static Class findClass(Class clazz) {
100: Class result = find(clazz);
101: clazz = clazz.getSuperclass();
102: while (result == null && clazz != Object.class) {
103: result = find(clazz);
104: clazz = clazz.getSuperclass();
105: }
106: return result;
107: }
108:
109: private static Class find(Class clazz) {
110: Class result;
111: try {
112: result = Class.forName(Delegator.class.getPackage()
113: .getName()
114: + ".spec." + clazz.getName());
115: } catch (ClassNotFoundException e) {
116: result = null;
117: }
118: return result;
119: }
120: }
|