001: package com.jeta.forms.store.memento;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.Externalizable;
006: import java.io.IOException;
007: import java.io.ObjectInputStream;
008: import java.io.ObjectOutputStream;
009:
010: /**
011: * This class is mainly needed when copying a form to the clipboard. The Swing
012: * copy/paste implementation serializes the data during the paste operation
013: * rather than the copy. If the user switches editors, this can cause problems
014: * with the form containment hierarhcy and it won't correctly save its state.
015: * So, we use this class to force the form to save to a byte array when the copy
016: * command is invoked. Since the form must have focus at that point, the state
017: * is correctly saved.
018: *
019: * @author Jeff Tassin
020: */
021: public class ComponentMementoProxy implements Externalizable {
022: public static final int VERSION = 1;
023:
024: static final long serialVersionUID = -6311643735573896684L;
025:
026: /**
027: * We must store the component memento as a byte array. If we stored as a
028: * ComponentMemento, the paste operation would attempt to re-serialize the
029: * memento. This causes problems if pasting from one form editor to a
030: * different form editor since the copied forms don't have valid gridview
031: * parents once they are deactivated.
032: */
033: private byte[] m_component_memento;
034:
035: /**
036: * Cached value of ComponentMemento once we deserialize from the byte array.
037: */
038: private ComponentMemento m_cm;
039:
040: /**
041: * Default ctor
042: */
043: public ComponentMementoProxy() {
044:
045: }
046:
047: public ComponentMementoProxy(ComponentMemento cm) {
048: setComponentMemento(cm);
049: }
050:
051: public ComponentMemento getComponentMemento() {
052: try {
053: if (m_cm == null) {
054: ByteArrayInputStream bis = new ByteArrayInputStream(
055: m_component_memento);
056: ObjectInputStream ois = new ObjectInputStream(bis);
057: m_cm = (ComponentMemento) ois.readObject();
058: System.out
059: .println("componentransferable getCOmponentMemento... ");
060: }
061: } catch (Exception e) {
062: e.printStackTrace();
063: }
064: return m_cm;
065: }
066:
067: void setComponentMemento(ComponentMemento cm) {
068: try {
069: m_cm = null;
070: ByteArrayOutputStream bos = new ByteArrayOutputStream();
071: ObjectOutputStream oos = new ObjectOutputStream(bos);
072: oos.writeObject(cm);
073: oos.close();
074: m_component_memento = bos.toByteArray();
075: } catch (Exception e) {
076: e.printStackTrace();
077: m_cm = cm;
078: }
079: }
080:
081: /**
082: * Externalizable Implementation
083: */
084: public void readExternal(java.io.ObjectInput in)
085: throws ClassNotFoundException, IOException {
086: int version = in.readInt();
087: m_component_memento = (byte[]) in.readObject();
088: m_cm = (ComponentMemento) in.readObject();
089: }
090:
091: /**
092: * Externalizable Implementation
093: */
094: public void writeExternal(java.io.ObjectOutput out)
095: throws IOException {
096: out.writeInt(VERSION);
097: out.writeObject(m_component_memento);
098: out.writeObject(m_cm);
099: }
100:
101: }
|