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
43 public class DatastoreItem
48 Entry provEntry = null;
54 IdentityHashMap jv2vobj;
57 * @return the Vobject bound to Jalview datamodel object
59 protected Vobject getjv2vObj(Object jvobj)
61 if (jv2vobj.containsKey(jvobj))
63 return cdoc.getObject((VorbaId) jv2vobj.get(jvobj));
65 if (Cache.log.isDebugEnabled())
67 Cache.log.debug("Returning null VorbaID binding for jalview object "
76 * @return Jalview datamodel object bound to the vamsas document object
78 protected Object getvObj2jv(uk.ac.vamsas.client.Vobject vobj)
82 VorbaId id = vobj.getVorbaId();
85 id = cdoc.registerObject(vobj);
87 .debug("Registering new object and returning null for getvObj2jv");
90 if (vobj2jv.containsKey(vobj.getVorbaId()))
92 return vobj2jv.get(vobj.getVorbaId());
97 protected void bindjvvobj(Object jvobj, uk.ac.vamsas.client.Vobject vobj)
99 VorbaId id = vobj.getVorbaId();
102 id = cdoc.registerObject(vobj);
103 if (id == null || vobj.getVorbaId() == null
104 || cdoc.getObject(id) != vobj)
106 Cache.log.error("Failed to get id for "
107 + (vobj.isRegisterable() ? "registerable"
108 : "unregisterable") + " object " + vobj);
112 if (vobj2jv.containsKey(vobj.getVorbaId())
113 && !((VorbaId) vobj2jv.get(vobj.getVorbaId())).equals(jvobj))
116 "Warning? Overwriting existing vamsas id binding for "
117 + vobj.getVorbaId(), new Exception(
118 "Overwriting vamsas id binding."));
120 else if (jv2vobj.containsKey(jvobj)
121 && !((VorbaId) jv2vobj.get(jvobj)).equals(vobj.getVorbaId()))
124 "Warning? Overwriting existing jalview object binding for "
125 + jvobj, new Exception(
126 "Overwriting jalview object binding."));
129 * Cache.log.error("Attempt to make conflicting object binding! "+vobj+" id "
130 * +vobj.getVorbaId()+" already bound to "+getvObj2jv(vobj)+" and "+jvobj+"
131 * already bound to "+getjv2vObj(jvobj),new Exception("Excessive call to
134 // we just update the hash's regardless!
135 Cache.log.debug("Binding " + vobj.getVorbaId() + " to " + jvobj);
136 vobj2jv.put(vobj.getVorbaId(), jvobj);
137 // JBPNote - better implementing a hybrid invertible hash.
138 jv2vobj.put(jvobj, vobj.getVorbaId());
141 public DatastoreItem()
146 public DatastoreItem(VamsasAppDatastore datastore)
149 initDatastoreItem(datastore);
150 // TODO Auto-generated constructor stub
153 VamsasAppDatastore datastore = null;
155 public void initDatastoreItem(VamsasAppDatastore ds)
158 initDatastoreItem(ds.getProvEntry(), ds.getClientDocument(), ds
159 .getVamsasObjectBinding(), ds.getJvObjectBinding());
162 public void initDatastoreItem(Entry provEntry, IClientDocument cdoc,
163 Hashtable vobj2jv, IdentityHashMap jv2vobj)
165 this.provEntry = provEntry;
167 this.vobj2jv = vobj2jv;
168 this.jv2vobj = jv2vobj;
171 protected boolean isModifiable(String modifiable)
173 return modifiable == null; // TODO: USE VAMSAS LIBRARY OBJECT LOCK METHODS)
176 protected Vector getjv2vObjs(Vector alsq)
178 Vector vObjs = new Vector();
179 Enumeration elm = alsq.elements();
180 while (elm.hasMoreElements())
182 vObjs.addElement(getjv2vObj(elm.nextElement()));
189 * get start<end range of segment, adjusting for inclusivity flag and
193 * @param ensureDirection
194 * when true - always ensure start is less than end.
195 * @return int[] { start, end, direction} where direction==1 for range running
198 public int[] getSegRange(Seg visSeg, boolean ensureDirection)
200 boolean incl = visSeg.getInclusive();
201 // adjust for inclusive flag.
202 int pol = (visSeg.getStart() <= visSeg.getEnd()) ? 1 : -1; // polarity of
204 int start = visSeg.getStart() + (incl ? 0 : pol);
205 int end = visSeg.getEnd() + (incl ? 0 : -pol);
206 if (ensureDirection && pol == -1)
208 // jalview doesn't deal with inverted ranges, yet.
214 { start, end, pol < 0 ? 1 : 0 };
220 protected jalview.datamodel.Provenance getJalviewProvenance(
223 // TODO: fix App and Action entries and check use of provenance in jalview.
224 jalview.datamodel.Provenance jprov = new jalview.datamodel.Provenance();
225 for (int i = 0; i < prov.getEntryCount(); i++)
227 jprov.addEntry(prov.getEntry(i).getUser(), prov.getEntry(i)
228 .getAction(), prov.getEntry(i).getDate(), prov.getEntry(i)
237 * @return default initial provenance list for a Jalview created vamsas
240 Provenance dummyProvenance()
242 return dummyProvenance(null);
245 protected Entry dummyPEntry(String action)
247 Entry entry = new Entry();
248 entry.setApp(this.provEntry.getApp());
251 entry.setAction(action);
255 entry.setAction("created.");
257 entry.setDate(new java.util.Date());
258 entry.setUser(this.provEntry.getUser());
262 protected Provenance dummyProvenance(String action)
264 Provenance prov = new Provenance();
265 prov.addEntry(dummyPEntry(action));
269 protected void addProvenance(Provenance p, String action)
271 p.addEntry(dummyPEntry(action));