From fe1e44708a89f96e32ee2aa5028495d0221e4e53 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Fri, 18 Mar 2016 12:56:20 +0100 Subject: [PATCH] Minor code improvements --- src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java | 2 +- src/main/java/eu/omp/irap/vespa/epntapclient/view/Dim.java | 54 ------------------------------------------------------ src/main/java/eu/omp/irap/vespa/epntapclient/view/GUIDim.java | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main/java/eu/omp/irap/vespa/epntapclient/view/ParamField.java | 10 +++++----- src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/RequestPanel.java | 35 +++++++++++++---------------------- src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ResultsPanel.java | 10 +++++----- src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ServicesPanel.java | 6 +++--- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java | 9 +++------ src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Const.java | 2 +- src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Utils.java | 13 ++++++++++--- src/main/java/eu/omp/irap/vespa/epntapclient/votable/view/VOTableView.java | 1 + 11 files changed, 96 insertions(+), 100 deletions(-) delete mode 100644 src/main/java/eu/omp/irap/vespa/epntapclient/view/Dim.java create mode 100644 src/main/java/eu/omp/irap/vespa/epntapclient/view/GUIDim.java diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java b/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java index 142e67d..e4479fe 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java @@ -74,7 +74,7 @@ public final class Queries { addJoin.add(param.getKey() + " = " + param.getValue().toString()); } } - String where = "".equals(addJoin.toString()) ? "" : " WHERE " + addJoin; + String where = addJoin.isEmpty() ? "" : " WHERE " + addJoin; return "SELECT TOP " + nbRow + " target_name, target_class FROM " + tableName + where; } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/Dim.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/Dim.java deleted file mode 100644 index db60374..0000000 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/Dim.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 - * . - */ - -package eu.omp.irap.vespa.epntapclient.view; - -/** - * A simple class containing GUI panel and elements dimensions. - * - * @author N. Jourdane - */ -public class Dim { - - /** The width of the left panel (services view). */ - public static final int LEFT_PANEL_WIDTH = 400; - - /** The minimum width of the left panel (services view). */ - public static final int LEFT_PANEL_MIN_WIDTH = 150; - - /** The width of the right panel (request view). */ - public static final int RIGHT_PANEL_WIDTH = 400; - - /** The minimum width of the right panel (request view). */ - public static final int RIGHT_PANEL_MIN_WIDTH = 220; - - /** The height of the top panel (request view and services view). */ - public static final int TOP_PANEL_HEIGHT = 250; - - /** The minimum height of the top panel (request view and services view). */ - public static final int TOP_PANEL_MIN_HEIGHT = 190; - - /** The height of the bottom panel (result view). */ - public static final int BOTTOM_PANEL_HEIGHT = 150; - - /** The minimum height of the bottom panel (result view). */ - public static final int BOTTOM_PANEL_MIN_HEIGHT = 100; - - - /** Private constructor to hide the implicit public one. */ - private Dim() { - } -} diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/GUIDim.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/GUIDim.java new file mode 100644 index 0000000..32dbe93 --- /dev/null +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/GUIDim.java @@ -0,0 +1,54 @@ +/* + * 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 + * . + */ + +package eu.omp.irap.vespa.epntapclient.view; + +/** + * A simple class containing GUI panel and elements dimensions. + * + * @author N. Jourdane + */ +public class GUIDim { + + /** The width of the left panel (services view). */ + public static final int LEFT_PANEL_WIDTH = 400; + + /** The minimum width of the left panel (services view). */ + public static final int LEFT_PANEL_MIN_WIDTH = 150; + + /** The width of the right panel (request view). */ + public static final int RIGHT_PANEL_WIDTH = 400; + + /** The minimum width of the right panel (request view). */ + public static final int RIGHT_PANEL_MIN_WIDTH = 220; + + /** The height of the top panel (request view and services view). */ + public static final int TOP_PANEL_HEIGHT = 250; + + /** The minimum height of the top panel (request view and services view). */ + public static final int TOP_PANEL_MIN_HEIGHT = 190; + + /** The height of the bottom panel (result view). */ + public static final int BOTTOM_PANEL_HEIGHT = 150; + + /** The minimum height of the bottom panel (result view). */ + public static final int BOTTOM_PANEL_MIN_HEIGHT = 100; + + + /** Private constructor to hide the implicit public one. */ + private GUIDim() { + } +} 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 b90d55b..b1563aa 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 @@ -151,7 +151,7 @@ public abstract class ParamField extends JPanel { */ @Override public void update(JTextField textField) { - if ("".equals(textField.getText())) { + if (textField.getText().isEmpty()) { mainView.event(Event.PARAMETER_CHANGED, paramName, null); } else { mainView.event(Event.PARAMETER_CHANGED, paramName, textField.getText()); @@ -193,7 +193,7 @@ public abstract class ParamField extends JPanel { */ @Override public void update(JTextField textField) { - if ("".equals(textField.getText())) { + if (textField.getText().isEmpty()) { textField.setBackground(Color.WHITE); mainView.event(Event.PARAMETER_REMOVED, paramName); } else { @@ -259,7 +259,7 @@ public abstract class ParamField extends JPanel { @Override public void update(JTextField field) { DateFormat df = new SimpleDateFormat(ParamField.DATE_FORMAT, Locale.ENGLISH); - if ("".equals(field.getText())) { + if (field.getText().isEmpty()) { field.setBackground(Color.WHITE); mainView.event(Event.PARAMETER_REMOVED, paramName + field.getName()); } else if (field.getText().matches(ParamField.DATE_REGEX)) { @@ -321,7 +321,7 @@ public abstract class ParamField extends JPanel { */ @Override public void update(JTextField field) { - if ("".equals(field.getText())) { + if (field.getText().isEmpty()) { field.setBackground(Color.WHITE); mainView.event(Event.PARAMETER_REMOVED, paramName + field.getName()); } else { @@ -385,7 +385,7 @@ public abstract class ParamField extends JPanel { } comboBox.getEditor().setItem(content); } - if ("".equals(content)) { + if (content.isEmpty()) { mainView.event(Event.PARAMETER_REMOVED, paramName); } else { mainView.event(Event.PARAMETER_CHANGED, paramName, content); diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/RequestPanel.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/RequestPanel.java index 64442bf..f424994 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/RequestPanel.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/RequestPanel.java @@ -29,7 +29,7 @@ import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextArea; -import eu.omp.irap.vespa.epntapclient.view.Dim; +import eu.omp.irap.vespa.epntapclient.view.GUIDim; import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView; import eu.omp.irap.vespa.epntapclient.view.Event; import eu.omp.irap.vespa.epntapclient.view.ParamField; @@ -43,19 +43,16 @@ import eu.omp.irap.vespa.epntapclient.view.ParamField.TargetNameField; * * @author N. Jourdane */ -public class RequestPanel extends JPanel implements ActionListener { +public class RequestPanel extends JPanel { /** The serial version UID. */ private static final long serialVersionUID = 1L; - /** The name of the button to send the query. */ - private static final String BTN_NAME = "btnSend"; - /** The main view of the application. */ - private EpnTapMainView mainView; + protected EpnTapMainView mainView; /** The text area where the user write the query. */ - private JTextArea queryArea; + protected JTextArea queryArea; /** * The parameters fields for the request. @@ -74,8 +71,8 @@ public class RequestPanel extends JPanel implements ActionListener { public RequestPanel(EpnTapMainView mainView) { this.mainView = mainView; setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); - setPreferredSize(new Dimension(Dim.RIGHT_PANEL_WIDTH, Dim.TOP_PANEL_HEIGHT)); - setMinimumSize(new Dimension(Dim.RIGHT_PANEL_MIN_WIDTH, Dim.TOP_PANEL_MIN_HEIGHT)); + setPreferredSize(new Dimension(GUIDim.RIGHT_PANEL_WIDTH, GUIDim.TOP_PANEL_HEIGHT)); + setMinimumSize(new Dimension(GUIDim.RIGHT_PANEL_MIN_WIDTH, GUIDim.TOP_PANEL_MIN_HEIGHT)); // TODO: Use a JScrollPane. // TODO: Get max row number from the GUI @@ -152,22 +149,16 @@ public class RequestPanel extends JPanel implements ActionListener { private JPanel buildButtonPanel() { JPanel buttonPanel = new JPanel(); JButton btnSend = new JButton("Send query"); - btnSend.setName(RequestPanel.BTN_NAME); - btnSend.addActionListener(this); + btnSend.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + mainView.event(Event.SEND_BUTTON_CLICKED, queryArea.getText()); + } + }); buttonPanel.add(btnSend); buttonPanel.setMaximumSize(new Dimension(1000, RequestPanel.BUTTON_PANEL_HEIGHT)); return buttonPanel; } - - /** - * This method is called when the user click on the `Send` button. - */ - @Override - public void actionPerformed(ActionEvent evt) { - if (((JButton) evt.getSource()).getName() == RequestPanel.BTN_NAME) { - mainView.event(Event.SEND_BUTTON_CLICKED, queryArea.getText()); - } - } - } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ResultsPanel.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ResultsPanel.java index 744a4d0..bad9f1f 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ResultsPanel.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ResultsPanel.java @@ -21,7 +21,7 @@ import java.awt.Dimension; import javax.swing.JPanel; -import eu.omp.irap.vespa.epntapclient.view.Dim; +import eu.omp.irap.vespa.epntapclient.view.GUIDim; import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView; import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView; @@ -47,9 +47,9 @@ public class ResultsPanel extends JPanel { setLayout(new BorderLayout()); this.add(voTableView); - setPreferredSize(new Dimension(Dim.LEFT_PANEL_WIDTH + Dim.RIGHT_PANEL_WIDTH, - Dim.BOTTOM_PANEL_HEIGHT)); - setMinimumSize(new Dimension(Dim.LEFT_PANEL_MIN_WIDTH + Dim.RIGHT_PANEL_MIN_WIDTH, - Dim.BOTTOM_PANEL_MIN_HEIGHT)); + setPreferredSize(new Dimension(GUIDim.LEFT_PANEL_WIDTH + GUIDim.RIGHT_PANEL_WIDTH, + GUIDim.BOTTOM_PANEL_HEIGHT)); + setMinimumSize(new Dimension(GUIDim.LEFT_PANEL_MIN_WIDTH + GUIDim.RIGHT_PANEL_MIN_WIDTH, + GUIDim.BOTTOM_PANEL_MIN_HEIGHT)); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ServicesPanel.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ServicesPanel.java index 245dd93..4878a90 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ServicesPanel.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/panels/ServicesPanel.java @@ -23,7 +23,7 @@ import javax.swing.JPanel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; -import eu.omp.irap.vespa.epntapclient.view.Dim; +import eu.omp.irap.vespa.epntapclient.view.GUIDim; import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView; import eu.omp.irap.vespa.epntapclient.view.Event; import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView; @@ -53,8 +53,8 @@ public class ServicesPanel extends JPanel { setLayout(new BorderLayout()); this.add(voTableView); - setPreferredSize(new Dimension(Dim.LEFT_PANEL_WIDTH, Dim.TOP_PANEL_HEIGHT)); - setMinimumSize(new Dimension(Dim.LEFT_PANEL_MIN_WIDTH, Dim.TOP_PANEL_MIN_HEIGHT)); + setPreferredSize(new Dimension(GUIDim.LEFT_PANEL_WIDTH, GUIDim.TOP_PANEL_HEIGHT)); + setMinimumSize(new Dimension(GUIDim.LEFT_PANEL_MIN_WIDTH, GUIDim.TOP_PANEL_MIN_HEIGHT)); // TODO: Support multi-selection this.voTableView.getTable().getSelectionModel() diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java index 9d8343c..72e6c97 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java @@ -72,7 +72,7 @@ public final class VOTableConnection { String uri = targetURL + "/sync"; String parameters = "REQUEST=doQuery&LANG=" + queryLanguage + "&QUERY="; try { - parameters += URLEncoder.encode(query, Const.CHARACTER_SET).replace("+", "%20") + parameters += URLEncoder.encode(query, Const.ENCODING).replace("+", "%20") .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") .replace("_", "%5F"); } catch (UnsupportedEncodingException e) { @@ -135,7 +135,6 @@ public final class VOTableConnection { while ((inputLine = in.readLine()) != null) { response.append(inputLine); } - in.close(); } catch (IOException e1) { VOTableConnection.logger.log(Level.WARNING, "Can not get input stream", e1); try (BufferedReader in = new BufferedReader( @@ -143,7 +142,6 @@ public final class VOTableConnection { while ((inputLine = in.readLine()) != null) { response.append(inputLine); } - in.close(); } catch (IOException e2) { throw new CantGetErrorStream(uri, e2); } @@ -162,13 +160,12 @@ public final class VOTableConnection { private static void printRequestResult(StringBuilder response, String resultFileName) throws IOException { - try (PrintWriter writer = new PrintWriter(resultFileName, Const.CHARACTER_SET)) { + try (PrintWriter writer = new PrintWriter(resultFileName, Const.ENCODING)) { writer.println(response); - writer.close(); } catch (FileNotFoundException e1) { throw new IOException("The file " + resultFileName + " is not found.", e1); } catch (UnsupportedEncodingException e2) { - throw new IOException("Bad encoding with " + Const.CHARACTER_SET, e2); + throw new IOException("Bad encoding with " + Const.ENCODING, e2); } } } \ No newline at end of file diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Const.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Const.java index 3d7002f..4fe90aa 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Const.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Const.java @@ -24,7 +24,7 @@ package eu.omp.irap.vespa.epntapclient.votable.utils; public class Const { /** The character set used (for http requests, writing in files, etc.). */ - public static final String CHARACTER_SET = "UTF-8"; + public static final String ENCODING = "UTF-8"; /** The temporary directory path. */ public static final String TMP_DIR = System.getProperty("java.io.tmpdir"); diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Utils.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Utils.java index 81261ba..6d86b77 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Utils.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/utils/Utils.java @@ -59,22 +59,29 @@ public class Utils { */ public StringJoiner(String separator) { this.separator = separator; - string = ""; + string = new String(); } /** - * add a new word to the joiner. + * Add a new word to the joiner. * * @param text The word to add. */ public void add(String text) { - if ("".equals(string)) { + if (string.isEmpty()) { string = text; } else { string += separator + text; } } + /** + * @return true if the string joiner content is empty. + */ + public boolean isEmpty() { + return string.isEmpty(); + } + @Override public String toString() { return string; 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 9b0715d..0e8f292 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 @@ -110,6 +110,7 @@ public class VOTableView extends JPanel implements TableModelListener { if (e.getType() != TableModelEvent.UPDATE) { return; } + // TODO : créer interface VOTableListener } } -- libgit2 0.21.2