Back to index

moin  1.9.0~rc2
FigureAttributes.java
Go to the documentation of this file.
00001 /*
00002  * @(#)FigureAttributes.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.standard;
00007 
00008 import java.util.*;
00009 import java.awt.Color;
00010 import java.io.IOException;
00011 import java.io.Serializable;
00012 
00013 import CH.ifa.draw.util.*;
00014 import CH.ifa.draw.framework.*;
00015 
00023 public  class   FigureAttributes
00024         extends Object
00025         implements Cloneable, Serializable {
00026 
00027     private Hashtable fMap;
00028 
00029     /*
00030      * Serialization support.
00031      */
00032     private static final long serialVersionUID = -6886355144423666716L;
00033     private int figureAttributesSerializedDataVersion = 1;
00034 
00038     public FigureAttributes() {
00039         fMap = new Hashtable();
00040     }
00041 
00046     public Object get(String name) {
00047         return fMap.get(name);
00048     }
00049 
00054     public void set(String name, Object value) {
00055         fMap.put(name, value);
00056     }
00057 
00061     public boolean hasDefined(String name) {
00062         return fMap.containsKey(name);
00063     }
00064 
00068    public Object clone() {
00069         try {
00070             FigureAttributes a = (FigureAttributes) super.clone();
00071             a.fMap = (Hashtable) fMap.clone();
00072             return a;
00073         } catch (CloneNotSupportedException e) {
00074                throw new InternalError();
00075         }
00076     }
00077 
00087     public void read(StorableInput dr) throws IOException {
00088         String s = dr.readString();
00089         if (!s.toLowerCase().equals("attributes"))
00090             throw new IOException("Attributes expected");
00091 
00092         fMap = new Hashtable();
00093         int size = dr.readInt();
00094         for (int i=0; i<size; i++) {
00095             String key = dr.readString();
00096             String valtype = dr.readString();
00097             Object val = null;
00098             if (valtype.equals("Color"))
00099                 val = new Color(dr.readInt(), dr.readInt(), dr.readInt());
00100             else if (valtype.equals("Boolean"))
00101                 val = new Boolean(dr.readString());
00102             else if (valtype.equals("String"))
00103                 val = dr.readString();
00104             else if (valtype.equals("Int"))
00105                 val = new Integer(dr.readInt());
00106             else if (valtype.equals("Storable"))
00107                 val = dr.readStorable();
00108             else if (valtype.equals("UNKNOWN"))
00109                 continue;
00110 
00111             fMap.put(key,val);
00112         }
00113     }
00114 
00124     public void write(StorableOutput dw) {
00125         dw.writeString("attributes");
00126 
00127         dw.writeInt(fMap.size());   // number of attributes
00128         Enumeration k = fMap.keys();
00129         while (k.hasMoreElements()) {
00130             String s = (String) k.nextElement();
00131             dw.writeString(s);
00132             Object v = fMap.get(s);
00133             if (v instanceof String) {
00134                 dw.writeString("String");
00135                 dw.writeString((String) v);
00136             } else if (v instanceof Color) {
00137                 dw.writeString("Color");
00138                 dw.writeInt(((Color)v).getRed());
00139                 dw.writeInt(((Color)v).getGreen());
00140                 dw.writeInt(((Color)v).getBlue());
00141             } else if (v instanceof Boolean) {
00142                 dw.writeString("Boolean");
00143                 if (((Boolean)v).booleanValue())
00144                     dw.writeString("TRUE");
00145                 else
00146                     dw.writeString("FALSE");
00147             } else if (v instanceof Integer) {
00148                 dw.writeString("Int");
00149                 dw.writeInt(((Integer)v).intValue());
00150             } else if (v instanceof Storable) {
00151                 dw.writeString("Storable");
00152                 dw.writeStorable((Storable)v);
00153             } else {
00154                 System.out.println(v);
00155                 dw.writeString("UNKNOWN");
00156             }
00157         }
00158     }
00159 }
00160