Saving and restoring the state of a pretend CAD system : Java Beans « Development Class « Java






Saving and restoring the state of a pretend CAD system

Saving and restoring the state of a pretend CAD system
    

// : c12:CADState.java
// Saving and restoring the state of a pretend CAD system.
// {Clean: CADState.out}
//package c12;
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

abstract class Shape implements Serializable {
  public static final int RED = 1, BLUE = 2, GREEN = 3;

  private int xPos, yPos, dimension;

  private static Random r = new Random();

  private static int counter = 0;

  public abstract void setColor(int newColor);

  public abstract int getColor();

  public Shape(int xVal, int yVal, int dim) {
    xPos = xVal;
    yPos = yVal;
    dimension = dim;
  }

  public String toString() {
    return getClass() + "color[" + getColor() + "] xPos[" + xPos
        + "] yPos[" + yPos + "] dim[" + dimension + "]\n";
  }

  public static Shape randomFactory() {
    int xVal = r.nextInt(100);
    int yVal = r.nextInt(100);
    int dim = r.nextInt(100);
    switch (counter++ % 3) {
    default:
    case 0:
      return new Circle(xVal, yVal, dim);
    case 1:
      return new Square(xVal, yVal, dim);
    case 2:
      return new Line(xVal, yVal, dim);
    }
  }
}

class Circle extends Shape {
  private static int color = RED;

  public Circle(int xVal, int yVal, int dim) {
    super(xVal, yVal, dim);
  }

  public void setColor(int newColor) {
    color = newColor;
  }

  public int getColor() {
    return color;
  }
}

class Square extends Shape {
  private static int color;

  public Square(int xVal, int yVal, int dim) {
    super(xVal, yVal, dim);
    color = RED;
  }

  public void setColor(int newColor) {
    color = newColor;
  }

  public int getColor() {
    return color;
  }
}

class Line extends Shape {
  private static int color = RED;

  public static void serializeStaticState(ObjectOutputStream os)
      throws IOException {
    os.writeInt(color);
  }

  public static void deserializeStaticState(ObjectInputStream os)
      throws IOException {
    color = os.readInt();
  }

  public Line(int xVal, int yVal, int dim) {
    super(xVal, yVal, dim);
  }

  public void setColor(int newColor) {
    color = newColor;
  }

  public int getColor() {
    return color;
  }
}

public class CADState {
  public static void main(String[] args) throws Exception {
    List shapeTypes, shapes;
    if (args.length == 0) {
      shapeTypes = new ArrayList();
      shapes = new ArrayList();
      // Add references to the class objects:
      shapeTypes.add(Circle.class);
      shapeTypes.add(Square.class);
      shapeTypes.add(Line.class);
      // Make some shapes:
      for (int i = 0; i < 10; i++)
        shapes.add(Shape.randomFactory());
      // Set all the static colors to GREEN:
      for (int i = 0; i < 10; i++)
        ((Shape) shapes.get(i)).setColor(Shape.GREEN);
      // Save the state vector:
      ObjectOutputStream out = new ObjectOutputStream(
          new FileOutputStream("CADState.out"));
      out.writeObject(shapeTypes);
      Line.serializeStaticState(out);
      out.writeObject(shapes);
    } else { // There's a command-line argument
      ObjectInputStream in = new ObjectInputStream(new FileInputStream(
          args[0]));
      // Read in the same order they were written:
      shapeTypes = (List) in.readObject();
      Line.deserializeStaticState(in);
      shapes = (List) in.readObject();
    }
    // Display the shapes:
    System.out.println(shapes);
  }
} ///:~


           
         
    
    
    
  








Related examples in the same category

1.JavaBean: BeanContextSupportJavaBean: BeanContextSupport
2.JavaBean: Test program that adds 100 beans to a contextJavaBean: Test program that adds 100 beans to a context
3.JavaBean: how to use the instantiateChild() convenience method to create a beanJavaBean: how to use the instantiateChild() convenience method to create a bean
4.JavaBean: illustrate delivery of the BeanContextMembershipEventJavaBean: illustrate delivery of the BeanContextMembershipEvent
5.JavaBean: creates all of the objects, a tests the service capabilitiesJavaBean: creates all of the objects, a tests the service capabilities
6.Bean ContainerBean Container
7.PropertyTablePropertyTable
8.Introspecting a BeanIntrospecting a Bean
9.Listening for Changes to the Selected File in a JFileChooser Dialog
10.Get a list of selected files
11.Listening for Changes to the Current Directory in a JFileChooser Dialog
12.Displaying the Current Directory in the Title of a JFileChooser Dialog
13.Setting an Accessory Component in a JFileChooser Dialog
14.Convert a bean to XML persistence
15.Listen for bean's property change event
16.List property names of a Bean
17.Prevent bean's property being serialized to XML
18.Create an instance a Bean
19.Convert an XML persistence to bean
20.Determine bean's property type
21.Listen for a constrained property change
22.Bean has a single property called property.
23.Implementing a Bound Property
24.Implementing a Constrained Property: fires a PropertyChangeEvent whenever its value is about to be changed.
25.Instantiating a Bean
26.Listing the Property Names of a Bean
27.Getting and Setting a Property of a Bean
28.Get and set the value of a property in a bean using Expression and Statement
29.Get and set an Object type property
30.gets and sets a primitive type property
31.gets and sets an array type property
32.Serializing a Bean to XML: XMLEncoder only persists the value of public properties.
33.Deserializing a Bean from XML
34.Preventing a Bean Property from Being Serialized to XML
35.Serializing an Immutable Bean Property to XML
36.Listening for a Property Change Event: A property change event is fired when a bound property is changed.
37.Listening for a Vetoable Property Change Event
38.Read bean's property value
39.An extension of ArrayList that provides some handy utilities for working with JavaBeans
40.An extension of Vector that provides some handy utilities for working with JavaBeans
41.extends SimpleBeanInfo
42.Get and set properties on a bean
43.Bean Utility
44.Is JavaBean Compliant Setter
45.This program demonstrates the use of an XML encoder and decoder to save and restore a frame.
46.This program demonstrates various persistence delegates.
47.Event Tracer