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;
21 import jalview.io.VamsasDatastore;
23 import uk.ac.vamsas.client.*;
24 import uk.ac.vamsas.*;
25 import uk.ac.vamsas.objects.*;
26 import uk.ac.vamsas.objects.core.Entry;
31 public class VamsasApplication
34 ClientHandle app=null;
37 Desktop jdesktop = null; // our jalview desktop reference
39 // Cache.preferences for vamsas client session arena
40 // preferences for check for default session at startup.
41 // user and organisation stuff.
42 public VamsasApplication(Desktop jdesktop,
46 // we should create a session URI from the sessionPath and pass it to
47 // the clientFactory - but the vamsas api doesn't cope with that yet.
49 throw new Error("Sorry - can't start from session file yet."); // make this a warning
51 private static uk.ac.vamsas.client.IClientFactory getClientFactory() throws IOException {
52 return new uk.ac.vamsas.client.simpleclient.SimpleClientFactory();
54 public VamsasApplication(Desktop jdesktop)
56 this.jdesktop = jdesktop;
57 initClientSession(null);
60 * @throws IOException or other if clientfactory instantiation failed.
61 * @return list of current sessions or null if no session exists.
63 public static String[] getSessionList() throws Exception {
64 return getClientFactory().getCurrentSessions();
67 * initialise, possibly with e valid session url
71 private boolean initClientSession(String sess) {
73 // Only need to tell the library what the application is here
74 app = new ClientHandle("jalview.bin.Jalview", jalview.bin.Cache.getProperty("VERSION"));
75 uk.ac.vamsas.client.IClientFactory clientfactory = getClientFactory();
78 vclient = clientfactory.getIClient(app);
82 vclient = clientfactory.getIClient(app,sess);
85 user = vclient.getUserHandle();
87 } catch (NoDefaultSessionException e)
93 jalview.bin.Cache.log.error("Couldn't instantiate vamsas client !",e);
100 * @return true if we are registered in a vamsas session
102 public boolean inSession()
104 return (vclient!=null);
107 * called to connect to session
108 * inits handlers, does an initial document update.
110 public void initial_update()
114 throw new Error("Impementation error! Vamsas Operations when client not initialised and connected.");
116 addDocumentUpdateHandler();
118 Cache.log.debug("Jalview loading the Vamsas Session for the first time.");
119 dealWithDocumentUpdate();
120 Cache.log.debug("... finished update for the first time.");
123 * Update all windows after a vamsas datamodel change.
124 * this could go on the desktop object!
127 protected void updateJalviewGui()
129 JInternalFrame[] frames = jdesktop.getAllFrames();
139 for (int i = frames.length - 1; i > -1; i--)
141 if (frames[i] instanceof AlignFrame)
143 AlignFrame af = (AlignFrame) frames[i];
144 af.alignPanel.alignmentChanged();
151 "Exception whilst refreshing jalview windows after a vamsas document update.",
155 public void push_update()
157 Cache.log.info("Jalview updating to the Vamsas Session.");
158 dealWithDocumentUpdate();
160 IClientDocument cdoc=null;
163 cdoc = vclient.getClientDocument();
167 Cache.log.error("Failed to get client document for update.");
168 // RAISE A WARNING DIALOG
172 updateVamsasDocument(cdoc);
174 cdoc.setVamsasRoots(cdoc.getVamsasRoots()); // propagate update flags back
175 vclient.updateDocument(cdoc);
177 Cache.log.info("Jalview finished updating to the Vamsas Session.");
180 public void end_session()
183 throw new Error("Jalview not connected to Vamsas session.");
184 Cache.log.info("Jalview disconnecting from the Vamsas Session.");
188 vclient.finalizeClient();
189 Cache.log.info("Jalview has left the session.");
191 Cache.log.warn("JV Client leaving a session that's its not joined yet.");
195 app=null; user = null;
201 Cache.log.error("Vamsas Session finalization threw exceptions!",e);
205 public void updateJalview(IClientDocument cdoc)
207 Cache.log.debug("Jalview updating from sesion document ..");
209 VamsasAppDatastore vds = new VamsasAppDatastore(cdoc, vobj2jv, jv2vobj,
211 vds.updateToJalview();
212 Cache.log.debug(".. finished updating from sesion document.");
215 private void ensureJvVamsas()
219 jv2vobj = new IdentityHashMap();
220 vobj2jv = new Hashtable();
225 * jalview object binding to VorbaIds
227 IdentityHashMap jv2vobj = null;
228 Hashtable vobj2jv = null;
229 public void updateVamsasDocument(IClientDocument doc)
232 VamsasAppDatastore vds = new VamsasAppDatastore(doc, vobj2jv, jv2vobj,
234 // wander through frames
235 JInternalFrame[] frames = Desktop.desktop.getAllFrames();
245 for (int i = frames.length - 1; i > -1; i--)
247 if (frames[i] instanceof AlignFrame)
249 AlignFrame af = (AlignFrame) frames[i];
251 // update alignment and root from frame.
252 vds.storeVAMSAS(af.getViewport(), af.getTitle());
258 Cache.log.error("Vamsas Document store exception", e);
262 private Entry baseProvEntry()
264 uk.ac.vamsas.objects.core.Entry pentry = new uk.ac.vamsas.objects.core.Entry();
265 pentry.setUser(user.getFullName());
266 pentry.setApp(app.getClientUrn());
267 pentry.setDate(new org.exolab.castor.types.Date(new java.util.Date()));
268 pentry.setAction("created");
271 protected void dealWithDocumentUpdate()
273 // called by update handler for document update.
274 Cache.log.debug("Updating jalview from changed vamsas document.");
277 IClientDocument cdoc = vclient.getClientDocument();
279 // cdoc.setVamsasRoots(cdoc.getVamsasRoots());
280 vclient.updateDocument(cdoc);
282 } catch (Exception ee) {
283 System.err.println("Exception whilst updating :");
284 ee.printStackTrace(System.err);
286 Cache.log.debug("Finished updating from document change.");
289 private void addDocumentUpdateHandler()
291 final VamsasApplication client = this;
292 vclient.addDocumentUpdateHandler(new PropertyChangeListener() {
293 public void propertyChange(PropertyChangeEvent evt)
295 Cache.log.debug("Dealing with document update event.");
296 client.dealWithDocumentUpdate();
297 Cache.log.debug("finished dealing with event.");
300 Cache.log.debug("Added Jalview handler for vamsas document updates.");
302 public void disableGui(boolean b)
304 Desktop.instance.setVamsasUpdate(b);
306 private boolean joinedSession=false;
307 private void startSession()
312 vclient.joinSession();
318 Cache.log.error("Failed to join vamsas session.",e);