bfc2123f2697d9deeb4afb51bba9daf98f4ebfae
[jalview.git] / src / jalview / io / vamsas / LocalDocSyncObject.java
1 package jalview.io.vamsas;
2
3 import uk.ac.vamsas.client.Vobject;
4
5 /**
6  * Implement the basic logic for synchronising changes to or from the Vamsas Document
7  * @author JimP
8  */
9 public abstract class LocalDocSyncObject extends DatastoreItem
10 {
11   /**
12    * 
13    * @return null or the local object that is being worked on. 
14    */
15   public abstract Object getLObject();
16   /**
17    * 
18    * @return null or the document object that is being worked on
19    */
20   public abstract Vobject getVObject();
21   
22   /**
23    * endpoint for synchronize when all opreations are finished.
24    */
25   public abstract void nextObject();
26   /**
27    * called if the local object can be safely updated from the bound document object.
28    */
29   public abstract void updateFromDoc();
30   /**
31    * called if the associated document object can be safely updated with the local changes
32    */
33   public abstract void updateToDoc();
34   /**
35    * @return true if the local object is modified
36    */
37   public abstract boolean locallyModified();
38   /**
39    * 
40    * @return true if the bound document object is modified
41    */
42   public abstract boolean documentModified();
43   /**
44    * 
45    * @return true if the document object is locked  w.r.t. this object's update.
46    */
47   public abstract boolean documentObjectLocked();
48   /**
49    * 
50    * @return a new datastore item instance which binds the local object to a new document object 
51    */
52   public abstract LocalDocSyncObject newDocumentObject(); // could make this constructor(Lobject)
53   /**
54    * 
55    * @return a new datastore item instance which binds the document object to a new local object. 
56    */
57   public abstract LocalDocSyncObject newLocalObject(); // make this constructor(Vobject)
58   /**
59    * apply the update/commit logic as defined in the vamsas paper
60    * @param documentIsUpdated true if a document update event is being handled  
61    */
62   public void synchronize(boolean documentIsUpdated) {
63     Object Lobject = getLObject();
64     Vobject Vobject = getVObject();
65     if (Lobject==null)
66     {
67       // no local binding for document object
68       newLocalObject().synchronize(documentIsUpdated);
69       return;
70     }
71     if (Vobject==null)
72     {
73       // no document binding for local object
74       newDocumentObject().synchronize(documentIsUpdated);
75     }
76     // Check binding is valid
77     if (getjv2vObj(Lobject)!=Vobject)
78     {
79       // no local binding for document object
80       newLocalObject().synchronize(documentIsUpdated);
81       // no document binding for local object
82       newDocumentObject().synchronize(documentIsUpdated);
83     }
84   }
85 }