From ca22857ffc5d0f1fc725dfe45cfe66057051d566 Mon Sep 17 00:00:00 2001 From: jprocter Date: Wed, 29 Feb 2012 16:20:12 +0000 Subject: [PATCH] (JAL-972) replaced biodas with jdas sources datastructures - refactored source list model to a sourceregistry object --- src/jalview/gui/DasSourceBrowser.java | 517 ++++++++------------------------- 1 file changed, 129 insertions(+), 388 deletions(-) diff --git a/src/jalview/gui/DasSourceBrowser.java b/src/jalview/gui/DasSourceBrowser.java index faf4712..9edb8b0 100755 --- a/src/jalview/gui/DasSourceBrowser.java +++ b/src/jalview/gui/DasSourceBrowser.java @@ -17,65 +17,50 @@ */ package jalview.gui; -import java.text.ParseException; -import java.util.*; +import jalview.jbgui.GDasSourceBrowser; +import jalview.util.TableSorter; +import jalview.ws.dbsources.das.api.DasSourceRegistryI; +import jalview.ws.dbsources.das.api.jalviewSourceI; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.util.ArrayList; import java.util.List; +import java.util.Properties; +import java.util.StringTokenizer; +import java.util.Vector; + +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.ListSelectionModel; +import javax.swing.SwingUtilities; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.table.AbstractTableModel; -import java.awt.*; -import java.awt.event.*; -import javax.swing.*; -import javax.swing.event.*; -import javax.swing.table.*; - -import org.biodas.jdas.dassources.Capabilities; -import org.biodas.jdas.dassources.utils.DasCoordinateSystemCollector; -import org.biodas.jdas.dassources.utils.DasTimeFormat; -import org.biodas.jdas.schema.registrycoordinates.DASCOORDINATESYSTEM; import org.biodas.jdas.schema.sources.CAPABILITY; import org.biodas.jdas.schema.sources.COORDINATES; -import org.biodas.jdas.schema.sources.MAINTAINER; import org.biodas.jdas.schema.sources.PROP; -import org.biodas.jdas.schema.sources.SOURCE; -import org.biodas.jdas.schema.sources.SOURCES; import org.biodas.jdas.schema.sources.VERSION; -import jalview.jbgui.*; -import jalview.util.*; - public class DasSourceBrowser extends GDasSourceBrowser implements Runnable, ListSelectionListener { - static org.biodas.jdas.schema.sources.SOURCE[] dasSources = null; - - Hashtable localSources = null; - - Vector selectedSources; - - public static String DEFAULT_REGISTRY = "http://www.dasregistry.org/das1/sources/"; + DasSourceRegistryI sourceRegistry = null; - /** - * true if thread is running and we are talking to DAS registry service - */ - public boolean loadingDasSources = false; - - protected static String getDasRegistryURL() - { - String registry = jalview.bin.Cache.getDefault("DAS_REGISTRY_URL", - DEFAULT_REGISTRY); - - if (registry.indexOf("/registry/das1/sources/") > -1) - { - jalview.bin.Cache.setProperty(jalview.bin.Cache.DAS_REGISTRY_URL, - DEFAULT_REGISTRY); - registry = DEFAULT_REGISTRY; - } - return registry; - } + Vector selectedSources; public DasSourceBrowser(FeatureSettings featureSettings) { fs = featureSettings; - String registry = getDasRegistryURL(); + // TODO DasSourceRegistryProvider API + sourceRegistry = jalview.bin.Cache.getDasSourceRegistry(); + String registry = sourceRegistry.getDasRegistryURL(); registryURL.setText(registry); @@ -115,7 +100,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } }); - if (dasSources != null) + if (sourceRegistry.getSources() != null) { init(); } @@ -123,6 +108,8 @@ public class DasSourceBrowser extends GDasSourceBrowser implements FeatureSettings fs = null; + private boolean loadingDasSources; + public DasSourceBrowser() { this(null); @@ -130,7 +117,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements public void paintComponent(java.awt.Graphics g) { - if (dasSources == null && !loadingDasSources) + if (sourceRegistry == null) { Thread worker = new Thread(this); worker.start(); @@ -139,17 +126,17 @@ public class DasSourceBrowser extends GDasSourceBrowser implements void init() { - int dSize = dasSources.length; + List sources = sourceRegistry.getSources(); + int dSize = sources.size(); Object[][] data = new Object[dSize][2]; for (int i = 0; i < dSize; i++) { - data[i][0] = dasSources[i].getTitle(); // what's equivalent of nickname - data[i][1] = new Boolean(selectedSources.contains(dasSources[i] - .getTitle())); + data[i][0] = sources.get(i).getTitle(); // what's equivalent of nickname + data[i][1] = new Boolean(selectedSources.contains(sources.get(i).getTitle())); } refreshTableData(data); - setCapabilities(dasSources); + setCapabilities(sourceRegistry); javax.swing.SwingUtilities.invokeLater(new Runnable() { @@ -187,27 +174,30 @@ public class DasSourceBrowser extends GDasSourceBrowser implements return; } - int dSize = dasSources.length; - for (int i = 0; i < dSize; i++) + int dSize = sourceRegistry.getSources().size(); + for (jalviewSourceI ds : sourceRegistry.getSources()) { - if (!dasSources[i].getTitle().equals(nickName)) + if (!ds.getTitle().equals(nickName)) { continue; } - SOURCE ds = dasSources[i]; - VERSION latest = getVersionFor(ds); - text.append("Id: " - + dasSources[i].getUri() + "
"); + VERSION latest = ds.getVersion(); + text.append("Id: " + ds.getUri() + + "
"); text.append("Nickname: " - + dasSources[i].getTitle() + "
"); - text.append("URL: " + latest.getUri() + + ds.getTitle() + "
"); + + if (!ds.isLocal()) + { + // TODO: IMMEDIATE verify that URI + ds.URI point to latest version for non-local sources. + if (ds.getDocHref()!=null) { + text.append("URL: " + ds.getDocHref() + "
"); - + } + text.append("Admin Email: " - + ds.getMAINTAINER().getEmail() + "" + "
"); + + ds.getEmail() + "\">" + ds.getEmail() + "" + "
"); text.append("Registered at: " + latest.getCreated() + "
"); @@ -215,7 +205,9 @@ public class DasSourceBrowser extends GDasSourceBrowser implements // TODO: Identify last successful test date // text.append("Last successful test: " // + latest.dasSources[i].getLeaseDate() + "
"); - + } else { + text.append("Source was added manually.
"); + } text.append("Labels: "); boolean b = false; for (PROP labl : latest.getPROP()) @@ -239,7 +231,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements CAPABILITY[] scap = latest.getCAPABILITY().toArray(new CAPABILITY[0]); for (int j = 0; j < scap.length; j++) { - text.append(scap[j]); + text.append(scap[j].getType()); if (j < scap.length - 1) { text.append(", "); @@ -257,7 +249,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements { text.append(", " + dcs.getTaxid()); } - if (dcs.getVersion().trim().length() > 0) + if (dcs.getVersion()!=null && dcs.getVersion().trim().length() > 0) { { text.append(", " + dcs.getVersion()); @@ -267,14 +259,12 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } text.append("Description: " - + dasSources[i].getDescription() + "
"); + + ds.getDescription() + "
"); - if (dasSources[i].getDocHref() != null - && dasSources[i].getDocHref().length() > 0) + if (ds.getDocHref() != null && ds.getDocHref().length() > 0) { text.append("Go to site"); + + ds.getDocHref() + "\">Go to site"); } text.append(""); @@ -302,8 +292,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements progressBar.setIndeterminate(true); setParentGuiEnabled(false); // Refresh the source list. - dasSources = null; - getDASSource(); + sourceRegistry.refreshSources(); init(); @@ -321,10 +310,10 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } } - public Vector getSelectedSources() + public Vector getSelectedSources() { // wait around if we're still loading. - while (dasSources == null) + while (sourceRegistry == null) { if (!loadingDasSources) { @@ -350,38 +339,18 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } } - Vector selected = new Vector(); - for (int r = 0; r < selectedSources.size(); r++) + Vector selected = new Vector(); + for (String source : selectedSources) { - for (int i = 0; i < dasSources.length; i++) + jalviewSourceI srce = sourceRegistry.getSource(source); + if (srce != null) { - if (dasSources[i].getTitle().equals(selectedSources.elementAt(r))) - { - selected.addElement(dasSources[i]); - break; - } + selected.addElement(srce); } } - return selected; } - /** - * retrieve das sources from registry and add local source list - * - * @return - */ - public SOURCE[] getDASSource() - { - if (dasSources == null) - { - dasSources = jalview.ws.DasSequenceFeatureFetcher.getDASSources(); - appendLocalSources(); - } - - return dasSources; - } - public void refresh_actionPerformed(ActionEvent e) { saveProperties(jalview.bin.Cache.applicationProperties); @@ -390,59 +359,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements worker.start(); } - private boolean isLaterThan(String ref, String newer) - { - Date refdate = null, newdate = null; - try - { - refdate = DasTimeFormat.fromDASString(ref); - - } catch (ParseException x) - { - return false; - } - try - { - newdate = DasTimeFormat.fromDASString(newer); - } catch (ParseException e) - { - // TODO: handle exception - } - if (refdate != null) - { - if (newdate != null) - { - return refdate.before(newdate); - } - return false; - } - if (newdate != null) - { - return true; - } - // assume first instance of source is newest in list. - TODO: check if - // natural ordering of source versions is newest first or oldest first - return false; - } - - private VERSION getVersionFor(SOURCE ds) - { - VERSION latest = null; - for (VERSION v : ds.getVERSION()) - { - if (latest == null - || isLaterThan(latest.getCreated(), v.getCreated())) - { - // TODO: das 1.6 - should just get the first version - ignore other - // versions since not specified how to construct URL from version's URI - // + source URI - latest = v; - } - } - return latest; - } - - private void setCapabilities(SOURCE[] sources) + private void setCapabilities(DasSourceRegistryI sourceRegistry2) { Vector authority = new Vector(); Vector type = new Vector(); @@ -452,10 +369,9 @@ public class DasSourceBrowser extends GDasSourceBrowser implements type.add("Any"); label.add("Any"); - for (int i = 0; i < sources.length; i++) + for (jalviewSourceI ds : sourceRegistry2.getSources()) { - SOURCE ds = sources[i]; - VERSION latest = getVersionFor(ds); + VERSION latest = ds.getVersion(); for (COORDINATES cs : latest.getCOORDINATES()) { @@ -505,9 +421,9 @@ public class DasSourceBrowser extends GDasSourceBrowser implements { int selectedRow = table.getSelectionModel().getMinSelectionIndex(); nickname = table.getValueAt(selectedRow, 0).toString(); - url = ((SOURCE) localSources.get(nickname)).getUri(); - seqsrc = ((SOURCE) localSources.get(nickname)).getVERSION().get(0) - .getCAPABILITY().contains(Capabilities.SEQUENCE); + jalviewSourceI source = sourceRegistry.getSource(nickname); + url = source.getUri(); + seqsrc = source.isSequenceSource(); } JTextField nametf = new JTextField(nickname, 40); @@ -539,58 +455,39 @@ public class DasSourceBrowser extends GDasSourceBrowser implements urltf.setText(urltf.getText() + "/"); } - SOURCE local = createLocalSource(urltf.getText(), nametf.getText(), seqs.isSelected(), true); - - if (localSources == null) - { - localSources = new Hashtable(); - } - - localSources.put(local.getTitle(), local); - - if (!newSource && !nickname.equals(nametf.getText())) - { - localSources.remove(nickname); - } - - int size = dasSources.length; - int adjust = newSource ? 1 : 0; + jalviewSourceI local = sourceRegistry.createLocalSource( + urltf.getText(), nametf.getText(), seqs.isSelected(), true); + List sources = sourceRegistry.getSources(); + int osize = sources.size(); + int size = osize + (newSource ? 1 : 0); - Object[][] data = new Object[size + adjust][2]; - for (int i = 0; i < size; i++) + Object[][] data = new Object[size][2]; + DASTableModel dtm = (table != null) ? (DASTableModel)((TableSorter) table.getModel()).getTableModel() + : null; + for (int i = 0; i < osize; i++) { - if (!newSource && dasSources[i].getTitle().equals(nickname)) + String osrc = (dtm == null || i >= osize) ? null : (String) dtm + .getValueAt(i, 0); + if (!newSource && osrc != null + && dtm.getValueAt(i, 0).equals(nickname)) { - dasSources[i] = local; data[i][0] = local.getTitle(); data[i][1] = new Boolean(true); } else { - data[i][0] = dasSources[i].getTitle(); - data[i][1] = new Boolean(selectedSources.contains(dasSources[i] - .getTitle())); + data[i][0] = osrc; + data[i][1] = new Boolean(selectedSources.contains(osrc)); } } - + // Always add a new source at the end if (newSource) { - data[size][0] = local.getTitle(); - data[size][1] = new Boolean(true); + data[osize][0] = local.getTitle(); + data[osize][1] = new Boolean(true); selectedSources.add(local.getTitle()); } - SOURCE[] tmp = new SOURCE[size + adjust]; - - System.arraycopy(dasSources, 0, tmp, 0, size); - - if (newSource) - { - tmp[size] = local; - } - - dasSources = tmp; - refreshTableData(data); SwingUtilities.invokeLater(new Runnable() @@ -605,42 +502,6 @@ public class DasSourceBrowser extends GDasSourceBrowser implements displayFullDetails(local.getTitle()); } - private SOURCE createLocalSource(String url, String name, - boolean sequence, boolean features) - { - SOURCE local = new SOURCE(); - - local.setUri(url); - local.setTitle(name); - local.setVERSION(new ArrayList()); - VERSION v = new VERSION(); - List cp = new ArrayList(); - if (sequence) - { -/* Could try and synthesize a coordinate system for the source if needbe - * COORDINATES coord = new COORDINATES(); - coord.setAuthority("NCBI"); - coord.setSource("Chromosome"); - coord.setTaxid("9606"); - coord.setVersion("35"); - version.getCOORDINATES().add(coord);*/ - CAPABILITY cap=new CAPABILITY(); - cap.setType("das1:"+Capabilities.SEQUENCE.getName()); - cp.add(cap); - } - if (features) - { - CAPABILITY cap = new CAPABILITY(); - cap.setType("das1:"+Capabilities.FEATURES.getName()); - cp.add(cap); - } - - v.getCAPABILITY().addAll(cp); - local.getVERSION().add(v); - - return local; - } - public void editRemoveLocalSource(MouseEvent evt) { int selectedRow = table.getSelectionModel().getMinSelectionIndex(); @@ -651,7 +512,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements String nickname = table.getValueAt(selectedRow, 0).toString(); - if (!localSources.containsKey(nickname)) + if (!sourceRegistry.getSource(nickname).isLocal()) { JOptionPane.showInternalMessageDialog(Desktop.desktop, "You can only edit or remove local DAS Sources!", @@ -674,27 +535,28 @@ public class DasSourceBrowser extends GDasSourceBrowser implements amendLocal(false); break; case 1: - localSources.remove(nickname); + sourceRegistry.removeLocalSource(sourceRegistry.getSource(nickname)); selectedSources.remove(nickname); - Object[][] data = new Object[dasSources.length - 1][2]; - SOURCE[] tmp = new SOURCE[dasSources.length - 1]; - int index = 0; - for (int i = 0; i < dasSources.length; i++) + Object[][] data = new Object[sourceRegistry.getSources().size()][2]; + DASTableModel dtm = (table != null) ? (DASTableModel) table + .getModel() : null; + int index = 0, + l = dtm.getRowCount(); + + for (int i = 0; i < l; i++) { - if (dasSources[i].getTitle().equals(nickname)) + String nm; + if ((nm = (String) table.getValueAt(i, 0)).equals(nickname)) { continue; } else { - tmp[index] = dasSources[i]; - data[index][0] = dasSources[i].getTitle(); - data[index][1] = new Boolean( - selectedSources.contains(dasSources[i].getTitle())); + data[index][0] = nm; + data[index][1] = new Boolean(selectedSources.contains(nm)); index++; } } - dasSources = tmp; refreshTableData(data); SwingUtilities.invokeLater(new Runnable() { @@ -709,48 +571,6 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } } - void appendLocalSources() - { - if (localSources == null) - { - return; - } - // note - we add all das sources to list so they can be filtered for the - // standard fetchDbRefs function - int size = dasSources != null ? dasSources.length : 0; - int lsize = localSources.size(); - - Object[][] data = new Object[size + lsize][2]; - for (int i = 0; i < size; i++) - { - data[i][0] = dasSources[i].getTitle(); - data[i][1] = new Boolean(selectedSources.contains(dasSources[i] - .getTitle())); - } - - SOURCE[] tmp = new SOURCE[size + lsize]; - if (dasSources != null) - { - System.arraycopy(dasSources, 0, tmp, 0, size); - } - - Enumeration en = localSources.keys(); - int index = size; - while (en.hasMoreElements()) - { - String key = en.nextElement().toString(); - data[index][0] = key; - data[index][1] = new Boolean(false); - tmp[index] = createLocalSource(localSources.get(key).getUri(), key, false, true); - - index++; - } - - dasSources = tmp; - - refreshTableData(data); - } - public void valueChanged(ListSelectionEvent evt) { // Called when the MainTable selection changes @@ -762,27 +582,25 @@ public class DasSourceBrowser extends GDasSourceBrowser implements displayFullDetails(null); // Filter the displayed data sources - int dSize = dasSources.length; ArrayList names = new ArrayList(); ArrayList selected = new ArrayList(); - SOURCE ds; // The features filter is not visible, but we must still // filter the das source list here. // July 2006 - only 6 sources fo not serve features Object[] dummyFeatureList = new Object[] { "features" }; - - for (int i = 0; i < dSize; i++) + List srcs=sourceRegistry.getSources(); + for (jalviewSourceI ds : srcs) { - ds = dasSources[i]; - VERSION v=getVersionFor(ds); + + VERSION v = ds.getVersion(); List coords = v.getCOORDINATES(); - if ((coords==null || coords.size()==0) + if (ds.isLocal() || ((coords == null || coords.size() == 0) && filter1.getSelectedIndex() == 0 && filter2.getSelectedIndex() == 0 - && filter3.getSelectedIndex() == 0) + && filter3.getSelectedIndex() == 0)) { // THIS IS A FIX FOR LOCAL SOURCES WHICH DO NOT // HAVE COORDINATE SYSTEMS, INFO WHICH AT PRESENT @@ -792,19 +610,19 @@ public class DasSourceBrowser extends GDasSourceBrowser implements continue; } - if (!selectedInList(dummyFeatureList, getCapabilityKeys(v)) + if (!selectedInList(dummyFeatureList, ds.getCapabilityList(v)) || !selectedInList(filter3.getSelectedValues(), - getLabelsFor(v))) + ds.getLabelsFor(v))) { continue; } - for (int j = 0; j labels=new ArrayList(); - for (PROP p: v.getPROP()) - { - if (p.getName().equalsIgnoreCase("LABEL")) - { - labels.add(p.getValue()); - } - } - return labels.toArray(new String[0]); - } - - private String[] getCapabilityKeys(VERSION v) - { - - ArrayList labels=new ArrayList(); - for (CAPABILITY p: v.getCAPABILITY()) - { - // TODO: work out what to do with namespace prefix - // does SEQUENCE == das1:SEQUENCE and das2:SEQUENCE ? - // for moment, just show all capabilities... - if (p.getType().startsWith("das1:")) - { - labels.add(p.getType()); - } - } - return labels.toArray(new String[0]); - } - - boolean selectedInList(Object[] selection, String[] items) + private boolean selectedInList(Object[] selection, String[] items) { for (int i = 0; i < selection.length; i++) { @@ -862,10 +650,14 @@ public class DasSourceBrowser extends GDasSourceBrowser implements { return true; } - + if (items==null || items.length==0) + { + return false; + } + String sel=(items[0].startsWith("das1:") ? "das1:":"")+selection[i]; for (int j = 0; j < items.length; j++) { - if (selection[i].equals(items[j])) + if (sel.equals(items[j])) { return true; } @@ -885,26 +677,11 @@ public class DasSourceBrowser extends GDasSourceBrowser implements { selectedSources.addElement(st.nextToken()); } - - Vector _localSources = jalview.bin.Cache.getLocalDasSources(); - if (_localSources != null) - { - if (localSources == null) - { - localSources = new Hashtable(); - } - Enumeration sources = _localSources.elements(); - while (sources.hasMoreElements()) - { - SOURCE source = (SOURCE) sources.nextElement(); - localSources.put(source.getTitle(), source); - } - } } public void reset_actionPerformed(ActionEvent e) { - registryURL.setText(DEFAULT_REGISTRY); + registryURL.setText(sourceRegistry.getDasRegistryURL()); } /** @@ -936,24 +713,8 @@ public class DasSourceBrowser extends GDasSourceBrowser implements properties.setProperty(jalview.bin.Cache.DAS_ACTIVE_SOURCE, sb.toString()); - if (localSources != null) - { - sb = new StringBuffer(); - Enumeration en = localSources.keys(); - while (en.hasMoreElements()) - { - String token = en.nextElement().toString(); - sb.append(token - + "|" - + (selectedInList(getCapabilityKeys(getVersionFor(localSources.get(token))), - new String[] { "das1:"+Capabilities.SEQUENCE.getName()}) ? "sequence:" : "") - + getVersionFor(localSources.get(token)).getUri() + "\t"); - } - - properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, - sb.toString()); - } - + String sourceprop = sourceRegistry.getLocalSourceString(); + properties.setProperty(jalview.bin.Cache.DAS_LOCAL_SOURCE, sourceprop); } class DASTableModel extends AbstractTableModel @@ -1045,7 +806,7 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } }); thr.start(); - while (loadingDasSources || dasSources == null) + while (loadingDasSources || sourceRegistry == null) { try { @@ -1057,26 +818,6 @@ public class DasSourceBrowser extends GDasSourceBrowser implements } } - public Vector resolveSourceNicknames(Vector sources) - { - - Vector resolved = new Vector(); - if (sources != null) - { - for (int i = 0; i < dasSources.length; i++) - { - if (sources.contains(dasSources[i].getTitle())) - { - if (!resolved.contains(dasSources[i])) - { - resolved.addElement(dasSources[i]); - } - } - } - } - return resolved; - } - /** * disable or enable the buttons on the source browser * -- 1.7.10.2