Reputation: 63
I need some help with Serialization of draw2d Layered Panes. I read about Serialization, and found that a Class can be serialized only if it implements Serializable
Interface, and all its fields are either themselves Serializable, or transient.
I have a very Complex Diagram that I need to serialize, and don't have a clue as to how to proceed? I found out that the LayeredPane class only contains one field of type List. In any case, can any one help with how one can write, say a recursive method or something, to make a LayeredPane Object Serializable?
@mKorbel A Sample scenario of the Problem I am facing is difficult to give, as its part of a really large application. Still, I've made up a case, which may give you an idea of the Problem:
public class Editor extends org.eclipse.ui.part.EditorPart {
org.eclipse.draw2d.FreeformLayer objectsLayer;
org.eclipse.draw2d.ConnectionLayer connectionLayer;
public void createPartControl(Composite parent) {
org.eclipse.draw2d.FigureCanvas canvas = new org.eclipse.draw2d.FigureCanvas(composite);
org.eclipse.draw2d.LayeredPane pane = new org.eclipse.draw2d.LayeredPane();
objectsLayer = new org.eclipse.draw2d.FreeformLayer();
connectionLayer = org.eclipse.draw2d.ConnectionLayer();
pane.add(objectsLayer);
pane.add(connectionLayer);
canvas.setContents(pane);
addFigures();
addConnections();
}
private void addFigures() {
// Adds Objects, i.e., org.eclipse.draw2d.Figure Objects, to the objectLayer
// which turn contains, 1 or more org.eclipse.draw2d.Panel Objects,
// with variable number of org.eclipse.draw2d.Label objects
}
private void addConnections() {
// Adds org.eclipse.draw2d.PolylineConnection objects to the connectionLayer
// between objects in the objectLayer
}
}
Upvotes: 0
Views: 244
Reputation: 2610
You have to extend the LayeredPane class, make it Serializable by implementing that interface and provide a method that rebuild the whole structure and properties of that LayeredPane from a model.
public class SerializableLayeredPanne extends LayeredPanne implements Serializable {
private static final long serialVersionUID = 1L;
/**
* the model you are able to FULLY restore layered pane and all its children from,
* it MUST be serializable
*/
private final Serializable model;
SerializableLayeredPanne(Serializable model) {
this.model = model;
}
public void init() {
// set font, color etc.
// add children
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
init();
}
}
So you have to add a Serializable model that contains all informations you need to build the figure tree from scratch.
Upvotes: 1