4 package org.vamsas.test.simpleclient.simpleapp;
7 import java.io.FileOutputStream;
8 import java.io.OutputStreamWriter;
9 import java.io.PrintWriter;
10 import java.util.Hashtable;
11 import java.util.IdentityHashMap;
12 import java.util.Vector;
13 import java.util.jar.JarOutputStream;
15 import javax.swing.JInternalFrame;
17 import org.vamsas.client.UserHandle;
18 import org.vamsas.client.simpleclient.FileWatcher;
19 import org.vamsas.client.simpleclient.VamsasArchive;
20 import org.vamsas.client.simpleclient.VamsasFile;
21 import org.vamsas.objects.core.Entry;
22 import org.vamsas.objects.core.VamsasDocument;
23 import org.vamsas.test.simpleclient.ArchiveClient;
24 import org.vamsas.test.simpleclient.ClientDoc;
30 public class VamsasClient extends ArchiveClient {
31 org.apache.commons.logging.Log log=org.apache.commons.logging.LogFactory.getLog(VamsasClient.class);
33 * create a new vamsas client session from the archive at sessionPath.
36 public VamsasClient(File sessionPath) {
37 super(System.getProperty("user.name"),System.getProperty("host.name"), "SimpleVamsasClientApp","0.1",
41 * Called by gui to read anything from the vamsas session into the apps datamodel
42 * after it has started up.
45 public void initial_update() {
46 log.info("Jalview loading the Vamsas Session.");
47 // load in the vamsas archive for the first time
48 ClientDoc cdoc = this.getUpdateable();
50 // TODO: flush any new VorbaIds to the document : updateVamsasClient may actually generate new Vamsas Document data in the form of vamsas element ids - these should be written back to the document.
51 //doUpdate(cdoc); // JBPNote: this should flush new VorbaIds but I've not tested it yet.
53 // then tell app to update its display based on the datamodel changes.
55 VamsasClientWatcher watcher=null;
57 * Called by app when internal datamodel should exported (syncrhonised outwards) to vamsas document
60 public void push_update() {
62 watchForChange=false; // this makes any watch(long) loops return.
63 // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped.
65 Thread.sleep(WATCH_SLEEP);
66 } catch (Exception e) {
70 ClientDoc cdoc = getUpdateable();
71 updateVamsasDocument(cdoc);
78 public void end_session() {
79 watchForChange=false; // this makes any watch(long) loops return.
80 // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped.
82 Thread.sleep(WATCH_SLEEP);
83 } catch (Exception e) {
87 // stop any update/watcher thread.
88 log.info("VamsasClientApplication disconnecting from the Vamsas Session.");
90 public void updateJalview(ClientDoc cdoc) {
91 ensureVamsasBindings();
92 VamsasDatastore vds = new VamsasDatastore(cdoc, vobj2jv, jv2vobj, baseProvEntry());
93 vds.updateToJalview();
95 private void ensureVamsasBindings() {
97 jv2vobj = new IdentityHashMap();
98 vobj2jv = new Hashtable();
102 * App's object binding to VorbaIds
104 IdentityHashMap jv2vobj = null;
105 Hashtable vobj2jv = null;
107 * called with a vamsas document which will be updated with new data from the app
110 public void updateVamsasDocument(ClientDoc doc) {
111 ensureVamsasBindings();
112 VamsasDatastore vds = new VamsasDatastore(doc, vobj2jv, jv2vobj, baseProvEntry());
113 // wander through frames
114 vds.storeVAMSAS(new Object()); // Object is the apps datamodel ;)
118 * @return a base provenance entry used by the VamsasDatastore object to get attributes from. this isn't particularly elegant either.
120 private Entry baseProvEntry() {
121 org.vamsas.objects.core.Entry pentry = new org.vamsas.objects.core.Entry();
122 pentry.setUser(this.getProvenanceUser());
123 pentry.setApp(this.getClientHandle().getClientName());
124 pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
125 pentry.setAction("created");
128 protected class VamsasClientWatcher extends Thread implements Runnable {
130 * @see java.lang.Thread#run()
132 VamsasClient client=null;
133 VamsasClientWatcher(VamsasClient client) {
134 this.client = client;
136 boolean running=false;
139 while (client.watchForChange) {
140 ClientDoc docio = client.watch(0);
142 // VamsasClient GUI bits should be disabled whilst an update is in progress so the user doesn't screw anything up.
143 client.disableGui(true);
144 log.debug("Updating VamsasClient app from changed vamsas document.");
145 client.updateJalview(docio);
146 log.debug("Finished updating from document change.");
149 client.disableGui(false);
160 public static void main(String[] args) {
161 // TODO Auto-generated method stub
165 * disable (if b is true) or enable (if b is true) the VamsasClient's vamsas session gui bits whilst a document change is being updated to the app.
168 public void disableGui(boolean b) {
169 // in jalview, we turn off the VAMSAS Session menu : Desktop.instance.setVamsasUpdate(b);
172 * spawn a new thread to start the VamsasClientWatcher.
175 public void startWatcher() {
177 watcher=new VamsasClientWatcher(this);
178 Thread thr = new Thread() {