jalview'esque vamsas session example for org.vamsas.test.simpleclient.ArchiveClient...
[vamsas.git] / src / org / vamsas / test / simpleclient / simpleapp / VamsasClient.java
1 /**
2  * 
3  */
4 package org.vamsas.test.simpleclient.simpleapp;
5
6 import java.io.File;
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;
14
15 import javax.swing.JInternalFrame;
16
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;
25
26 /**
27  * @author jimp
28  *
29  */
30 public class VamsasClient extends ArchiveClient {
31         org.apache.commons.logging.Log log=org.apache.commons.logging.LogFactory.getLog(VamsasClient.class);
32   /**
33    * create a new vamsas client session from the archive at sessionPath.
34    * @param sessionPath
35    */
36   public VamsasClient(File sessionPath) {
37                 super(System.getProperty("user.name"),System.getProperty("host.name"), "SimpleVamsasClientApp","0.1",
38                                 sessionPath);
39         }
40   /**
41    * Called by gui to read anything from the vamsas session into the apps datamodel
42    * after it has started up.
43    *
44    */
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();
49                 updateJalview(cdoc);
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.
52     cdoc.closeDoc();
53                 // then tell app to update its display based on the datamodel changes.
54         }
55   VamsasClientWatcher watcher=null;
56         /**
57    * Called by app when internal datamodel should exported (syncrhonised outwards) to vamsas document 
58    *
59          */
60   public void push_update() {
61
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. 
64     try {
65       Thread.sleep(WATCH_SLEEP); 
66     } catch (Exception e) {
67       
68     };
69     
70     ClientDoc cdoc = getUpdateable();
71     updateVamsasDocument(cdoc);
72     doUpdate(cdoc);
73     cdoc.closeDoc();
74     cdoc=null;
75     watchForChange=true;
76     startWatcher();
77         }
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. 
81     try {
82       Thread.sleep(WATCH_SLEEP); 
83     } catch (Exception e) {
84       
85     };
86     
87                 // stop any update/watcher thread.
88                 log.info("VamsasClientApplication disconnecting from the Vamsas Session.");
89         }
90         public void updateJalview(ClientDoc cdoc) {
91                 ensureVamsasBindings();
92     VamsasDatastore vds = new VamsasDatastore(cdoc, vobj2jv, jv2vobj, baseProvEntry());
93     vds.updateToJalview();
94         }
95         private void ensureVamsasBindings() {
96     if (jv2vobj==null) {
97       jv2vobj = new IdentityHashMap();
98       vobj2jv = new Hashtable();
99     }
100         }
101         /**
102          * App's object binding to VorbaIds
103          */
104         IdentityHashMap jv2vobj = null;
105         Hashtable vobj2jv = null;
106         /**
107    * called with a vamsas document which will be updated with new data from the app
108    * @param doc
109          */
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 ;)
115         }
116   /**
117    * 
118    * @return a base provenance entry used by the VamsasDatastore object to get attributes from. this isn't particularly elegant either.
119    */
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");
126     return pentry;
127         }
128   protected class VamsasClientWatcher extends Thread implements Runnable {
129                 /* (non-Javadoc)
130                  * @see java.lang.Thread#run()
131                  */
132                 VamsasClient client=null;
133                 VamsasClientWatcher(VamsasClient client) {
134                         this.client = client;
135                 }
136     boolean running=false;
137                 public void run() {
138                   running=true;
139                         while (client.watchForChange) {
140                           ClientDoc docio = client.watch(0);
141                           if (docio!=null) {
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.");
147           docio.closeDoc();
148           docio=null;
149           client.disableGui(false);
150                           }
151       }
152       running=false;
153       
154                 }
155                 
156         }
157         /**
158          * @param args
159          */
160         public static void main(String[] args) {
161                 // TODO Auto-generated method stub
162
163         }
164   /**
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.
166    * @param b 
167    */
168   public void disableGui(boolean b) {
169       // in jalview, we turn off the VAMSAS Session menu : Desktop.instance.setVamsasUpdate(b);    
170   }
171   /**
172    * spawn a new thread to start the VamsasClientWatcher.
173    *
174    */
175   public void startWatcher() {
176     if (watcher==null)
177       watcher=new VamsasClientWatcher(this);
178     Thread thr = new Thread() {
179       public void run() {
180         watcher.start();
181       }
182     };
183     thr.start();
184   }
185
186 }