-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSuggestWordScreenController.java
More file actions
60 lines (48 loc) · 2.47 KB
/
Copy pathSuggestWordScreenController.java
File metadata and controls
60 lines (48 loc) · 2.47 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
package com.abhidhan.dictionary.ui.controller;
import com.abhidhan.dictionary.backend.service.DictionaryService;
import com.abhidhan.dictionary.backend.model.Suggestion;
import com.abhidhan.dictionary.ui.SuggestWordScreen;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SuggestWordScreenController {
private final SuggestWordScreen suggestWordScreen;
private final DictionaryService dictionaryService;
public SuggestWordScreenController(SuggestWordScreen suggestWordScreen, DictionaryService dictionaryService) {
this.suggestWordScreen = suggestWordScreen;
this.dictionaryService = dictionaryService;
this.suggestWordScreen.getSuggestButton().addActionListener(new SuggestButtonListener());
this.suggestWordScreen.getCancelButton().addActionListener(new CancelButtonListener());
}
class SuggestButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
suggestWord();
}
}
class CancelButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
suggestWordScreen.dispose();
}
}
private void suggestWord() {
String word = suggestWordScreen.getWordField().getText().trim();
String meaning = suggestWordScreen.getMeaningField().getText().trim();
String language = (String) suggestWordScreen.getLanguageComboBox().getSelectedItem();
if (word.isEmpty() || meaning.isEmpty() || language == null || language.isEmpty()) {
JOptionPane.showMessageDialog(suggestWordScreen, "Word, meaning, and language are required!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Create a Suggestion object (we'll leave suggestedBy null for now)
Suggestion newSuggestion = new Suggestion(word, meaning, language, null, "pending");
try {
dictionaryService.addSuggestion(newSuggestion);
JOptionPane.showMessageDialog(suggestWordScreen, "Suggestion submitted successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
suggestWordScreen.dispose();
} catch (Exception ex) {
JOptionPane.showMessageDialog(suggestWordScreen, "Error submitting suggestion: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
}