diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/Application.java b/src/main/java/ovh/alexisdelhaie/endpoint/Application.java index 07adf0a..a241036 100644 --- a/src/main/java/ovh/alexisdelhaie/endpoint/Application.java +++ b/src/main/java/ovh/alexisdelhaie/endpoint/Application.java @@ -1,8 +1,10 @@ package ovh.alexisdelhaie.endpoint; import com.formdev.flatlaf.FlatIntelliJLaf; +import ovh.alexisdelhaie.endpoint.utils.Tools; import javax.swing.*; +import java.awt.*; public class Application { @@ -12,7 +14,7 @@ public class Application { dialog.pack(); dialog.setTitle("EndPoint"); dialog.setVisible(true); - dialog.centerFrame(); + Tools.centerFrame(dialog); } } diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.form b/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.form index 6953221..ef73f21 100644 --- a/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.form +++ b/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.form @@ -1,6 +1,6 @@
- + @@ -8,7 +8,7 @@ - + @@ -22,9 +22,9 @@ - + - + @@ -32,7 +32,7 @@ - + @@ -40,7 +40,7 @@ - + @@ -50,12 +50,51 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.java b/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.java index 9ce2885..2fc1ecc 100644 --- a/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.java +++ b/src/main/java/ovh/alexisdelhaie/endpoint/MainWindow.java @@ -6,14 +6,21 @@ import ovh.alexisdelhaie.endpoint.http.HttpClient; import ovh.alexisdelhaie.endpoint.http.Request; import ovh.alexisdelhaie.endpoint.http.RequestBuilder; import ovh.alexisdelhaie.endpoint.http.Response; +import ovh.alexisdelhaie.endpoint.utils.MessageDialog; import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.Objects; import java.util.Optional; public class MainWindow extends JFrame { @@ -23,65 +30,107 @@ public class MainWindow extends JFrame { public final static int HEIGHT = 720; private JPanel contentPane; - private JComboBox comboBox1; - private JTextField textField1; + private JComboBox methodBox; + private JTextField urlField; private JButton sendButton; private JTabbedPane tabbedPane1; private JButton newTabButton; + private JProgressBar progressBar1; - private ConfigurationProperties props; + private final ConfigurationProperties props; + private final HashMap urls; public MainWindow() { props = new ConfigurationProperties(); + urls = new HashMap<>(); setContentPane(contentPane); setMinimumSize(new Dimension(WIDTH, HEIGHT)); setSize(WIDTH, HEIGHT); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - TabBuilder.create(tabbedPane1, "New request"); + TabBuilder.create(tabbedPane1, "New request", urls); + Component tab = tabbedPane1.getSelectedComponent(); + urls.put(tab.hashCode(), ""); newTabButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); - TabBuilder.create(tabbedPane1, "New request"); + TabBuilder.create(tabbedPane1, "New request", urls); + Component tab = tabbedPane1.getSelectedComponent(); + urls.put(tab.hashCode(), ""); } }); sendButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); - try { + if (!urlField.getText().isBlank()) { sendRequest(); - } catch (IOException | NoSuchAlgorithmException | KeyManagementException ioException) { - ioException.printStackTrace(); + } else { + MessageDialog.info("Url field empty", "Please enter an url"); + } + } + }); + tabbedPane1.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + if (tabbedPane1.getSelectedIndex() != -1) { + urlField.setText(urls.get(tabbedPane1.getSelectedComponent().hashCode())); + } + } + }); + urlField.getDocument().addDocumentListener(new DocumentListener() { + public void changedUpdate(DocumentEvent e) { + warn(); + } + public void removeUpdate(DocumentEvent e) { + warn(); + } + public void insertUpdate(DocumentEvent e) { + warn(); + } + + public void warn() { + if (tabbedPane1.getSelectedIndex() != -1) { + urls.put(tabbedPane1.getSelectedComponent().hashCode(), urlField.getText()); } } }); } - public 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); - } - - private void sendRequest() throws IOException, NoSuchAlgorithmException, KeyManagementException { + private void sendRequest() { Optional possibleTab = getSelectedTab(); if (possibleTab.isPresent()) { JSplitPane tab = possibleTab.get(); - String url = textField1.getText(); - HttpClient h = new HttpClient(props); - Request r = new RequestBuilder(url) - .build(); - Optional possibleRes = h.get(r); - if (possibleRes.isPresent()) { - Response res = possibleRes.get(); - int i = tabbedPane1.indexOfComponent(tab); - JTextArea t = TabBuilder.getResponseArea(i); - t.setText(res.getBody()); - } - } + int i = tabbedPane1.indexOfComponent(tab); + JTextArea bodyField = TabBuilder.getResponseArea(i); + progressBar1.setVisible(true); + new Thread(() -> { + try { + String url = urlField.getText(); + HttpClient h = new HttpClient(props); + Request r = new RequestBuilder(url) + .build(); + Optional possibleRes = Optional.empty(); + switch ((String) Objects.requireNonNull(methodBox.getSelectedItem())) { + case "GET" -> possibleRes = h.get(r); + case "POST" -> possibleRes = h.post(r, ""); + case "PUT" -> possibleRes = h.put(r, ""); + case "DELETE" -> possibleRes = h.delete(r); + case "HEAD" -> possibleRes = h.head(r); + } + if (possibleRes.isPresent()) { + Response res = possibleRes.get(); + bodyField.setText(res.getBody()); + } + } catch (KeyManagementException | IOException | NoSuchAlgorithmException e) { + bodyField.setText(e.getMessage()); + } finally { + progressBar1.setVisible(false); + } + }).start(); + } else { + MessageDialog.error("Error", "Cannot get current tab"); + } } private Optional getSelectedTab() { diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/builder/TabBuilder.java b/src/main/java/ovh/alexisdelhaie/endpoint/builder/TabBuilder.java index bde3569..d30fa97 100644 --- a/src/main/java/ovh/alexisdelhaie/endpoint/builder/TabBuilder.java +++ b/src/main/java/ovh/alexisdelhaie/endpoint/builder/TabBuilder.java @@ -1,21 +1,33 @@ package ovh.alexisdelhaie.endpoint.builder; +import ovh.alexisdelhaie.endpoint.utils.InsertToTableDialog; +import ovh.alexisdelhaie.endpoint.utils.KeyValuePair; + import javax.swing.*; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.table.DefaultTableModel; import javax.swing.table.TableColumn; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; import java.util.HashMap; +import java.util.Map; +import java.util.Optional; public class TabBuilder { private static HashMap indexes = new HashMap<>(); - public static void create(JTabbedPane tab, String label) { + public static void create(JTabbedPane tab, String label, HashMap urls) { Component c = tab.add("", buildMainPanel()); int index = tab.indexOfComponent(c); updateIndexes(index); - tab.setTabComponentAt(index, buildTabPanel(tab, c, label)); + tab.setTabComponentAt(index, buildTabPanel(tab, c, label, urls)); tab.setSelectedComponent(c); } @@ -24,7 +36,7 @@ public class TabBuilder { indexes.remove("main[waiting].responseTextArea"); } - private static JPanel buildTabPanel(JTabbedPane tab, Component c, String label) { + private static JPanel buildTabPanel(JTabbedPane tab, Component c, String label, HashMap urls) { JPanel p = new JPanel(new GridBagLayout()); p.setOpaque(false); JLabel l = new JLabel(label); @@ -35,11 +47,11 @@ public class TabBuilder { p.add(l, g); g.gridx++; g.weightx = 0; - p.add(buildCloseButton(tab, c), g); + p.add(buildCloseButton(tab, c, urls), g); return p; } - private static JButton buildCloseButton(JTabbedPane tab, Component c) { + private static JButton buildCloseButton(JTabbedPane tab, Component c, HashMap urls) { JButton b = new JButton("×"); b.setBorderPainted(false); b.setFocusPainted(false); @@ -49,6 +61,7 @@ public class TabBuilder { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); + urls.remove(c.hashCode()); tab.remove(c); } }); @@ -70,9 +83,9 @@ public class TabBuilder { private static JTabbedPane buildParametersTabbedPane() { JTabbedPane p = new JTabbedPane(); - p.add("Params", buildTable()); + p.add("Params", buildParamsTab()); p.add("Authorization", new JPanel()); - p.add("Headers", buildTable()); + p.add("Headers", buildParamsTab()); p.add("Body", new JTextArea()); return p; } @@ -92,4 +105,54 @@ public class TabBuilder { return (JTextArea) indexes.get("main[" + index + "].responseTextArea"); } + private static JPanel buildParamsTab() { + String[] headers = {"Keys", "Values"}; + Object[][] datas = {}; + DefaultTableModel model = new DefaultTableModel(datas, headers); + JPanel p = new JPanel(); + JTable t = new JTable(model); + JButton addButton = new JButton("Add new parameter"); + JButton delButton = new JButton("Delete parameter"); + delButton.setEnabled(false); + t.getSelectionModel().addListSelectionListener(new ListSelectionListener(){ + public void valueChanged(ListSelectionEvent event) { + delButton.setEnabled(t.getSelectedRows().length > 0); + } + }); + addButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + super.mouseClicked(e); + Optional result = InsertToTableDialog.showDialog("Enter value"); + if (result.isPresent()) { + DefaultTableModel m = (DefaultTableModel) t.getModel(); + m.addRow(new Object[]{result.get().getKey(), result.get().getValue()}); + } + } + }); + delButton.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent e) { + super.mouseClicked(e); + int n = t.getSelectedRows().length; + if (n > 0) { + DefaultTableModel m = (DefaultTableModel) t.getModel(); + for(int i = 0; i < n; i++) { + m.removeRow(t.getSelectedRow()); + } + } + } + }); + p.add(addButton); + p.add(delButton); + p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); + + JPanel pp = new JPanel(); + pp.add(p); + JScrollPane sp = new JScrollPane(t); + pp.add(sp); + pp.setLayout(new BoxLayout(pp, BoxLayout.Y_AXIS)); + return pp; + } + } diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/utils/InsertToTableDialog.form b/src/main/java/ovh/alexisdelhaie/endpoint/utils/InsertToTableDialog.form new file mode 100644 index 0000000..91d4557 --- /dev/null +++ b/src/main/java/ovh/alexisdelhaie/endpoint/utils/InsertToTableDialog.form @@ -0,0 +1,105 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/utils/InsertToTableDialog.java b/src/main/java/ovh/alexisdelhaie/endpoint/utils/InsertToTableDialog.java new file mode 100644 index 0000000..02796c5 --- /dev/null +++ b/src/main/java/ovh/alexisdelhaie/endpoint/utils/InsertToTableDialog.java @@ -0,0 +1,79 @@ +package ovh.alexisdelhaie.endpoint.utils; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.Optional; + +public class InsertToTableDialog extends JDialog { + + public static final int WIDTH = 325; + public static final int HEIGHT = 195; + + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + private JTextField keyField; + private JTextField valueField; + private JLabel message; + + private boolean accepted = false; + + private InsertToTableDialog(String message) { + setTitle("Insert"); + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + this.message.setText(message); + + buttonOK.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onOK(); + } + }); + + buttonCancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + } + + private void onOK() { + accepted = true; + dispose(); + } + + private void onCancel() { + dispose(); + } + + public static Optional showDialog(String message) { + InsertToTableDialog dialog = new InsertToTableDialog(message); + dialog.setModal(true); + dialog.setMinimumSize(new Dimension(WIDTH, HEIGHT)); + dialog.setMaximumSize(new Dimension(WIDTH, HEIGHT)); + dialog.setResizable(false); + Tools.centerFrame(dialog); + dialog.setVisible(true); + if (dialog.accepted && !dialog.keyField.getText().isBlank()) { + return Optional.of(new KeyValuePair(dialog.keyField.getText(), dialog.valueField.getText())); + } + return Optional.empty(); + } +} diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/utils/KeyValuePair.java b/src/main/java/ovh/alexisdelhaie/endpoint/utils/KeyValuePair.java new file mode 100644 index 0000000..1766c68 --- /dev/null +++ b/src/main/java/ovh/alexisdelhaie/endpoint/utils/KeyValuePair.java @@ -0,0 +1,20 @@ +package ovh.alexisdelhaie.endpoint.utils; + +public class KeyValuePair { + + private String key; + private String value; + + public KeyValuePair(String key, String value) { + this.key = key; + this.value = value; + } + + public String getKey() { + return key; + } + + public String getValue() { + return value; + } +} diff --git a/src/main/java/ovh/alexisdelhaie/endpoint/utils/Tools.java b/src/main/java/ovh/alexisdelhaie/endpoint/utils/Tools.java new file mode 100644 index 0000000..8a64724 --- /dev/null +++ b/src/main/java/ovh/alexisdelhaie/endpoint/utils/Tools.java @@ -0,0 +1,21 @@ +package ovh.alexisdelhaie.endpoint.utils; + +import java.awt.*; + +public class Tools { + + public static Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); + + public static void centerFrame(Dialog dialog) { + int y = (int)( screen.getHeight() / 2 ) - dialog.getHeight() / 2; + int x = (int)( screen.getWidth() / 2 ) - dialog.getWidth() / 2; + dialog.setLocation(x, y); + } + + public static void centerFrame(Frame frame) { + int y = (int)( screen.getHeight() / 2 ) - frame.getHeight() / 2; + int x = (int)( screen.getWidth() / 2 ) - frame.getWidth() / 2; + frame.setLocation(x, y); + } + +}