From 75cf0885124634496765be1a0fe94d16078bfe43 Mon Sep 17 00:00:00 2001 From: Alexis Delhaie Date: Mon, 28 Oct 2019 20:37:24 +0100 Subject: [PATCH] =?UTF-8?q?Changement=20de=20la=20MainWindow=20de=20JDialo?= =?UTF-8?q?g=20a=20JFrame=20&=20correction=20de=20la=20checkbox=20SSL=20qu?= =?UTF-8?q?i=20=C3=A9tait=20disponible=20en=20HTTP=20apr=C3=A8s=20avoir=20?= =?UTF-8?q?fait=20une=20requete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ovh/alexisdelhaie/curling/web/Client.java | 8 ++--- .../curling/windows/MainWindow.java | 36 +++++++++++++++---- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/ovh/alexisdelhaie/curling/web/Client.java b/src/ovh/alexisdelhaie/curling/web/Client.java index a621d3a..f0225d8 100644 --- a/src/ovh/alexisdelhaie/curling/web/Client.java +++ b/src/ovh/alexisdelhaie/curling/web/Client.java @@ -92,7 +92,7 @@ public class Client { if (request != null) { return CompletableFuture.supplyAsync(() -> { - String result = String.format("%s\n\n", log); + String result = String.format("%s\n---- HTTP Response ----\n", log); try { HttpResponse r = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); result += String.format("> Status : %d\n", r.statusCode()); @@ -103,7 +103,7 @@ public class Client { } result += "\n"; } - result += String.format("\n%s", (String)r.body()); + result += String.format("\n---- Body ----\n%s", (String)r.body()); } catch (IOException e) { result += String.format("\n/!\\ %s", e.getMessage()); error = new RequestError(e.getMessage(), e.getLocalizedMessage(), e); @@ -143,10 +143,10 @@ public class Client { log += String.format(" %s\n", url); for (Map.Entry header : headers.entrySet()) { b.setHeader(header.getKey(), header.getValue()); - log += String.format("< %s: %s", header.getKey(), header.getValue()); + log += String.format("< %s: %s\n", header.getKey(), header.getValue()); } if(method == Method.POST || method == Method.PUT) { - log += String.format("%s", body); + log += String.format("\n---- Body ----\n%s", body); } b.timeout(Duration.ofMillis(timeout)); return b.build(); diff --git a/src/ovh/alexisdelhaie/curling/windows/MainWindow.java b/src/ovh/alexisdelhaie/curling/windows/MainWindow.java index 0d061f9..3287f41 100644 --- a/src/ovh/alexisdelhaie/curling/windows/MainWindow.java +++ b/src/ovh/alexisdelhaie/curling/windows/MainWindow.java @@ -15,7 +15,7 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; -public class MainWindow extends JDialog { +public class MainWindow extends JFrame { public static int WINDOW_HEIGHT = 700; public static int WINDOW_WIDTH = 1100; @@ -43,7 +43,7 @@ public class MainWindow extends JDialog { private boolean allowInvalidSSL; public MainWindow() { - super((Dialog) null); + super(); setTitle(WINDOW_TITLE); allowInvalidSSL = false; headers = new HashMap<>(); @@ -52,7 +52,7 @@ public class MainWindow extends JDialog { setContentPane(contentPane); setMinimumSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT)); setSize(WINDOW_WIDTH, WINDOW_HEIGHT); - setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); httpTypeBox.addActionListener((event) -> { allowInvalidSSLField.setEnabled(httpTypeBox.getSelectedItem().equals("HTTPS")); @@ -86,9 +86,11 @@ public class MainWindow extends JDialog { xFormEncodedButton.addActionListener((event) -> { FormEncodedDialog fe = new FormEncodedDialog(); String datas = fe.showDialog(); - bodyArea.setText(datas); - headers.put(CONTENT_TYPE, WWW_FORM_URLENCODED); - updateHeaderList(); + if(!datas.isBlank()) { + bodyArea.setText(datas); + headers.put(CONTENT_TYPE, WWW_FORM_URLENCODED); + updateHeaderList(); + } }); runButton.addActionListener(this::start); @@ -104,6 +106,10 @@ public class MainWindow extends JDialog { } private void start(ActionEvent ev) { + if(urlField.getText().isBlank()) { + JOptionPane.showMessageDialog(this, "L'URL est manquante"); + return; + } startLoading(true); Client c = new Client(bodyArea.getText(), headers, getMethod(), urlBuilder(), getAllowInvalidSSL()); CompletableFuture f = c.run(); @@ -150,7 +156,15 @@ public class MainWindow extends JDialog { newHeaderButton.setEnabled(!b); addAuthButton.setEnabled(!b); runButton.setEnabled(!b); - allowInvalidSSLField.setEnabled(!b); + setEnableAllowInvalidSSLField(!b); + } + + private void setEnableAllowInvalidSSLField(boolean b) { + if(httpTypeBox.getSelectedItem().equals("HTTPS")) { + allowInvalidSSLField.setEnabled(b); + } else { + allowInvalidSSLField.setEnabled(false); + } } private boolean getAllowInvalidSSL() { @@ -169,5 +183,13 @@ public class MainWindow extends JDialog { public void showFrame() { pack(); setVisible(true); + centerFrame(); + } + + private void centerFrame() { + Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + int y = (int)( screen.getHeight() / 2 ) - this.getHeight() / 2; + int x = (int)( screen.getWidth() / 2 ) - this.getWidth() / 2; + this.setLocation(x, y); } }