]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/page/SerializablePageDesc.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / page / SerializablePageDesc.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.page;
13
14 import java.io.IOException;
15 import java.io.Serializable;
16 import java.io.StreamCorruptedException;
17
18 import org.simantics.utils.page.PageCentering;
19 import org.simantics.utils.page.PageDesc;
20 import org.simantics.utils.page.PageOrientation;
21
22 /**
23  * A serializable wrapper for {@link PageDesc}.
24  */
25 public class SerializablePageDesc implements Serializable {
26
27     private static final long               serialVersionUID = -1992775876802871858L;
28
29     private transient PageDesc desc;
30
31     public SerializablePageDesc(PageDesc desc) {
32         this.desc = desc;
33     }
34
35     public PageDesc getDesc() {
36         return desc;
37     }
38
39     @Override
40     public String toString() {
41         return String.format("%s [%s]", getClass().getSimpleName(), desc.toString());
42     }
43
44     private void writeObject(java.io.ObjectOutputStream out) throws IOException {
45         out.defaultWriteObject();
46         out.writeUTF(desc.getText());
47         out.writeDouble(desc.getWidth());
48         out.writeDouble(desc.getHeight());
49         out.writeByte(desc.getOrientation().ordinal());
50         out.writeByte(desc.getCentering().ordinal());
51     }
52
53     private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
54         in.defaultReadObject();
55         String text = in.readUTF();
56         double widthInMM = in.readDouble();
57         double heightInMM = in.readDouble();
58         byte o = in.readByte();
59         byte c = in.readByte();
60         PageOrientation[] orientations = PageOrientation.values();
61         if (o < 0 || o >= orientations.length)
62             throw new StreamCorruptedException("invalid orientation: " + o);
63         PageOrientation orientation = orientations[o];
64         PageCentering[] centerings = PageCentering.values();
65         if (c < 0 || c >= centerings.length)
66             throw new StreamCorruptedException("invalid centering: " + c);
67         PageCentering centering = centerings[c];
68
69         desc = new PageDesc(text, orientation, centering, widthInMM, heightInMM);
70     }
71
72 }