type change
[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.alignPanel.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       watcher.start();
115     }
116     // collect all uncached alignments and put them into the vamsas dataset.
117     // store them.
118     Cache.log.info("Jalview updating the Vamsas Session.");
119   }
120   public void end_session() {
121     //   stop any update/watcher thread.
122     watchForChange=false; // this makes any watch(long) loops return.
123     // we should also wait arount for this.WATCH_SLEEP to really make sure the watcher thread has stopped.
124     try {
125       Thread.sleep(WATCH_SLEEP);
126     } catch (Exception e) {
127
128     };
129     Cache.log.info("Jalview disconnecting from the Vamsas Session.");
130   }
131   public void updateJalview(ClientDoc cdoc) {
132     ensureJvVamsas();
133     VamsasDatastore vds = new VamsasDatastore(cdoc, vobj2jv, jv2vobj, baseProvEntry());
134     vds.updateToJalview();
135   }
136   private void ensureJvVamsas() {
137     if (jv2vobj==null) {
138       jv2vobj = new IdentityHashMap();
139       vobj2jv = new Hashtable();
140     }
141   }
142   /**
143    * jalview object binding to VorbaIds
144    */
145   IdentityHashMap jv2vobj = null;
146   Hashtable vobj2jv = null;
147   public void updateVamsasDocument(ClientDoc doc) {
148     ensureJvVamsas();
149     VamsasDatastore vds = new VamsasDatastore(doc, vobj2jv, jv2vobj, baseProvEntry());
150     // wander through frames
151     JInternalFrame[] frames = Desktop.desktop.getAllFrames();
152
153     if (frames == null)
154     {
155       return;
156     }
157
158     try
159     {
160       //REVERSE ORDER
161       for (int i = frames.length - 1; i > -1; i--)
162       {
163         if (frames[i] instanceof AlignFrame)
164         {
165           AlignFrame af = (AlignFrame) frames[i];
166
167           // update alignment and root from frame.
168           vds.storeVAMSAS(af.getViewport(), af.getTitle());
169         }
170       }
171     }
172     catch (Exception e) {
173       Cache.log.error("Vamsas Document store exception",e);
174     }
175   }
176   private Entry baseProvEntry() {
177     org.vamsas.objects.core.Entry pentry = new org.vamsas.objects.core.Entry();
178     pentry.setUser(this.getProvenanceUser());
179     pentry.setApp(this.getClientHandle().getClientName());
180     pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
181     pentry.setAction("created");
182     return pentry;
183   }
184   protected class VamsasClientWatcher extends Thread {
185     /* (non-Javadoc)
186      * @see java.lang.Thread#run()
187      */
188     VamsasClient client=null;
189     VamsasClientWatcher(VamsasClient client) {
190       this.client = client;
191     }
192     boolean running=false;
193     public void run() {
194       running=true;
195       while (client.watchForChange) {
196         ClientDoc docio = client.watch(0);
197         if (docio!=null) {
198           client.disableGui(true);
199           Cache.log.debug("Updating jalview from changed vamsas document.");
200           client.updateJalview(docio);
201           Cache.log.debug("Finished updating from document change.");
202           docio.closeDoc();
203           docio=null;
204           client.disableGui(false);
205         }
206       }
207       running=false;
208
209     }
210
211   }
212   public void disableGui(boolean b) {
213     Desktop.instance.setVamsasUpdate(b);
214   }
215   public void startWatcher() {
216     if (watcher==null)
217       watcher=new VamsasClientWatcher(this);
218     Thread thr = new Thread() {
219       public void run() {
220         watcher.start();
221       }
222     };
223     thr.start();
224   }
225 }