7
7
import java .awt .*;
8
8
import java .awt .event .ActionEvent ;
9
9
import java .awt .event .ActionListener ;
10
+ import java .time .format .DateTimeFormatter ;
10
11
11
12
/**
12
13
* Panel for flashcard creator.
@@ -72,30 +73,18 @@ public void actionPerformed(ActionEvent e) {
72
73
//Success view.
73
74
CreateFlashcardResponseModel responseModel = controller .create (term_text .getText (),
74
75
definition_text .getText (), -1 );
75
- if (responseModel .getFlashcardId () == - 1 ) {
76
+ if (! responseModel .existsDuplicate () ) {
76
77
successView (responseModel );
77
78
} 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
+ duplicateView (responseModel );
94
80
}
95
81
} catch (RuntimeException error ) {
96
82
//Failure view.
97
- int action = JOptionPane .showConfirmDialog (this ,
98
- error + "\n re-edit?" );
83
+ Object [] options = {"re_edit" , "back to flashcard set" };
84
+ int action = JOptionPane .showOptionDialog (this ,
85
+ error .getMessage (),
86
+ "Failure" , JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE ,
87
+ null , options , options [0 ]);
99
88
if (action == JOptionPane .NO_OPTION ) {
100
89
fcCMain .dispose ();
101
90
}
@@ -105,7 +94,9 @@ public void actionPerformed(ActionEvent e) {
105
94
private void successView (CreateFlashcardResponseModel responseModel ){
106
95
Object [] options = {"create another card" , "back to flashcard set" };
107
96
int action = JOptionPane .showOptionDialog (this ,
108
- "Card created:\n " + responseModel .getTerm () + "\n " + responseModel .getDefinition (),
97
+ "Card created at " + responseModel .getCreationDate ().format (DateTimeFormatter .
98
+ ofPattern ("yyyy-MM-dd HH:mm" )) + "\n Term: " + responseModel .getTerm () + "\n Definition: " +
99
+ responseModel .getDefinition () + "\n id: " + responseModel .getFlashcardId (),
109
100
"Success" , JOptionPane .YES_NO_OPTION , JOptionPane .QUESTION_MESSAGE ,
110
101
null , options , options [0 ]);
111
102
if (action == JOptionPane .YES_OPTION ) {
@@ -115,4 +106,24 @@ private void successView(CreateFlashcardResponseModel responseModel){
115
106
fcCMain .dispose ();
116
107
}
117
108
}
109
+ private void duplicateView (CreateFlashcardResponseModel responseModel ){
110
+ Object [] options = {"re-edit current card" , "overwrite" , "back to flashcard set" };
111
+ int action = JOptionPane .showOptionDialog (this ,
112
+ "Term already exist:\n " + responseModel .getTerm () + "\n " + responseModel .getDefinition ()
113
+ + "\n current card:\n " + term_text .getText () + "\n " + definition_text .getText (),
114
+ "conflict" , JOptionPane .YES_NO_CANCEL_OPTION , JOptionPane .WARNING_MESSAGE ,
115
+ null , options , options [0 ]);
116
+ if (action == JOptionPane .YES_OPTION ) {
117
+ existing_definition .setText ("<html>existing definition :<br>" + responseModel .getDefinition () +
118
+ "</html>" );
119
+ existing_term .setText ("<html>existing term :<br>" + responseModel .getTerm () +
120
+ "</html>" );
121
+ } else if (action == JOptionPane .NO_OPTION ) {
122
+ responseModel = controller .create (term_text .getText ().replace ("\n " , " " ),
123
+ definition_text .getText ().replace ("\n " , " " ), responseModel .getFlashcardId ());
124
+ successView (responseModel );
125
+ } else {
126
+ fcCMain .dispose ();
127
+ }
128
+ }
118
129
}
0 commit comments