Convert project from JavaFX to Swing #1

Merged
mojitaurelie merged 6 commits from swing into master 2020-10-25 21:52:36 +01:00
8 changed files with 421 additions and 43 deletions
Showing only changes of commit 78fb958c22 - Show all commits

View File

@@ -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);
}
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ovh.alexisdelhaie.endpoint.MainWindow">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="4" column-count="9" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="1098" height="665"/>
@@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<component id="d63fe" class="javax.swing.JComboBox" binding="comboBox1" default-binding="true">
<component id="d63fe" class="javax.swing.JComboBox" binding="methodBox">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
@@ -22,9 +22,9 @@
</model>
</properties>
</component>
<component id="4f0bf" class="javax.swing.JTextField" binding="textField1" default-binding="true">
<component id="4f0bf" class="javax.swing.JTextField" binding="urlField">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<grid row="0" column="1" row-span="1" col-span="7" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
@@ -32,7 +32,7 @@
</component>
<component id="842fb" class="javax.swing.JButton" binding="sendButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="8" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Send"/>
@@ -40,7 +40,7 @@
</component>
<tabbedpane id="59105" binding="tabbedPane1" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<grid row="2" column="0" row-span="1" col-span="9" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/>
</grid>
</constraints>
@@ -50,12 +50,51 @@
</tabbedpane>
<component id="269e7" class="javax.swing.JButton" binding="newTabButton">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="1" column="8" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="+"/>
</properties>
</component>
<component id="865d3" class="javax.swing.JProgressBar" binding="progressBar1" default-binding="true">
<constraints>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<indeterminate value="true"/>
<visible value="false"/>
</properties>
</component>
<hspacer id="99ebb">
<constraints>
<grid row="3" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="106fa">
<constraints>
<grid row="3" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="82267">
<constraints>
<grid row="3" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="b9351">
<constraints>
<grid row="3" column="6" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="14145">
<constraints>
<grid row="3" column="7" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<hspacer id="330cc">
<constraints>
<grid row="3" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
</grid>
</form>

View File

@@ -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<String> comboBox1;
private JTextField textField1;
private JComboBox<String> 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<Integer, String> 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<JSplitPane> 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<Response> 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<Response> 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<JSplitPane> getSelectedTab() {

View File

@@ -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<String, Component> indexes = new HashMap<>();
public static void create(JTabbedPane tab, String label) {
public static void create(JTabbedPane tab, String label, HashMap<Integer, String> 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<Integer, String> 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<Integer, String> 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<KeyValuePair> 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;
}
}

View File

@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ovh.alexisdelhaie.endpoint.utils.InsertToTableDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>
<xy x="48" y="54" width="436" height="214"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<hspacer id="98af6">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="true" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="buttonOK">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="OK"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="c8c26" class="javax.swing.JLabel" binding="message">
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="24"/>
<text value="Label"/>
</properties>
</component>
<component id="b2442" class="javax.swing.JTextField" binding="keyField">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="2a539" class="javax.swing.JTextField" binding="valueField">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="ae42e" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Key"/>
</properties>
</component>
<component id="481b3" class="javax.swing.JLabel">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Value"/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>

View File

@@ -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<KeyValuePair> 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();
}
}

View File

@@ -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;
}
}

View File

@@ -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);
}
}