-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLastRequestsGUI.java
More file actions
526 lines (345 loc) · 14.5 KB
/
Copy pathLastRequestsGUI.java
File metadata and controls
526 lines (345 loc) · 14.5 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.IOException;
/**
* This class reprsent the left part of the insomnia app GUI
* This part hold and show the last request by they group
*
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.2.5
*/
public class LastRequestsGUI extends JPanel
{
/* Fields */
// Insomnia text label
private JLabel insomniaLabel;
// requests groups tabbed pane
private JTabbedPane requestGroupsTabs;
// groups name combo box
private JComboBox<String> groupsComboBox;
// hold the Groups panels
private HashMap<String, JPanel> requestGroupPanels = new HashMap<>();
// add group tab panel
private JPanel addGroupTabPanel;
// get new tab name Text field
private JTextField getNewGroupNameField;
// add new group button
private JButton addGroupButton;
// remove group tab panel
private JPanel removeGroupTabPanel;
// remove group button
private JButton removeGroupButton;
// last requests panel
private JPanel lastRequestsPanel;
// hold the object of this class
private final LastRequestsGUI THIS;
// Event handler of buttons
private EventHandler handler;
// back text color
private Color backTextColor = new Color(71, 71, 69);
// background color
private Color backgroundColor;
/* Constructor */
/**
* Create new panel for last requests
*
*
* @param bgColor : the color of the background
*/
public LastRequestsGUI(Color bgColor)
{
// set the super class
super();
this.setLayout(new BorderLayout()); // set layout manager
THIS = this;
// set handler
handler = new EventHandler();
// set background color
backgroundColor = bgColor;
// set the insomnia label
insomniaLabel = new JLabel(" Insomnia "); // set text
insomniaLabel.setHorizontalAlignment(SwingConstants.LEFT);
insomniaLabel.setPreferredSize(new Dimension(170, 50)); // set size
insomniaLabel.setFont(insomniaLabel.getFont().deriveFont(22.0f)); // set text size
insomniaLabel.setForeground(Color.WHITE); // set text color
insomniaLabel.setBackground(new Color(102, 96, 178)); // set back ground color
insomniaLabel.setOpaque(true); // apply color changes
this.add(insomniaLabel, BorderLayout.NORTH); // add to the main panel
// set the '+' tab
addGroupTabPanel = new JPanel(); // create new panel
addGroupTabPanel.setLayout(new FlowLayout()); // set the layout manager
addGroupTabPanel.setBackground(backgroundColor); // set background color
addGroupTabPanel.setOpaque(true); // apply color changes
requestGroupPanels.put("+", addGroupTabPanel); // add to the hashMap
// set new tab name text field
getNewGroupNameField = new JTextField("new group name ... ");
getNewGroupNameField.setForeground(backTextColor); // set foreground color
getNewGroupNameField.setPreferredSize(new Dimension(190, 33)); // set size
getNewGroupNameField.setFont(insomniaLabel.getFont().deriveFont(15.0f)); // set text size
getNewGroupNameField.addFocusListener(handler); // set handler
addGroupTabPanel.add(getNewGroupNameField); // add text field
// set add button
addGroupButton = new JButton(" Add "); // creat new button
addGroupButton.setPreferredSize(new Dimension(70, 35)); // set size
addGroupButton.addActionListener(handler); // set handler
addGroupTabPanel.add(addGroupButton); // add button
// set the '-' tab
removeGroupTabPanel = new JPanel(); // create new panel
removeGroupTabPanel.setLayout(new FlowLayout()); // set layout manager
removeGroupTabPanel.setBackground(backgroundColor); // set background color
removeGroupTabPanel.setOpaque(true); // apply color changes
requestGroupPanels.put("-", removeGroupTabPanel); // add to the hash map
// set groups combo box
groupsComboBox = new JComboBox<>(); // create new combo box
groupsComboBox.setPreferredSize(new Dimension(193, 33)); // set size
groupsComboBox.setFont(groupsComboBox.getFont().deriveFont(15.0f)); // set text size
removeGroupTabPanel.add(groupsComboBox); // add groups combo box
// set remove button
removeGroupButton = new JButton(" Remove "); // creat new button
removeGroupButton.setPreferredSize(new Dimension(85, 35)); // set size
removeGroupButton.addActionListener(handler); // set handler
removeGroupTabPanel.add(removeGroupButton); // add button
// set last requests panel
lastRequestsPanel = newEmptyPanel(); // create new panel
requestGroupPanels.put("last requests", lastRequestsPanel); // add to the hashMap
// set request grouping
requestGroupsTabs = new JTabbedPane(); // create new tabbed pane
requestGroupsTabs.setBackground(backgroundColor); // set color
requestGroupsTabs.setOpaque(true); // apply color changes
requestGroupsTabs.addTab("last requests", lastRequestsPanel);
requestGroupsTabs.addTab(" + ", addGroupTabPanel);
requestGroupsTabs.addTab(" - ", removeGroupTabPanel);
this.add(requestGroupsTabs, BorderLayout.CENTER); // add to the main panel
}
/* Methods */
/**
* This method show the saved request in GUI
*
*
* @param reqKind : method of request
* @param reqName : name of the request
* @param gpName : name of the request group
* @param reqDetails : a {@code String} that could set as {@link Jurl#main(String[])} input
*/
public void saveRequest(String reqKind, String reqName, String gpName, String reqDetails)
{
requestGroupPanels.get(gpName).add(new ReqeustButton(reqKind, reqName, gpName, reqDetails));
}
/**
* This method load saved request to GUI
*
* @param requests : saved requests in DataBase
*/
public void loadRequests(HashMap<String, ArrayList<String>> requests)
{
int gpCntr = 0, reqCntr = 0; String gpLast;
for (String gpName : requests.keySet())
{
gpLast = gpName;
if (!".lastRequestsFolder".equals(gpName))
addGroup(gpName);
else
gpLast = "last requests";
reqCntr = 0;
for (String reqName : requests.get(gpName))
{
try
{
Request hold = DataBase.openRequest(gpCntr, reqCntr);
addRequest(gpLast, new ReqeustButton(RequestKinds.getKind(hold.getRequestKind()), reqName, gpLast, hold.getReqDetails()));
}
catch (IOException e) {}
reqCntr++;
}
gpCntr++;
}
}
/**
* @return a combo box of groups
*/
public JComboBox<String> getGroupsComboBox()
{
JComboBox<String> output = new JComboBox<>();
output.setPreferredSize(new Dimension(193, 33)); // set size
output.setFont(output.getFont().deriveFont(15.0f)); // set text size
for (String group : requestGroupPanels.keySet())
if (group.equals("+") || group.equals("-"))
continue;
else
output.addItem(group);
return output;
}
// This return true if the client has been choose a name for new Group
private boolean isNewGroupNameEmpty()
{
if (getNewGroupNameField.getText().length() == 0
||
getNewGroupNameField.getText().equals("new group name ... "))
{
JOptionPane.showMessageDialog(this, "Please choose a name for new group !", "error", JOptionPane.ERROR_MESSAGE);
return true;
}
return false;
}
// This method check that client new group name has already choosen or not
private boolean isAlreadyAdded(String newGroupName)
{
if (requestGroupPanels.keySet().contains(newGroupName))
return true;
return false;
}
// this method return a new empty panel
private JPanel newEmptyPanel()
{
JPanel output = new JPanel(); // create new panel
output.setLayout(new BoxLayout(output, BoxLayout.Y_AXIS)); // set the layout manager
output.setBackground(backgroundColor); // set the background color
output.setOpaque(true); // apply color changes
return output;
}
// this method add new group by its name
private void addGroup(String gpName)
{
JPanel newGroupPanel = newEmptyPanel(); // create new panel
groupsComboBox.addItem(gpName); // add new group name to combo box
requestGroupPanels.put(gpName, newGroupPanel); // add to the hashMap
requestGroupsTabs.insertTab(gpName, null, newGroupPanel, null, 1); // add new tab
}
// this method add a request to its group panel
private void addRequest(String gpName, ReqeustButton req)
{
requestGroupPanels.get(gpName).add(req);
}
// this class represent a button object for reqeusts
private class ReqeustButton extends JButton
{
/* Fields */
// request method
private String requestKind = null;
// request name
private String requestName = null;
// request group name
private String groupName = null;
// request details (for Jurl main)
private String requestDetailsString = null;
/* Constructor */
/**
* Create a new button for a request
*
*
* @param reqKind : method of the request
* @param reqName : a name for this request
* @param gpName : name of the this request group
* @param reqDetails : a string of this request detials
*/
public ReqeustButton(String reqKind, String reqName, String gpName, String reqDetails)
{
super();
this.requestKind = reqKind.toUpperCase();
this.requestName = reqName;
this.groupName = gpName;
this.requestDetailsString = reqDetails;
super.setMinimumSize(new Dimension(170, 35)); // set maximum size of the button
super.setFont (super.getFont().deriveFont(12.0f)); // set text size
super.setForeground(Color.BLACK); // set text color
super.setBackground(new Color(153, 153, 153)); // set background color
super.setOpaque(true);
super.setText(buildButtonText());
super.addActionListener(handler);
}
/* Methods */
/**
* @return details of this request
*/
public String getDetails() { return requestDetailsString; }
// return button text
private String buildButtonText()
{
String colorCode = null;
switch (this.requestKind.toUpperCase())
{
case "GET": colorCode = "rgb(121, 108, 197)"; break;
case "POST": colorCode = "rgb(121, 168, 70)"; break;
case "PUT": colorCode = "rgb(197, 122, 46)"; break;
case "DELETE" : colorCode = "rgb(193, 78, 73)"; break;
default: break;
}
return "<html>" + "<b style=\"color:" + colorCode + ";\">" +
("DELETE".equals(requestKind) ? "DEL": requestKind) +
"    </b>" + requestName + "</html>";
}
}
// This class do the event handling of LastRequestsGUI class
private class EventHandler implements ActionListener, FocusListener
{
/**
* This method handl the event of the add button and remove button
*
* @param e : ActionEvent
*/
public void actionPerformed(ActionEvent e)
{
/* add button case */
if (e.getSource().equals(getNewGroupNameField) || e.getSource().equals(addGroupButton))
{
// check that client has choose a name for new group or not
if (isNewGroupNameEmpty())
return;
String newGroupName = getNewGroupNameField.getText(); // get name of the new group
// check the given name
if (isAlreadyAdded(newGroupName))
{
JOptionPane.showMessageDialog(THIS, "This group has already added !", "error", JOptionPane.ERROR_MESSAGE);
return;
}
addGroup(newGroupName);
}
/* remove button case */
if (e.getSource().equals(removeGroupButton))
{
// get the client choose
String groupNameToRemove = (String)groupsComboBox.getSelectedItem();
requestGroupsTabs.remove(requestGroupPanels.get(groupNameToRemove)); // remove from tabs
requestGroupPanels.remove(groupNameToRemove); // remove from hashMap
groupsComboBox.removeItem(groupNameToRemove); // remove from combo box
Insomnia.removeGroup(groupNameToRemove);
}
/* request button case */
if (e.getSource() instanceof ReqeustButton)
{
MainFrame.openRequest(((ReqeustButton) e.getSource()).getDetails());
}
}
/**
* This mehtod handle the
*/
public void focusGained(FocusEvent e)
{
/* get group name case */
if (e.getSource().equals(getNewGroupNameField))
{
getNewGroupNameField.setForeground(Color.BLACK);
getNewGroupNameField.setOpaque(true);
getNewGroupNameField.setText("");
}
}
public void focusLost(FocusEvent e)
{
/* get group name case */
if (e.getSource().equals(getNewGroupNameField))
{
if (getNewGroupNameField.getText().length() == 0)
{
getNewGroupNameField.setForeground(backTextColor);
getNewGroupNameField.setOpaque(true);
getNewGroupNameField.setText("new group name ... ");
}
}
}
}
}