Back to index

moin  1.9.0~rc2
FigureSelection.java
Go to the documentation of this file.
00001 /*
00002  * @(#)FigureSelection.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.framework;
00007 
00008 import CH.ifa.draw.util.*;
00009 import java.util.*;
00010 import java.io.*;
00011 
00020 public class FigureSelection extends Object {
00021 
00022     private byte[] fData; // flattend figures, ready to be resurrected
00026     public final static String TYPE = "CH.ifa.draw.Figures";
00027 
00031     public FigureSelection(Vector figures) {
00032         // a FigureSelection is represented as a flattened ByteStream
00033         // of figures.
00034         ByteArrayOutputStream output = new ByteArrayOutputStream(200);
00035         StorableOutput writer = new StorableOutput(output);
00036         writer.writeInt(figures.size());
00037         Enumeration selected = figures.elements();
00038         while (selected.hasMoreElements()) {
00039             Figure figure = (Figure) selected.nextElement();
00040             writer.writeStorable(figure);
00041         }
00042         writer.close();
00043         fData = output.toByteArray();
00044     }
00045 
00049     public String getType() {
00050         return TYPE;
00051     }
00052 
00059     public Object getData(String type) {
00060         if (type.equals(TYPE)) {
00061             InputStream input = new ByteArrayInputStream(fData);
00062             Vector result = new Vector(10);
00063             StorableInput reader = new StorableInput(input);
00064             int numRead = 0;
00065             try {
00066                 int count = reader.readInt();
00067                 while (numRead < count) {
00068                     Figure newFigure = (Figure) reader.readStorable();
00069                     result.addElement(newFigure);
00070                     numRead++;
00071                 }
00072             } catch (IOException e) {
00073                 System.out.println(e.toString());
00074             }
00075             return result;
00076         }
00077         return null;
00078     }
00079 }
00080