home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / ExternalizableButton.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  1.9 KB  |  93 lines

  1.  
  2.  
  3. /**
  4.  * A subclass of ExplicitButton that uses Externalization to
  5.  * gain bit0by-bit control of its Serialized state.
  6.  * 
  7.  * @see sunw.demo.buttons.ExplicitButton
  8.  * @see jav.io.Externalizable
  9.  */
  10.  
  11. package sunw.demo.buttons;
  12.  
  13. import java.awt.*;
  14. import java.beans.*;
  15. import java.io.*;
  16.  
  17. public class ExternalizableButton extends ExplicitButton
  18.             implements Externalizable {
  19.  
  20.     // We take complete control of our own persistence state with externalization.
  21.  
  22.     public void writeExternal(ObjectOutput out) throws IOException {
  23.     out.writeInt(currentMagic);
  24.  
  25.     Rectangle bounds = getBounds();
  26.     out.writeInt(bounds.x);
  27.     out.writeInt(bounds.y);
  28.         out.writeInt(bounds.width);
  29.     out.writeInt(bounds.height);
  30.  
  31.     out.writeUTF(getLabel());
  32.  
  33.     Color bg = getBackground();
  34.     if (bg == null) {
  35.         out.writeInt(0);
  36.     } else {
  37.         out.writeInt(bg.getRGB());
  38.     }
  39.  
  40.     Color fg = getForeground();
  41.     if (fg == null) {
  42.         out.writeInt(0);
  43.     } else {
  44.         out.writeInt(fg.getRGB());
  45.     }
  46.  
  47.     Font f = getFont();
  48.     if (f == null) {
  49.         out.writeInt(-1);
  50.     } else {
  51.         out.writeInt(f.getStyle());
  52.         out.writeInt(f.getSize());
  53.         out.writeUTF(f.getName());
  54.     }
  55.     }
  56.  
  57.     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  58.     int magic = in.readInt();
  59.     if (magic != currentMagic) {
  60.         throw new IOException("magic number mismatch for ExternalizableButton");
  61.     }
  62.  
  63.     int x = in.readInt();
  64.     int y = in.readInt();
  65.     int width = in.readInt();
  66.     int height = in.readInt();
  67.     setBounds(x, y, width, height);
  68.  
  69.     setLabel(in.readUTF());
  70.  
  71.     int bg = in.readInt();
  72.     if (bg != 0) {
  73.         setBackground(new Color(bg));
  74.     } 
  75.  
  76.     int fg = in.readInt();
  77.     if (fg != 0) {
  78.         setForeground(new Color(fg));
  79.     } 
  80.     
  81.     int style = in.readInt();
  82.     if (style >= 0) {
  83.         int size = in.readInt();
  84.         String name = in.readUTF();
  85.         Font f = new Font(name, style, size);
  86.         setFont(f);
  87.     }
  88.  
  89.     }
  90.  
  91.     private final static int currentMagic = 0xAAAAAAAA;
  92. }
  93.