Example #1
0
  /**
   * Reads the attributes from a StorableInput. FigureAttributes store the following types directly:
   * Color, Boolean, String, Int. Other attribute types have to implement the Storable interface or
   * they have to be wrapped by an object that implements Storable.
   *
   * @see Storable
   * @see #write
   */
  public void read(StorableInput dr) throws IOException {
    String s = dr.readString();
    if (!s.toLowerCase().equals("attributes")) {
      throw new IOException("Attributes expected");
    }

    fMap = new HashMap<FigureAttributeConstant, Object>();
    int size = dr.readInt();
    for (int i = 0; i < size; i++) {
      String key = dr.readString();
      String valtype = dr.readString();
      Object val = null;
      if (valtype.equals("Color")) {
        val = new Color(dr.readInt(), dr.readInt(), dr.readInt());
      } else if (valtype.equals("Boolean")) {
        val = new Boolean(dr.readString());
      } else if (valtype.equals("String")) {
        val = dr.readString();
      } else if (valtype.equals("Int")) {
        val = new Integer(dr.readInt());
      } else if (valtype.equals("Storable")) {
        val = dr.readStorable();
      } else if (valtype.equals(Figure.POPUP_MENU)) {
        // read String but don't store it
        continue;
      } else if (valtype.equals("UNKNOWN")) {
        continue;
      }
      // try to get defined constant
      FigureAttributeConstant attributeConstant = FigureAttributeConstant.getConstant(key);
      set(attributeConstant, val);
    }
  }
Example #2
0
  /**
   * Writes the attributes to a StorableInput. FigureAttributes store the following types directly:
   * Color, Boolean, String, Int. Other attribute types have to implement the Storable interface or
   * they have to be wrapped by an object that implements Storable.
   *
   * @see Storable
   * @see #write
   */
  public void write(StorableOutput dw) {
    dw.writeString("attributes");

    dw.writeInt(fMap.size()); // number of attributes
    Iterator<FigureAttributeConstant> iter = fMap.keySet().iterator();
    while (iter.hasNext()) {
      FigureAttributeConstant fac = iter.next();
      String attributeName = fac.getName();
      Object attributeValue = fMap.get(fac);

      dw.writeString(attributeName);

      if (attributeValue instanceof String) {
        dw.writeString("String");
        dw.writeString((String) attributeValue);
      } else if (attributeValue instanceof Color) {
        writeColor(dw, "Color", (Color) attributeValue);
      } else if (attributeValue instanceof Boolean) {
        dw.writeString("Boolean");
        if (((Boolean) attributeValue).booleanValue()) {
          dw.writeString("TRUE");
        } else {
          dw.writeString("FALSE");
        }
      } else if (attributeValue instanceof Integer) {
        dw.writeString("Int");
        dw.writeInt(((Integer) attributeValue).intValue());
      } else if (attributeValue instanceof Storable) {
        dw.writeString("Storable");
        dw.writeStorable((Storable) attributeValue);
      } else if (attributeValue instanceof javax.swing.JPopupMenu) {
        dw.writeString(Figure.POPUP_MENU);
      } else {
        System.err.println("Unknown attribute: " + attributeValue);
        dw.writeString("UNKNOWN");
      }
    }
  }