+ /**
+ * construct and initialise datastore object and retrieve object bound to vobj2 and validate it against boundType
+ * @param datastore2
+ * @param vobj2
+ * @param boundType
+ */
+ public DatastoreItem(VamsasAppDatastore datastore2, Vobject vobj2, Class boundType)
+ {
+ this(datastore2);
+ vobj = vobj2;
+ jvobj = getvObj2jv(vobj2);
+ tojalview=true;
+ if (jvobj!=null && !(boundType.isAssignableFrom(jvobj.getClass())))
+ {
+ throw new Error("Implementation Error: Vamsas Document Class "+vobj.getClass()+" should bind to a "+boundType+" (found a "+jvobj.getClass()+")");
+ }
+ dsReg.registerDsObj(this);
+ }
+ /**
+ * construct and initialise datastore object and retrieve document object bound to Jalview object jvobj2 and validate it against boundType
+ * @param datastore2 the datastore
+ * @param jvobj2 the jalview object
+ * @param boundToType - the document object class that the bound object should be assignable from
+ */
+ public DatastoreItem(VamsasAppDatastore datastore2, Object jvobj2, Class boundToType)
+ {
+ this(datastore2);
+ jvobj = jvobj2;
+ tojalview=false;
+ vobj = getjv2vObj(jvobj);
+ if (vobj!=null && !(boundToType.isAssignableFrom(vobj.getClass())))
+ {
+ throw new Error("Implementation Error: Jalview Class "+jvobj2.getClass()+" should bind to a "+boundToType+" (found a "+vobj.getClass()+")");
+ }
+ dsReg.registerDsObj(this);
+ }
+ /**
+ * create a new vobj to be added to the document
+ * for the jalview object jvobj
+ * (jvobj!=null, vobj==null)
+ */
+ public abstract void addToDocument();
+ /**
+ * handle a conflict where both an existing vobj has been updated
+ * and a local jalview object has been updated. This method
+ * is only called from doSync, when an incoming update from the vamsas
+ * session conflicts with local modifications made by the Jalview user.
+ * (jvobj!=null, vobj!=null)
+ */
+ public abstract void conflict();
+ /**
+ * update an existing vobj in the document with the data and settings from jvobj
+ * (jvobj!=null, vobj!=null)
+ */
+ public abstract void updateToDoc();
+ /**
+ * update the local jalview object with the data from an existing vobj in the document
+ * (jvobj!=null, vobj!=null)
+ */
+ public abstract void updateFromDoc();
+ /**
+ * create a new local jvobj bound to the vobj in the document.
+ * (jvobj==null, vobj!=null)
+ */
+ public abstract void addFromDocument();
+ boolean addtodoc=false, conflicted=false,updated=false,addfromdoc=false,success=false;
+
+ private boolean updatedtodoc;
+
+ private boolean updatedfromdoc;
+ /**
+ * Sync jalview to document. Enact addToDocument, conflict or update dependent on
+ * existence of a vobj bound to the local jvobj.
+ */
+ protected void doSync()
+ {
+ dsReg.registerDsObj(this);
+ if (vobj == null)
+ {
+ log.debug("adding new vobject to document.");
+ addtodoc=true;
+ addToDocument();
+ }
+ else
+ {
+ if (vobj.isUpdated())
+ {
+ log.debug("Handling update conflict for existing bound vobject.");
+ conflicted=true;
+ conflict();
+ }
+ else
+ {
+ log.debug("updating existing vobject in document.");
+ updatedtodoc=true;
+ updateToDoc();
+ }
+ }
+ // no exceptions were encountered...
+ success=true;
+ }
+ /**
+ * Update jalview from document. enact addFromDocument if no local jvobj exists, or update iff jvobj
+ * exists and the vobj.isUpdated() flag is set.
+ */
+ protected void doJvUpdate()
+ {
+ dsReg.registerDsObj(this);
+ if (jvobj == null)
+ {
+ log.debug("adding new vobject to Jalview from Document");
+ addfromdoc=true;
+ addFromDocument();
+ }
+ else
+ {
+ if (vobj.isUpdated())
+ {
+ log.debug("updating Jalview from existing bound vObject");
+ updatedfromdoc=true;
+ updateFromDoc();
+ }
+ }
+ }
+