6 import java.beans.PropertyChangeEvent;
7 import java.beans.PropertyChangeListener;
9 import java.io.IOException;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.util.Hashtable;
13 import java.util.IdentityHashMap;
14 import java.util.Vector;
15 import java.util.jar.JarOutputStream;
17 import javax.swing.JInternalFrame;
19 import jalview.bin.Cache;
20 import jalview.io.VamsasAppDatastore;
21 import jalview.io.VamsasDatastore;
23 import uk.ac.vamsas.client.*;
24 import uk.ac.vamsas.*;
25 import uk.ac.vamsas.objects.*;
26 import uk.ac.vamsas.objects.core.Entry;
31 public class VamsasApplication
34 ClientHandle app=null;
37 Desktop jdesktop = null; // our jalview desktop reference
39 // Cache.preferences for vamsas client session arena
40 // preferences for check for default session at startup.
41 // user and organisation stuff.
42 public VamsasApplication(Desktop jdesktop,
46 // we should create a session URI from the sessionPath and pass it to
47 // the clientFactory - but the vamsas api doesn't cope with that yet.
49 throw new Error("Sorry - can't start from session file yet."); // make this a warning
51 private static uk.ac.vamsas.client.IClientFactory getClientFactory() throws IOException {
52 return new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
54 public VamsasApplication(Desktop jdesktop)
56 this.jdesktop = jdesktop;
57 initClientSession(null);
60 * @throws IOException or other if clientfactory instantiation failed.
61 * @return list of current sessions or null if no session exists.
63 public static String[] getSessionList() throws Exception {
64 uk.ac.vamsas.client.IClientFactory clientfactory = null;
65 clientfactory = getClientFactory();
66 return clientfactory.getCurrentSessions();
69 * initialise, possibly with e valid session url
73 private boolean initClientSession(String sess) {
75 // Only need to tell the library what the application is here
76 app = new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache.getProperty("VERSION"));
77 uk.ac.vamsas.client.IClientFactory clientfactory = getClientFactory();
80 vclient = clientfactory.getIClient(app);
84 vclient = clientfactory.getIClient(app,sess);
87 user = vclient.getUserHandle();
89 } catch (NoDefaultSessionException e)
95 jalview.bin.Cache.log.error("Couldn't instantiate vamsas client !",e);
102 * @return true if we are registered in a vamsas session
104 public boolean inSession()
106 return (vclient!=null);
109 * called to connect to session
110 * inits handlers, does an initial document update.
112 public void initial_update()
116 throw new Error("Impementation error! Vamsas Operations when client not initialised and connected.");
118 this.addDocumentUpdateHandler();
120 Cache.log.info("Jalview loading the Vamsas Session.");
121 // load in the vamsas archive for the first time
122 IClientDocument cdoc=null;
125 cdoc = vclient.getClientDocument();
127 catch (IOException e)
129 Cache.log.error("Serious: Exception whilst getting vamsas document",e);
134 cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags back
137 vclient.updateDocument(cdoc);
141 Cache.log.error("Serious: Exception whilst updating vamsas document.",e);
147 * Update all windows after a vamsas datamodel change.
148 * this could go on the desktop object!
151 protected void updateJalviewGui()
153 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
163 for (int i = frames.length - 1; i > -1; i--)
165 if (frames[i] instanceof AlignFrame)
167 AlignFrame af = (AlignFrame) frames[i];
168 af.alignPanel.alignmentChanged();
175 "Exception whilst refreshing jalview windows after a vamsas document update.",
180 * this will close all windows currently in Jalview.
183 protected void closeWindows() {
184 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
193 for (int i = frames.length - 1; i > -1; i--) {
196 } catch (Exception e) {
197 Cache.log.error("Whilst closing windows",e);
202 public void get_update(VamsasArchive doc) {
203 // Close windows - load update.
204 Cache.log.info("Jalview updating from Vamsas Session.");
207 public void push_update()
210 Cache.log.info("Jalview updating the Vamsas Session.");
211 IClientDocument cdoc=null;
214 cdoc = vclient.getClientDocument();
218 Cache.log.error("Failed to get client document for update.");
219 // RAISE A WARNING DIALOG
223 updateVamsasDocument(cdoc);
225 cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags back
226 vclient.updateDocument(cdoc);
231 public void end_session()
234 throw new Error("Jalview not connected to Vamsas session.");
235 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
239 vclient.finalizeClient();
240 Cache.log.info("Jalview has left the session.");
242 Cache.log.warn("JV Client leaving a session that's its not joined yet.");
245 app=null; user = null;
251 Cache.log.error("Vamsas Session finalization threw exceptions!",e);
255 public void updateJalview(IClientDocument cdoc)
258 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
260 vds.updateToJalview();
262 private void ensureJvVamsas()
266 jv2vobj = new IdentityHashMap();
267 vobj2jv = new Hashtable();
272 * jalview object binding to VorbaIds
274 IdentityHashMap jv2vobj = null;
275 Hashtable vobj2jv = null;
276 public void updateVamsasDocument(IClientDocument doc)
279 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
281 // wander through frames
282 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
292 for (int i = frames.length - 1; i > -1; i--)
294 if (frames[i] instanceof AlignFrame)
296 AlignFrame af = (AlignFrame) frames[i];
298 // update alignment and root from frame.
299 vds.storeVAMSAS(af.getViewport(), af.getTitle());
305 Cache.log.error("Vamsas Document store exception", e);
309 private Entry baseProvEntry()
311 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
312 pentry.setUser(user.getFullName());
313 pentry.setApp(app.getClientUrn());
314 pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
315 pentry.setAction("created");
319 private void addDocumentUpdateHandler()
321 final IClient vorbaclient = vclient;
322 final VamsasApplication client = this;
323 vorbaclient.addDocumentUpdateHandler(new PropertyChangeListener() {
324 public void propertyChange(PropertyChangeEvent evt)
326 // update handler for document update.
327 Cache.log.debug("Updating jalview from changed vamsas document.");
328 client.disableGui(true);
330 IClientDocument cdoc = vorbaclient.getClientDocument();
331 client.updateJalview(cdoc);
332 // cdoc.setVamsasRoots(cdoc.getVamsasRoots());
333 vorbaclient.updateDocument(cdoc);
335 } catch (Exception ee) {
336 System.err.println("Exception whilst updating :");
337 ee.printStackTrace(System.err);
339 Cache.log.debug("Finished updating from document change.");
340 client.disableGui(false);
344 public void disableGui(boolean b)
346 Desktop.instance.setVamsasUpdate(b);
348 private boolean joinedSession=false;
349 private void startSession()
354 vclient.joinSession();
360 Cache.log.error("Failed to join vamsas session.",e);