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