-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewRequestGUI.java
More file actions
1078 lines (754 loc) · 33.5 KB
/
Copy pathNewRequestGUI.java
File metadata and controls
1078 lines (754 loc) · 33.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
/**
* This class represnt the center part of the Insomnia app GUI
* This part is for make a new request
*
*
* @author Mohammad Mahdi Malmasi
* @version 0.3.0
*/
public class NewRequestGUI extends JPanel
{
/* Fields */
// requset kind combo box
private JComboBox<String> requestKindComboBox = new JComboBox<>();
private static final String GET = "<html><p style=\"color:rgb(121, 108, 197);\"> GET </p></html>";
private static final String PUT = "<html><p style=\"color:rgb(197, 122, 46);\"> PUT </p></html>";
private static final String POST = "<html><p style=\"color:rgb(121, 168, 70);\"> POST </p></html>";
private static final String DELETE = "<html><p style=\"color:rgb(193, 78, 73);\"> DELET </p></html>";
// get url text field
private JTextField getUrlTextField = new JTextField(" https:// ?! ");
// request send button
private JButton sendButton = new JButton("Send");
// save button
private JButton saveButton = new JButton("Save");
// request details tabs
private JTabbedPane requestDetailsTabs = new JTabbedPane();
// body tab panel
private JPanel bodyTabMainPanel = new JPanel();
// body kinds combo box
private JComboBox<String> bodyKindsComboBox = new JComboBox<>();
// body tab kinds panel (Card Layout)
private JPanel bodyTabSubPanel = new JPanel();
// form data kind card panel (used in bodyTabSubPanel)
private JPanel formDataKindPanel = new JPanel();
// form datas
private ArrayList<NameValueData> formDatas = new ArrayList<>();
// JSON kind card panel (used in bodyTabSubPanel)
private JPanel jsonKindPanel = new JPanel();
// JSON text area
private JTextArea jsonTextArea = new JTextArea();
// binary file kind card panel (used in bodyTabSubPanel)
private JPanel binaryFileKindPanel = new JPanel();
// choosen file status label
private JLabel choosenBinaryFileName = new JLabel("No file selected !");
// reset choosen file button
private JButton resetChoosenFileButton = new JButton(" Reset file");
// choose file button
private JButton chooseFileButton = new JButton(" Choose file ");
// Query tab panel
private JPanel queryTabPanel = new JPanel();
// Query datas
private ArrayList<NameValueData> queryDatas = new ArrayList<>();
// Header tab panel
private JPanel headerTabPanel = new JPanel();
// Header datas
private ArrayList<NameValueData> headerDatas = new ArrayList<>();
// a pointer to this object
private NewRequestGUI THIS;
private String reqDetails = null;
// Event handler of buttons
private EventHandler mainHandler = new EventHandler();
// back text color
private Color backTextColor = new Color(104, 106, 112);
// background color
private Color backgroundColor;
/* Constructor */
/**
* Create new panel for new request part
*
*
* @param bgColor : the color of the background
*/
public NewRequestGUI(Color bgColor)
{
// set the super class
super();
this.setLayout(new BorderLayout()); // set the layout manager
this.setBackground(backgroundColor); // set background color
this.setOpaque(true); // apply color changes
THIS = this;
// set background color
backgroundColor = bgColor;
// set url part
urlPartGuiInit();
// set the tabs
requestDetailsTabs.setBackground(backgroundColor); // set color
requestDetailsTabs.setOpaque(true); // apply color changes
this.add(requestDetailsTabs, BorderLayout.CENTER);
// set body tab
bodyTabGuiInit();
// set query tab
queryTabGuiInit();
// set header tab
headerTabGuiInit();
}
/* Methods */
/**
* @return the user choosen url
*/
public String getUrl()
{
if (getUrlTextField.getText().equals(" https:// ?! "))
{
JOptionPane.showMessageDialog(THIS, "Please choose a url to send reqeust.", "error", JOptionPane.ERROR_MESSAGE);
return null;
}
return getUrlTextField.getText();
}
/**
* @return user choosen method
*/
public String getMethod()
{
String method;
switch ((String) requestKindComboBox.getSelectedItem())
{
case POST:
method = "post";
break;
case PUT:
method = "put";
break;
case DELETE:
method = "delete";
break;
default:
method = "get";
}
return " -m " + method;
}
/**
* @return user choosen headers for new request
*/
public String getHeaders()
{
String output = " -h ";
for (NameValueData data : headerDatas)
{
if (!data.isSelected())
continue;
if (data.getDataName().equals("new header ...") || data.getDataValue().equals("new value ..."))
{
JOptionPane.showMessageDialog(THIS, "Some of your choosen headers are incorrect !", "error", JOptionPane.ERROR_MESSAGE);
return null;
}
output = output + data.getDataName() + ":" + data.getDataValue() + ",";
}
if (output.equals(" -h "))
return "";
return output.substring(0, output.length()-1);
}
/**
* @return user choosen query
*/
public String getQuery()
{
String output = " -q ";
for (NameValueData data : queryDatas)
{
if (!data.isSelected())
continue;
if (data.getDataValue().equals("new value ..."))
{
JOptionPane.showMessageDialog(THIS, "Your Query is incorrect !", "error", JOptionPane.ERROR_MESSAGE);
return null;
}
output = output + data.getDataName() + "=" + data.getDataValue() + "&";
}
if (output.equals(" -q "))
return "";
return output.substring(0, output.length()-1);
}
/**
* @return a {@code String} to set the request body
*/
public String getBody()
{
switch ((String) bodyKindsComboBox.getSelectedItem())
{
case "Form data":
return getFormData();
case "JSON":
return getJson();
case "Binary file":
return getBinaryFile();
default:
return null;
}
}
/**
* This method set fields of this panel with given details
*
* @param detatils : request details
*/
public void setPanel(String detatils)
{
String[] args = detatils.split(" ");
ArrayList<String> inputs = new ArrayList<>();
for (String arg : args)
inputs.add(arg);
getUrlTextField.setText(args[0]);
switch (inputs.get(inputs.indexOf("-m")+1).toLowerCase())
{
case "post":
requestKindComboBox.setSelectedItem(GET);
break;
case "put":
requestKindComboBox.setSelectedItem(PUT);
break;
case "delete":
requestKindComboBox.setSelectedItem(DELETE);
break;
default: requestKindComboBox.setSelectedItem(GET);
}
if (inputs.contains("-h"))
{
String headers = inputs.get(inputs.indexOf("-h")+1);
headerTabPanel.removeAll();
for (int i = 0; i < headerDatas.size();)
headerDatas.remove(0);
NameValueData hold = null;
for (String pair : headers.split(","))
{
hold = new NameValueData(pair.substring(0, pair.indexOf(":")),
pair.substring(pair.indexOf(":")+1),
headerTabPanel,
headerDatas);
headerTabPanel.add(hold);
headerDatas.add(hold);
}
}
else
{
headerTabPanel.removeAll();
for (int i = 0; i < headerDatas.size();)
headerDatas.remove(0);
NameValueData hold = new NameValueData("new header ...", "new value ...", headerTabPanel, headerDatas);
headerTabPanel.add(hold);
headerDatas.add(hold);
}
if (inputs.contains("-q"))
{
String query = inputs.get(inputs.indexOf("-q")+1);
queryTabPanel.removeAll();
for (int i = 0; i < queryDatas.size();)
queryDatas.remove(0);
NameValueData hold = null;
for (String pair : query.split("&"))
{
hold = new NameValueData(pair.substring(0, pair.indexOf("=")),
pair.substring(pair.indexOf("=")+1),
queryTabPanel,
queryDatas);
queryTabPanel.add(hold);
queryDatas.add(hold);
}
}
else
{
queryTabPanel.removeAll();
for (int i = 0; i < queryDatas.size();)
queryDatas.remove(0);
NameValueData hold = new NameValueData("new name ...", "new value ...", queryTabPanel, queryDatas);
queryTabPanel.add(hold);
queryDatas.add(hold);
}
if (inputs.contains("-d"))
{
String multiPartFormDatas = inputs.get(inputs.indexOf("-d")+1);
formDataKindPanel.removeAll();
for (int i = 0; i < formDatas.size();)
formDatas.remove(0);
NameValueData hold = null;
for (String pair : multiPartFormDatas.split("&"))
{
hold = new NameValueData(pair.substring(0, pair.indexOf("=")),
pair.substring(pair.indexOf("=")+1),
formDataKindPanel,
formDatas);
formDataKindPanel.add(hold);
formDatas.add(hold);
}
}
else
{
formDataKindPanel.removeAll();
for (int i = 0; i < formDatas.size();)
formDatas.remove(0);
NameValueData hold = new NameValueData("new name ...", "new value ...", formDataKindPanel, formDatas);
formDataKindPanel.add(hold);
formDatas.add(hold);
}
if (inputs.contains("-j"))
{
String json = inputs.get(inputs.indexOf("-j")+1);
jsonTextArea.setText(json);
}
else
jsonTextArea.setText("");
if (inputs.contains("-u"))
{
String path = inputs.get(inputs.indexOf("-u")+1);
choosenBinaryFileName.setText(path);
}
else
choosenBinaryFileName.setText("No file selected !");
}
// This method set the north part of the new request details
private void urlPartGuiInit()
{
// set the north panel of the request detail
JPanel northPanel = new JPanel(); // creat new panel
northPanel.setBackground(Color.WHITE); // set the back ground color
northPanel.setOpaque(true); // apply color changes
northPanel.setLayout(new FlowLayout(0, 0, 0)); // set the layout manager
this.add(northPanel, BorderLayout.NORTH); // add to panel
// set the kind combo box
requestKindComboBox.setPreferredSize(new Dimension(92, 50)); // set size
requestKindComboBox.addItem(GET);
requestKindComboBox.addItem(POST);
requestKindComboBox.addItem(PUT);
requestKindComboBox.addItem(DELETE);
requestKindComboBox.setFont(requestKindComboBox.getFont().deriveFont(15.0f)); // set the text size
requestKindComboBox.setBackground(Color.WHITE); // set background color
requestKindComboBox.setForeground(new Color(102, 96, 178)); // set foreground color
requestKindComboBox.setOpaque(true); // apply color changes
requestDetailsTabs.addFocusListener(mainHandler);
northPanel.add(requestKindComboBox); // add kind combo box
// set url text field
getUrlTextField.setPreferredSize(new Dimension(300, 50)); // set size
getUrlTextField.setFont(getUrlTextField.getFont().deriveFont(17.0f)); // set text size
getUrlTextField.setBackground(Color.WHITE); // set background color
getUrlTextField.setForeground(backTextColor); // set foreground color
getUrlTextField.setOpaque(true); // apply color changes
getUrlTextField.addActionListener(mainHandler);
getUrlTextField.addFocusListener(mainHandler);
northPanel.add(getUrlTextField); // add get url text field
// set send button
sendButton.setPreferredSize(new Dimension(57, 50)); // set size
sendButton.setBackground(Color.WHITE); // set background color
sendButton.setForeground(new Color(76, 159, 103)); // set foreground color
sendButton.setOpaque(true); // apply color changes
sendButton.addActionListener(mainHandler);
sendButton.addFocusListener(mainHandler);
northPanel.add(sendButton); // add send button
// set the save button
saveButton.setPreferredSize(new Dimension(57, 50)); // set size
saveButton.setBackground(Color.WHITE); // set background color
saveButton.setForeground(new Color(76, 159, 103)); // set foreground color
saveButton.setOpaque(true); // apply color changes
saveButton.addActionListener(mainHandler);
saveButton.addFocusListener(mainHandler);
northPanel.add(saveButton); // add save button
}
// This method set the body tab of the new request
private void bodyTabGuiInit()
{
// set body tab panel
bodyTabMainPanel.setBackground(backgroundColor); // set background color
bodyTabMainPanel.setOpaque(true); // apply color changes
bodyTabMainPanel.setLayout(new BorderLayout()); // set layout manager
requestDetailsTabs.add("Body", bodyTabMainPanel); // add body panel
// set body kinds combo box
bodyKindsComboBox.setMaximumSize(new Dimension(925, 30));
bodyKindsComboBox.setBackground(backgroundColor); // set background color
bodyKindsComboBox.setOpaque(true); // apply color changes
bodyKindsComboBox.addItem("Form data");
bodyKindsComboBox.addItem("JSON");
bodyKindsComboBox.addItem("Binary file");
bodyKindsComboBox.addActionListener(mainHandler);
bodyKindsComboBox.addFocusListener(mainHandler);
bodyTabMainPanel.add(bodyKindsComboBox, BorderLayout.NORTH); // add to tab panel
// set body kinds panel (CardLayout)
bodyTabSubPanel.setLayout(new CardLayout()); // set layout manager
bodyTabSubPanel.setBackground(backgroundColor); // set the background color
bodyTabSubPanel.setOpaque(true); // apply color changes
// set body kinds panel scroll pane
JScrollPane bodyTabSubPanelScrollPane = new JScrollPane(bodyTabSubPanel);
bodyTabSubPanelScrollPane.setBackground(backgroundColor);
bodyTabSubPanelScrollPane.setOpaque(true);
bodyTabMainPanel.add(bodyTabSubPanelScrollPane, BorderLayout.CENTER); // add edit panel
// set the form data Panel
formDataKindPanel.setLayout(new BoxLayout(formDataKindPanel, BoxLayout.Y_AXIS)); // set layout manager
formDataKindPanel.setBackground(backgroundColor); // set background color
formDataKindPanel.setOpaque(true); // apply color changes
bodyTabSubPanel.add("f", formDataKindPanel); // add to edit panel
// set form data
formDatas.add(new NameValueData("new name ...", "new value ...", formDataKindPanel, formDatas));
formDataKindPanel.add(formDatas.get(0));
// set Json panel
jsonKindPanel.setLayout(new GridLayout(1, 1, 12, 12)); //set layout manager
jsonKindPanel.setBackground(backgroundColor); // set background color
jsonKindPanel.setOpaque(true); // apply color changes
bodyTabSubPanel.add("j", jsonKindPanel); // add to edit panel
// set Json text area
jsonTextArea.setFont(jsonTextArea.getFont().deriveFont(15.0f)); // set text size
jsonTextArea.setBorder(BorderFactory.createLineBorder(backgroundColor, 30));
jsonTextArea.addFocusListener(mainHandler);
jsonKindPanel.add(jsonTextArea); // add text area
// set binary file panel
binaryFileKindPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // set layout manager
binaryFileKindPanel.setBackground(backgroundColor); // set background color
binaryFileKindPanel.setOpaque(true); // apply color changes
bodyTabSubPanel.add("b", binaryFileKindPanel); // add binary file panel
// set choosen file label
choosenBinaryFileName.setPreferredSize(new Dimension(300, 35)); // set size
choosenBinaryFileName.setFont(choosenBinaryFileName.getFont().deriveFont(14.0f)); // set text size
choosenBinaryFileName.setBackground(Color.WHITE); // set back ground color
choosenBinaryFileName.setOpaque(true); // apply color changes
binaryFileKindPanel.add(choosenBinaryFileName); // add to the binary file panel
// set reset button
resetChoosenFileButton.setPreferredSize(new Dimension(100, 35)); // set button size
resetChoosenFileButton.setBackground(backgroundColor); resetChoosenFileButton.setForeground(Color.RED);
resetChoosenFileButton.setOpaque(true); // apply color changes
resetChoosenFileButton.addActionListener(mainHandler);
binaryFileKindPanel.add(resetChoosenFileButton); // add to the binary file panel
// set choose button
chooseFileButton.setPreferredSize(new Dimension(105, 35)); // set button size
chooseFileButton.setBackground(backgroundColor); chooseFileButton.setForeground(Color.GREEN);
chooseFileButton.setOpaque(true); // apply color changes
chooseFileButton.addActionListener(mainHandler);
binaryFileKindPanel.add(chooseFileButton); // add to the binary panel
}
// This method set the query tab of the new request
private void queryTabGuiInit()
{
// set the Query Panel
queryTabPanel.setLayout(new BoxLayout(queryTabPanel, BoxLayout.Y_AXIS)); // set layout manager
queryTabPanel.setBackground(backgroundColor); // set background color
queryTabPanel.setOpaque(true); // apply color changes
requestDetailsTabs.add("Query", queryTabPanel); // add Query tab
// set Query datas
queryDatas.add(new NameValueData("new name ...", "new value ...", queryTabPanel, queryDatas));
queryTabPanel.add(queryDatas.get(0));
}
// This method set the header tab of the new requset
private void headerTabGuiInit()
{
// set the Header Panel
headerTabPanel.setLayout(new BoxLayout(headerTabPanel, BoxLayout.Y_AXIS)); // set layout manager
headerTabPanel.setBackground(backgroundColor); // set background color
headerTabPanel.setOpaque(true); // apply color changes
requestDetailsTabs.add("Header", headerTabPanel); // add Query tab
// set Header datas
headerDatas.add(new NameValueData("new header ...", "new value ...", headerTabPanel, headerDatas));
headerTabPanel.add(headerDatas.get(0));
}
// This method generate a String for request body in form data case
private String getFormData()
{
String output = " -d ";
for (NameValueData data : formDatas)
{
if (!data.isSelected())
continue;
if (data.getDataName().equals("new name ...") || data.getDataValue().equals("new value ..."))
{
JOptionPane.showMessageDialog(THIS, "Some of your given datas for form data are incorrect !", "error", JOptionPane.ERROR_MESSAGE);
return null;
}
output = output + data.getDataName() + "=" + data.getDataValue() + "&";
}
if (output.equals(" -d ")) return "";
output = output.substring(0, output.length()-1);
return output;
}
// This method generate a String for request body in JSON case
private String getJson()
{
String output = " -j ";
try
{
output = output + jsonTextArea.getText();
if (output.equals(" -j "))
return "";
}
catch (NullPointerException e) { return ""; }
return output;
}
// This method generate a String for request body in binary file case
private String getBinaryFile()
{
if (choosenBinaryFileName.getText().equals("No file selected !"))
return "";
return " -u " + choosenBinaryFileName.getText();
}
/* inner Classes */
// This class represnt to text fields and a select button and a delet button
// The object of this class used to hold the headers, Quary datas and etc
private class NameValueData extends JPanel
{
/* Fields */
// get data name text field
private JTextField dataNameTextField;
// get data value text field
private JTextField dataValueTextField;
// select button
private JRadioButton selectButton;
// delet button
private JButton deletButton;
// where this object will added
private JPanel targetPanel;
// where this object will be saved
private ArrayList<NameValueData> holdPlace;
// a pointer to this object
private NameValueData THIS = this;
// buttons and fields mainHandler
private DataEventHandler dataHandler = new DataEventHandler();
// name string
private String bgNameString;
// value string
private String bgValueString;
/* Constructor */
/**
* Create new data holder with given texts for fields
*
* @param bgNameText : name text
* @param bgValueText : value text
* @param targetPanel : where this object will added
* @param holdPlace : where this object will be saved
*/
public NameValueData(String bgNameText, String bgValueText, JPanel targetPanel, ArrayList<NameValueData> holdPlace)
{
// set super class
super();
super.setMaximumSize(new Dimension(925, 50)); // set size
super.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); // set layout manager
super.setBackground(backgroundColor); // set background color
super.setOpaque(true); // apply color changes
bgNameString = bgNameText;
bgValueString = bgValueText;
this.targetPanel = targetPanel;
this.holdPlace = holdPlace;
// set name text field
dataNameTextField = new JTextField(bgNameText); // create new text field
dataNameTextField.setForeground(backTextColor); // set text color
dataNameTextField.setOpaque(true); // apply color changes
dataNameTextField.setPreferredSize(new Dimension(224, 40)); // set size
dataNameTextField.addActionListener(dataHandler);
dataNameTextField.addFocusListener(dataHandler);
super.add(dataNameTextField); // add to the panel
// set value text field
dataValueTextField = new JTextField(bgValueText); // create new text field
dataValueTextField.setForeground(backTextColor); // set text color
dataValueTextField.setOpaque(true); // apply color changes
dataValueTextField.setPreferredSize(new Dimension(224, 40)); // set size
dataValueTextField.addActionListener(dataHandler);
dataValueTextField.addFocusListener(dataHandler);
super.add(dataValueTextField); // add to the panel
// set select button
selectButton = new JRadioButton(); // create new button
selectButton.putClientProperty("JComponent.sizeVariant", "large"); // set size
SwingUtilities.updateComponentTreeUI(this);
selectButton.addActionListener(dataHandler);
selectButton.addFocusListener(dataHandler);
super.add(selectButton); // add to the panel
// set delet button
deletButton = new JButton("✘"); // create new button
deletButton.setPreferredSize(new Dimension(40, 40)); // set button size
deletButton.setFont(deletButton.getFont().deriveFont(17.0f)); // set text size
deletButton.setBackground(backgroundColor); // set background color
deletButton.setForeground(Color.RED); // set text color
deletButton.setOpaque(true); // apply color changes
deletButton.addActionListener(dataHandler);
deletButton.addFocusListener(dataHandler);
super.add(deletButton); // add to the panel
}
/* Methods */
/**
* @return name of this pair
*/
public String getDataName() { return dataNameTextField.getText(); }
/**
* @return value of this pair
*/
public String getDataValue() { return dataValueTextField.getText(); }
/**
* @return {@code true} if this pair selecet to send
*/
public boolean isSelected() { return selectButton.isSelected(); }
private class DataEventHandler implements ActionListener, FocusListener
{
public void actionPerformed(ActionEvent e)
{
/* delet button */
if (e.getSource().equals(THIS.deletButton))
{
if (holdPlace.size() > 1)
{
holdPlace.get(0).requestFocus();
targetPanel.remove(THIS);
targetPanel.revalidate();
targetPanel.repaint();
holdPlace.remove(THIS);
}
}
}
public void focusGained(FocusEvent e)
{
if (e.getSource().equals(THIS.dataNameTextField)
||
e.getSource().equals(THIS.dataValueTextField))
{
JTextField focusedTextField = (JTextField)e.getSource();
if (focusedTextField.getText().equals(bgNameString) || focusedTextField.getText().equals(bgValueString))
{
focusedTextField.setText("");
focusedTextField.setForeground(Color.BLACK);
focusedTextField.setOpaque(true);
}
if (holdPlace.indexOf(THIS) == holdPlace.size()-1)
{
holdPlace.add(new NameValueData(bgNameString, bgValueString, targetPanel, holdPlace));
targetPanel.add(holdPlace.get(holdPlace.size()-1));
targetPanel.revalidate();
targetPanel.repaint();
}
}
}
public void focusLost(FocusEvent e)
{
if (e.getSource().equals(THIS.dataNameTextField)
||
e.getSource().equals(THIS.dataValueTextField))
{
JTextField focusLostedTextField = (JTextField)e.getSource();
if (focusLostedTextField.getText().length() != 0)
return;
String toPutString;
if (e.getSource().equals(THIS.dataNameTextField))
toPutString = bgNameString;
else
toPutString = bgValueString;
focusLostedTextField.setText(toPutString);
focusLostedTextField.setForeground(backTextColor);
focusLostedTextField.setOpaque(true);
}
}
}
}
// This class do the even handling of NewRequestGUI class
private class EventHandler implements ActionListener, FocusListener
{
private JFrame getNameFrame;
private JPanel getNamePanel;
private JTextField getNameTextField;
private JComboBox<String> getGroupComboBox;
private JButton saveNewButton;
public void actionPerformed(ActionEvent e)
{
/* body kinds combo box event mainHandler */
if (e.getSource().equals(bodyKindsComboBox))
{
if (bodyKindsComboBox.getSelectedItem().equals("Form data"))
((CardLayout) bodyTabSubPanel.getLayout()).show(bodyTabSubPanel, "f");
else if (bodyKindsComboBox.getSelectedItem().equals("JSON"))
((CardLayout) bodyTabSubPanel.getLayout()).show(bodyTabSubPanel, "j");
else if (bodyKindsComboBox.getSelectedItem().equals("Binary file"))
((CardLayout) bodyTabSubPanel.getLayout()).show(bodyTabSubPanel, "b");
}
if (e.getSource().equals(sendButton))
{
System.out.println("send button action");
setReqDetailsString();
Insomnia.setReqString(reqDetails);
Insomnia.setWhatToDo("send");
Insomnia.start();
}
if (e.getSource().equals(saveButton))
{
getNameFrame = new JFrame();
getNameFrame.setLocationRelativeTo(THIS);
getNameFrame.setMinimumSize(new Dimension(300, 150));
getNameFrame.setLocation(550, 250);
getNameFrame.setResizable(false);
getNamePanel = new JPanel(new GridLayout(3, 1, 10, 10));
getNamePanel.requestDefaultFocus();
getNamePanel.setFocusable(true);
getNameFrame.add(getNamePanel);
getNameTextField = new JTextField("new reqeust name ...");
getNameTextField.addFocusListener(this);
getNamePanel.add(getNameTextField);
getGroupComboBox = MainFrame.getGroups();
getNamePanel.add(getGroupComboBox);
saveNewButton = new JButton("save");
saveNewButton.addActionListener(this);
getNamePanel.add(saveNewButton);
getNameFrame.setVisible(true);
}
if (e.getSource().equals(saveNewButton))
{
String reqName = getNameTextField.getText();
String gpName = (String) getGroupComboBox.getSelectedItem();
if (gpName.equals("last requests"))
gpName = "";