14
14
*/
15
15
public class CreateFlashcardPanel extends JPanel implements ActionListener {
16
16
CreateFlashcardController controller ;
17
- JTextArea term_text ;
18
- JTextArea definition_text ;
17
+ JTextArea term_text , definition_text ;
18
+ JLabel existing_term , existing_definition ;
19
19
JFrame fcCMain ;
20
- public CreateFlashcardPanel (CreateFlashcardController controller , JFrame fcCMain ){
20
+
21
+ public CreateFlashcardPanel (CreateFlashcardController controller , JFrame fcCMain ) {
21
22
//Initiating all the sub panels.
22
23
this .fcCMain = fcCMain ;
23
24
this .controller = controller ;
24
25
this .setLayout (new BorderLayout ());
25
26
JPanel buttonPanel = new JPanel (new FlowLayout ());
26
- JPanel labelPanel = new JPanel (new GridLayout (1 ,2 ));
27
- JPanel textPanel = new JPanel (new GridLayout (1 , 2 ));
27
+ JPanel labelPanel = new JPanel (new GridLayout (1 , 2 ));
28
+ JPanel textPanel = new JPanel (new GridLayout (2 , 2 ));
28
29
29
30
//Creating the button panel.
30
31
JButton confirm = new JButton ("confirm" );
@@ -33,7 +34,6 @@ public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain
33
34
cancel .addActionListener (e -> this .fcCMain .dispose ());
34
35
buttonPanel .add (confirm );
35
36
buttonPanel .add (cancel );
36
-
37
37
JLabel term_label = new JLabel ("term" );
38
38
JLabel definition_label = new JLabel ("definition" );
39
39
labelPanel .add (term_label );
@@ -44,48 +44,75 @@ public CreateFlashcardPanel(CreateFlashcardController controller, JFrame fcCMain
44
44
definition_text = new JTextArea ();
45
45
term_text .setLineWrap (true );
46
46
definition_text .setLineWrap (true );
47
+ existing_term = new JLabel ("" );
48
+ existing_definition = new JLabel ("" );
47
49
textPanel .add (term_text );
48
50
textPanel .add (definition_text );
51
+ textPanel .add (existing_term );
52
+ textPanel .add (existing_definition );
49
53
50
54
//Creating the label panel.
51
55
this .add (labelPanel , BorderLayout .NORTH );
52
56
this .add (textPanel );
53
57
this .add (buttonPanel , BorderLayout .SOUTH );
54
- this .setSize (1000 ,500 );
58
+ this .setSize (1000 , 500 );
55
59
this .setVisible (true );
56
60
57
61
58
-
59
62
}
60
63
61
64
/**
62
65
* Response based on creation success or failure.
66
+ *
63
67
* @param e the event to be processed
64
68
*/
65
69
@ Override
66
70
public void actionPerformed (ActionEvent e ) {
67
- try {
71
+ try {
68
72
//Success view.
69
- CreateFlashcardResponseModel responseModel = controller .create (term_text .getText ().replace ("\n " , " " ),
70
- definition_text .getText ().replace ("\n " , " " ));
71
- int action = JOptionPane .showConfirmDialog (this ,
72
- "Card created:\n " +responseModel .getTerm ()+ "\n " + responseModel .getDefinition ()
73
- +"\n create another card?" );
74
- if (action == JOptionPane .YES_OPTION ){
75
- term_text .setText ("" );
76
- definition_text .setText ("" );
77
- }else {
78
- fcCMain .dispose ();
73
+ CreateFlashcardResponseModel responseModel = controller .create (term_text .getText (),
74
+ definition_text .getText (), -1 );
75
+ if (responseModel .getFlashcardId () == -1 ) {
76
+ successView (responseModel );
77
+ } else {
78
+ Object [] options = {"re-edit current card" , "overwrite" , "back to flashcard set" };
79
+ int action = JOptionPane .showOptionDialog (this ,
80
+ "Term already exist:\n " + responseModel .getTerm () + "\n " + responseModel .getDefinition ()
81
+ + "\n current card:\n " + term_text .getText () + "\n " + definition_text .getText (),
82
+ "conflict" , JOptionPane .YES_NO_CANCEL_OPTION , JOptionPane .WARNING_MESSAGE ,
83
+ null , options , options [0 ]);
84
+ if (action == JOptionPane .YES_OPTION ) {
85
+ existing_definition .setText ("<html>existing definition :<br>" + responseModel .getDefinition () +
86
+ "</html>" );
87
+ } else if (action == JOptionPane .NO_OPTION ) {
88
+ responseModel = controller .create (term_text .getText ().replace ("\n " , " " ),
89
+ definition_text .getText ().replace ("\n " , " " ), responseModel .getFlashcardId ());
90
+ successView (responseModel );
91
+ } else {
92
+ fcCMain .dispose ();
93
+ }
79
94
}
80
- }catch (RuntimeException error ){
95
+ } catch (RuntimeException error ) {
81
96
//Failure view.
82
97
int action = JOptionPane .showConfirmDialog (this ,
83
- error + "\n Recreate?" );
84
- if (action == JOptionPane .YES_OPTION ){
85
- term_text .setText ("" );
86
- }else {
98
+ error + "\n re-edit?" );
99
+ if (action == JOptionPane .NO_OPTION ) {
87
100
fcCMain .dispose ();
88
101
}
89
102
}
90
103
}
104
+
105
+ private void successView (CreateFlashcardResponseModel responseModel ){
106
+ Object [] options = {"create another card" , "back to flashcard set" };
107
+ int action = JOptionPane .showOptionDialog (this ,
108
+ "Card created:\n " + responseModel .getTerm () + "\n " + responseModel .getDefinition (),
109
+ "Success" , JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE ,
110
+ null , options , options [0 ]);
111
+ if (action == JOptionPane .YES_OPTION ) {
112
+ term_text .setText ("" );
113
+ definition_text .setText ("" );
114
+ } else {
115
+ fcCMain .dispose ();
116
+ }
117
+ }
91
118
}
0 commit comments