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;
21 import uk.ac.vamsas.client.ClientHandle;
22 import uk.ac.vamsas.client.IClient;
23 import uk.ac.vamsas.client.IClientDocument;
24 import uk.ac.vamsas.client.NoDefaultSessionException;
25 import uk.ac.vamsas.client.UserHandle;
26 import uk.ac.vamsas.client.VorbaId;
27 import uk.ac.vamsas.client.picking.IMessageHandler;
28 import uk.ac.vamsas.client.picking.IPickManager;
29 import uk.ac.vamsas.client.picking.Message;
30 import uk.ac.vamsas.client.picking.MouseOverMessage;
31 import uk.ac.vamsas.objects.core.Entry;
36 public class VamsasApplication
39 ClientHandle app=null;
42 Desktop jdesktop = null; // our jalview desktop reference
44 // Cache.preferences for vamsas client session arena
45 // preferences for check for default session at startup.
46 // user and organisation stuff.
47 public VamsasApplication(Desktop jdesktop,
51 // we should create a session URI from the sessionPath and pass it to
52 // the clientFactory - but the vamsas api doesn't cope with that yet.
54 throw new Error("Sorry - can't start from session file yet."); // make this a warning
56 private static uk.ac.vamsas.client.IClientFactory getClientFactory() throws IOException {
57 return new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
59 public VamsasApplication(Desktop jdesktop)
61 this.jdesktop = jdesktop;
62 initClientSession(null);
65 * @throws IOException or other if clientfactory instantiation failed.
66 * @return list of current sessions or null if no session exists.
68 public static String[] getSessionList() throws Exception {
69 return getClientFactory().getCurrentSessions();
72 * initialise, possibly with e valid session url
76 private boolean initClientSession(String sess) {
78 // Only need to tell the library what the application is here
79 app = new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache.getProperty("VERSION"));
80 uk.ac.vamsas.client.IClientFactory clientfactory = getClientFactory();
83 vclient = clientfactory.getIClient(app);
87 vclient = clientfactory.getIClient(app,sess);
90 user = vclient.getUserHandle();
92 } catch (NoDefaultSessionException e)
98 jalview.bin.Cache.log.error("Couldn't instantiate vamsas client !",e);
105 * @return true if we are registered in a vamsas session
107 public boolean inSession()
109 return (vclient!=null);
112 * called to connect to session
113 * inits handlers, does an initial document update.
115 public void initial_update()
119 throw new Error("Impementation error! Vamsas Operations when client not initialised and connected.");
121 addDocumentUpdateHandler();
123 Cache.log.debug("Jalview loading the Vamsas Session for the first time.");
124 dealWithDocumentUpdate(false); // we don't push an update out to the document yet.
125 Cache.log.debug("... finished update for the first time.");
128 * Update all windows after a vamsas datamodel change.
129 * this could go on the desktop object!
132 protected void updateJalviewGui()
134 JInternalFrame[] frames = jdesktop.getAllFrames();
144 for (int i = frames.length - 1; i > -1; i--)
146 if (frames[i] instanceof AlignFrame)
148 AlignFrame af = (AlignFrame) frames[i];
149 af.alignPanel.alignmentChanged();
156 "Exception whilst refreshing jalview windows after a vamsas document update.",
160 public void push_update()
162 Cache.log.info("Jalview updating to the Vamsas Session.");
163 dealWithDocumentUpdate(true);
165 IClientDocument cdoc=null;
168 cdoc = vclient.getClientDocument();
172 Cache.log.error("Failed to get client document for update.");
173 // RAISE A WARNING DIALOG
177 updateVamsasDocument(cdoc);
179 cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags back
180 vclient.updateDocument(cdoc);
182 Cache.log.info("Jalview finished updating to the Vamsas Session.");
185 public void end_session()
188 throw new Error("Jalview not connected to Vamsas session.");
189 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
193 vclient.finalizeClient();
194 Cache.log.info("Jalview has left the session.");
196 Cache.log.warn("JV Client leaving a session that's its not joined yet.");
200 app=null; user = null;
206 Cache.log.error("Vamsas Session finalization threw exceptions!",e);
210 public void updateJalview(IClientDocument cdoc)
212 Cache.log.debug("Jalview updating from sesion document ..");
214 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
216 vds.updateToJalview();
217 Cache.log.debug(".. finished updating from sesion document.");
220 private void ensureJvVamsas()
224 jv2vobj = new IdentityHashMap();
225 vobj2jv = new Hashtable();
230 * jalview object binding to VorbaIds
232 IdentityHashMap jv2vobj = null;
233 Hashtable vobj2jv = null;
234 public void updateVamsasDocument(IClientDocument doc)
237 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
239 // wander through frames
240 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
250 for (int i = frames.length - 1; i > -1; i--)
252 if (frames[i] instanceof AlignFrame)
254 AlignFrame af = (AlignFrame) frames[i];
256 // update alignment and root from frame.
257 vds.storeVAMSAS(af.getViewport(), af.getTitle());
261 for (int i = frames.length - 1; i > -1; i--)
263 if (frames[i] instanceof AlignFrame)
265 AlignFrame af = (AlignFrame) frames[i];
267 // add any AlignedCodonFrame mappings on this alignment to any other.
268 vds.storeSequenceMappings(af.getViewport(), af.getTitle());
274 Cache.log.error("Vamsas Document store exception", e);
278 private Entry baseProvEntry()
280 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
281 pentry.setUser(user.getFullName());
282 pentry.setApp(app.getClientUrn());
283 pentry.setDate(new java.util.Date());
284 pentry.setAction("created");
288 * do a vamsas document update or update jalview from the vamsas document
289 * @param fromJalview true to update from jalview to the vamsas document
291 protected void dealWithDocumentUpdate(boolean fromJalview)
293 // called by update handler for document update.
294 Cache.log.debug("Updating jalview from changed vamsas document.");
297 long time = System.currentTimeMillis();
298 IClientDocument cdoc = vclient.getClientDocument();
299 if (Cache.log.isDebugEnabled())
301 Cache.log.debug("Time taken to get ClientDocument = "+(System.currentTimeMillis()-time));
302 time = System.currentTimeMillis();
306 this.updateVamsasDocument(cdoc);
307 if (Cache.log.isDebugEnabled())
309 Cache.log.debug("Time taken to update Vamsas Document from jalview\t= "+(System.currentTimeMillis()-time));
310 time = System.currentTimeMillis();
312 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
313 if (Cache.log.isDebugEnabled())
315 Cache.log.debug("Time taken to set Document Roots\t\t= "+(System.currentTimeMillis()-time));
316 time = System.currentTimeMillis();
321 if (Cache.log.isDebugEnabled())
323 Cache.log.debug("Time taken to update Jalview from vamsas document Roots\t= "+(System.currentTimeMillis()-time));
324 time = System.currentTimeMillis();
328 vclient.updateDocument(cdoc);
329 if (Cache.log.isDebugEnabled())
331 Cache.log.debug("Time taken to update Session Document\t= "+(System.currentTimeMillis()-time));
332 time = System.currentTimeMillis();
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.");
342 private void addDocumentUpdateHandler()
344 final VamsasApplication client = this;
345 vclient.addDocumentUpdateHandler(new PropertyChangeListener() {
346 public void propertyChange(PropertyChangeEvent evt)
348 Cache.log.debug("Dealing with document update event.");
349 client.dealWithDocumentUpdate(false);
350 Cache.log.debug("finished dealing with event.");
353 Cache.log.debug("Added Jalview handler for vamsas document updates.");
355 public void disableGui(boolean b)
357 Desktop.instance.setVamsasUpdate(b);
359 private boolean joinedSession=false;
360 private VamsasListener picker=null;
361 private void startSession()
366 vclient.joinSession();
372 Cache.log.error("Failed to join vamsas session.",e);
376 final IPickManager pm = vclient.getPickManager();
377 final StructureSelectionManager ssm = StructureSelectionManager.getStructureSelectionManager();
378 pm.registerMessageHandler(new IMessageHandler() {
380 public void handleMessage(Message message)
382 if (message instanceof MouseOverMessage && vobj2jv!=null)
384 MouseOverMessage mm = (MouseOverMessage) message;
385 String mstring = mm.getVorbaID()+" "+mm.getPosition();
386 if (last!=null && mstring.equals(last))
390 //if (Cache.log.isDebugEnabled())
392 // Cache.log.debug("Received MouseOverMessage "+mm.getVorbaID()+" "+mm.getPosition());
394 Object jvobj = vobj2jv.get(mm.getVorbaID());
395 if (jvobj != null && jvobj instanceof SequenceI)
398 // Cache.log.debug("Handling Mouse over "+mm.getVorbaID()+" bound to "+jvobj+" at "+mm.getPosition());
399 // position is in sequence or in aligned sequence ???????
400 ssm.mouseOverVamsasSequence((SequenceI) jvobj, mm.getPosition());
405 picker = new VamsasListener() {
408 public void mouseOver(SequenceI seq, int index)
412 if (seq!=last || i!=index)
414 VorbaId v = (VorbaId) jv2vobj.get(seq);
417 Cache.log.debug("Mouse over "+v.getId()+" bound to "+seq+" at "+index);
420 MouseOverMessage message = new MouseOverMessage(v.getId(), index);
421 pm.sendMessage(message);
426 ssm.addStructureViewerListener(picker); // better method here
427 } catch (Exception e)
429 Cache.log.error("Failed to init Vamsas Picking",e);