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());
263 Cache.log.error("Vamsas Document store exception", e);
267 private Entry baseProvEntry()
269 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
270 pentry.setUser(user.getFullName());
271 pentry.setApp(app.getClientUrn());
272 pentry.setDate(new java.util.Date());
273 pentry.setAction("created");
277 * do a vamsas document update or update jalview from the vamsas document
278 * @param fromJalview true to update from jalview to the vamsas document
280 protected void dealWithDocumentUpdate(boolean fromJalview)
282 // called by update handler for document update.
283 Cache.log.debug("Updating jalview from changed vamsas document.");
286 long time = System.currentTimeMillis();
287 IClientDocument cdoc = vclient.getClientDocument();
288 if (Cache.log.isDebugEnabled())
290 Cache.log.debug("Time taken to get ClientDocument = "+(System.currentTimeMillis()-time));
291 time = System.currentTimeMillis();
295 this.updateVamsasDocument(cdoc);
296 if (Cache.log.isDebugEnabled())
298 Cache.log.debug("Time taken to update Vamsas Document from jalview\t= "+(System.currentTimeMillis()-time));
299 time = System.currentTimeMillis();
301 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
302 if (Cache.log.isDebugEnabled())
304 Cache.log.debug("Time taken to set Document Roots\t\t= "+(System.currentTimeMillis()-time));
305 time = System.currentTimeMillis();
310 if (Cache.log.isDebugEnabled())
312 Cache.log.debug("Time taken to update Jalview from vamsas document Roots\t= "+(System.currentTimeMillis()-time));
313 time = System.currentTimeMillis();
317 vclient.updateDocument(cdoc);
318 if (Cache.log.isDebugEnabled())
320 Cache.log.debug("Time taken to update Session Document\t= "+(System.currentTimeMillis()-time));
321 time = System.currentTimeMillis();
324 } catch (Exception ee) {
325 System.err.println("Exception whilst updating :");
326 ee.printStackTrace(System.err);
328 Cache.log.debug("Finished updating from document change.");
331 private void addDocumentUpdateHandler()
333 final VamsasApplication client = this;
334 vclient.addDocumentUpdateHandler(new PropertyChangeListener() {
335 public void propertyChange(PropertyChangeEvent evt)
337 Cache.log.debug("Dealing with document update event.");
338 client.dealWithDocumentUpdate(false);
339 Cache.log.debug("finished dealing with event.");
342 Cache.log.debug("Added Jalview handler for vamsas document updates.");
344 public void disableGui(boolean b)
346 Desktop.instance.setVamsasUpdate(b);
348 private boolean joinedSession=false;
349 private VamsasListener picker=null;
350 private void startSession()
355 vclient.joinSession();
361 Cache.log.error("Failed to join vamsas session.",e);
365 final IPickManager pm = vclient.getPickManager();
366 final StructureSelectionManager ssm = StructureSelectionManager.getStructureSelectionManager();
367 pm.registerMessageHandler(new IMessageHandler() {
369 public void handleMessage(Message message)
371 if (message instanceof MouseOverMessage && vobj2jv!=null)
373 MouseOverMessage mm = (MouseOverMessage) message;
374 String mstring = mm.getVorbaID()+" "+mm.getPosition();
375 if (last!=null && mstring.equals(last))
379 Object jvobj = vobj2jv.get(mm.getVorbaID());
380 if (jvobj != null && jvobj instanceof SequenceI)
383 // Cache.log.debug("Handling Mouse over "+mm.getVorbaID()+" bound to "+jvobj+" at "+mm.getPosition());
384 // position is in sequence or in aligned sequence ???????
385 ssm.mouseOverVamsasSequence((SequenceI) jvobj, mm.getPosition());
390 picker = new VamsasListener() {
393 public void mouseOver(SequenceI seq, int index)
395 if (seq!=last || i!=index)
397 VorbaId v = (VorbaId) jv2vobj.get(seq);
400 // Cache.log.debug("Mouse over "+v.getId()+" bound to "+seq+" at "+index);
403 MouseOverMessage message = new MouseOverMessage(v.getId(), index);
404 pm.sendMessage(message);
409 ssm.addStructureViewerListener(picker); // better method here
410 } catch (Exception e)
412 Cache.log.error("Failed to init Vamsas Picking",e);