Auth dialog, Parsing URL Parameters

This commit is contained in:
Alexis Delhaie
2020-11-18 19:16:58 +01:00
parent ebf343b063
commit 12456e3474
12 changed files with 447 additions and 24 deletions

View File

@@ -1,17 +1,18 @@
package ovh.alexisdelhaie.endpoint; package ovh.alexisdelhaie.endpoint;
import com.formdev.flatlaf.FlatIntelliJLaf; import ovh.alexisdelhaie.endpoint.configuration.ConfigurationProperties;
import ovh.alexisdelhaie.endpoint.utils.Tools; import ovh.alexisdelhaie.endpoint.utils.Tools;
import javax.swing.*; import javax.swing.*;
import java.awt.*;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class Application { public class Application {
public static void main(String[] args) throws UnsupportedLookAndFeelException, IOException { public static void main(String[] args) throws UnsupportedLookAndFeelException, IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
UIManager.setLookAndFeel(new FlatIntelliJLaf()); ConfigurationProperties props = new ConfigurationProperties();
MainWindow dialog = new MainWindow(); UIManager.setLookAndFeel(Tools.getLookAndFeel(props.getStringProperty("theme", "IntelliJ")));
MainWindow dialog = new MainWindow(props);
dialog.pack(); dialog.pack();
dialog.setTitle("EndPoint"); dialog.setTitle("EndPoint");
dialog.setVisible(true); dialog.setVisible(true);

View File

@@ -14,6 +14,7 @@ import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import javax.swing.event.DocumentEvent; import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener; import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
@@ -25,6 +26,8 @@ import java.security.NoSuchAlgorithmException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainWindow extends JFrame { public class MainWindow extends JFrame {
@@ -64,8 +67,8 @@ public class MainWindow extends JFrame {
private final ConfigurationProperties props; private final ConfigurationProperties props;
private final ConcurrentHashMap<Integer, RequestTab> tabs; private final ConcurrentHashMap<Integer, RequestTab> tabs;
public MainWindow() throws IOException { public MainWindow(ConfigurationProperties props) throws IOException {
props = new ConfigurationProperties(); this.props = props;
tabs = new ConcurrentHashMap<>(); tabs = new ConcurrentHashMap<>();
setIconImage(ImageIO.read(MainWindow.class.getResource("/icon.png"))); setIconImage(ImageIO.read(MainWindow.class.getResource("/icon.png")));
setContentPane(contentPane); setContentPane(contentPane);
@@ -77,7 +80,7 @@ public class MainWindow extends JFrame {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
super.mouseClicked(e); super.mouseClicked(e);
ConfigurationDialog.showDialog(props); ConfigurationDialog.showDialog(props, MainWindow.this);
} }
}); });
newTabButton.addMouseListener(new MouseAdapter() { newTabButton.addMouseListener(new MouseAdapter() {
@@ -141,6 +144,7 @@ public class MainWindow extends JFrame {
URL u = new URL((!urlField.getText().toLowerCase().startsWith("http://") && URL u = new URL((!urlField.getText().toLowerCase().startsWith("http://") &&
!urlField.getText().toLowerCase().startsWith("https://")) ? !urlField.getText().toLowerCase().startsWith("https://")) ?
"http://" + urlField.getText() : urlField.getText()); "http://" + urlField.getText() : urlField.getText());
parseParamsFromUrl(urlField.getText());
if (u.getPath().isBlank()) { if (u.getPath().isBlank()) {
title.setText(u.getHost()); title.setText(u.getHost());
} else { } else {
@@ -282,4 +286,25 @@ public class MainWindow extends JFrame {
return Optional.empty(); return Optional.empty();
} }
private void parseParamsFromUrl(String url) {
Optional<JSplitPane> possibleTab = getSelectedTab();
if (possibleTab.isPresent()) {
int id = tabbedPane1.indexOfComponent(possibleTab.get());
JTable table = TabBuilder.getParamsTable(id);
DefaultTableModel m = (DefaultTableModel) table.getModel();
for (int i = m.getRowCount() - 1; i > -1; i--) {
m.removeRow(i);
}
Pattern pattern = Pattern.compile("[^&?]*?=[^&?]*", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(url);
while (matcher.find()) {
String param = matcher.group();
String[] kv = param.split("=");
if (kv.length == 2) {
m.addRow(new Object[]{kv[0], kv[1]});
}
}
}
}
} }

View File

@@ -0,0 +1,16 @@
package ovh.alexisdelhaie.endpoint.builder;
import javax.swing.table.DefaultTableModel;
public class ReadOnlyTableModel extends DefaultTableModel {
public ReadOnlyTableModel(Object[][] objects, String[] headers) {
super(objects, headers);
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
}

View File

@@ -2,10 +2,7 @@ package ovh.alexisdelhaie.endpoint.builder;
import ovh.alexisdelhaie.endpoint.utils.RequestTab; import ovh.alexisdelhaie.endpoint.utils.RequestTab;
import ovh.alexisdelhaie.endpoint.utils.Tools; import ovh.alexisdelhaie.endpoint.utils.Tools;
import ovh.alexisdelhaie.endpoint.utils.adapter.CustomDeleteMouseAdapter; import ovh.alexisdelhaie.endpoint.utils.adapter.*;
import ovh.alexisdelhaie.endpoint.utils.adapter.CustomNewMouseAdapter;
import ovh.alexisdelhaie.endpoint.utils.adapter.DeleteParamMouseAdapter;
import ovh.alexisdelhaie.endpoint.utils.adapter.NewParamMouseAdapter;
import javax.swing.*; import javax.swing.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
@@ -93,7 +90,6 @@ public class TabBuilder {
private static JTabbedPane buildParametersTabbedPane(JTextField urlField) { private static JTabbedPane buildParametersTabbedPane(JTextField urlField) {
JTabbedPane p = new JTabbedPane(); JTabbedPane p = new JTabbedPane();
p.add("Params", buildParamsTab(true, urlField)); p.add("Params", buildParamsTab(true, urlField));
p.add("Authorization", new JPanel());
p.add("Headers", buildParamsTab(false, null)); p.add("Headers", buildParamsTab(false, null));
JTextArea body = new JTextArea(); JTextArea body = new JTextArea();
indexes.put("main[waiting].body", body); indexes.put("main[waiting].body", body);
@@ -138,6 +134,10 @@ public class TabBuilder {
return Tools.tableToHashMap(t); return Tools.tableToHashMap(t);
} }
public static JTable getParamsTable(int index) {
return (JTable) indexes.get("main[" + index + "].params");
}
public static HashMap<String, String> getHeaders(int index) { public static HashMap<String, String> getHeaders(int index) {
JTable t = (JTable) indexes.get("main[" + index + "].headers"); JTable t = (JTable) indexes.get("main[" + index + "].headers");
return Tools.tableToHashMap(t); return Tools.tableToHashMap(t);
@@ -149,7 +149,8 @@ public class TabBuilder {
private static JPanel buildParamsTab(boolean isParam, JTextField urlField) { private static JPanel buildParamsTab(boolean isParam, JTextField urlField) {
String[] headers = {"Keys", "Values"}; String[] headers = {"Keys", "Values"};
DefaultTableModel model = new DefaultTableModel(new Object[][]{}, headers); DefaultTableModel model = (isParam)? new ReadOnlyTableModel(new Object[][]{}, headers) :
new DefaultTableModel(new Object[][]{}, headers);
JPanel p = new JPanel(); JPanel p = new JPanel();
JTable t = new JTable(model); JTable t = new JTable(model);
indexes.put((isParam) ? "main[waiting].params" : "main[waiting].headers", t); indexes.put((isParam) ? "main[waiting].params" : "main[waiting].headers", t);
@@ -157,17 +158,19 @@ public class TabBuilder {
JButton delButton = new JButton("Remove"); JButton delButton = new JButton("Remove");
delButton.setEnabled(false); delButton.setEnabled(false);
t.getSelectionModel().addListSelectionListener(event -> delButton.setEnabled(t.getSelectedRows().length > 0)); t.getSelectionModel().addListSelectionListener(event -> delButton.setEnabled(t.getSelectedRows().length > 0));
p.add(addButton);
p.add(delButton);
if (isParam) { if (isParam) {
addButton.addMouseListener(new NewParamMouseAdapter(t, urlField)); addButton.addMouseListener(new NewParamMouseAdapter(t, urlField));
delButton.addMouseListener(new DeleteParamMouseAdapter(t, urlField)); delButton.addMouseListener(new DeleteParamMouseAdapter(t, urlField));
} else { } else {
addButton.addMouseListener(new CustomNewMouseAdapter(t)); addButton.addMouseListener(new CustomNewMouseAdapter(t));
delButton.addMouseListener(new CustomDeleteMouseAdapter(t)); delButton.addMouseListener(new CustomDeleteMouseAdapter(t));
JButton authButton = new JButton("Set authorization");
authButton.addMouseListener(new AuthorizationAdapter(t));
p.add(authButton);
} }
p.add(addButton);
p.add(delButton);
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS)); p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
JPanel pp = new JPanel(); JPanel pp = new JPanel();
pp.add(p); pp.add(p);
JScrollPane sp = new JScrollPane(t); JScrollPane sp = new JScrollPane(t);

View File

@@ -13,7 +13,7 @@ public class AboutDialog extends JDialog {
public static final int WIDTH = 740; public static final int WIDTH = 740;
public static final int HEIGHT = 500; public static final int HEIGHT = 500;
public static final String VERSION = "0.1.4"; public static final String VERSION = "0.1.5";
private JPanel contentPane; private JPanel contentPane;
private JLabel version; private JLabel version;

View File

@@ -49,7 +49,7 @@
</component> </component>
</children> </children>
</grid> </grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="7" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="e3588" layout-manager="GridLayoutManager" row-count="9" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/> <margin top="0" left="0" bottom="0" right="0"/>
<constraints> <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"/> <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"/>
@@ -73,7 +73,7 @@
</hspacer> </hspacer>
<vspacer id="d1324"> <vspacer id="d1324">
<constraints> <constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/> <grid row="8" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<component id="c7f68" class="javax.swing.JLabel"> <component id="c7f68" class="javax.swing.JLabel">
@@ -168,6 +168,40 @@
</constraints> </constraints>
<properties/> <properties/>
</component> </component>
<component id="ff47a" class="javax.swing.JLabel">
<constraints>
<grid row="6" 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>
<font style="1"/>
<text value="Appearance"/>
</properties>
</component>
<component id="e33fb" class="javax.swing.JLabel">
<constraints>
<grid row="7" 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="Theme"/>
</properties>
</component>
<hspacer id="1d971">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d304c" class="javax.swing.JComboBox" binding="theme">
<constraints>
<grid row="7" column="2" row-span="1" col-span="3" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="IntelliJ"/>
<item value="Darcula"/>
<item value="System"/>
</model>
</properties>
</component>
</children> </children>
</grid> </grid>
</children> </children>

View File

@@ -1,9 +1,11 @@
package ovh.alexisdelhaie.endpoint.configuration; package ovh.alexisdelhaie.endpoint.configuration;
import ovh.alexisdelhaie.endpoint.utils.MessageDialog;
import ovh.alexisdelhaie.endpoint.utils.Tools; import ovh.alexisdelhaie.endpoint.utils.Tools;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Objects;
public class ConfigurationDialog extends JDialog { public class ConfigurationDialog extends JDialog {
@@ -17,10 +19,11 @@ public class ConfigurationDialog extends JDialog {
private JComboBox<String> httpVersion; private JComboBox<String> httpVersion;
private JSpinner timeout; private JSpinner timeout;
private JButton aboutButton; private JButton aboutButton;
private JComboBox theme;
private final ConfigurationProperties props; private final ConfigurationProperties props;
private ConfigurationDialog(ConfigurationProperties props) { private ConfigurationDialog(ConfigurationProperties props, JFrame frame) {
setContentPane(contentPane); setContentPane(contentPane);
setModal(true); setModal(true);
getRootPane().setDefaultButton(buttonOK); getRootPane().setDefaultButton(buttonOK);
@@ -32,6 +35,7 @@ public class ConfigurationDialog extends JDialog {
allowDowngrade.setSelected(this.props.getBooleanProperty("allowDowngrade", true)); allowDowngrade.setSelected(this.props.getBooleanProperty("allowDowngrade", true));
httpVersion.setSelectedItem(this.props.getStringProperty("httpVersion", "HTTP/1.0")); httpVersion.setSelectedItem(this.props.getStringProperty("httpVersion", "HTTP/1.0"));
timeout.setValue(this.props.getIntegerProperty("timeout", 10000)); timeout.setValue(this.props.getIntegerProperty("timeout", 10000));
theme.setSelectedItem(this.props.getStringProperty("theme", "IntelliJ"));
allowInvalidSsl.addActionListener((e) -> { allowInvalidSsl.addActionListener((e) -> {
this.props.setProperty("allowInvalidSsl", String.valueOf(allowInvalidSsl.isSelected())); this.props.setProperty("allowInvalidSsl", String.valueOf(allowInvalidSsl.isSelected()));
@@ -45,15 +49,24 @@ public class ConfigurationDialog extends JDialog {
timeout.addChangeListener((e) -> { timeout.addChangeListener((e) -> {
this.props.setProperty("timeout", String.valueOf(timeout.getValue())); this.props.setProperty("timeout", String.valueOf(timeout.getValue()));
}); });
theme.addActionListener((e) -> {
String value = (String) theme.getSelectedItem();
this.props.setProperty("theme", value);
try {
UIManager.setLookAndFeel(Tools.getLookAndFeel(Objects.requireNonNull(value)));
SwingUtilities.updateComponentTreeUI(frame);
} catch(UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException | ClassNotFoundException err) {
MessageDialog.error("Error while changing theme", err.getMessage());
}
});
} }
private void onOK() { private void onOK() {
dispose(); dispose();
} }
public static void showDialog(ConfigurationProperties props) { public static void showDialog(ConfigurationProperties props, JFrame frame) {
ConfigurationDialog dialog = new ConfigurationDialog(props); ConfigurationDialog dialog = new ConfigurationDialog(props, frame);
dialog.setModal(true); dialog.setModal(true);
dialog.setMinimumSize(new Dimension(WIDTH, HEIGHT)); dialog.setMinimumSize(new Dimension(WIDTH, HEIGHT));
dialog.setMaximumSize(new Dimension(WIDTH, HEIGHT)); dialog.setMaximumSize(new Dimension(WIDTH, HEIGHT));

View File

@@ -0,0 +1,167 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ovh.alexisdelhaie.endpoint.utils.AuthorizationDialog">
<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="297"/>
</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="4" 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="83a13" class="javax.swing.JLabel">
<constraints>
<grid row="0" 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>
<font size="24"/>
<text value="Authorization"/>
</properties>
</component>
<hspacer id="b508b">
<constraints>
<grid row="0" column="1" 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="7da3c" binding="bearerPanel" 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="3" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="f9a92" class="javax.swing.JLabel">
<constraints>
<grid row="0" 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="Token"/>
</properties>
</component>
<component id="1c1f0" class="javax.swing.JTextField" binding="bearerTokenField">
<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">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
</children>
</grid>
<component id="1107a" class="javax.swing.JComboBox" binding="typeBox">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="Bearer"/>
<item value="Basic"/>
</model>
</properties>
</component>
<component id="f7e01" 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="Type"/>
</properties>
</component>
<grid id="d162d" binding="basicPanel" layout-manager="GridLayoutManager" row-count="2" 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="2" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<visible value="false"/>
</properties>
<border type="none"/>
<children>
<component id="79b35" class="javax.swing.JLabel">
<constraints>
<grid row="0" 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="Username"/>
</properties>
</component>
<component id="a6880" 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="Password"/>
</properties>
</component>
<component id="ae39c" class="javax.swing.JTextField" binding="usernameBasicField">
<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">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="26ce0" class="javax.swing.JTextField" binding="passwordBasicField">
<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>
</children>
</grid>
</children>
</grid>
</children>
</grid>
</form>

View File

@@ -0,0 +1,100 @@
package ovh.alexisdelhaie.endpoint.utils;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import java.util.Optional;
public class AuthorizationDialog extends JDialog {
public static final int WIDTH = 325;
public static final int HEIGHT = 220;
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
private JComboBox<String> typeBox;
private JPanel bearerPanel;
private JTextField bearerTokenField;
private JTextField usernameBasicField;
private JTextField passwordBasicField;
private JPanel basicPanel;
private boolean accepted = false;
private String finalValue = "";
public AuthorizationDialog() {
setTitle("Set authorization");
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);
buttonOK.addActionListener(e -> onOK());
buttonCancel.addActionListener(e -> onCancel());
typeBox.addItemListener((e) -> {
String type = (String) typeBox.getSelectedItem();
switch (Objects.requireNonNull(type)) {
case "Basic" -> {
bearerPanel.setVisible(false);
basicPanel.setVisible(true);
}
case "Bearer" -> {
basicPanel.setVisible(false);
bearerPanel.setVisible(true);
}
}
});
// 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() {
String type = (String) typeBox.getSelectedItem();
switch (Objects.requireNonNull(type)) {
case "Basic" -> finalValue = Tools.toBase64(
String.format(
"%s:%s",
usernameBasicField.getText(),
passwordBasicField.getText()
)
);
case "Bearer" -> finalValue = String.format("Bearer %s", bearerTokenField.getText());
}
accepted = true;
dispose();
}
private void onCancel() {
dispose();
}
public static Optional<KeyValuePair> showDialog() {
AuthorizationDialog dialog = new AuthorizationDialog();
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) {
return Optional.of(new KeyValuePair("authorization", dialog.finalValue));
}
return Optional.empty();
}
}

View File

@@ -1,8 +1,13 @@
package ovh.alexisdelhaie.endpoint.utils; package ovh.alexisdelhaie.endpoint.utils;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.FlatIntelliJLaf;
import javax.swing.*; import javax.swing.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import java.awt.*; import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Base64;
import java.util.HashMap; import java.util.HashMap;
public class Tools { public class Tools {
@@ -32,4 +37,21 @@ public class Tools {
return result; return result;
} }
public static String toBase64(String decoded) {
byte[] encodedBytes = Base64.getEncoder().encode(decoded.getBytes());
return new String(encodedBytes);
}
public static String getLookAndFeel(String theme) {
switch (theme) {
case "IntelliJ" -> {
return FlatIntelliJLaf.class.getName();
}
case "Darcula" -> {
return FlatDarculaLaf.class.getName();
}
}
return UIManager.getSystemLookAndFeelClassName();
}
} }

View File

@@ -0,0 +1,36 @@
package ovh.alexisdelhaie.endpoint.utils.adapter;
import ovh.alexisdelhaie.endpoint.utils.AuthorizationDialog;
import ovh.alexisdelhaie.endpoint.utils.KeyValuePair;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Optional;
public class AuthorizationAdapter extends MouseAdapter {
protected final JTable table;
public AuthorizationAdapter(JTable table) {
this.table = table;
}
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
Optional<KeyValuePair> result = AuthorizationDialog.showDialog();
if (result.isPresent()) {
DefaultTableModel m = (DefaultTableModel) table.getModel();
for (int i = m.getRowCount() - 1; i > -1; i--) {
String key = (String) m.getValueAt(i,0);
if (key.toLowerCase().equals(result.get().getKey().toLowerCase())) {
m.removeRow(i);
}
}
m.addRow(new Object[]{result.get().getKey(), result.get().getValue()});
}
}
}

View File

@@ -28,6 +28,12 @@ public class CustomNewMouseAdapter extends MouseAdapter {
Optional<KeyValuePair> result = InsertToTableDialog.showDialog("Enter value"); Optional<KeyValuePair> result = InsertToTableDialog.showDialog("Enter value");
if (result.isPresent()) { if (result.isPresent()) {
DefaultTableModel m = (DefaultTableModel) table.getModel(); DefaultTableModel m = (DefaultTableModel) table.getModel();
for (int i = m.getRowCount() - 1; i > -1; i--) {
String key = (String) m.getValueAt(i,0);
if (key.toLowerCase().equals(result.get().getKey().toLowerCase())) {
m.removeRow(i);
}
}
m.addRow(new Object[]{result.get().getKey(), result.get().getValue()}); m.addRow(new Object[]{result.get().getKey(), result.get().getValue()});
valid = true; valid = true;
} }