package com.vci.rmip.workflow.client.editor.code;
|
|
import java.util.ArrayList;
|
import java.util.Collection;
|
import java.util.Hashtable;
|
import java.util.List;
|
import java.util.Map;
|
|
import com.mxgraph.model.mxGraphModel.mxCollapseChange;
|
import com.mxgraph.model.mxGraphModel.mxGeometryChange;
|
import com.mxgraph.model.mxGraphModel.mxStyleChange;
|
import com.mxgraph.model.mxGraphModel.mxValueChange;
|
import com.mxgraph.model.mxGraphModel.mxVisibleChange;
|
|
public class GraphCodecRegistry {
|
|
/**
|
* Maps from constructor names to codecs.
|
*/
|
protected static Hashtable<String, GraphObjectCodec> codecs = new Hashtable<String, GraphObjectCodec>();
|
|
/**
|
* Maps from classnames to codecnames.
|
*/
|
protected static Hashtable<String, String> aliases = new Hashtable<String, String>();
|
|
/**
|
* Holds the list of known packages. Packages are used to prefix short
|
* class names (eg. mxCell) in XML markup.
|
*/
|
protected static List<String> packages = new ArrayList<String>();
|
|
// Registers the known codecs and package names
|
static
|
{
|
addPackage("com.mxgraph");
|
addPackage("com.mxgraph.util");
|
addPackage("com.mxgraph.model");
|
addPackage("com.mxgraph.view");
|
addPackage("java.lang");
|
addPackage("java.util");
|
|
register(new GraphObjectCodec(new ArrayList<Object>()));
|
register(new GraphModelCodec());
|
register(new GraphCellCodec());
|
register(new GraphStylesheetCodec());
|
|
register(new GraphRootChangeCodec());
|
register(new GraphChildChangeCodec());
|
register(new GraphTerminalChangeCodec());
|
register(new GraphGenericChangeCodec(new mxValueChange(), "value"));
|
register(new GraphGenericChangeCodec(new mxStyleChange(), "style"));
|
register(new GraphGenericChangeCodec(new mxGeometryChange(), "geometry"));
|
register(new GraphGenericChangeCodec(new mxCollapseChange(), "collapsed"));
|
register(new GraphGenericChangeCodec(new mxVisibleChange(), "visible"));
|
}
|
|
/**
|
* Registers a new codec and associates the name of the template constructor
|
* in the codec with the codec object. Automatically creates an alias if the
|
* codename and the classname are not equal.
|
*/
|
public static GraphObjectCodec register(GraphObjectCodec codec)
|
{
|
if (codec != null)
|
{
|
String name = codec.getName();
|
codecs.put(name, codec);
|
|
String classname = getName(codec.getTemplate());
|
|
if (!classname.equals(name))
|
{
|
addAlias(classname, name);
|
}
|
}
|
|
return codec;
|
}
|
|
/**
|
* Adds an alias for mapping a classname to a codecname.
|
*/
|
public static void addAlias(String classname, String codecname)
|
{
|
aliases.put(classname, codecname);
|
}
|
|
/**
|
* Returns a codec that handles the given object, which can be an object
|
* instance or an XML node.
|
*
|
* @param name Java class name.
|
*/
|
public static GraphObjectCodec getCodec(String name)
|
{
|
String tmp = aliases.get(name);
|
|
if (tmp != null)
|
{
|
name = tmp;
|
}
|
|
GraphObjectCodec codec = codecs.get(name);
|
|
// Registers a new default codec for the given name
|
// if no codec has been previously defined.
|
if (codec == null)
|
{
|
Object instance = getInstanceForName(name);
|
|
if (instance != null)
|
{
|
try
|
{
|
codec = new GraphObjectCodec(instance);
|
register(codec);
|
}
|
catch (Exception e)
|
{
|
// ignore
|
}
|
}
|
}
|
|
return codec;
|
}
|
|
/**
|
* Adds the given package name to the list of known package names.
|
*
|
* @param packagename Name of the package to be added.
|
*/
|
public static void addPackage(String packagename)
|
{
|
packages.add(packagename);
|
}
|
|
/**
|
* Creates and returns a new instance for the given class name.
|
*
|
* @param name Name of the class to be instantiated.
|
* @return Returns a new instance of the given class.
|
*/
|
public static Object getInstanceForName(String name)
|
{
|
Class<?> clazz = getClassForName(name);
|
|
if (clazz != null)
|
{
|
if (clazz.isEnum())
|
{
|
// For an enum, use the first constant as the default instance
|
return clazz.getEnumConstants()[0];
|
}
|
else
|
{
|
try
|
{
|
return clazz.newInstance();
|
}
|
catch (Exception e)
|
{
|
// ignore
|
}
|
}
|
}
|
|
return null;
|
}
|
|
/**
|
* Returns a class that corresponds to the given name.
|
*
|
* @param name
|
* @return Returns the class for the given name.
|
*/
|
public static Class<?> getClassForName(String name)
|
{
|
try
|
{
|
return Class.forName(name);
|
}
|
catch (Exception e)
|
{
|
// ignore
|
}
|
|
for (int i = 0; i < packages.size(); i++)
|
{
|
try
|
{
|
String s = packages.get(i);
|
|
return Class.forName(s + "." + name);
|
}
|
catch (Exception e)
|
{
|
// ignore
|
}
|
}
|
|
return null;
|
}
|
|
/**
|
* Returns the name that identifies the codec associated
|
* with the given instance..
|
*
|
* The I/O system uses unqualified classnames, eg. for a
|
* <code>com.mxgraph.model.mxCell</code> this returns
|
* <code>mxCell</code>.
|
*
|
* @param instance Instance whose node name should be returned.
|
* @return Returns a string that identifies the codec.
|
*/
|
public static String getName(Object instance)
|
{
|
Class<? extends Object> type = instance.getClass();
|
|
if (type.isArray() || Collection.class.isAssignableFrom(type)
|
|| Map.class.isAssignableFrom(type))
|
{
|
return "Array";
|
}
|
else
|
{
|
if (packages.contains(type.getPackage().getName()))
|
{
|
return type.getSimpleName();
|
}
|
else
|
{
|
return type.getName();
|
}
|
}
|
}
|
|
}
|