Re: Copying a StyledDocument deeply

From: Thomas Weidenfeller (nobody_at_ericsson.invalid)
Date: 04/20/04


Date: Tue, 20 Apr 2004 17:47:58 +0200

VI wrote:
> I
> immediately make a copy of this (to another instance oldDoc) in the
> constructor of the class (oldDoc = doc)

If "oldDoc = doc" is really your code (why didn't you show us your real
code?), then you are not copying the object, you are just duplicating
the reference. If this is the case, then of course comparing the objects
behind the references must result in them being equal, because both
references point to the very same object.

You are talking about StyledDocument, but that is an interface only.
Which class are you really using? But anyhow, AFAIR
DefaultStyledDocument and HTMLDocument don't support any convenient way
of deep-cloning (copying) them. The simplest but slightly inefficient
way might be to serialize/deserialize them to get a clone.

A better idea might be to register a document listener in order to be
informed about chnges.

In case you use HTMLDocument, you might want to examine the source code
and figure out the behavior of the c'tor
HTMLDocument(AbstractDocument.Content c, StyleSheet styles): Does it
clone arguments? Or do the getContent() and getStyleSheet() methods
return clones?

If they clone instead of just passing on references, a construct like

        HTMLDocument backup =
           new HTMLDocument(doc.getContent(), doc.getStyleSheet());

might give you what you want. I leave this as an excercise to you. The
source comes with every SDK in the scr.zip file.

/Thomas



Relevant Pages

  • Re: super.clone() puzzlement
    ... this means copying any mutable objects that comprise the ... internal "deep structure" of the object being cloned and replacing the ... references to these objects with references to the copies. ... known as shallow clone. ...
    (comp.lang.java.programmer)
  • Re: Poincare and Einstein on Relativity
    ... "copying of examples"? ... useful references. ... I don't see anything surprising except that Whittaker was pretty off. ... English translation of a key paper by Poincare. ...
    (sci.physics.relativity)
  • Re: Poincare and Einstein on Relativity
    ... "copying of examples"? ... that a certain Leveugle is mentioned as an author about that conspiracy ... useful references. ... As a nice surprise, completely at the end it contains an English ...
    (sci.physics.relativity)
  • Re: ArrayList.addAll syntax
    ... Thomas Hawtin wrote: ... I have tried looping through, copying, ... == on references, such as Strings, tests whether the same object is ... new Strings ...
    (comp.lang.java.help)
  • Re: super.clone() puzzlement
    ... this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. ... known as shallow clone. ...
    (comp.lang.java.programmer)