0

我需要一些有关 draw2d 分层窗格序列化的帮助。我阅读了有关序列化的内容,发现只有实现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
    }
}
4

1 回答 1

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

}

因此,您必须添加一个包含从头开始构建图形树所需的所有信息的可序列化模型。

于 2012-12-11T13:30:31.287 回答