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,
287 vds.updateToJalview();
288 Cache.log.debug(".. finished updating from sesion document.");
292 private void ensureJvVamsas()
296 jv2vobj = new IdentityHashMap();
297 vobj2jv = new Hashtable();
302 * jalview object binding to VorbaIds
304 IdentityHashMap jv2vobj = null;
306 Hashtable vobj2jv = null;
308 public void updateVamsasDocument(IClientDocument doc)
311 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
313 // wander through frames
314 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
324 for (int i = frames.length - 1; i > -1; i--)
326 if (frames[i] instanceof AlignFrame)
328 AlignFrame af = (AlignFrame) frames[i];
330 // update alignment and root from frame.
331 vds.storeVAMSAS(af.getViewport(), af.getTitle());
335 for (int i = frames.length - 1; i > -1; i--)
337 if (frames[i] instanceof AlignFrame)
339 AlignFrame af = (AlignFrame) frames[i];
341 // add any AlignedCodonFrame mappings on this alignment to any other.
342 vds.storeSequenceMappings(af.getViewport(), af.getTitle());
345 } catch (Exception e)
347 Cache.log.error("Vamsas Document store exception", e);
351 private Entry baseProvEntry()
353 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
354 pentry.setUser(user.getFullName());
355 pentry.setApp(app.getClientUrn());
356 pentry.setDate(new java.util.Date());
357 pentry.setAction("created");
362 * do a vamsas document update or update jalview from the vamsas document
365 * true to update from jalview to the vamsas document
367 protected void dealWithDocumentUpdate(boolean fromJalview)
369 // called by update handler for document update.
370 Cache.log.debug("Updating jalview from changed vamsas document.");
374 long time = System.currentTimeMillis();
375 IClientDocument cdoc = vclient.getClientDocument();
376 if (Cache.log.isDebugEnabled())
378 Cache.log.debug("Time taken to get ClientDocument = "
379 + (System.currentTimeMillis() - time));
380 time = System.currentTimeMillis();
384 this.updateVamsasDocument(cdoc);
385 if (Cache.log.isDebugEnabled())
388 .debug("Time taken to update Vamsas Document from jalview\t= "
389 + (System.currentTimeMillis() - time));
390 time = System.currentTimeMillis();
392 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
393 if (Cache.log.isDebugEnabled())
395 Cache.log.debug("Time taken to set Document Roots\t\t= "
396 + (System.currentTimeMillis() - time));
397 time = System.currentTimeMillis();
403 if (Cache.log.isDebugEnabled())
406 .debug("Time taken to update Jalview from vamsas document Roots\t= "
407 + (System.currentTimeMillis() - time));
408 time = System.currentTimeMillis();
412 vclient.updateDocument(cdoc);
413 if (Cache.log.isDebugEnabled())
415 Cache.log.debug("Time taken to update Session Document\t= "
416 + (System.currentTimeMillis() - time));
417 time = System.currentTimeMillis();
420 } catch (Exception ee)
422 System.err.println("Exception whilst updating :");
423 ee.printStackTrace(System.err);
425 Cache.log.debug("Finished updating from document change.");
429 private void addDocumentUpdateHandler()
431 final VamsasApplication client = this;
432 vclient.addDocumentUpdateHandler(new PropertyChangeListener()
434 public void propertyChange(PropertyChangeEvent evt)
436 Cache.log.debug("Dealing with document update event.");
437 client.dealWithDocumentUpdate(false);
438 Cache.log.debug("finished dealing with event.");
441 Cache.log.debug("Added Jalview handler for vamsas document updates.");
444 public void disableGui(boolean b)
446 Desktop.instance.setVamsasUpdate(b);
449 private boolean joinedSession = false;
451 private VamsasListener picker = null;
453 private void startSession()
459 vclient.joinSession();
460 joinedSession = true;
461 } catch (Exception e)
464 Cache.log.error("Failed to join vamsas session.", e);
469 final IPickManager pm = vclient.getPickManager();
470 final StructureSelectionManager ssm = StructureSelectionManager
471 .getStructureSelectionManager();
472 pm.registerMessageHandler(new IMessageHandler()
476 public void handleMessage(Message message)
478 if (message instanceof MouseOverMessage && vobj2jv != null)
480 MouseOverMessage mm = (MouseOverMessage) message;
481 String mstring = mm.getVorbaID() + " " + mm.getPosition();
482 if (last != null && mstring.equals(last))
486 // if (Cache.log.isDebugEnabled())
488 // Cache.log.debug("Received MouseOverMessage "+mm.getVorbaID()+"
489 // "+mm.getPosition());
491 Object jvobj = vobj2jv.get(mm.getVorbaID());
492 if (jvobj != null && jvobj instanceof SequenceI)
495 // Cache.log.debug("Handling Mouse over "+mm.getVorbaID()+"
496 // bound to "+jvobj+" at "+mm.getPosition());
497 // position is in sequence or in aligned sequence ???????
498 ssm.mouseOverVamsasSequence((SequenceI) jvobj, mm
504 picker = new VamsasListener()
506 SequenceI last = null;
510 public void mouseOver(SequenceI seq, int index)
514 if (seq != last || i != index)
516 VorbaId v = (VorbaId) jv2vobj.get(seq);
519 Cache.log.debug("Mouse over " + v.getId() + " bound to "
520 + seq + " at " + index);
523 MouseOverMessage message = new MouseOverMessage(v.getId(),
525 pm.sendMessage(message);
530 ssm.addStructureViewerListener(picker); // better method here
531 } catch (Exception e)
533 Cache.log.error("Failed to init Vamsas Picking", e);