1 /*------------------------------------------------------------------------------
2 Name: StorableFigureHolder.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package javaclients.graphical;
8
9 import CH.ifa.draw.framework.Figure;
10 import CH.ifa.draw.util.Storable;
11 import CH.ifa.draw.util.StorableInput;
12 import CH.ifa.draw.util.StorableOutput;
13
14 import java.io.IOException;
15 import java.io.ByteArrayOutputStream;
16 import java.io.ByteArrayInputStream;
17
18 import java.util.logging.Logger;
19 import java.util.logging.Level;
20
21 /**
22 * This is a placeholder for the Figure when it is sent over xmlBlaster
23 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
24 */
25 public class StorableFigureHolder implements Storable {
26
27 private final static String ME = "MessageContent";
28 private Figure fig;
29 private String figId;
30 private String toFront;
31 private static Logger log = Logger.getLogger(StorableFigureHolder.class.getName());
32
33 /**
34 * We pass the used own defined attributes here since for LineFigure these are not
35 * serialized internally
36 * @param log
37 * @param fig
38 */
39 public StorableFigureHolder(Figure fig, String figId, String toFront) {
40 StorableFigureHolder.log = log;
41 this.fig = fig;
42 this.figId = figId;
43 this.toFront = toFront;
44 }
45
46 public StorableFigureHolder() {
47 this(null, null, null);
48
49 }
50
51 public void write(StorableOutput out) {
52 if (this.figId != null) out.writeString(this.figId);
53 else out.writeString("");
54 if (this.toFront != null) out.writeString(this.toFront);
55 else out.writeString("");
56 out.writeStorable(this.fig);
57 }
58
59 public void read(StorableInput in) throws IOException {
60 this.figId = in.readString();
61 this.toFront = in.readString();
62 this.fig = (Figure)in.readStorable();
63 if (this.figId.length() < 1) this.figId = null;
64 if (this.toFront.length() < 1) this.toFront = null;
65 }
66
67 public String getFigureId() {
68 return this.figId;
69 }
70
71 public String getToFront() {
72 return this.toFront;
73 }
74
75 public void setFigureId(String figId) {
76 this.figId = figId;
77 }
78
79 public void setToFront(String toFront) {
80 this.toFront = toFront;
81 }
82
83 public Figure getFigure() {
84 if (log.isLoggable(Level.FINE)) {
85 log.fine("getFigure: figureId='" + this.figId +
86 "' toFront='" + this.toFront +
87 "' toFront='" + this.fig.displayBox().x +
88 "' toFront='" + this.fig.displayBox().y +
89 "' toFront='" + this.fig.displayBox().width +
90 "' toFront='" + this.fig.displayBox().height + "'");
91 }
92 return this.fig;
93 }
94
95 public byte[] toBytes() throws IOException {
96 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
97 StorableOutput out = new StorableOutput(byteStream);
98 out.writeStorable(this);
99 out.close();
100 return byteStream.toByteArray();
101 }
102
103 public static StorableFigureHolder fromBytes(byte[] bytes) throws IOException {
104 ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
105 StorableInput in = new StorableInput(byteStream);
106 return (StorableFigureHolder)in.readStorable();
107 }
108
109
110 /*
111 private void recursiveErase(Figure fig) throws XmlBlasterException {
112 erase(fig);
113 FigureEnumeration iter = fig.figures();
114 while (iter.hasNextFigure()) {
115 Figure child = iter.nextFigure();
116 log.severe("recursiveErase " + child);
117 recursiveErase(child);
118 }
119 }
120 */
121
122 }
syntax highlighted by Code2HTML, v. 0.9.1