gluonhq / rich-text-area

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Open & Save options

RachidTagzen opened this issue · comments

Is their any possibility to have this 2 options :

  • Save the document (text & decoration) into an external file.
  • Open the external file into the document (text & decoration).

I tried to deserialize the DecorationModel & its subclasses, but I always got exceptions because they're not serializable.
So, I which if a method exists to facilitate the operation of saving & opening.

I have been able to serialize Document by making the foreground and background variables of TextDecoration.java transient, adding arrays foregroundColorValues[] and backgroundColorValues[] along with writeObject and readObject methods as follows,

 private void writeObject (ObjectOutputStream stream) throws IOException, ClassNotFoundException {
        foregroundColorValues = new double[]{foreground.getRed(), foreground.getGreen(), foreground.getBlue(), foreground.getOpacity()};
        backgroundColorValues = new double[]{background.getRed(), background.getGreen(), background.getBlue(), background.getOpacity()};
        stream.defaultWriteObject();
    }
    private void readObject (ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        foreground = new Color(foregroundColorValues[0], foregroundColorValues[1], foregroundColorValues[2], foregroundColorValues[3]);
        background = new Color(backgroundColorValues[0], backgroundColorValues[2], backgroundColorValues[3], backgroundColorValues[3]);
    }

Then you can open and save documents. Unfortunately, the rich text area is subject to crashes if you: (edit; save; open; edit again; save again; open again). I believe this is a (serious) bug in the RTA itself, independent of serialization (see my comment #292).

I have been able to serialize Document by making the foreground and background variables of TextDecoration.java transient, adding arrays foregroundColorValues[] and backgroundColorValues[] along with writeObject and readObject methods as follows,

 private void writeObject (ObjectOutputStream stream) throws IOException, ClassNotFoundException {
        foregroundColorValues = new double[]{foreground.getRed(), foreground.getGreen(), foreground.getBlue(), foreground.getOpacity()};
        backgroundColorValues = new double[]{background.getRed(), background.getGreen(), background.getBlue(), background.getOpacity()};
        stream.defaultWriteObject();
    }
    private void readObject (ObjectInputStream stream) throws IOException, ClassNotFoundException {
        stream.defaultReadObject();
        foreground = new Color(foregroundColorValues[0], foregroundColorValues[1], foregroundColorValues[2], foregroundColorValues[3]);
        background = new Color(backgroundColorValues[0], backgroundColorValues[2], backgroundColorValues[3], backgroundColorValues[3]);
    }

Then you can open and save documents. Unfortunately, the rich text area is subject to crashes if you: (edit; save; open; edit again; save again; open again). I believe this is a (serious) bug in the RTA itself, independent of serialization (see my comment #292).

Thanks.
I already tried somethings like your suggestion, and I got crashes.
I'm not a pro off Java, but why not set the classes implements Serializable?