6 import jalview.bin.Cache;
7 import jalview.datamodel.SequenceI;
8 import jalview.io.VamsasAppDatastore;
9 import jalview.structure.StructureSelectionManager;
10 import jalview.structure.VamsasListener;
12 import java.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
15 import java.io.IOException;
16 import java.util.Hashtable;
17 import java.util.IdentityHashMap;
19 import javax.swing.JInternalFrame;
20 import javax.swing.JOptionPane;
22 import uk.ac.vamsas.client.ClientHandle;
23 import uk.ac.vamsas.client.IClient;
24 import uk.ac.vamsas.client.IClientDocument;
25 import uk.ac.vamsas.client.InvalidSessionDocumentException;
26 import uk.ac.vamsas.client.NoDefaultSessionException;
27 import uk.ac.vamsas.client.UserHandle;
28 import uk.ac.vamsas.client.VorbaId;
29 import uk.ac.vamsas.client.picking.IMessageHandler;
30 import uk.ac.vamsas.client.picking.IPickManager;
31 import uk.ac.vamsas.client.picking.Message;
32 import uk.ac.vamsas.client.picking.MouseOverMessage;
33 import uk.ac.vamsas.objects.core.Entry;
39 public class VamsasApplication
41 IClient vclient = null;
43 ClientHandle app = null;
45 UserHandle user = null;
47 Desktop jdesktop = null; // our jalview desktop reference
49 // Cache.preferences for vamsas client session arena
50 // preferences for check for default session at startup.
51 // user and organisation stuff.
52 public VamsasApplication(Desktop jdesktop, File sessionPath)
55 // we should create a session URI from the sessionPath and pass it to
56 // the clientFactory - but the vamsas api doesn't cope with that yet.
57 this.jdesktop = jdesktop;
58 initClientSession(null, sessionPath);
61 private static uk.ac.vamsas.client.IClientFactory getClientFactory()
64 return new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
68 * Start a new vamsas session
72 public VamsasApplication(Desktop jdesktop)
74 this.jdesktop = jdesktop;
75 initClientSession(null, null);
79 * init a connection to the session at the given url
84 public VamsasApplication(Desktop jdesktop, String sessionUrl)
86 this.jdesktop = jdesktop;
87 initClientSession(sessionUrl, null);
92 * or other if clientfactory instantiation failed.
93 * @return list of current sessions or null if no session exists.
95 public static String[] getSessionList() throws Exception
97 return getClientFactory().getCurrentSessions();
101 * initialise, possibly with either a valid session url or a file for a new
105 * null or a valid session url
106 * @param vamsasDocument
107 * null or a valid vamsas document file
108 * @return false if no vamsas connection was made
110 private boolean initClientSession(String sess, File vamsasDocument)
114 // Only need to tell the library what the application is here
115 app = getJalviewHandle();
116 uk.ac.vamsas.client.IClientFactory clientfactory = getClientFactory();
117 if (vamsasDocument != null)
122 "Implementation Error - cannot import existing vamsas document into an existing session, Yet!");
126 vclient = clientfactory.openAsNewSessionIClient(app,
128 } catch (InvalidSessionDocumentException e)
131 .showInternalMessageDialog(
134 "VAMSAS Document could not be opened as a new session - please choose another",
135 "VAMSAS Document Import Failed",
136 JOptionPane.ERROR_MESSAGE);
142 // join existing or create a new session
145 vclient = clientfactory.getNewSessionIClient(app);
149 vclient = clientfactory.getIClient(app, sess);
152 // set some properties for our VAMSAS interaction
154 user = vclient.getUserHandle();
156 } catch (Exception e)
158 jalview.bin.Cache.log
159 .error("Couldn't instantiate vamsas client !", e);
165 private void setVclientConfig()
172 if (vclient instanceof uk.ac.vamsas.client.simpleclient.SimpleClient)
174 uk.ac.vamsas.client.simpleclient.SimpleClientConfig cfg = ((uk.ac.vamsas.client.simpleclient.SimpleClient) vclient).getSimpleClientConfig();
175 cfg._validatemergedroots = false;
176 cfg._validateupdatedroots= true; //we may write rubbish otherwise.
181 Cache.log.warn("Probable SERIOUS VAMSAS client incompatibility - carrying on regardless",e);
185 Cache.log.warn("Probable VAMSAS client incompatibility - carrying on regardless",e);
190 * make the appHandle for Jalview
194 private ClientHandle getJalviewHandle()
196 return new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache
197 .getProperty("VERSION"));
202 * @return true if we are registered in a vamsas session
204 public boolean inSession()
206 return (vclient != null);
210 * called to connect to session inits handlers, does an initial document
213 public void initial_update()
218 "Impementation error! Vamsas Operations when client not initialised and connected.");
220 addDocumentUpdateHandler();
223 .debug("Jalview loading the Vamsas Session for the first time.");
224 dealWithDocumentUpdate(false); // we don't push an update out to the
226 Cache.log.debug("... finished update for the first time.");
230 * Update all windows after a vamsas datamodel change. this could go on the
234 protected void updateJalviewGui()
236 JInternalFrame[] frames = jdesktop.getAllFrames();
246 for (int i = frames.length - 1; i > -1; i--)
248 if (frames[i] instanceof AlignFrame)
250 AlignFrame af = (AlignFrame) frames[i];
251 af.alignPanel.alignmentChanged();
254 } catch (Exception e)
258 "Exception whilst refreshing jalview windows after a vamsas document update.",
263 public void push_update()
265 Cache.log.info("Jalview updating to the Vamsas Session.");
266 dealWithDocumentUpdate(true);
268 * IClientDocument cdoc=null; try { cdoc = vclient.getClientDocument(); }
269 * catch (Exception e) { Cache.log.error("Failed to get client document for
270 * update."); // RAISE A WARNING DIALOG disableGui(false); return; }
271 * updateVamsasDocument(cdoc); updateJalviewGui();
272 * cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags
273 * back vclient.updateDocument(cdoc);
275 Cache.log.info("Jalview finished updating to the Vamsas Session.");
278 public void end_session()
281 throw new Error("Jalview not connected to Vamsas session.");
282 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
287 vclient.finalizeClient();
288 Cache.log.info("Jalview has left the session.");
293 .warn("JV Client leaving a session that's its not joined yet.");
295 joinedSession = false;
301 } catch (Exception e)
303 Cache.log.error("Vamsas Session finalization threw exceptions!", e);
307 public void updateJalview(IClientDocument cdoc)
309 Cache.log.debug("Jalview updating from sesion document ..");
311 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
312 baseProvEntry(), alRedoState);
313 vds.updateToJalview();
314 Cache.log.debug(".. finished updating from sesion document.");
318 private void ensureJvVamsas()
322 jv2vobj = new IdentityHashMap();
323 vobj2jv = new Hashtable();
324 alRedoState = new Hashtable();
329 * jalview object binding to VorbaIds
331 IdentityHashMap jv2vobj = null;
333 Hashtable vobj2jv = null;
334 Hashtable alRedoState = null;
335 public void updateVamsasDocument(IClientDocument doc)
338 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
339 baseProvEntry(), alRedoState);
340 // wander through frames
341 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
351 for (int i = frames.length - 1; i > -1; i--)
353 if (frames[i] instanceof AlignFrame)
355 AlignFrame af = (AlignFrame) frames[i];
357 // update alignment and root from frame.
358 vds.storeVAMSAS(af.getViewport(), af.getTitle());
362 for (int i = frames.length - 1; i > -1; i--)
364 if (frames[i] instanceof AlignFrame)
366 AlignFrame af = (AlignFrame) frames[i];
368 // add any AlignedCodonFrame mappings on this alignment to any other.
369 vds.storeSequenceMappings(af.getViewport(), af.getTitle());
372 } catch (Exception e)
374 Cache.log.error("Vamsas Document store exception", e);
378 private Entry baseProvEntry()
380 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
381 pentry.setUser(user.getFullName());
382 pentry.setApp(app.getClientUrn());
383 pentry.setDate(new java.util.Date());
384 pentry.setAction("created");
389 * do a vamsas document update or update jalview from the vamsas document
392 * true to update from jalview to the vamsas document
394 protected void dealWithDocumentUpdate(boolean fromJalview)
396 // called by update handler for document update.
397 Cache.log.debug("Updating jalview from changed vamsas document.");
401 long time = System.currentTimeMillis();
402 IClientDocument cdoc = vclient.getClientDocument();
403 if (Cache.log.isDebugEnabled())
405 Cache.log.debug("Time taken to get ClientDocument = "
406 + (System.currentTimeMillis() - time));
407 time = System.currentTimeMillis();
411 this.updateVamsasDocument(cdoc);
412 if (Cache.log.isDebugEnabled())
415 .debug("Time taken to update Vamsas Document from jalview\t= "
416 + (System.currentTimeMillis() - time));
417 time = System.currentTimeMillis();
419 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
420 if (Cache.log.isDebugEnabled())
422 Cache.log.debug("Time taken to set Document Roots\t\t= "
423 + (System.currentTimeMillis() - time));
424 time = System.currentTimeMillis();
430 if (Cache.log.isDebugEnabled())
433 .debug("Time taken to update Jalview from vamsas document Roots\t= "
434 + (System.currentTimeMillis() - time));
435 time = System.currentTimeMillis();
439 vclient.updateDocument(cdoc);
440 if (Cache.log.isDebugEnabled())
442 Cache.log.debug("Time taken to update Session Document\t= "
443 + (System.currentTimeMillis() - time));
444 time = System.currentTimeMillis();
447 } catch (Exception ee)
449 System.err.println("Exception whilst updating :");
450 ee.printStackTrace(System.err);
452 Cache.log.debug("Finished updating from document change.");
456 private void addDocumentUpdateHandler()
458 final VamsasApplication client = this;
459 vclient.addDocumentUpdateHandler(new PropertyChangeListener()
461 public void propertyChange(PropertyChangeEvent evt)
463 Cache.log.debug("Dealing with document update event.");
464 client.dealWithDocumentUpdate(false);
465 Cache.log.debug("finished dealing with event.");
468 Cache.log.debug("Added Jalview handler for vamsas document updates.");
471 public void disableGui(boolean b)
473 Desktop.instance.setVamsasUpdate(b);
476 private boolean joinedSession = false;
478 private VamsasListener picker = null;
480 private void startSession()
486 vclient.joinSession();
487 joinedSession = true;
488 } catch (Exception e)
491 Cache.log.error("Failed to join vamsas session.", e);
496 final IPickManager pm = vclient.getPickManager();
497 final StructureSelectionManager ssm = StructureSelectionManager
498 .getStructureSelectionManager();
499 pm.registerMessageHandler(new IMessageHandler()
503 public void handleMessage(Message message)
505 if (message instanceof MouseOverMessage && vobj2jv != null)
507 MouseOverMessage mm = (MouseOverMessage) message;
508 String mstring = mm.getVorbaID() + " " + mm.getPosition();
509 if (last != null && mstring.equals(last))
513 // if (Cache.log.isDebugEnabled())
515 // Cache.log.debug("Received MouseOverMessage "+mm.getVorbaID()+"
516 // "+mm.getPosition());
518 Object jvobj = vobj2jv.get(mm.getVorbaID());
519 if (jvobj != null && jvobj instanceof SequenceI)
522 // Cache.log.debug("Handling Mouse over "+mm.getVorbaID()+"
523 // bound to "+jvobj+" at "+mm.getPosition());
524 // position is in sequence or in aligned sequence ???????
525 ssm.mouseOverVamsasSequence((SequenceI) jvobj, mm
531 picker = new VamsasListener()
533 SequenceI last = null;
537 public void mouseOver(SequenceI seq, int index)
541 if (seq != last || i != index)
543 VorbaId v = (VorbaId) jv2vobj.get(seq);
546 Cache.log.debug("Mouse over " + v.getId() + " bound to "
547 + seq + " at " + index);
550 MouseOverMessage message = new MouseOverMessage(v.getId(),
552 pm.sendMessage(message);
557 ssm.addStructureViewerListener(picker); // better method here
558 } catch (Exception e)
560 Cache.log.error("Failed to init Vamsas Picking", e);