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;
* @version 0.0
*/
import com.insomnia.parser.RequestData;
import com.insomnia.parser.ResponseData;
import javax.swing.*;
......@@ -19,6 +20,7 @@ import static java.awt.Frame.*;
public class InsomniaGUI {
JFrame frame ;
RespondPanel response;
RequestPanel request;
ResponseData data_req = null;
public void load(ResponseData d){
......@@ -26,6 +28,15 @@ public class InsomniaGUI {
response.load(d);
}
public boolean hasRequest(){
return data_req!=null;
}
public void loadRequest (RequestData r){
request.load(r);
}
public InsomniaGUI() throws IOException {
frame = new JFrame();
frame.setTitle("Insomnia");
......@@ -34,14 +45,14 @@ public class InsomniaGUI {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
RequestPanel request = new RequestPanel(this);
request = new RequestPanel(this);
request.createRequestPanel();
response = new RespondPanel();
response.createRespondPanel();
JPanel panelRight = new JPanel(new GridLayout(1 ,2 ));
panelRight.add(request);
panelRight.add(response);
LeftPanel leftPanel = new LeftPanel();
LeftPanel leftPanel = new LeftPanel(this);
leftPanel.createLeftPanel();
//menu upper_Items
......
......@@ -10,16 +10,21 @@ import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
public class LeftPanel extends JPanel {
Border border = BorderFactory.createLineBorder(Color.BLACK,3);
VerticalFlowLayout vfLayout = new VerticalFlowLayout();
JButton addRequest = new JButton("Add Request");
JButton addRequest = new JButton("Save Request");
JMenu menu = new JMenu("Insomnia");
JPanel requestList = new JPanel(vfLayout);
JScrollPane scrollRequests = new JScrollPane(requestList);
JPanel up_left = new JPanel(new GridLayout(2,1));
InsomniaGUI main_class;
public LeftPanel (InsomniaGUI m){
main_class = m;
}
//left panel (list of requests)
public void createLeftPanel() {
......@@ -33,8 +38,11 @@ public class LeftPanel extends JPanel {
addRequest.addMouseListener(new MouseAdapter() {
@Override
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");
JLabel label = new JLabel("new request");
JTextField textField = new JTextField();
......@@ -49,20 +57,39 @@ public class LeftPanel extends JPanel {
newRequestFrame.add(panel);
newRequestFrame.setPreferredSize(new Dimension(200,200));
newRequestFrame.setVisible(true);
requestData.setName(textField.getText());
create.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
requestData.setName(textField.getText());
JButton newRequest = new JButton(requestData.getName());
newRequest.addMouseListener(new MouseRequestClick(requestData , responseData ));
String name = textField.getText();
//Check Request exist
//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.validate();
//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.setBackground(Color.GRAY);
this.add(up_left, BorderLayout.NORTH);
......@@ -91,16 +118,17 @@ public class LeftPanel extends JPanel {
}
private class MouseRequestClick extends MouseAdapter {
Saver saver = new Saver();
RequestData requestData = new RequestData();
ResponseData responseData = new ResponseData(requestData);
MouseRequestClick(RequestData requestData , ResponseData responseData){
this.requestData = requestData;
this.responseData = responseData;
String name;
MouseRequestClick(String name){
this.name =name;
}
@Override
public void mouseClicked(MouseEvent 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 {
private JPanel rightOfHeader = new JPanel();
private JPanel center_header = new JPanel(new GridLayout(1,2));
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 JTextField URLTextField = new JTextField("http://api.myproduct.com/v1/users");
private JButton sendButton = new JButton("send");
......@@ -212,8 +212,8 @@ public class RequestPanel extends JPanel {
int selectedItem = -2;
MouseAdapter body_insert = null;
void initBody(){
if (selectedItem == Json_FormData.getSelectedIndex()) return;
void initBody(String... args){
if (args.length==0 && selectedItem == Json_FormData.getSelectedIndex()) return;
selectedItem = Json_FormData.getSelectedIndex();
if(body_pane.getComponentCount()>0) {
......@@ -231,6 +231,7 @@ public class RequestPanel extends JPanel {
};
textArea.setToolTipText("Enter JSON");
textArea.setBorder(border);
if (args.length>0) textArea.setText(args[0]);
body_pane.add(textArea);
} else if(Json_FormData.getSelectedItem().equals("Binary Data")){
JTextField path = new JTextField("File Path"){
......@@ -239,6 +240,7 @@ public class RequestPanel extends JPanel {
return new Dimension(body_pane.getSize().width-16,28);
}
};
if (args.length>0) path.setText(args[0]);
path.setBorder(border);
body_pane.add(path,BorderLayout.NORTH);
......@@ -258,12 +260,6 @@ public class RequestPanel extends JPanel {
}
});
}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
......@@ -274,52 +270,70 @@ public class RequestPanel extends JPanel {
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();
addBody();
}
};
}
body_insert.mouseClicked(null);
if (args.length==0) body_insert.mouseClicked(null);
}
body_pane.revalidate();
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
public JPanel centerUp(){
sendButton.addMouseListener(new MouseAdapter() {
......@@ -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 {
addToMainRequestPanel(centerUp() , tabbedPaneCenterRequest());
......
......@@ -5,10 +5,11 @@
* version: 0.0
*/
package com.insomnia.parser;
import java.io.Serializable;
import java.util.HashMap;
public class RequestData {
private String name;
public class RequestData implements Serializable {
private String name = "";
//methods requests
public static String GET = "GET", POST = "POST", DELETE="DELETE", PUT="PUT";
//address of request
......
......@@ -3,14 +3,11 @@
*/
package com.insomnia.parser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.util.List;
import java.util.Map;
public class ResponseData {
public class ResponseData implements Serializable {
private RequestData requestData;
private long startTime,finalTime;
......@@ -145,4 +142,5 @@ public class ResponseData {
public void setStatus(int status) {
this.status = status;
}
}
......@@ -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);
try {
......@@ -82,12 +82,12 @@ public class Saver {
}
}
public RequestData readRequest (String fileName){
public ResponseData readRequestAndResponse (String fileName){
try {
FileInputStream file = new FileInputStream(new File(getRequestsDir(),fileName));
FileInputStream file = new FileInputStream(new File(getRequestsDir(),fileName+".insomnia"));
ObjectInputStream in = new ObjectInputStream(file);
RequestData data = (RequestData) in.readObject();
ResponseData data = (ResponseData) in.readObject();
in.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