4 package uk.ac.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;
18 import uk.ac.vamsas.client.UserHandle;
19 import uk.ac.vamsas.client.simpleclient.FileWatcher;
20 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
21 import uk.ac.vamsas.client.simpleclient.VamsasFile;
22 import uk.ac.vamsas.objects.core.Entry;
23 import uk.ac.vamsas.objects.core.VamsasDocument;
24 import uk.ac.vamsas.test.simpleclient.ArchiveClient;
25 import uk.ac.vamsas.test.simpleclient.ClientDoc;
31 public class VamsasClient extends ArchiveClient {
32 org.apache.commons.logging.Log log=org.apache.commons.logging.LogFactory.getLog(VamsasClient.class);
34 * create a new vamsas client session from the archive at sessionPath.
37 public VamsasClient(File sessionPath) {
38 super(System.getProperty("user.name"),System.getProperty("host.name"), "SimpleVamsasClientApp","0.1",
42 * Called by gui to read anything from the vamsas session into the apps datamodel
43 * after it has started up.
46 public void initial_update() {
47 log.info("Jalview loading the Vamsas Session.");
48 // load in the vamsas archive for the first time
49 ClientDoc cdoc = this.getUpdateable();
51 // 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.
52 //doUpdate(cdoc); // JBPNote: this should flush new VorbaIds but I've not tested it yet.
54 // then tell app to update its display based on the datamodel changes.
56 VamsasClientWatcher watcher=null;
58 * Called by app when internal datamodel should exported (syncrhonised outwards) to vamsas document
61 public void push_update() {
63 watchForChange=false; // this makes any watch(long) loops return.
64 // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped.
66 Thread.sleep(WATCH_SLEEP);
67 } catch (Exception e) {
71 ClientDoc cdoc = getUpdateable();
72 updateVamsasDocument(cdoc);
79 public void end_session() {
80 watchForChange=false; // this makes any watch(long) loops return.
81 // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped.
83 Thread.sleep(WATCH_SLEEP);
84 } catch (Exception e) {
88 // stop any update/watcher thread.
89 log.info("VamsasClientApplication disconnecting from the Vamsas Session.");
91 public void updateJalview(ClientDoc cdoc) {
92 ensureVamsasBindings();
93 VamsasDatastore vds = new VamsasDatastore(cdoc, vobj2jv, jv2vobj, baseProvEntry());
94 vds.updateToJalview();
96 private void ensureVamsasBindings() {
98 jv2vobj = new IdentityHashMap();
99 vobj2jv = new Hashtable();
103 * App's object binding to VorbaIds
105 IdentityHashMap jv2vobj = null;
106 Hashtable vobj2jv = null;
108 * called with a vamsas document which will be updated with new data from the app
111 public void updateVamsasDocument(ClientDoc doc) {
112 ensureVamsasBindings();
113 VamsasDatastore vds = new VamsasDatastore(doc, vobj2jv, jv2vobj, baseProvEntry());
114 // wander through frames
115 vds.storeVAMSAS(new Object()); // Object is the apps datamodel ;)
119 * @return a base provenance entry used by the VamsasDatastore object to get attributes from. this isn't particularly elegant either.
121 private Entry baseProvEntry() {
122 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
123 pentry.setUser(this.getProvenanceUser());
124 pentry.setApp(this.getClientHandle().getClientName());
125 pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
126 pentry.setAction("created");
129 protected class VamsasClientWatcher extends Thread implements Runnable {
131 * @see java.lang.Thread#run()
133 VamsasClient client=null;
134 VamsasClientWatcher(VamsasClient client) {
135 this.client = client;
137 boolean running=false;
140 while (client.watchForChange) {
141 ClientDoc docio = client.watch(0);
143 // VamsasClient GUI bits should be disabled whilst an update is in progress so the user doesn't screw anything up.
144 client.disableGui(true);
145 log.debug("Updating VamsasClient app from changed vamsas document.");
146 client.updateJalview(docio);
147 log.debug("Finished updating from document change.");
150 client.disableGui(false);
161 public static void main(String[] args) {
162 // TODO Auto-generated method stub
166 * 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.
169 public void disableGui(boolean b) {
170 // in jalview, we turn off the VAMSAS Session menu : Desktop.instance.setVamsasUpdate(b);
173 * spawn a new thread to start the VamsasClientWatcher.
176 public void startWatcher() {
178 watcher=new VamsasClientWatcher(this);
179 Thread thr = new Thread() {