From 26c7a6bfa287c3ebaa8eb08421f195ee0bc8edc4 Mon Sep 17 00:00:00 2001
From: Nathanael Jourdane <nathanael.jourdane@irap.omp.eu>
Date: Fri, 15 Apr 2016 16:40:44 +0200
Subject: [PATCH] VOTable: Code restructuration in order that the controller do not need the view.

---
 src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java                   | 54 +++++++++++++++++++++++++++++++++---------------------
 src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java | 84 +++++++++++++++++++++++++++++++++++++++++-------------------------------------------
 src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java             |  4 ++++
 src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableViewListener.java     | 26 ++++++++++++++++++++++++++
 4 files changed, 104 insertions(+), 64 deletions(-)
 create mode 100644 src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableViewListener.java

diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java
index e139814..0d0d6fe 100644
--- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java
+++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/VOTableApp.java
@@ -26,6 +26,7 @@ import com.google.gson.Gson;
 
 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController;
 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
+import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView;
 
 /**
  * Simple class to have a main function to display a voTable from a XML file.
@@ -37,6 +38,9 @@ public class VOTableApp {
 	/** The logger for the class VOTableApp. */
 	protected static final Logger logger = Logger.getLogger(VOTableApp.class.getName());
 
+	private static final String WRONG_COMMAND = "Usage: VOtableApp path/to/VOTable.xml\n"
+			+ "OR: VOtableApp http://url.to.service/or/registry type language \"YOUR QUERY\"";
+
 
 	/** Private constructor to hide the implicit public one. */
 	private VOTableApp() {
@@ -49,34 +53,42 @@ public class VOTableApp {
 	 */
 	public static void main(final String[] args) {
 		VOTableApp.logger.info("Lauching VOTable app with arguments: " + new Gson().toJson(args));
-		SwingUtilities.invokeLater(new Runnable() {
+		VOTableController ctrl;
+
+		if (args.length == 1) {
+			ctrl = new VOTableController(args[0]);
+		} else if (args.length == 3) {
+			ctrl = new VOTableController(args[0], args[1], args[2]);
+		} else {
+			System.console().writer().println(VOTableApp.WRONG_COMMAND);
+			return;
+		}
+
+		try {
+			ctrl.readTable();
+		} catch (VOTableException e) {
+			System.console().writer().println("Error: " + e.getMessage());
+			VOTableApp.logger.log(Level.WARNING, e.getMessage(), e);
+			return;
+		}
+
+		VOTableView view = new VOTableView();
+		view.fillTable(ctrl.getColumns(), ctrl.getData());
+		SwingUtilities.invokeLater(VOTableApp.run(view, args[0]));
+	}
+
+	private static Runnable run(final VOTableView voTableView, final String title) {
+		return new Runnable() {
 
 			@Override
 			public void run() {
-				VOTableController voTableControl;
-				if (args.length == 1) {
-					try {
-						voTableControl = new VOTableController(args[0]);
-					} catch (VOTableException e) {
-						System.console().writer().println("Error: " + e.getMessage());
-						VOTableApp.logger.log(Level.WARNING, e.getMessage(), e);
-						return;
-					}
-				} else if (args.length == 3) {
-					voTableControl = new VOTableController(args[0], args[1], args[2]);
-				} else {
-					System.console().writer().println("Usage: VOtableApp path/to/VOTable.xml\n"
-							+ "OR: VOtableApp http://url.to.service/or/registry type language \"YOUR QUERY\"");
-					return;
-				}
-
-				JFrame frame = new JFrame(args[0]);
+				JFrame frame = new JFrame(title);
 				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-				frame.setContentPane(voTableControl.getView());
+				frame.setContentPane(voTableView);
 				frame.setVisible(true);
 				frame.setSize(800, 600);
 				frame.setLocationRelativeTo(null);
 			}
-		});
+		};
 	}
 }
\ No newline at end of file
diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java
index bfe9179..b598ae4 100644
--- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java
+++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java
@@ -17,6 +17,7 @@
 package eu.omp.irap.vespa.epntapclient.votable.controller;
 
 import java.io.IOException;
+import java.util.List;
 import java.util.logging.Logger;
 
 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDisplayVOTableException;
@@ -27,39 +28,43 @@ import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantDi
 import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException;
 import eu.omp.irap.vespa.epntapclient.votable.model.Table;
 import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
-import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView;
+import eu.omp.irap.vespa.epntapclient.votable.view.VOTableViewListener;
 
 /**
  * @author N. Jourdane
  */
-public class VOTableController {
+public class VOTableController implements VOTableViewListener {
 
 	/** The logger for the class VOTableController. */
 	private static final Logger logger = Logger.getLogger(VOTableController.class.getName());
 
-	/** The view of the VOTable */
-	private VOTableView view;
-
 	/** The VOTable model */
 	private VOTABLE voTable;
 
+	private String voTablePath;
 
-	/**
-	 * Method constructor
-	 */
-	public VOTableController() {
-		view = new VOTableView();
+	private String targetURL;
+
+	private String queryLanguage;
+
+	private String query;
+
+	private String[] voTableColumns;
+
+	private List<Object[]> voTableData;
+
+
+	/** Private constructor to hide the implicit public one. */
+	private VOTableController() {
 	}
 
 	/**
 	 * Method constructor
 	 *
 	 * @param voTablePath The path of the VOTable XML file.
-	 * @throws VOTableException If something went wrong on the VOTable view or when filling table.
 	 */
-	public VOTableController(String voTablePath) throws VOTableException {
-		view = new VOTableView();
-		fillTable(voTablePath);
+	public VOTableController(String voTablePath) {
+		this.voTablePath = voTablePath;
 	}
 
 	/**
@@ -70,36 +75,23 @@ public class VOTableController {
 	 * @param query The query to ask to the registry.
 	 */
 	public VOTableController(String targetURL, String queryLanguage, String query) {
-		view = new VOTableView();
-
-		try {
-			fillTable(VOTableConnection.sendQuery(targetURL, queryLanguage, query));
-		} catch (VOTableException e) {
-			VOTableController.logger.info("VOTableController error: " + e);
-		}
-	}
-
-	/**
-	 * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org").
-	 * @param queryLanguage The language used for the queries (ie. "ADQL").
-	 * @param query The query to ask to the registry.
-	 * @throws CantSendQueryException If the query can not be sent.
-	 * @throws CantDisplayVOTableException If the table can not be filled.
-	 */
-	public String fillTable(String targetURL, String queryLanguage, String query)
-			throws CantDisplayVOTableException, CantSendQueryException {
-		String voTablePath = VOTableConnection.sendQuery(targetURL, queryLanguage, query);
-		fillTable(voTablePath);
-		return voTablePath;
+		voTablePath = null;
+		this.targetURL = targetURL;
+		this.queryLanguage = queryLanguage;
+		this.query = query;
 	}
 
 	/**
 	 * @param voTablePath The path of the VOTable file.
 	 * @throws CantDisplayVOTableException If the VOTable can not be filled.
+	 * @throws CantSendQueryException
 	 */
-	public void fillTable(String voTablePath)
-			throws CantDisplayVOTableException {
+	// TODO: can't fill table exception
+	public void readTable() throws CantDisplayVOTableException, CantSendQueryException {
 
+		if (voTablePath == null) {
+			voTablePath = VOTableConnection.sendQuery(targetURL, queryLanguage, query);
+		}
 		voTable = VOTableParser.parseVOTable(voTablePath);
 
 		if (voTable.getRESOURCE().size() > 1) {
@@ -120,16 +112,22 @@ public class VOTableController {
 		VOTableDataParser dataParser;
 		try {
 			dataParser = new VOTableDataParser(table);
-			view.fillTable(dataParser.getColumnsName(), dataParser.getDataArray());
+			voTableColumns = dataParser.getColumnsName();
+			voTableData = dataParser.getDataArray();
 		} catch (IOException e) {
 			throw new CantModifyVOTableException(voTablePath, e);
 		}
 	}
 
-	/**
-	 * @return The view of the VOTable.
-	 */
-	public VOTableView getView() {
-		return view;
+	public String[] getColumns() {
+		return voTableColumns;
+	}
+
+	public List<Object[]> getData() {
+		return voTableData;
+	}
+
+	public String getVOTablePath() {
+		return voTablePath;
 	}
 }
diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java
index c3be494..b0cf41f 100644
--- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java
+++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java
@@ -49,6 +49,7 @@ public class VOTableView extends JPanel implements TableModelListener {
 	/**
 	 * Method constructor
 	 */
+	// TODO public VOTableView(VOTableViewListener listener) -> listen for select events
 	public VOTableView() {
 		tableData = new DefaultTableModel() {
 
@@ -63,7 +64,10 @@ public class VOTableView extends JPanel implements TableModelListener {
 		};
 
 		table = new JTable(tableData);
+		buildVOTableView();
+	}
 
+	private void buildVOTableView() {
 		JScrollPane scrollPane = new JScrollPane(table);
 		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableViewListener.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableViewListener.java
new file mode 100644
index 0000000..d33823c
--- /dev/null
+++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableViewListener.java
@@ -0,0 +1,26 @@
+/*
+ * This file is a part of EpnTAPClient.
+ * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
+ * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
+ * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
+ * 
+ * This program is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
+ * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
+ * the GNU General Public License along with this program. If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+package eu.omp.irap.vespa.epntapclient.votable.view;
+
+
+/**
+ * 
+ * @author N. Jourdane
+ */
+public interface VOTableViewListener {
+
+}
--
libgit2 0.21.2