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();
60 * Start a new vamsas session
63 public VamsasApplication(Desktop jdesktop)
65 this.jdesktop = jdesktop;
66 initClientSession(null);
69 * init a connection to the session at the given url
73 public VamsasApplication(Desktop jdesktop, String sessionUrl)
75 this.jdesktop = jdesktop;
76 initClientSession(sessionUrl);
79 * @throws IOException or other if clientfactory instantiation failed.
80 * @return list of current sessions or null if no session exists.
82 public static String[] getSessionList() throws Exception {
83 return getClientFactory().getCurrentSessions();
86 * initialise, possibly with e valid session url
90 private boolean initClientSession(String sess) {
92 // Only need to tell the library what the application is here
93 app = new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache.getProperty("VERSION"));
94 uk.ac.vamsas.client.IClientFactory clientfactory = getClientFactory();
97 vclient = clientfactory.getNewSessionIClient(app);
101 vclient = clientfactory.getIClient(app,sess);
104 user = vclient.getUserHandle();
109 jalview.bin.Cache.log.error("Couldn't instantiate vamsas client !",e);
116 * @return true if we are registered in a vamsas session
118 public boolean inSession()
120 return (vclient!=null);
123 * called to connect to session
124 * inits handlers, does an initial document update.
126 public void initial_update()
130 throw new Error("Impementation error! Vamsas Operations when client not initialised and connected.");
132 addDocumentUpdateHandler();
134 Cache.log.debug("Jalview loading the Vamsas Session for the first time.");
135 dealWithDocumentUpdate(false); // we don't push an update out to the document yet.
136 Cache.log.debug("... finished update for the first time.");
139 * Update all windows after a vamsas datamodel change.
140 * this could go on the desktop object!
143 protected void updateJalviewGui()
145 JInternalFrame[] frames = jdesktop.getAllFrames();
155 for (int i = frames.length - 1; i > -1; i--)
157 if (frames[i] instanceof AlignFrame)
159 AlignFrame af = (AlignFrame) frames[i];
160 af.alignPanel.alignmentChanged();
167 "Exception whilst refreshing jalview windows after a vamsas document update.",
171 public void push_update()
173 Cache.log.info("Jalview updating to the Vamsas Session.");
174 dealWithDocumentUpdate(true);
176 IClientDocument cdoc=null;
179 cdoc = vclient.getClientDocument();
183 Cache.log.error("Failed to get client document for update.");
184 // RAISE A WARNING DIALOG
188 updateVamsasDocument(cdoc);
190 cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags back
191 vclient.updateDocument(cdoc);
193 Cache.log.info("Jalview finished updating to the Vamsas Session.");
196 public void end_session()
199 throw new Error("Jalview not connected to Vamsas session.");
200 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
204 vclient.finalizeClient();
205 Cache.log.info("Jalview has left the session.");
207 Cache.log.warn("JV Client leaving a session that's its not joined yet.");
211 app=null; user = null;
217 Cache.log.error("Vamsas Session finalization threw exceptions!",e);
221 public void updateJalview(IClientDocument cdoc)
223 Cache.log.debug("Jalview updating from sesion document ..");
225 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
227 vds.updateToJalview();
228 Cache.log.debug(".. finished updating from sesion document.");
231 private void ensureJvVamsas()
235 jv2vobj = new IdentityHashMap();
236 vobj2jv = new Hashtable();
241 * jalview object binding to VorbaIds
243 IdentityHashMap jv2vobj = null;
244 Hashtable vobj2jv = null;
245 public void updateVamsasDocument(IClientDocument doc)
248 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
250 // wander through frames
251 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
261 for (int i = frames.length - 1; i > -1; i--)
263 if (frames[i] instanceof AlignFrame)
265 AlignFrame af = (AlignFrame) frames[i];
267 // update alignment and root from frame.
268 vds.storeVAMSAS(af.getViewport(), af.getTitle());
272 for (int i = frames.length - 1; i > -1; i--)
274 if (frames[i] instanceof AlignFrame)
276 AlignFrame af = (AlignFrame) frames[i];
278 // add any AlignedCodonFrame mappings on this alignment to any other.
279 vds.storeSequenceMappings(af.getViewport(), af.getTitle());
285 Cache.log.error("Vamsas Document store exception", e);
289 private Entry baseProvEntry()
291 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
292 pentry.setUser(user.getFullName());
293 pentry.setApp(app.getClientUrn());
294 pentry.setDate(new java.util.Date());
295 pentry.setAction("created");
299 * do a vamsas document update or update jalview from the vamsas document
300 * @param fromJalview true to update from jalview to the vamsas document
302 protected void dealWithDocumentUpdate(boolean fromJalview)
304 // called by update handler for document update.
305 Cache.log.debug("Updating jalview from changed vamsas document.");
308 long time = System.currentTimeMillis();
309 IClientDocument cdoc = vclient.getClientDocument();
310 if (Cache.log.isDebugEnabled())
312 Cache.log.debug("Time taken to get ClientDocument = "+(System.currentTimeMillis()-time));
313 time = System.currentTimeMillis();
317 this.updateVamsasDocument(cdoc);
318 if (Cache.log.isDebugEnabled())
320 Cache.log.debug("Time taken to update Vamsas Document from jalview\t= "+(System.currentTimeMillis()-time));
321 time = System.currentTimeMillis();
323 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
324 if (Cache.log.isDebugEnabled())
326 Cache.log.debug("Time taken to set Document Roots\t\t= "+(System.currentTimeMillis()-time));
327 time = System.currentTimeMillis();
332 if (Cache.log.isDebugEnabled())
334 Cache.log.debug("Time taken to update Jalview from vamsas document Roots\t= "+(System.currentTimeMillis()-time));
335 time = System.currentTimeMillis();
339 vclient.updateDocument(cdoc);
340 if (Cache.log.isDebugEnabled())
342 Cache.log.debug("Time taken to update Session Document\t= "+(System.currentTimeMillis()-time));
343 time = System.currentTimeMillis();
346 } catch (Exception ee) {
347 System.err.println("Exception whilst updating :");
348 ee.printStackTrace(System.err);
350 Cache.log.debug("Finished updating from document change.");
353 private void addDocumentUpdateHandler()
355 final VamsasApplication client = this;
356 vclient.addDocumentUpdateHandler(new PropertyChangeListener() {
357 public void propertyChange(PropertyChangeEvent evt)
359 Cache.log.debug("Dealing with document update event.");
360 client.dealWithDocumentUpdate(false);
361 Cache.log.debug("finished dealing with event.");
364 Cache.log.debug("Added Jalview handler for vamsas document updates.");
366 public void disableGui(boolean b)
368 Desktop.instance.setVamsasUpdate(b);
370 private boolean joinedSession=false;
371 private VamsasListener picker=null;
372 private void startSession()
377 vclient.joinSession();
383 Cache.log.error("Failed to join vamsas session.",e);
387 final IPickManager pm = vclient.getPickManager();
388 final StructureSelectionManager ssm = StructureSelectionManager.getStructureSelectionManager();
389 pm.registerMessageHandler(new IMessageHandler() {
391 public void handleMessage(Message message)
393 if (message instanceof MouseOverMessage && vobj2jv!=null)
395 MouseOverMessage mm = (MouseOverMessage) message;
396 String mstring = mm.getVorbaID()+" "+mm.getPosition();
397 if (last!=null && mstring.equals(last))
401 //if (Cache.log.isDebugEnabled())
403 // Cache.log.debug("Received MouseOverMessage "+mm.getVorbaID()+" "+mm.getPosition());
405 Object jvobj = vobj2jv.get(mm.getVorbaID());
406 if (jvobj != null && jvobj instanceof SequenceI)
409 // Cache.log.debug("Handling Mouse over "+mm.getVorbaID()+" bound to "+jvobj+" at "+mm.getPosition());
410 // position is in sequence or in aligned sequence ???????
411 ssm.mouseOverVamsasSequence((SequenceI) jvobj, mm.getPosition());
416 picker = new VamsasListener() {
419 public void mouseOver(SequenceI seq, int index)
423 if (seq!=last || i!=index)
425 VorbaId v = (VorbaId) jv2vobj.get(seq);
428 Cache.log.debug("Mouse over "+v.getId()+" bound to "+seq+" at "+index);
431 MouseOverMessage message = new MouseOverMessage(v.getId(), index);
432 pm.sendMessage(message);
437 ssm.addStructureViewerListener(picker); // better method here
438 } catch (Exception e)
440 Cache.log.error("Failed to init Vamsas Picking",e);