import java.applet.Applet;
import java.io.*;
import java.util.*;
import java.awt.*;
public class Util
{
public static Properties getPropertiesFromParams (
Applet applet,
String paramInfo[][])
{
Properties props = new Properties ();
String prop;
String val;
try
{
for (int i = 0; i < paramInfo.length; i++)
{
prop = paramInfo[i][0];
val = applet.getParameter (prop);
if (null != val)
props.put (prop, val);
}
}
catch (Throwable e)
{
}
return (props);
}
public static Properties mergeProperties(
Properties origProps,
Properties newProps,
boolean preserve)
{
Properties props = new Properties (origProps);
Enumeration nameEnum = newProps.propertyNames ();
String propName;
try
{
while (nameEnum.hasMoreElements ())
{
propName = (String) nameEnum.nextElement ();
if ((false == preserve) ||
(false == origProps.containsKey (propName)))
{
origProps.put (propName, newProps.getProperty (propName));
}
}
}
catch (Exception e)
{
(new MessageBox (
"Error",
"Unable to merge property lists:\n" +
Util.getExceptionTrace (e))).show ();
}
return (props);
}
public static Hashtable getHashtableFromProperties (
Properties props)
{
Hashtable hash = new Hashtable ();
Enumeration nameEnum = props.propertyNames ();
String propName;
Object propVal;
while (nameEnum.hasMoreElements ())
{
propName = (String) nameEnum.nextElement ();
propVal = props.getProperty (propName);
hash.put (propName, propVal);
}
return (hash);
}
public static Object[] loadExtensions (
InterfaceCompare comp,
String dir)
{
Vector objVector = new Vector ();
int i;
try
{
File file = new File (dir);
String filenameList[] = file.list (new ClassFilenameFilter ());
if (null == filenameList)
return (null);
Class objClass;
Object objInst;
for (i = 0; i < filenameList.length; i++)
{
try
{
String className = filenameList[i].substring (0, filenameList[i].length () - 6);
objClass = Class.forName (className);
if (!objClass.isInterface ())
{
objInst = objClass.newInstance ();
if (comp.implementsInterface (objInst))
{
System.out.println (
"Extension " +
objInst.getClass ().getName () +
" loaded");
objVector.addElement (objInst);
}
}
} catch (Exception e)
{
}
}
} catch (Exception e)
{
(new MessageBox (
"Error",
"Unable to load extensions:\n" +
Util.getExceptionTrace (e))).show ();
}
Object [] array = new Object [objVector.size ()];
for (i = 0; i < array.length; i++)
array[i] = objVector.elementAt (i);
return (array);
}
public static Object[] loadExtensions (
InterfaceCompare comp,
Properties props,
String paramName)
{
Vector objVector = new Vector ();
String paramVal = (String) props.getProperty (paramName);
int index = 0;
String className;
Class objClass;
Object objInst;
if (null == paramVal)
return (null);
do
{
index = paramVal.indexOf (';', index);
if (-1 == index)
{
className = paramVal;
paramVal = null;
}
else
{
className = paramVal.substring (0, index);
paramVal = paramVal.substring (index + 1);
}
try
{
if (0 == className.length ())
break;
objClass = Class.forName (className);
if (!objClass.isInterface ())
{
objInst = objClass.newInstance ();
if (comp.implementsInterface (objInst))
{
System.out.println (
"Extension " +
objInst.getClass ().getName () +
" loaded");
objVector.addElement (objInst);
}
}
}
catch (Exception e)
{
}
} while (null != paramVal);
Object [] array = new Object [objVector.size ()];
for (int i = 0; i < array.length; i++)
array[i] = objVector.elementAt (i);
return (array);
}
public static String getExceptionTrace (Throwable e)
{
StringWriter strWriter = new StringWriter ();
PrintWriter prtWriter = new PrintWriter (strWriter);
e.printStackTrace (prtWriter);
return (strWriter.toString ());
}
static void addGridBagComponent (
Container cont,
Component comp,
int gridX,
int gridY,
int gridWidth,
int gridHeight,
double weightX,
double weightY)
{
GridBagLayout gbl = (GridBagLayout) cont.getLayout ();
GridBagConstraints c = new GridBagConstraints ();
c.fill = GridBagConstraints.BOTH;
c.gridx = gridX;
c.gridy = gridY;
c.gridwidth = gridWidth;
c.gridheight = gridHeight;
c.weightx = weightX;
c.weighty = weightY;
cont.add (comp);
gbl.setConstraints (comp, c);
}
}
class ClassFilenameFilter
implements FilenameFilter
{
public boolean accept (
File dir,
String name)
{
return (name.endsWith (".class"));
}
}