imported Vamsas bindings and alpha vamsas client code from VamJalview cvs branch...
[jalview.git] / src / jalview / gui / VamsasClient.java
1 /**
2  * 
3  */
4 package jalview.gui;
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 jalview.bin.Cache;
18 import jalview.io.VamsasDatastore;
19
20 import org.vamsas.client.UserHandle;
21 import org.vamsas.client.simpleclient.FileWatcher;
22 import org.vamsas.client.simpleclient.VamsasArchive;
23 import org.vamsas.client.simpleclient.VamsasFile;
24 import org.vamsas.objects.core.Entry;
25 import org.vamsas.objects.core.VamsasDocument;
26 import org.vamsas.test.simpleclient.ArchiveClient;
27 import org.vamsas.test.simpleclient.ClientDoc;
28
29 /**
30  * @author jimp
31  *
32  */
33 public class VamsasClient extends ArchiveClient {
34         // Cache.preferences for vamsas client session arena
35         // preferences for check for default session at startup.
36         // user and organisation stuff.
37         public VamsasClient(Desktop jdesktop,
38                         File sessionPath) {
39                 super(System.getProperty("user.name"),System.getProperty("host.name"), "jalview","2.7",
40                                 sessionPath);
41         }
42         public void initial_update() {
43                 Cache.log.info("Jalview loading the Vamsas Session.");
44                 // load in the vamsas archive for the first time
45                 ClientDoc cdoc = this.getUpdateable();
46                 updateJalview(cdoc);
47     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
48
49     if (frames == null)
50     {
51         return;
52     }
53
54     try
55     { 
56       //REVERSE ORDER
57         for (int i = frames.length - 1; i > -1; i--)
58         {
59             if (frames[i] instanceof AlignFrame)
60             {
61               AlignFrame af = (AlignFrame) frames[i];
62               af.alignmentChanged();
63             }
64         }
65     } catch (Exception e) {
66       Cache.log.warn("Exception whilst refreshing jalview windows after a vamsas document update.", e);
67     }
68     doUpdate(cdoc);
69     cdoc.closeDoc();
70   }
71         /**
72          * this will close all windows currently in Jalview.
73          *
74          
75         protected void closeWindows() {
76                 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
77
78         if (frames == null)
79         {
80             return;
81         }
82
83         try
84         {       
85             for (int i = frames.length - 1; i > -1; i--) {
86                 frames[i].dispose();    
87             }
88         } catch (Exception e) {
89                 Cache.log.error("Whilst closing windows",e);
90         }
91                 
92         }
93   
94         public void get_update(VamsasArchive doc) {
95                 // Close windows - load update.
96                 Cache.log.info("Jalview updating from Vamsas Session.");
97         }
98   */
99   VamsasClientWatcher watcher=null;
100         public void push_update() {
101           watchForChange=false;
102     try {
103       Thread.sleep(WATCH_SLEEP); 
104     } catch (Exception e) {
105       
106     };
107     ClientDoc cdoc = getUpdateable();
108     updateVamsasDocument(cdoc);
109     doUpdate(cdoc);
110     cdoc.closeDoc();
111     cdoc=null;
112     watchForChange=true;
113     if (watcher!=null) {
114       Thread wthread = new Thread() {
115         public void run() {
116           watcher.run();          
117         }
118       };
119       wthread.start();
120       
121     }
122                 // collect all uncached alignments and put them into the vamsas dataset.
123                 // store them.
124                 Cache.log.info("Jalview updating the Vamsas Session.");
125         }
126         public void end_session() {
127           //   stop any update/watcher thread.
128     watchForChange=false; // this makes any watch(long) loops return. 
129     // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped. 
130     try {
131       Thread.sleep(WATCH_SLEEP); 
132     } catch (Exception e) {
133       
134     };
135     Cache.log.info("Jalview disconnecting from the Vamsas Session.");
136         }
137         public void updateJalview(ClientDoc cdoc) {
138                 ensureJvVamsas();
139     VamsasDatastore vds = new VamsasDatastore(cdoc, vobj2jv, jv2vobj, baseProvEntry());
140     vds.updateToJalview();
141         }
142         private void ensureJvVamsas() {
143     if (jv2vobj==null) {
144       jv2vobj = new IdentityHashMap();
145       vobj2jv = new Hashtable();
146     }
147         }
148         /**
149          * jalview object binding to VorbaIds
150          */
151         IdentityHashMap jv2vobj = null;
152         Hashtable vobj2jv = null;
153         public void updateVamsasDocument(ClientDoc doc) {
154           ensureJvVamsas();
155     VamsasDatastore vds = new VamsasDatastore(doc, vobj2jv, jv2vobj, baseProvEntry());
156     // wander through frames 
157                 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
158
159         if (frames == null)
160         {
161             return;
162         }
163
164         try
165         {       
166                 //REVERSE ORDER
167             for (int i = frames.length - 1; i > -1; i--)
168             {
169                 if (frames[i] instanceof AlignFrame)
170                 {
171                         AlignFrame af = (AlignFrame) frames[i];
172                         
173                         // update alignment and root from frame.
174                         vds.storeVAMSAS(af.getViewport(), af.getTitle());
175                 }
176             }
177         }
178         catch (Exception e) {
179                 Cache.log.error("Vamsas Document store exception",e);
180         }
181         }
182         private Entry baseProvEntry() {
183     org.vamsas.objects.core.Entry pentry = new org.vamsas.objects.core.Entry();
184     pentry.setUser(this.getProvenanceUser());
185     pentry.setApp(this.getClientHandle().getClientName());
186     pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
187     pentry.setAction("created");
188     return pentry;
189         }
190   protected class VamsasClientWatcher extends Thread implements Runnable {
191                 /* (non-Javadoc)
192                  * @see java.lang.Thread#run()
193                  */
194                 VamsasClient client=null;
195                 VamsasClientWatcher(VamsasClient client) {
196                         this.client = client;
197                 }
198     boolean running=false;
199                 public void run() {
200                   running=true;
201                         while (client.watchForChange) {
202                           ClientDoc docio = client.watch(0);
203                           if (docio!=null) {
204           client.disableGui(true);
205           Cache.log.debug("Updating jalview from changed vamsas document.");
206                             client.updateJalview(docio);
207           Cache.log.debug("Finished updating from document change.");
208           docio.closeDoc();
209           docio=null;
210           client.disableGui(false);
211                           }
212       }
213       running=false;
214       
215                 }
216                 
217         }
218   public void disableGui(boolean b) {
219       Desktop.instance.setVamsasUpdate(b);    
220   }
221   public void startWatcher() {
222     if (watcher==null)
223       watcher=new VamsasClientWatcher(this);
224     Thread thr = new Thread() {
225       public void run() {
226         watcher.start();
227       }
228     };
229     thr.start();
230   }
231 }