Commit 2b7258bf authored by 9731301's avatar 9731301

add saveRequest button

parent fa3710c7
This diff is collapsed.
...@@ -5,6 +5,7 @@ package com.insomnia.GUI; ...@@ -5,6 +5,7 @@ package com.insomnia.GUI;
* @version 0.0 * @version 0.0
*/ */
import com.insomnia.parser.RequestData;
import com.insomnia.parser.ResponseData; import com.insomnia.parser.ResponseData;
import javax.swing.*; import javax.swing.*;
...@@ -19,6 +20,7 @@ import static java.awt.Frame.*; ...@@ -19,6 +20,7 @@ import static java.awt.Frame.*;
public class InsomniaGUI { public class InsomniaGUI {
JFrame frame ; JFrame frame ;
RespondPanel response; RespondPanel response;
RequestPanel request;
ResponseData data_req = null; ResponseData data_req = null;
public void load(ResponseData d){ public void load(ResponseData d){
...@@ -26,6 +28,15 @@ public class InsomniaGUI { ...@@ -26,6 +28,15 @@ public class InsomniaGUI {
response.load(d); response.load(d);
} }
public boolean hasRequest(){
return data_req!=null;
}
public void loadRequest (RequestData r){
request.load(r);
}
public InsomniaGUI() throws IOException { public InsomniaGUI() throws IOException {
frame = new JFrame(); frame = new JFrame();
frame.setTitle("Insomnia"); frame.setTitle("Insomnia");
...@@ -34,14 +45,14 @@ public class InsomniaGUI { ...@@ -34,14 +45,14 @@ public class InsomniaGUI {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true); frame.setVisible(true);
RequestPanel request = new RequestPanel(this); request = new RequestPanel(this);
request.createRequestPanel(); request.createRequestPanel();
response = new RespondPanel(); response = new RespondPanel();
response.createRespondPanel(); response.createRespondPanel();
JPanel panelRight = new JPanel(new GridLayout(1 ,2 )); JPanel panelRight = new JPanel(new GridLayout(1 ,2 ));
panelRight.add(request); panelRight.add(request);
panelRight.add(response); panelRight.add(response);
LeftPanel leftPanel = new LeftPanel(); LeftPanel leftPanel = new LeftPanel(this);
leftPanel.createLeftPanel(); leftPanel.createLeftPanel();
//menu upper_Items //menu upper_Items
......
...@@ -10,16 +10,21 @@ import java.awt.*; ...@@ -10,16 +10,21 @@ import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
import java.io.File;
public class LeftPanel extends JPanel { public class LeftPanel extends JPanel {
Border border = BorderFactory.createLineBorder(Color.BLACK,3); Border border = BorderFactory.createLineBorder(Color.BLACK,3);
VerticalFlowLayout vfLayout = new VerticalFlowLayout(); VerticalFlowLayout vfLayout = new VerticalFlowLayout();
JButton addRequest = new JButton("Add Request"); JButton addRequest = new JButton("Save Request");
JMenu menu = new JMenu("Insomnia"); JMenu menu = new JMenu("Insomnia");
JPanel requestList = new JPanel(vfLayout); JPanel requestList = new JPanel(vfLayout);
JScrollPane scrollRequests = new JScrollPane(requestList); JScrollPane scrollRequests = new JScrollPane(requestList);
JPanel up_left = new JPanel(new GridLayout(2,1)); JPanel up_left = new JPanel(new GridLayout(2,1));
InsomniaGUI main_class;
public LeftPanel (InsomniaGUI m){
main_class = m;
}
//left panel (list of requests) //left panel (list of requests)
public void createLeftPanel() { public void createLeftPanel() {
...@@ -33,8 +38,11 @@ public class LeftPanel extends JPanel { ...@@ -33,8 +38,11 @@ public class LeftPanel extends JPanel {
addRequest.addMouseListener(new MouseAdapter() { addRequest.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
RequestData requestData = new RequestData();
ResponseData responseData = new ResponseData(requestData); if (!main_class.hasRequest()){
JOptionPane.showMessageDialog(main_class.frame,"You need to send a request first.","Error",JOptionPane.ERROR_MESSAGE);
return;
}
JFrame newRequestFrame = new JFrame("request name"); JFrame newRequestFrame = new JFrame("request name");
JLabel label = new JLabel("new request"); JLabel label = new JLabel("new request");
JTextField textField = new JTextField(); JTextField textField = new JTextField();
...@@ -49,20 +57,39 @@ public class LeftPanel extends JPanel { ...@@ -49,20 +57,39 @@ public class LeftPanel extends JPanel {
newRequestFrame.add(panel); newRequestFrame.add(panel);
newRequestFrame.setPreferredSize(new Dimension(200,200)); newRequestFrame.setPreferredSize(new Dimension(200,200));
newRequestFrame.setVisible(true); newRequestFrame.setVisible(true);
requestData.setName(textField.getText());
create.addMouseListener(new MouseAdapter() { create.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
super.mouseClicked(e); super.mouseClicked(e);
requestData.setName(textField.getText()); String name = textField.getText();
JButton newRequest = new JButton(requestData.getName()); //Check Request exist
newRequest.addMouseListener(new MouseRequestClick(requestData , responseData ));
//Save Request
ResponseData data = main_class.data_req;
data.getRequestData().setName(name);
new Saver().saveRequestAndResponse(data);
//Add Request to list
JButton newRequest = new JButton(name);
newRequest.addMouseListener(new MouseRequestClick(name));
requestList.add(newRequest); requestList.add(newRequest);
requestList.validate();
//ToDo:add action listener to button requests //ToDo:add action listener to button requests
} }
}); });
} }
}); });
for(File f : Saver.getRequestFiles()){
String name = f.getName();
if (name.endsWith(".insomnia")) {
name = name.substring(0,name.length()-9);
JButton newRequest = new JButton(name);
newRequest.addMouseListener(new MouseRequestClick(name));
requestList.add(newRequest);
}
}
requestList.validate();
this.setLayout(new BorderLayout()); this.setLayout(new BorderLayout());
this.setBackground(Color.GRAY); this.setBackground(Color.GRAY);
this.add(up_left, BorderLayout.NORTH); this.add(up_left, BorderLayout.NORTH);
...@@ -91,16 +118,17 @@ public class LeftPanel extends JPanel { ...@@ -91,16 +118,17 @@ public class LeftPanel extends JPanel {
} }
private class MouseRequestClick extends MouseAdapter { private class MouseRequestClick extends MouseAdapter {
Saver saver = new Saver(); String name;
RequestData requestData = new RequestData(); MouseRequestClick(String name){
ResponseData responseData = new ResponseData(requestData); this.name =name;
MouseRequestClick(RequestData requestData , ResponseData responseData){
this.requestData = requestData;
this.responseData = responseData;
} }
@Override @Override
public void mouseClicked(MouseEvent e){ public void mouseClicked(MouseEvent e){
super.mouseClicked(e); super.mouseClicked(e);
ResponseData data = new Saver().readRequestAndResponse(name);
data.getRequestData().setName("");
main_class.load(data);
main_class.loadRequest(data.getRequestData());
} }
} }
} }
......
...@@ -38,7 +38,7 @@ public class RequestPanel extends JPanel { ...@@ -38,7 +38,7 @@ public class RequestPanel extends JPanel {
private JPanel rightOfHeader = new JPanel(); private JPanel rightOfHeader = new JPanel();
private JPanel center_header = new JPanel(new GridLayout(1,2)); private JPanel center_header = new JPanel(new GridLayout(1,2));
private JPanel header = new JPanel(new BorderLayout()); private JPanel header = new JPanel(new BorderLayout());
private String s []={"Patch","Put","Post","Delete","Get"}; private String s []={"Get","Post","Put","Delete"};
private JComboBox centerMenu = new JComboBox(s); private JComboBox centerMenu = new JComboBox(s);
private JTextField URLTextField = new JTextField("http://api.myproduct.com/v1/users"); private JTextField URLTextField = new JTextField("http://api.myproduct.com/v1/users");
private JButton sendButton = new JButton("send"); private JButton sendButton = new JButton("send");
...@@ -212,8 +212,8 @@ public class RequestPanel extends JPanel { ...@@ -212,8 +212,8 @@ public class RequestPanel extends JPanel {
int selectedItem = -2; int selectedItem = -2;
MouseAdapter body_insert = null; MouseAdapter body_insert = null;
void initBody(){ void initBody(String... args){
if (selectedItem == Json_FormData.getSelectedIndex()) return; if (args.length==0 && selectedItem == Json_FormData.getSelectedIndex()) return;
selectedItem = Json_FormData.getSelectedIndex(); selectedItem = Json_FormData.getSelectedIndex();
if(body_pane.getComponentCount()>0) { if(body_pane.getComponentCount()>0) {
...@@ -231,6 +231,7 @@ public class RequestPanel extends JPanel { ...@@ -231,6 +231,7 @@ public class RequestPanel extends JPanel {
}; };
textArea.setToolTipText("Enter JSON"); textArea.setToolTipText("Enter JSON");
textArea.setBorder(border); textArea.setBorder(border);
if (args.length>0) textArea.setText(args[0]);
body_pane.add(textArea); body_pane.add(textArea);
} else if(Json_FormData.getSelectedItem().equals("Binary Data")){ } else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField path = new JTextField("File Path"){ JTextField path = new JTextField("File Path"){
...@@ -239,6 +240,7 @@ public class RequestPanel extends JPanel { ...@@ -239,6 +240,7 @@ public class RequestPanel extends JPanel {
return new Dimension(body_pane.getSize().width-16,28); return new Dimension(body_pane.getSize().width-16,28);
} }
}; };
if (args.length>0) path.setText(args[0]);
path.setBorder(border); path.setBorder(border);
body_pane.add(path,BorderLayout.NORTH); body_pane.add(path,BorderLayout.NORTH);
...@@ -258,12 +260,6 @@ public class RequestPanel extends JPanel { ...@@ -258,12 +260,6 @@ public class RequestPanel extends JPanel {
} }
}); });
}else { }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) { final JPanel vl = new JPanel(vfLayout) {
@Override @Override
...@@ -274,52 +270,70 @@ public class RequestPanel extends JPanel { ...@@ -274,52 +270,70 @@ public class RequestPanel extends JPanel {
body_pane.add(vl); body_pane.add(vl);
if (body_insert==null){ 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() { body_insert = new MouseAdapter() {
@Override @Override
public void mouseClicked(MouseEvent e) { public void mouseClicked(MouseEvent e) {
if (e != null && !isLastComponent(((Component) e.getSource()).getParent().getParent(), vl)) return; if (e != null && !isLastComponent(((Component) e.getSource()).getParent().getParent(), vl)) return;
JTextField newHeaderField = new JTextField("key"); addBody();
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); if (args.length==0) body_insert.mouseClicked(null);
} }
body_pane.revalidate(); body_pane.revalidate();
body_pane.repaint(); body_pane.repaint();
} }
private void addBody(String... args){
Border border = BorderFactory.createLineBorder(Color.BLACK,3);
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);
JTextField newHeaderField = new JTextField("key");
newHeaderField.addMouseListener(body_insert);
newHeaderField.setBorder(border);
JTextField newValueField = new JTextField("value");
newValueField.setBorder(border);
if (args.length>0){
newHeaderField.setText(args[0]);
newValueField.setText(args[1]);
}
JButton newDelete = new JButton();
newDelete.setPreferredSize(new Dimension(20, 20));
newDelete.setIcon(new ImageIcon(scaled));
final JPanel vl = (JPanel) body_pane.getComponent(0);
newDelete.addMouseListener(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();
}
});
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();
}
//request_up //request_up
public JPanel centerUp(){ public JPanel centerUp(){
sendButton.addMouseListener(new MouseAdapter() { sendButton.addMouseListener(new MouseAdapter() {
...@@ -362,6 +376,48 @@ public class RequestPanel extends JPanel { ...@@ -362,6 +376,48 @@ public class RequestPanel extends JPanel {
} }
public void load (RequestData r){
switch (r.getMethod()){
case "PUT":
centerMenu.setSelectedIndex(2);
break;
case "POST":
centerMenu.setSelectedIndex(1);
break;
case "DELETE":
centerMenu.setSelectedIndex(3);
break;
default:
centerMenu.setSelectedIndex(0);
break;
}
URLTextField.setText(r.getUrl());
if (r.isJson()){
Json_FormData.setSelectedIndex(1);
initBody(r.getBody());
}else if(r.getUploadPath().length()>1){
Json_FormData.setSelectedIndex(2);
initBody(r.getUploadPath());
}else{
Json_FormData.setSelectedIndex(0);
initBody("r");
if (body_insert!=null &&!r.getBody().isEmpty()) {
if (!r.getBody().contains("&")) {
String[] f1 = r.getBody().split("=");
addBody(f1);
} else {
String[] mFields = r.getBody().split("&");
for (String f : mFields) {
String[] f0 = f.split("=");
addBody(f0);
}
}
}
}
}
public void createRequestPanel() throws IOException { public void createRequestPanel() throws IOException {
addToMainRequestPanel(centerUp() , tabbedPaneCenterRequest()); addToMainRequestPanel(centerUp() , tabbedPaneCenterRequest());
......
...@@ -5,10 +5,11 @@ ...@@ -5,10 +5,11 @@
* version: 0.0 * version: 0.0
*/ */
package com.insomnia.parser; package com.insomnia.parser;
import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
public class RequestData { public class RequestData implements Serializable {
private String name; private String name = "";
//methods requests //methods requests
public static String GET = "GET", POST = "POST", DELETE="DELETE", PUT="PUT"; public static String GET = "GET", POST = "POST", DELETE="DELETE", PUT="PUT";
//address of request //address of request
......
...@@ -3,14 +3,11 @@ ...@@ -3,14 +3,11 @@
*/ */
package com.insomnia.parser; package com.insomnia.parser;
import java.io.BufferedReader; import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ResponseData { public class ResponseData implements Serializable {
private RequestData requestData; private RequestData requestData;
private long startTime,finalTime; private long startTime,finalTime;
...@@ -145,4 +142,5 @@ public class ResponseData { ...@@ -145,4 +142,5 @@ public class ResponseData {
public void setStatus(int status) { public void setStatus(int status) {
this.status = status; this.status = status;
} }
} }
...@@ -63,9 +63,9 @@ public class Saver { ...@@ -63,9 +63,9 @@ public class Saver {
} }
} }
public void saveRequest2 (RequestData data) { public void saveRequestAndResponse (ResponseData data) {
String fileName = data.getName()+".insomnia"; String fileName = data.getRequestData().getName()+".insomnia";
File file = new File(getRequestsDir(),fileName); File file = new File(getRequestsDir(),fileName);
try { try {
...@@ -82,12 +82,12 @@ public class Saver { ...@@ -82,12 +82,12 @@ public class Saver {
} }
} }
public RequestData readRequest (String fileName){ public ResponseData readRequestAndResponse (String fileName){
try { try {
FileInputStream file = new FileInputStream(new File(getRequestsDir(),fileName)); FileInputStream file = new FileInputStream(new File(getRequestsDir(),fileName+".insomnia"));
ObjectInputStream in = new ObjectInputStream(file); ObjectInputStream in = new ObjectInputStream(file);
RequestData data = (RequestData) in.readObject(); ResponseData data = (ResponseData) in.readObject();
in.close(); in.close();
file.close(); file.close();
......
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