X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src2%2Fnet%2Fmiginfocom%2Flayout%2FLayoutUtil.java;fp=src%2Fnet%2Fmiginfocom%2Flayout%2FLayoutUtil.java;h=c81782734ce720efd946bc9ef5bdc612e4df51ab;hb=665d2c2f4c1310e6985b93b7c2c8a8eec2fa9086;hp=4a23e0f71a23d0dc23a2fcc7abba19fa74d63d8d;hpb=0e684f72690bd6532272a39ab6c188a27559fd09;p=jalview.git diff --git a/src/net/miginfocom/layout/LayoutUtil.java b/src2/net/miginfocom/layout/LayoutUtil.java similarity index 82% rename from src/net/miginfocom/layout/LayoutUtil.java rename to src2/net/miginfocom/layout/LayoutUtil.java index 4a23e0f..c817827 100644 --- a/src/net/miginfocom/layout/LayoutUtil.java +++ b/src2/net/miginfocom/layout/LayoutUtil.java @@ -1,18 +1,7 @@ package net.miginfocom.layout; -//import java.beans.Beans; -//import java.beans.ExceptionListener; -//import java.beans.Introspector; -//import java.beans.PersistenceDelegate; -//import java.beans.XMLDecoder; -//import java.beans.XMLEncoder; -//import java.io.ByteArrayInputStream; -//import java.io.ByteArrayOutputStream; -import java.io.EOFException; -import java.io.IOException; -//import java.io.ObjectInput; -//import java.io.ObjectOutput; -//import java.io.OutputStream; +import java.beans.*; +import java.io.*; import java.util.IdentityHashMap; import java.util.TreeSet; import java.util.WeakHashMap; @@ -76,18 +65,17 @@ public final class LayoutUtil private static volatile WeakHashMap DT_MAP = null; // The Containers that have design time. Value not used. private static int eSz = 0; private static int globalDebugMillis = 0; - - // public static final boolean HAS_BEANS = hasBeans(); - -// private static boolean hasBeans() -// { -// try { -// LayoutUtil.class.getClassLoader().loadClass("java.beans.Beans"); -// return true; -// } catch (Throwable e) { -// return false; -// } -// } + public static final boolean HAS_BEANS = hasBeans(); + + private static boolean hasBeans() + { + try { + LayoutUtil.class.getClassLoader().loadClass("java.beans.Beans"); + return true; + } catch (Throwable e) { + return false; + } + } private LayoutUtil() { @@ -152,7 +140,7 @@ public final class LayoutUtil public static boolean isDesignTime(ContainerWrapper cw) { if (DT_MAP == null) - return false;// BH 2018 //HAS_BEANS && Beans.isDesignTime(); + return HAS_BEANS && Beans.isDesignTime(); // assume design time "in general" (cw is null) if there is at least one container with design time // (for storing constraints creation strings in method putCCString()) @@ -202,17 +190,17 @@ public final class LayoutUtil } } -// /** Sets/add the persistence delegates to be used for a class. -// * @param c The class to set the registered delegate for. -// * @param del The new delegate or null to erase to old one. -// */ -// static synchronized void setDelegate(Class c, PersistenceDelegate del) -// { -// try { -// Introspector.getBeanInfo(c, Introspector.IGNORE_ALL_BEANINFO).getBeanDescriptor().setValue("persistenceDelegate", del); -// } catch (Exception ignored) { -// } -// } + /** Sets/add the persistence delegates to be used for a class. + * @param c The class to set the registered delegate for. + * @param del The new delegate or null to erase to old one. + */ + static synchronized void setDelegate(Class c, PersistenceDelegate del) + { + try { + Introspector.getBeanInfo(c, Introspector.IGNORE_ALL_BEANINFO).getBeanDescriptor().setValue("persistenceDelegate", del); + } catch (Exception ignored) { + } + } /** Returns strings set with {@link #putCCString(Object, String)} or null if nothing is associated or * {@link LayoutUtil#isDesignTime(ContainerWrapper)} returns false. @@ -499,89 +487,89 @@ public final class LayoutUtil return (i != null && i[side] != null) ? i[side] : (getDefault ? PlatformDefaults.getPanelInsets(side) : UnitValue.ZERO); } -// /** Writes the object and CLOSES the stream. Uses the persistence delegate registered in this class. -// * @param os The stream to write to. Will be closed. -// * @param o The object to be serialized. -// * @param listener The listener to receive the exceptions if there are any. If null not used. -// */ -// static void writeXMLObject(OutputStream os, Object o, ExceptionListener listener) -// { -// ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); -// Thread.currentThread().setContextClassLoader(LayoutUtil.class.getClassLoader()); -// -// XMLEncoder encoder = new XMLEncoder(os); -// -// if (listener != null) -// encoder.setExceptionListener(listener); -// -// encoder.writeObject(o); -// encoder.close(); // Must be closed to write. -// -// Thread.currentThread().setContextClassLoader(oldClassLoader); -// } -// -// private static ByteArrayOutputStream writeOutputStream = null; -// /** Writes an object to XML. -// * @param out The object out to write to. Will not be closed. -// * @param o The object to write. -// */ -// public static synchronized void writeAsXML(ObjectOutput out, Object o) throws IOException -// { -// if (writeOutputStream == null) -// writeOutputStream = new ByteArrayOutputStream(16384); -// -// writeOutputStream.reset(); -// -// writeXMLObject(writeOutputStream, o, new ExceptionListener() { -// @Override -// public void exceptionThrown(Exception e) { -// e.printStackTrace(); -// }}); -// -// byte[] buf = writeOutputStream.toByteArray(); -// -// out.writeInt(buf.length); -// out.write(buf); -// } -// -// private static byte[] readBuf = null; -// /** Reads an object from in using the -// * @param in The object input to read from. -// * @return The object. Never null. -// * @throws IOException If there was a problem saving as XML -// */ -// public static synchronized Object readAsXML(ObjectInput in) throws IOException -// { -// if (readBuf == null) -// readBuf = new byte[16384]; -// -// Thread cThread = Thread.currentThread(); -// ClassLoader oldCL = null; -// -// try { -// oldCL = cThread.getContextClassLoader(); -// cThread.setContextClassLoader(LayoutUtil.class.getClassLoader()); -// } catch(SecurityException ignored) { -// } -// -// Object o = null; -// try { -// int length = in.readInt(); -// if (length > readBuf.length) -// readBuf = new byte[length]; -// -// in.readFully(readBuf, 0, length); -// -// o = new XMLDecoder(new ByteArrayInputStream(readBuf, 0, length)).readObject(); -// -// } catch(EOFException ignored) { -// } -// -// if (oldCL != null) -// cThread.setContextClassLoader(oldCL); -// -// return o; -// } + /** Writes the object and CLOSES the stream. Uses the persistence delegate registered in this class. + * @param os The stream to write to. Will be closed. + * @param o The object to be serialized. + * @param listener The listener to receive the exceptions if there are any. If null not used. + */ + static void writeXMLObject(OutputStream os, Object o, ExceptionListener listener) + { + ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); + Thread.currentThread().setContextClassLoader(LayoutUtil.class.getClassLoader()); + + XMLEncoder encoder = new XMLEncoder(os); + + if (listener != null) + encoder.setExceptionListener(listener); + + encoder.writeObject(o); + encoder.close(); // Must be closed to write. + + Thread.currentThread().setContextClassLoader(oldClassLoader); + } + + private static ByteArrayOutputStream writeOutputStream = null; + /** Writes an object to XML. + * @param out The object out to write to. Will not be closed. + * @param o The object to write. + */ + public static synchronized void writeAsXML(ObjectOutput out, Object o) throws IOException + { + if (writeOutputStream == null) + writeOutputStream = new ByteArrayOutputStream(16384); + + writeOutputStream.reset(); + + writeXMLObject(writeOutputStream, o, new ExceptionListener() { + @Override + public void exceptionThrown(Exception e) { + e.printStackTrace(); + }}); + + byte[] buf = writeOutputStream.toByteArray(); + + out.writeInt(buf.length); + out.write(buf); + } + + private static byte[] readBuf = null; + /** Reads an object from in using the + * @param in The object input to read from. + * @return The object. Never null. + * @throws IOException If there was a problem saving as XML + */ + public static synchronized Object readAsXML(ObjectInput in) throws IOException + { + if (readBuf == null) + readBuf = new byte[16384]; + + Thread cThread = Thread.currentThread(); + ClassLoader oldCL = null; + + try { + oldCL = cThread.getContextClassLoader(); + cThread.setContextClassLoader(LayoutUtil.class.getClassLoader()); + } catch(SecurityException ignored) { + } + + Object o = null; + try { + int length = in.readInt(); + if (length > readBuf.length) + readBuf = new byte[length]; + + in.readFully(readBuf, 0, length); + + o = new XMLDecoder(new ByteArrayInputStream(readBuf, 0, length)).readObject(); + + } catch(EOFException ignored) { + } + + if (oldCL != null) + cThread.setContextClassLoader(oldCL); + + return o; + } private static final IdentityHashMap SER_MAP = new IdentityHashMap(2);