6 import java.beans.PropertyChangeEvent;
7 import java.beans.PropertyChangeListener;
9 import java.io.IOException;
10 import java.io.OutputStreamWriter;
11 import java.io.PrintWriter;
12 import java.util.Hashtable;
13 import java.util.IdentityHashMap;
14 import java.util.Vector;
15 import java.util.jar.JarOutputStream;
17 import javax.swing.JInternalFrame;
19 import jalview.bin.Cache;
20 import jalview.io.VamsasAppDatastore;
22 import uk.ac.vamsas.client.*;
23 import uk.ac.vamsas.*;
24 import uk.ac.vamsas.objects.*;
25 import uk.ac.vamsas.objects.core.Entry;
30 public class VamsasApplication
33 ClientHandle app=null;
36 Desktop jdesktop = null; // our jalview desktop reference
38 // Cache.preferences for vamsas client session arena
39 // preferences for check for default session at startup.
40 // user and organisation stuff.
41 public VamsasApplication(Desktop jdesktop,
45 // we should create a session URI from the sessionPath and pass it to
46 // the clientFactory - but the vamsas api doesn't cope with that yet.
48 throw new Error("Sorry - can't start from session file yet."); // make this a warning
50 private static uk.ac.vamsas.client.IClientFactory getClientFactory() throws IOException {
51 return new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
53 public VamsasApplication(Desktop jdesktop)
55 this.jdesktop = jdesktop;
56 initClientSession(null);
59 * @throws IOException or other if clientfactory instantiation failed.
60 * @return list of current sessions or null if no session exists.
62 public static String[] getSessionList() throws Exception {
63 return getClientFactory().getCurrentSessions();
66 * initialise, possibly with e valid session url
70 private boolean initClientSession(String sess) {
72 // Only need to tell the library what the application is here
73 app = new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache.getProperty("VERSION"));
74 uk.ac.vamsas.client.IClientFactory clientfactory = getClientFactory();
77 vclient = clientfactory.getIClient(app);
81 vclient = clientfactory.getIClient(app,sess);
84 user = vclient.getUserHandle();
86 } catch (NoDefaultSessionException e)
92 jalview.bin.Cache.log.error("Couldn't instantiate vamsas client !",e);
99 * @return true if we are registered in a vamsas session
101 public boolean inSession()
103 return (vclient!=null);
106 * called to connect to session
107 * inits handlers, does an initial document update.
109 public void initial_update()
113 throw new Error("Impementation error! Vamsas Operations when client not initialised and connected.");
115 addDocumentUpdateHandler();
117 Cache.log.debug("Jalview loading the Vamsas Session for the first time.");
118 dealWithDocumentUpdate(false); // we don't push an update out to the document yet.
119 Cache.log.debug("... finished update for the first time.");
122 * Update all windows after a vamsas datamodel change.
123 * this could go on the desktop object!
126 protected void updateJalviewGui()
128 JInternalFrame[] frames = jdesktop.getAllFrames();
138 for (int i = frames.length - 1; i > -1; i--)
140 if (frames[i] instanceof AlignFrame)
142 AlignFrame af = (AlignFrame) frames[i];
143 af.alignPanel.alignmentChanged();
150 "Exception whilst refreshing jalview windows after a vamsas document update.",
154 public void push_update()
156 Cache.log.info("Jalview updating to the Vamsas Session.");
157 dealWithDocumentUpdate(true);
159 IClientDocument cdoc=null;
162 cdoc = vclient.getClientDocument();
166 Cache.log.error("Failed to get client document for update.");
167 // RAISE A WARNING DIALOG
171 updateVamsasDocument(cdoc);
173 cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags back
174 vclient.updateDocument(cdoc);
176 Cache.log.info("Jalview finished updating to the Vamsas Session.");
179 public void end_session()
182 throw new Error("Jalview not connected to Vamsas session.");
183 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
187 vclient.finalizeClient();
188 Cache.log.info("Jalview has left the session.");
190 Cache.log.warn("JV Client leaving a session that's its not joined yet.");
194 app=null; user = null;
200 Cache.log.error("Vamsas Session finalization threw exceptions!",e);
204 public void updateJalview(IClientDocument cdoc)
206 Cache.log.debug("Jalview updating from sesion document ..");
208 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
210 vds.updateToJalview();
211 Cache.log.debug(".. finished updating from sesion document.");
214 private void ensureJvVamsas()
218 jv2vobj = new IdentityHashMap();
219 vobj2jv = new Hashtable();
224 * jalview object binding to VorbaIds
226 IdentityHashMap jv2vobj = null;
227 Hashtable vobj2jv = null;
228 public void updateVamsasDocument(IClientDocument doc)
231 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
233 // wander through frames
234 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
244 for (int i = frames.length - 1; i > -1; i--)
246 if (frames[i] instanceof AlignFrame)
248 AlignFrame af = (AlignFrame) frames[i];
250 // update alignment and root from frame.
251 vds.storeVAMSAS(af.getViewport(), af.getTitle());
257 Cache.log.error("Vamsas Document store exception", e);
261 private Entry baseProvEntry()
263 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
264 pentry.setUser(user.getFullName());
265 pentry.setApp(app.getClientUrn());
266 pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
267 pentry.setAction("created");
271 * do a vamsas document update or update jalview from the vamsas document
272 * @param fromJalview true to update from jalview to the vamsas document
274 protected void dealWithDocumentUpdate(boolean fromJalview)
276 // called by update handler for document update.
277 Cache.log.debug("Updating jalview from changed vamsas document.");
280 IClientDocument cdoc = vclient.getClientDocument();
283 this.updateVamsasDocument(cdoc);
284 cdoc.setVamsasRoots(cdoc.getVamsasRoots());
289 vclient.updateDocument(cdoc);
291 } catch (Exception ee) {
292 System.err.println("Exception whilst updating :");
293 ee.printStackTrace(System.err);
295 Cache.log.debug("Finished updating from document change.");
298 private void addDocumentUpdateHandler()
300 final VamsasApplication client = this;
301 vclient.addDocumentUpdateHandler(new PropertyChangeListener() {
302 public void propertyChange(PropertyChangeEvent evt)
304 Cache.log.debug("Dealing with document update event.");
305 client.dealWithDocumentUpdate(false);
306 Cache.log.debug("finished dealing with event.");
309 Cache.log.debug("Added Jalview handler for vamsas document updates.");
311 public void disableGui(boolean b)
313 Desktop.instance.setVamsasUpdate(b);
315 private boolean joinedSession=false;
316 private void startSession()
321 vclient.joinSession();
327 Cache.log.error("Failed to join vamsas session.",e);