序列化draw2d LayeredPane

时间:2012-10-05 13:14:20

标签: java serialization draw2d

我需要一些有关draw2d Layered Panes序列化的帮助。我读到了序列化,发现只有在实现Serializable接口时,它才能被序列化,并且它的所有字段本身都是可序列化的,或者是瞬态的。

我有一个非常复杂的图表,我需要序列化,并没有关于如何进行的线索?我发现LayeredPane类只包含一个List类型的字段。在任何情况下,任何人都可以帮助人们如何编写,比如递归方法或其他东西,以使LayeredPane对象可序列化吗?

@mKorbel 我面临的问题的示例场景很难给出,因为它是一个非常大的应用程序的一部分。不过,我已经编写了一个案例,可能会让您了解问题:

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
    }
}

1 个答案:

答案 0 :(得分:1)

您必须扩展LayeredPane类,通过实现该接口使其可序列化,并提供一种从模型重建该LayeredPane的整个结构和属性的方法。

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();
    }

}

因此,您必须添加一个Serializable模型,其中包含从头开始构建图树所需的所有信息。