-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainFrame.java
318 lines (225 loc) · 9.42 KB
/
MainFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Scanner;
import java.io.*;
/**
* This class is the main frame of the app
* This class is a connector between difrent parts of gui
*
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.1.0
*/
public class MainFrame
{
/* Feilds */
// main frame
private static JFrame frame = new JFrame(" Insomnia ");
// request details panel
private static LastRequestsGUI lastRequestsPanel;
// new request panel
private static NewRequestGUI newRequestPanel;
// response panel
private static ResponseGUI responsePanel;
// menu bar of main fram
private static MenuBar frameMenuBar = new MenuBar();
// application menu
private static Menu applicationMenu = new Menu("Application");
private static MenuItem followRedirectsItem = new MenuItem("Follow redirects: ☑️");
// view menu
private static Menu viewMenu = new Menu("View");
private static MenuItem toggleFullScreenItem = new MenuItem("Toggle Full Screen");
private static MenuItem toggleSidebarItem = new MenuItem("Toggle Sidebar");
// help menu
private static Menu helpMenu = new Menu("Help");
private static MenuItem aboutItem = new MenuItem("About");
private static MenuItem helpItem = new MenuItem("Help");
// a handler for events
private static EventHandler handler = new EventHandler();
// background color of panels
private static Color backgroundColor = new Color(46, 47, 44);
/* Constructor */
/**
* Create the app GUI frame
*/
public static void MainFrameInit()
{
// set the frame
frame.setLayout(new BorderLayout()); // set frame layout
frame.setMinimumSize(new Dimension(1200, 600)); // set minimum size
frame.setLocation(150, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// create last requests panel
lastRequestsPanel = new LastRequestsGUI(backgroundColor);
frame.add(lastRequestsPanel, BorderLayout.WEST);
// create new request panel
newRequestPanel = new NewRequestGUI(backgroundColor);
frame.add(newRequestPanel, BorderLayout.CENTER);
// create last request panel
responsePanel = new ResponseGUI(backgroundColor);
frame.add(responsePanel, BorderLayout.EAST);
// load last requests
DataBase.init();
lastRequestsPanel.loadRequests(DataBase.getRequests());
// set application menu
applicationMenu.setShortcut(new MenuShortcut(KeyEvent.VK_1, false));
followRedirectsItem.setShortcut(new MenuShortcut(KeyEvent.VK_R, false));
followRedirectsItem.addActionListener(handler);
applicationMenu.add(followRedirectsItem);
// set view menu
viewMenu.setShortcut(new MenuShortcut(KeyEvent.VK_2, false));
toggleFullScreenItem.setShortcut(new MenuShortcut(KeyEvent.VK_U, false));
toggleFullScreenItem.addActionListener(handler);
viewMenu.add(toggleFullScreenItem);
toggleSidebarItem.setShortcut(new MenuShortcut(KeyEvent.VK_L, false));
toggleSidebarItem.addActionListener(handler);
viewMenu.add(toggleSidebarItem);
// set help menu
helpMenu.setShortcut(new MenuShortcut(KeyEvent.VK_3, false));
aboutItem.setShortcut(new MenuShortcut(KeyEvent.VK_B, false));
aboutItem.addActionListener(handler);
helpMenu.add(aboutItem);
helpItem.setShortcut(new MenuShortcut(KeyEvent.VK_P, false));
helpItem.addActionListener(handler);
helpMenu.add(helpItem);
frameMenuBar.add(applicationMenu);
frameMenuBar.add(viewMenu);
frameMenuBar.add(helpMenu);
frame.setMenuBar(frameMenuBar);
}
/**
* This method set view of the center panel with selected request
*
* @param details : details of request
*/
public static void openRequest(String details)
{
newRequestPanel.setPanel(details);
}
/**
* This method save a request and add a button for it in gui
*
*
* @param gpName : name of the request group
* @param reqName : name of the request
* @param reqKind : request method
* @param reqDetails : a string of request details
*/
public static void save(String gpName, String reqName, String reqKind, String reqDetails)
{
if (gpName.equals(""))
gpName = "last requests";
lastRequestsPanel.saveRequest(reqKind, reqName, gpName, reqDetails);
}
/**
* This method show the main frame
*
* @see JFrame#setVisible(boolean)
*/
public static void showFrame()
{
frame.setVisible(true);
}
/**
* @return a combo box of groups
*/
public static JComboBox<String> getGroups()
{
return lastRequestsPanel.getGroupsComboBox();
}
/**
* This method updata the gui respones part
*
*
* @param statusCode : status code of response
* @param responseMessage : message of the request
* @param responseTime : time of the response
* @param contentLength : content length of re response
* @param contentFormat : format of the response body
* @param headers : headers of response
*/
public static void updateGUI(int statusCode, String responseMessage, long responseTime, long contentLength, String contentFormat, HashMap<String, String> headers)
{
responsePanel.setTime(responseTime);
responsePanel.setResponseCodeAndMessage(statusCode, responseMessage);
responsePanel.setContentLength(contentLength);
responsePanel.setHeaders(headers);
responsePanel.update(contentFormat);
}
/**
* This method shows errors
*/
public static void showError()
{
Scanner in = null;
try{ in = new Scanner(new File(DataBase.getErrorsLogGuiPath())); }
catch (FileNotFoundException ex) {}
String message = in.nextLine();
JOptionPane.showMessageDialog(frame, message, "error", JOptionPane.ERROR_MESSAGE);
}
// an inner class for handling events
private static class EventHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(followRedirectsItem))
{
Insomnia.setFollowRedirects(!Insomnia.isFollowRedirects());
if (Insomnia.isFollowRedirects())
followRedirectsItem.setLabel("Follow redirects: ☑️");
else
followRedirectsItem.setLabel("Follow redirects: ◻️");
}
if (e.getSource().equals(toggleFullScreenItem))
{
if (frame.getExtendedState() == 0)
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
else
frame.setExtendedState(0);
}
if (e.getSource().equals(toggleSidebarItem))
{
if (lastRequestsPanel.isVisible())
lastRequestsPanel.setVisible(false);
else
lastRequestsPanel.setVisible(true);
}
if (e.getSource().equals(helpItem))
{
JFrame helpTextFrame = new JFrame("Help");
helpTextFrame.setLocationRelativeTo(frame);
helpTextFrame.setMinimumSize(new Dimension(450, 300));
helpTextFrame.setLocation(500, 250);
helpTextFrame.setResizable(false);
JPanel helpFramePanel = new JPanel(new GridLayout(1, 1, 30, 30));
helpFramePanel.setBackground(backgroundColor);
helpFramePanel.setOpaque(true);
helpTextFrame.add(helpFramePanel);
JTextArea helpTextArea = new JTextArea();
helpTextArea.setBackground(backgroundColor);
helpTextArea.setForeground(Color.WHITE);
helpTextArea.setOpaque(true);
helpTextArea.setEditable(false);
helpTextArea.setFont(helpTextArea.getFont().deriveFont(20));
helpTextArea.setText(" Insomnia \n\n" +
"you can send a http request with this app\n" +
"supported methods for your reqeusts are: GET, POST, PUT and DELETE\n" +
"if you want to send a file as your request form data body, please\n" +
"write '(FILE)' at the end of the key and give the ablsoute path of\n" +
"your file as value.\n" +
"in headers tab, if you click on one them, the value and key of that\n" +
"will be copy on your clipboard.\n" +
"you can set follow redirect and system tray in application menu in \n" +
"menu bar");
helpFramePanel.add(helpTextArea);
helpTextFrame.setVisible(true);
}
if (e.getSource().equals(aboutItem))
JOptionPane.showMessageDialog(frame, "you can find me at https://github.com/MmahdiM79 ;)", "contact us", JOptionPane.INFORMATION_MESSAGE);
}
}
}