From 05b140ed5a9dbf4ba71d2b34433f8f2b0e4db0e7 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Thu, 10 Mar 2016 11:13:07 +0100 Subject: [PATCH] TargetNameField JComboBox is filled with actual name suggestions. --- src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java index 870c1c4..e3ffdbf 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java @@ -25,11 +25,20 @@ import javax.swing.text.JTextComponent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableConnection; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.BadRequestException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.HTTPRequestException; + public abstract class ParamField extends JPanel { /** The logger for this class. */ private static final Logger logger = LogManager.getLogger(ParamField.class); + private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete"; private static final String DATE_FORMAT = "dd/MM/yyyy"; private static final String DATE_REGEX = "(^(((0[1-9]|1[0-9]|2[0-8])[\\/](0[1-9]|1[012]))|((29|30|31)[\\/](0[13578]|1[02]))|((29|30)[\\/](0[4,6,9]|11)))[\\/](19|[2-9][0-9])\\d\\d$)|(^29[\\/]02[\\/](19|[2-9][0-9])(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)$)"; @@ -176,7 +185,21 @@ public abstract class ParamField extends JPanel { } private String[] getTargetNames(String begining) { - String[] targetNames = { begining + "Bird", begining + "Cat", begining + "Dog" }; + StringBuilder resolverResult = null; + try { + resolverResult = VOTableConnection.sendGet(RESOLVER_URL, "q=\"" + begining + "\""); + } catch (HTTPRequestException | BadRequestException e) { + logger.fatal("Can not send sersolver query: ", e); + } + JsonObject root = new JsonParser().parse(resolverResult.toString()).getAsJsonObject(); + int count = Integer.parseInt(root.get("count").toString()); + String[] targetNames = new String[count]; + JsonArray hits = root.getAsJsonArray("hits"); + for (int i = 0; i < count; i++) { + JsonObject elmt = hits.get(i).getAsJsonObject(); + targetNames[i] = elmt.get("name").toString().replace("\"", ""); + // TODO: Display "[name] ([type])" on the JComboBox, but only "[name]" on the query. + } return targetNames; } -- libgit2 0.21.2