merge from 2_4_Release branch
[jalview.git] / src / jalview / io / vamsas / LocalDocSyncObject.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3  * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18  */
19 package jalview.io.vamsas;
20
21 import uk.ac.vamsas.client.Vobject;
22
23 /**
24  * Implement the basic logic for synchronising changes to or from the Vamsas
25  * Document
26  * 
27  * @author JimP
28  */
29 public abstract class LocalDocSyncObject extends DatastoreItem
30 {
31   /**
32    * 
33    * @return null or the local object that is being worked on.
34    */
35   public abstract Object getLObject();
36
37   /**
38    * 
39    * @return null or the document object that is being worked on
40    */
41   public abstract Vobject getVObject();
42
43   /**
44    * endpoint for synchronize when all opreations are finished.
45    */
46   public abstract void nextObject();
47
48   /**
49    * called if the local object can be safely updated from the bound document
50    * object.
51    */
52   public abstract void updateFromDoc();
53
54   /**
55    * called if the associated document object can be safely updated with the
56    * local changes
57    */
58   public abstract void updateToDoc();
59
60   /**
61    * @return true if the local object is modified
62    */
63   public abstract boolean locallyModified();
64
65   /**
66    * 
67    * @return true if the bound document object is modified
68    */
69   public abstract boolean documentModified();
70
71   /**
72    * 
73    * @return true if the document object is locked w.r.t. this object's update.
74    */
75   public abstract boolean documentObjectLocked();
76
77   /**
78    * 
79    * @return a new datastore item instance which binds the local object to a new
80    *         document object
81    */
82   public abstract LocalDocSyncObject newDocumentObject(); // could make this
83                                                           // constructor(Lobject)
84
85   /**
86    * 
87    * @return a new datastore item instance which binds the document object to a
88    *         new local object.
89    */
90   public abstract LocalDocSyncObject newLocalObject(); // make this
91                                                         // constructor(Vobject)
92
93   /**
94    * apply the update/commit logic as defined in the vamsas paper
95    * 
96    * @param documentIsUpdated
97    *                true if a document update event is being handled
98    */
99   public void synchronize(boolean documentIsUpdated)
100   {
101     Object Lobject = getLObject();
102     Vobject Vobject = getVObject();
103     if (Lobject == null)
104     {
105       // no local binding for document object
106       newLocalObject().synchronize(documentIsUpdated);
107       return;
108     }
109     if (Vobject == null)
110     {
111       // no document binding for local object
112       newDocumentObject().synchronize(documentIsUpdated);
113     }
114     // Check binding is valid
115     if (getjv2vObj(Lobject) != Vobject)
116     {
117       // no local binding for document object
118       newLocalObject().synchronize(documentIsUpdated);
119       // no document binding for local object
120       newDocumentObject().synchronize(documentIsUpdated);
121     }
122   }
123 }