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