2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.io.vamsas;
21 import jalview.bin.Cache;
22 import jalview.gui.TreePanel;
23 import jalview.io.VamsasAppDatastore;
25 import java.util.Enumeration;
26 import java.util.Hashtable;
27 import java.util.IdentityHashMap;
28 import java.util.Vector;
30 import uk.ac.vamsas.client.IClientDocument;
31 import uk.ac.vamsas.client.Vobject;
32 import uk.ac.vamsas.client.VorbaId;
33 import uk.ac.vamsas.objects.core.Entry;
34 import uk.ac.vamsas.objects.core.Provenance;
35 import uk.ac.vamsas.objects.core.Seg;
38 * Holds all the common machinery for binding objects to vamsas objects
42 public class DatastoreItem
47 Entry provEntry = null;
53 IdentityHashMap jv2vobj;
55 * @return the Vobject bound to Jalview datamodel object
57 protected Vobject getjv2vObj(Object jvobj)
59 if (jv2vobj.containsKey(jvobj))
61 return cdoc.getObject( (VorbaId) jv2vobj.get(jvobj));
63 if (Cache.log.isDebugEnabled())
65 Cache.log.debug("Returning null VorbaID binding for jalview object "+jvobj);
73 * @return Jalview datamodel object bound to the vamsas document object
75 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
79 VorbaId id = vobj.getVorbaId();
82 id = cdoc.registerObject(vobj);
84 .debug("Registering new object and returning null for getvObj2jv");
87 if (vobj2jv.containsKey(vobj.getVorbaId()))
89 return vobj2jv.get(vobj.getVorbaId());
94 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
96 VorbaId id = vobj.getVorbaId();
99 id = cdoc.registerObject(vobj);
100 if (id == null || vobj.getVorbaId() == null || cdoc.getObject(id)!=vobj)
102 Cache.log.error("Failed to get id for " +
103 (vobj.isRegisterable() ? "registerable" :
104 "unregisterable") + " object " + vobj);
108 if (vobj2jv.containsKey(vobj.getVorbaId()) &&
109 ! ( (VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
111 Cache.log.debug("Warning? Overwriting existing vamsas id binding for " +
113 new Exception("Overwriting vamsas id binding."));
115 else if (jv2vobj.containsKey(jvobj) &&
116 ! ( (VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
119 "Warning? Overwriting existing jalview object binding for " + jvobj,
120 new Exception("Overwriting jalview object binding."));
122 /* Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id " +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+" already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to bindjvvobj"));
124 // we just update the hash's regardless!
125 Cache.log.debug("Binding "+vobj.getVorbaId()+" to "+jvobj);
126 vobj2jv.put(vobj.getVorbaId(), jvobj);
127 // JBPNote - better implementing a hybrid invertible hash.
128 jv2vobj.put(jvobj, vobj.getVorbaId());
131 public DatastoreItem() {
134 public DatastoreItem(VamsasAppDatastore datastore)
137 initDatastoreItem(datastore);
138 // TODO Auto-generated constructor stub
140 VamsasAppDatastore datastore = null;
141 public void initDatastoreItem(VamsasAppDatastore ds)
144 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(), ds.getVamsasObjectBinding(), ds.getJvObjectBinding());
146 public void initDatastoreItem(Entry provEntry, IClientDocument cdoc, Hashtable vobj2jv, IdentityHashMap jv2vobj)
148 this.provEntry = provEntry;
150 this.vobj2jv = vobj2jv;
151 this.jv2vobj = jv2vobj;
154 protected boolean isModifiable(String modifiable)
156 return modifiable==null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
159 protected Vector getjv2vObjs(Vector alsq)
161 Vector vObjs = new Vector();
162 Enumeration elm = alsq.elements();
163 while (elm.hasMoreElements())
165 vObjs.addElement(getjv2vObj(elm.nextElement()));
171 * get start<end range of segment, adjusting for inclusivity flag and
175 * @param ensureDirection when true - always ensure start is less than end.
176 * @return int[] { start, end, direction} where direction==1 for range running from end to start.
178 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
180 boolean incl = visSeg.getInclusive();
181 // adjust for inclusive flag.
182 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
184 int start = visSeg.getStart() + (incl ? 0 : pol);
185 int end = visSeg.getEnd() + (incl ? 0 : -pol);
186 if (ensureDirection && pol == -1)
188 // jalview doesn't deal with inverted ranges, yet.
195 start, end, pol < 0 ? 1 : 0};
200 protected jalview.datamodel.Provenance getJalviewProvenance(Provenance prov)
202 // TODO: fix App and Action entries and check use of provenance in jalview.
203 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
204 for (int i = 0; i < prov.getEntryCount(); i++)
206 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i).getAction(),
207 prov.getEntry(i).getDate(),
208 prov.getEntry(i).getId());
216 * @return default initial provenance list for a Jalview created vamsas
219 Provenance dummyProvenance()
221 return dummyProvenance(null);
224 protected Entry dummyPEntry(String action)
226 Entry entry = new Entry();
227 entry.setApp(this.provEntry.getApp());
230 entry.setAction(action);
234 entry.setAction("created.");
236 entry.setDate(new java.util.Date());
237 entry.setUser(this.provEntry.getUser());
241 protected Provenance dummyProvenance(String action)
243 Provenance prov = new Provenance();
244 prov.addEntry(dummyPEntry(action));
248 protected void addProvenance(Provenance p, String action)
250 p.addEntry(dummyPEntry(action));