Commit d4a0e680 authored by 9731301's avatar 9731301

complete send button actionListener

parent c3e683b0
......@@ -10,6 +10,8 @@
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/GUI/InsomniaGUI.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/GUI/InsomniaGUI.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/GUI/RequestPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/GUI/RequestPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/GUI/RespondPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/GUI/RespondPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/com/insomnia/parser/RequestLoader.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/parser/RequestLoader.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -144,7 +146,7 @@
<workItem from="1590682418201" duration="308000" />
<workItem from="1590757579057" duration="2543000" />
<workItem from="1590934179600" duration="31544000" />
<workItem from="1591024948574" duration="4476000" />
<workItem from="1591024948574" duration="14918000" />
</task>
<servers />
</component>
......@@ -155,10 +157,10 @@
<option name="ADD_EXTERNAL_FILES_SILENTLY" value="true" />
</component>
<component name="WindowStateProjectService">
<state x="472" y="159" key="#com.intellij.ide.util.MemberChooser" timestamp="1590962459070">
<state x="472" y="159" key="#com.intellij.ide.util.MemberChooser" timestamp="1591034675582">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="472" y="159" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1590962459070" />
<state x="472" y="159" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1591034675582" />
<state x="499" y="197" key="#com.intellij.ide.util.TreeClassChooserDialog" timestamp="1590961999762">
<screen x="0" y="0" width="1536" height="824" />
</state>
......@@ -175,22 +177,22 @@
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="402" y="167" key="FileChooserDialogImpl/0.0.1536.824@0.0.1536.824" timestamp="1590494460700" />
<state width="1493" height="2" key="GridCell.Tab.0.bottom" timestamp="1591028946800">
<state width="1493" height="105" key="GridCell.Tab.0.bottom" timestamp="1591040084950">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1591028946800" />
<state width="1493" height="2" key="GridCell.Tab.0.center" timestamp="1591028946800">
<state width="1493" height="105" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1591040084950" />
<state width="1493" height="105" key="GridCell.Tab.0.center" timestamp="1591040084950">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1591028946800" />
<state width="1493" height="2" key="GridCell.Tab.0.left" timestamp="1591028946800">
<state width="1493" height="105" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1591040084950" />
<state width="1493" height="105" key="GridCell.Tab.0.left" timestamp="1591040084950">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1591028946800" />
<state width="1493" height="2" key="GridCell.Tab.0.right" timestamp="1591028946800">
<state width="1493" height="105" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1591040084950" />
<state width="1493" height="105" key="GridCell.Tab.0.right" timestamp="1591040084950">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state width="1493" height="2" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1591028946800" />
<state width="1493" height="105" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1591040084950" />
<state x="249" y="0" key="SettingsEditor" timestamp="1590962002174">
<screen x="0" y="0" width="1536" height="824" />
</state>
......
......@@ -223,7 +223,7 @@ public class InsomniaGUI {
panel.add(panelRight , BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.pack();
frame.setSize(800,400);
}
......
package com.insomnia.GUI;
import com.insomnia.parser.RequestData;
import com.insomnia.parser.RequestLoader;
import com.insomnia.parser.ResponseData;
import com.insomnia.parser.Saver;
import javax.imageio.ImageIO;
import javax.swing.*;
......@@ -8,14 +10,21 @@ import javax.swing.text.Utilities;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.DataInput;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
public class RequestPanel extends JPanel {
private VerticalFlowLayout vfLayout = new VerticalFlowLayout();
private String[] ss = {"JSON","Form Data"};
private String[] ss = {"Form Data","JSON","Binary Data"};
private JComboBox Json_FormData = new JComboBox(ss);
private JPanel body_pane = new JPanel(){
@Override
public Dimension getPreferredSize(){
return body.getSize();
}
};
private JPanel body = new JPanel();
private JPanel auth = new JPanel();
private JPanel query = new JPanel();
......@@ -42,8 +51,18 @@ public class RequestPanel extends JPanel {
BufferedImage master;
master = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\Insomnia\\src\\image\\535246.png"));
Image scaled = master.getScaledInstance(20,20, java.awt.Image.SCALE_SMOOTH);
body.setBackground(Color.GRAY);
body.add(Json_FormData,BorderLayout.NORTH);
body.add(body_pane);
Json_FormData.setSelectedIndex(0);
initBody();
Json_FormData.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
initBody();
}
});
auth.setBackground(Color.GRAY);
query.setBackground(Color.GRAY);
mainCenterHeader.setBackground(Color.GRAY);
......@@ -106,6 +125,14 @@ public class RequestPanel extends JPanel {
}
});
body_pane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (((JPanel) body_pane.getComponent(0)).getComponentCount()==0 && body_insert!=null && selectedItem==0) body_insert.mouseClicked(null);
}
});
headerField.addMouseListener(m);
......@@ -127,7 +154,11 @@ public class RequestPanel extends JPanel {
boolean isLastComponent(Component c){
return mainCenterHeader.getComponent(mainCenterHeader.getComponentCount()-1)==c;
return isLastComponent(c,mainCenterHeader);
}
boolean isLastComponent(Component c,JPanel p){
return p.getComponent(p.getComponentCount()-1)==c;
}
public HashMap<String,String> getHeaders(){
......@@ -149,23 +180,190 @@ public class RequestPanel extends JPanel {
return headers;
}
public HashMap<String,String> getFormDataBody(){
HashMap<String,String> bodydata = new HashMap<>();
for(Component c : ((JPanel)body_pane.getComponent(0)).getComponents()){
if (c instanceof JPanel){
JPanel nh = (JPanel) c;
JPanel l = (JPanel) nh.getComponent(1);
JTextField h = (JTextField) l.getComponent(0);
JTextField v = (JTextField) l.getComponent(1);
if (!h.getText().trim().isEmpty()){
bodydata.put(h.getText().trim(),v.getText());
}
}
}
return bodydata;
}
public String getFormDataBodyAsString(){
HashMap<String,String> b = getFormDataBody();
if(b.size()==0) return "";
String f = "";
for (String key : b.keySet()){
f = f+key+"="+b.get(key)+"&";
}
return f.substring(0,f.length()-1);
}
int selectedItem = -2;
MouseAdapter body_insert = null;
void initBody(){
if (selectedItem == Json_FormData.getSelectedIndex()) return;
selectedItem = Json_FormData.getSelectedIndex();
if(body_pane.getComponentCount()>0) {
Component[] components = body_pane.getComponents();
for(Component c : components) body_pane.remove(c);
}
Border border = BorderFactory.createLineBorder(Color.BLACK,3);
if(Json_FormData.getSelectedItem().equals("JSON")) {
JTextArea textArea = new JTextArea(){
@Override
public Dimension getPreferredSize(){
return body_pane.getSize();
}
};
textArea.setToolTipText("Enter JSON");
textArea.setBorder(border);
body_pane.add(textArea);
} else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField path = new JTextField("File Path"){
@Override
public Dimension getPreferredSize(){
return new Dimension(body_pane.getSize().width-16,28);
}
};
path.setBorder(border);
body_pane.add(path,BorderLayout.NORTH);
JButton selector = new JButton("Choose File");
body_pane.add(selector,BorderLayout.NORTH);
selector.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
FileDialog fc = new FileDialog((Frame) null,"Select a file to upload");
fc.setMode(FileDialog.LOAD);
fc.setVisible(true);
String f = fc.getFile();
if (f!=null && !f.isEmpty()) {
path.setText(new File(fc.getDirectory(),fc.getName()).getAbsolutePath());
}
}
});
}else {
BufferedImage master = null;
try {
master = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\Insomnia\\src\\image\\535246.png"));
} catch (IOException e) {
}
final Image scaled = master.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH);
final JPanel vl = new JPanel(vfLayout) {
@Override
public Dimension getPreferredSize() {
return body_pane.getSize();
}
};
body_pane.add(vl);
if (body_insert==null){
MouseAdapter deleteClick = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
JButton Btn = (JButton) e.getSource();
//to repair showing
vl.remove(((JPanel) Btn.getParent()).getParent());
vl.revalidate();
vl.repaint();
}
};
body_insert = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e != null && !isLastComponent(((Component) e.getSource()).getParent().getParent(), vl)) return;
JTextField newHeaderField = new JTextField("key");
newHeaderField.addMouseListener(this);
newHeaderField.setBorder(border);
JTextField newValueField = new JTextField("value");
newValueField.setBorder(border);
JButton newDelete = new JButton();
newDelete.setPreferredSize(new Dimension(20, 20));
newDelete.setIcon(new ImageIcon(scaled));
newDelete.addMouseListener(deleteClick);
JPanel newRightOfHeader = new JPanel();
newRightOfHeader.add(newDelete);
JPanel new_center_header = new JPanel(new GridLayout(1, 2));
new_center_header.add(newHeaderField);
new_center_header.add(newValueField);
JPanel newHeader = new JPanel(new BorderLayout());
newHeader.add(newRightOfHeader, BorderLayout.EAST);
newHeader.add(new_center_header, BorderLayout.CENTER);
vl.add(newHeader);
vl.validate();
}
};
}
body_insert.mouseClicked(null);
}
body_pane.revalidate();
body_pane.repaint();
}
//request_up
public JPanel centerUp(){
sendButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
RequestData requestData = new RequestData();
// requestData.setUploadPath();
requestData.setUrl(URLTextField.getText());
super.mouseClicked(e);
Saver saver = new Saver();
//set if it is Json or not
if(Json_FormData.getActionCommand().equals("JSON"));
if(Json_FormData.getSelectedItem().equals("JSON")) {
requestData.setJson(true);
JTextArea textArea = (JTextArea) body_pane.getComponent(0);
requestData.setBody(textArea.getText().trim());
}
else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField p = (JTextField) body_pane.getComponent(0);
if (!p.getText().isEmpty() && !p.getText().equals("File Path"))
requestData.setUploadPath(p.getText());
}else {
requestData.setBody(getFormDataBodyAsString());
}
requestData.setHeaders(getHeaders());
for(String h : requestData.getHeaders().keySet()){
System.out.println(h+" : "+requestData.getHeaders().get(h));
requestData.setFollowRedirects(true);
requestData.setShowResponseHeaders(true);
requestData.setSaveRequest(false);
ResponseData data = RequestLoader.run(requestData);
if (data.hasSyntaxError()){
System.out.println("Syntax Error : "+data.getSyntaxErrorLog());
}else{
System.out.println("Connection time : "+((data.getFinalTime()-data.getStartTime())/1000)+"s");
System.out.println("Response Status : "+ data.getResponseStatus());
if (data.isSuccessful()){
System.out.println("Response : "+data.getResponseString());
if (data.getRequestData().isShowResponseHeaders()&& data.getResponseHeaders()!=null){
System.out.println("******* Response Headers *******");
for (String key : data.getResponseHeaders().keySet()){
System.out.println(key+" : "+data.getResponseHeaders().get(key).toString());
}
}
}else {
System.out.println("Error : "+data.getErrorMessage());
}
}
}
});
......
......@@ -10,7 +10,12 @@ public class RespondPanel extends JPanel {
private Border border = BorderFactory.createLineBorder(Color.BLACK,3);
private VerticalFlowLayout vfLayout = new VerticalFlowLayout();
private JPanel row = new JPanel();
private JTextArea massageBody = new JTextArea();
private JTextArea massageBody = new JTextArea() {
@Override
public Dimension getPreferredSize() {
return row.getSize();
}
};
private JPanel mainCenterHeader_respond = new JPanel(vfLayout);
private JScrollPane scrollPane_Header_Respond = new JScrollPane(mainCenterHeader_respond);
private JPanel header_MainPanel = new JPanel(new BorderLayout());
......@@ -22,7 +27,7 @@ public class RespondPanel extends JPanel {
public JTabbedPane tabbedPaneCenterResponse() {
massageBody.setLineWrap(true);
massageBody.setPreferredSize(new Dimension(200, 300));
massageBody.setBorder(border);
row.add(massageBody);
row.setBackground(Color.GRAY);
scrollPane_Header_Respond.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
......
......@@ -88,13 +88,13 @@ public class RequestLoader {
String crlf = "\r\n";
if (!data.getBody().isEmpty()) {
HashMap<String, String> fields = new HashMap<>();
if (!data.getBody().contains(";")) {
String[] f1 = data.getBody().split(":");
if (!data.getBody().contains("&")) {
String[] f1 = data.getBody().split("=");
fields.put(f1[0], f1[1]);
} else {
String[] mFields = data.getBody().split(";");
String[] mFields = data.getBody().split("&");
for (String f : mFields) {
String[] f0 = f.split(":");
String[] f0 = f.split("=");
fields.put(f0[0], f0[1]);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment