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 user = vclient.getUserHandle();
154 } catch (Exception e)
156 jalview.bin.Cache.log
157 .error("Couldn't instantiate vamsas client !", e);
164 * make the appHandle for Jalview
168 private ClientHandle getJalviewHandle()
170 return new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache
171 .getProperty("VERSION"));
176 * @return true if we are registered in a vamsas session
178 public boolean inSession()
180 return (vclient != null);
184 * called to connect to session inits handlers, does an initial document
187 public void initial_update()
192 "Impementation error! Vamsas Operations when client not initialised and connected.");
194 addDocumentUpdateHandler();
197 .debug("Jalview loading the Vamsas Session for the first time.");
198 dealWithDocumentUpdate(false); // we don't push an update out to the
200 Cache.log.debug("... finished update for the first time.");
204 * Update all windows after a vamsas datamodel change. this could go on the
208 protected void updateJalviewGui()
210 JInternalFrame[] frames = jdesktop.getAllFrames();
220 for (int i = frames.length - 1; i > -1; i--)
222 if (frames[i] instanceof AlignFrame)
224 AlignFrame af = (AlignFrame) frames[i];
225 af.alignPanel.alignmentChanged();
228 } catch (Exception e)
232 "Exception whilst refreshing jalview windows after a vamsas document update.",
237 public void push_update()
239 Cache.log.info("Jalview updating to the Vamsas Session.");
240 dealWithDocumentUpdate(true);
242 * IClientDocument cdoc=null; try { cdoc = vclient.getClientDocument(); }
243 * catch (Exception e) { Cache.log.error("Failed to get client document for
244 * update."); // RAISE A WARNING DIALOG disableGui(false); return; }
245 * updateVamsasDocument(cdoc); updateJalviewGui();
246 * cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags
247 * back vclient.updateDocument(cdoc);
249 Cache.log.info("Jalview finished updating to the Vamsas Session.");
252 public void end_session()
255 throw new Error("Jalview not connected to Vamsas session.");
256 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
261 vclient.finalizeClient();
262 Cache.log.info("Jalview has left the session.");
267 .warn("JV Client leaving a session that's its not joined yet.");
269 joinedSession = false;
275 } catch (Exception e)
277 Cache.log.error("Vamsas Session finalization threw exceptions!", e);
281 public void updateJalview(IClientDocument cdoc)
283 Cache.log.debug("Jalview updating from sesion document ..");
285 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
286 baseProvEntry(), alRedoState);
287 vds.updateToJalview();
288 Cache.log.debug(".. finished updating from sesion document.");
292 private void ensureJvVamsas()
296 jv2vobj = new IdentityHashMap();
297 vobj2jv = new Hashtable();
298 alRedoState = new Hashtable();
303 * jalview object binding to VorbaIds
305 IdentityHashMap jv2vobj = null;
307 Hashtable vobj2jv = null;
308 Hashtable alRedoState = null;
309 public void updateVamsasDocument(IClientDocument doc)
312 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
313 baseProvEntry(), alRedoState);
314 // wander through frames
315 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
325 for (int i = frames.length - 1; i > -1; i--)
327 if (frames[i] instanceof AlignFrame)
329 AlignFrame af = (AlignFrame) frames[i];
331 // update alignment and root from frame.
332 vds.storeVAMSAS(af.getViewport(), af.getTitle());
336 for (int i = frames.length - 1; i > -1; i--)
338 if (frames[i] instanceof AlignFrame)
340 AlignFrame af = (AlignFrame) frames[i];
342 // add any AlignedCodonFrame mappings on this alignment to any other.
343 vds.storeSequenceMappings(af.getViewport(), af.getTitle());
346 } catch (Exception e)
348 Cache.log.error("Vamsas Document store exception", e);
352 private Entry baseProvEntry()
354 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
355 pentry.setUser(user.getFullName());
356 pentry.setApp(app.getClientUrn());
357 pentry.setDate(new java.util.Date());
358 pentry.setAction("created");
363 * do a vamsas document update or update jalview from the vamsas document
366 * true to update from jalview to the vamsas document
368 protected void dealWithDocumentUpdate(boolean fromJalview)
370 // called by update handler for document update.
371 Cache.log.debug("Updating jalview from changed vamsas document.");
375 long time = System.currentTimeMillis();
376 IClientDocument cdoc = vclient.getClientDocument();
377 if (Cache.log.isDebugEnabled())
379 Cache.log.debug("Time taken to get ClientDocument = "
380 + (System.currentTimeMillis() - time));
381 time = System.currentTimeMillis();
385 this.updateVamsasDocument(cdoc);
386 if (Cache.log.isDebugEnabled())
389 .debug("Time taken to update Vamsas Document from jalview\t= "
390 + (System.currentTimeMillis() - time));
391 time = System.currentTimeMillis();
393 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
394 if (Cache.log.isDebugEnabled())
396 Cache.log.debug("Time taken to set Document Roots\t\t= "
397 + (System.currentTimeMillis() - time));
398 time = System.currentTimeMillis();
404 if (Cache.log.isDebugEnabled())
407 .debug("Time taken to update Jalview from vamsas document Roots\t= "
408 + (System.currentTimeMillis() - time));
409 time = System.currentTimeMillis();
413 vclient.updateDocument(cdoc);
414 if (Cache.log.isDebugEnabled())
416 Cache.log.debug("Time taken to update Session Document\t= "
417 + (System.currentTimeMillis() - time));
418 time = System.currentTimeMillis();
421 } catch (Exception ee)
423 System.err.println("Exception whilst updating :");
424 ee.printStackTrace(System.err);
426 Cache.log.debug("Finished updating from document change.");
430 private void addDocumentUpdateHandler()
432 final VamsasApplication client = this;
433 vclient.addDocumentUpdateHandler(new PropertyChangeListener()
435 public void propertyChange(PropertyChangeEvent evt)
437 Cache.log.debug("Dealing with document update event.");
438 client.dealWithDocumentUpdate(false);
439 Cache.log.debug("finished dealing with event.");
442 Cache.log.debug("Added Jalview handler for vamsas document updates.");
445 public void disableGui(boolean b)
447 Desktop.instance.setVamsasUpdate(b);
450 private boolean joinedSession = false;
452 private VamsasListener picker = null;
454 private void startSession()
460 vclient.joinSession();
461 joinedSession = true;
462 } catch (Exception e)
465 Cache.log.error("Failed to join vamsas session.", e);
470 final IPickManager pm = vclient.getPickManager();
471 final StructureSelectionManager ssm = StructureSelectionManager
472 .getStructureSelectionManager();
473 pm.registerMessageHandler(new IMessageHandler()
477 public void handleMessage(Message message)
479 if (message instanceof MouseOverMessage && vobj2jv != null)
481 MouseOverMessage mm = (MouseOverMessage) message;
482 String mstring = mm.getVorbaID() + " " + mm.getPosition();
483 if (last != null && mstring.equals(last))
487 // if (Cache.log.isDebugEnabled())
489 // Cache.log.debug("Received MouseOverMessage "+mm.getVorbaID()+"
490 // "+mm.getPosition());
492 Object jvobj = vobj2jv.get(mm.getVorbaID());
493 if (jvobj != null && jvobj instanceof SequenceI)
496 // Cache.log.debug("Handling Mouse over "+mm.getVorbaID()+"
497 // bound to "+jvobj+" at "+mm.getPosition());
498 // position is in sequence or in aligned sequence ???????
499 ssm.mouseOverVamsasSequence((SequenceI) jvobj, mm
505 picker = new VamsasListener()
507 SequenceI last = null;
511 public void mouseOver(SequenceI seq, int index)
515 if (seq != last || i != index)
517 VorbaId v = (VorbaId) jv2vobj.get(seq);
520 Cache.log.debug("Mouse over " + v.getId() + " bound to "
521 + seq + " at " + index);
524 MouseOverMessage message = new MouseOverMessage(v.getId(),
526 pm.sendMessage(message);
531 ssm.addStructureViewerListener(picker); // better method here
532 } catch (Exception e)
534 Cache.log.error("Failed to init Vamsas Picking", e);