Repository navigation
Expand file tree
/
Copy pathFeedbackController.java
More file actions
363 lines (313 loc) Β· 12.4 KB
/
Copy pathFeedbackController.java
File metadata and controls
363 lines (313 loc) Β· 12.4 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
package com.simulator;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.TextFieldListCell;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.regex.Pattern;
/**
* π§ FEEDBACK CONTROLLER
* Professional feedback form with email sending capabilities
*/
public class FeedbackController implements Initializable {
@FXML private TextField nameField;
@FXML private TextField emailField;
@FXML private TextField locationField;
@FXML private ComboBox<String> osComboBox;
@FXML private ComboBox<String> issueTypeComboBox;
@FXML private TextArea descriptionArea;
@FXML private Button attachButton;
@FXML private ListView<String> attachmentList;
@FXML private Button cancelButton;
@FXML private Button sendButton;
@FXML private ProgressBar progressBar;
private final ObservableList<File> attachments = FXCollections.observableArrayList();
private final ObservableList<String> attachmentNames = FXCollections.observableArrayList();
private EmailService emailService;
private Stage dialogStage;
// Constants
private static final long MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
private static final String EMAIL_PATTERN =
"^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
@Override
public void initialize(URL location, ResourceBundle resources) {
setupComponents();
setupValidation();
emailService = EmailService.getInstance();
}
/**
* π¨ SETUP: Initialize form components
*/
private void setupComponents() {
// Initialize OS ComboBox
osComboBox.setItems(FXCollections.observableArrayList(
System.getProperty("os.name") + " " + System.getProperty("os.version"),
"Windows 10", "Windows 11", "macOS", "Ubuntu", "Other Linux", "Other"
));
osComboBox.getSelectionModel().selectFirst();
// Initialize Issue Type ComboBox
issueTypeComboBox.setItems(FXCollections.observableArrayList(
"π Bug Report", "π‘ Feature Request", "β Question",
"π Performance Issue", "π Documentation", "π¨ UI/UX Feedback", "π§ Other"
));
issueTypeComboBox.getSelectionModel().selectFirst();
// Setup attachment list
attachmentList.setItems(attachmentNames);
attachmentList.setCellFactory(TextFieldListCell.forListView());
// Setup context menu for attachments
setupAttachmentContextMenu();
// Set tooltips
attachButton.setTooltip(new Tooltip("Add files (Images, Videos, Documents)"));
sendButton.setTooltip(new Tooltip("Send feedback via email"));
System.out.println("β
Feedback form components initialized");
}
/**
* π SETUP: Input validation
*/
private void setupValidation() {
// Real-time validation
nameField.textProperty().addListener((obs, old, text) -> updateSendButtonState());
emailField.textProperty().addListener((obs, old, text) -> updateSendButtonState());
descriptionArea.textProperty().addListener((obs, old, text) -> updateSendButtonState());
// Email format validation
emailField.focusedProperty().addListener((obs, wasFocused, isFocused) -> {
if (!isFocused && !emailField.getText().isEmpty()) {
validateEmailFormat();
}
});
}
/**
* π SETUP: Attachment context menu
*/
private void setupAttachmentContextMenu() {
ContextMenu contextMenu = new ContextMenu();
MenuItem removeItem = new MenuItem("Remove");
removeItem.setOnAction(e -> {
int selectedIndex = attachmentList.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
attachments.remove(selectedIndex);
attachmentNames.remove(selectedIndex);
System.out.println("π Attachment removed: " + selectedIndex);
}
});
contextMenu.getItems().add(removeItem);
attachmentList.setContextMenu(contextMenu);
}
/**
* π ACTION: Select files for attachment
*/
@FXML
private void selectFiles() {
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Files to Attach");
// Set extension filters
fileChooser.getExtensionFilters().addAll(
new FileChooser.ExtensionFilter("All Supported",
"*.jpg", "*.jpeg", "*.png", "*.gif", "*.mp4", "*.mov", "*.avi",
"*.pdf", "*.doc", "*.docx", "*.txt", "*.log"),
new FileChooser.ExtensionFilter("Images", "*.jpg", "*.jpeg", "*.png", "*.gif"),
new FileChooser.ExtensionFilter("Videos", "*.mp4", "*.mov", "*.avi"),
new FileChooser.ExtensionFilter("Documents", "*.pdf", "*.doc", "*.docx", "*.txt", "*.log"),
new FileChooser.ExtensionFilter("All Files", "*.*")
);
List<File> selectedFiles = fileChooser.showOpenMultipleDialog(attachButton.getScene().getWindow());
if (selectedFiles != null) {
for (File file : selectedFiles) {
if (file.length() > MAX_FILE_SIZE) {
showAlert("File Too Large",
"File '" + file.getName() + "' exceeds 10MB limit.");
continue;
}
if (!attachments.contains(file)) {
attachments.add(file);
attachmentNames.add(file.getName() + " (" + formatFileSize(file.length()) + ")");
System.out.println("π Attachment added: " + file.getName());
}
}
}
} catch (Exception e) {
System.err.println("β Error selecting files: " + e.getMessage());
showAlert("Error", "Failed to select files: " + e.getMessage());
}
}
/**
* β
VALIDATION: Update send button state
*/
private void updateSendButtonState() {
boolean isValid = !StringUtils.isBlank(nameField.getText()) &&
!StringUtils.isBlank(emailField.getText()) &&
!StringUtils.isBlank(descriptionArea.getText()) &&
isValidEmail(emailField.getText());
sendButton.setDisable(!isValid);
}
/**
* π§ VALIDATION: Email format
*/
private boolean isValidEmail(String email) {
return Pattern.compile(EMAIL_PATTERN).matcher(email).matches();
}
/**
* β οΈ VALIDATION: Show email format error
*/
private void validateEmailFormat() {
if (!isValidEmail(emailField.getText())) {
emailField.setStyle("-fx-border-color: red;");
showAlert("Invalid Email", "Please enter a valid email address.");
} else {
emailField.setStyle("");
}
}
/**
* π¨ ACTION: Send feedback email
*/
@FXML
private void sendFeedback() {
if (!validateForm()) {
return;
}
// Disable form during sending
setFormEnabled(false);
progressBar.setVisible(true);
// Create email data
FeedbackData feedbackData = createFeedbackData();
// Send email asynchronously
Task<Boolean> emailTask = new Task<Boolean>() {
@Override
protected Boolean call() throws Exception {
return emailService.sendFeedbackEmail(feedbackData);
}
@Override
protected void succeeded() {
Platform.runLater(() -> {
progressBar.setVisible(false);
showAlert("Feedback Sent!",
"Thank you for your feedback! We'll get back to you soon.");
closeDialog();
});
}
@Override
protected void failed() {
Platform.runLater(() -> {
setFormEnabled(true);
progressBar.setVisible(false);
showAlert("Send Failed",
"Failed to send feedback: " + getException().getMessage());
});
}
};
new Thread(emailTask).start();
System.out.println("π¨ Feedback email sending initiated");
}
/**
* π DATA: Create feedback data object
*/
private FeedbackData createFeedbackData() {
FeedbackData data = new FeedbackData();
data.setName(nameField.getText().trim());
data.setEmail(emailField.getText().trim());
data.setLocation(locationField.getText().trim());
data.setOperatingSystem(osComboBox.getValue());
data.setIssueType(issueTypeComboBox.getValue());
data.setDescription(descriptionArea.getText().trim());
data.setAttachments(new ArrayList<>(attachments));
// Add system information
data.setAppVersion(AlgorithmSimulatorApplication.getVersion());
data.setJavaVersion(System.getProperty("java.version"));
data.setJavaVendor(System.getProperty("java.vendor"));
return data;
}
/**
* β
VALIDATION: Validate entire form
*/
private boolean validateForm() {
List<String> errors = new ArrayList<>();
if (StringUtils.isBlank(nameField.getText())) {
errors.add("Name is required");
}
if (StringUtils.isBlank(emailField.getText())) {
errors.add("Email is required");
} else if (!isValidEmail(emailField.getText())) {
errors.add("Invalid email format");
}
if (StringUtils.isBlank(descriptionArea.getText())) {
errors.add("Issue description is required");
}
if (!errors.isEmpty()) {
showAlert("Validation Error", String.join("\n", errors));
return false;
}
return true;
}
/**
* π UTILITY: Enable/disable form components
*/
private void setFormEnabled(boolean enabled) {
nameField.setDisable(!enabled);
emailField.setDisable(!enabled);
locationField.setDisable(!enabled);
osComboBox.setDisable(!enabled);
issueTypeComboBox.setDisable(!enabled);
descriptionArea.setDisable(!enabled);
attachButton.setDisable(!enabled);
sendButton.setDisable(!enabled);
}
/**
* π UTILITY: Format file size
*/
private String formatFileSize(long bytes) {
if (bytes < 1024) return bytes + " B";
if (bytes < 1024 * 1024) return (bytes / 1024) + " KB";
return (bytes / (1024 * 1024)) + " MB";
}
/**
* β οΈ UTILITY: Show alert dialog
*/
private void showAlert(String title, String message) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
// Apply theme to alert
try {
if (alert.getDialogPane().getScene() != null) {
ThemeManager.getInstance().registerScene(alert.getDialogPane().getScene());
}
} catch (Exception e) {
System.out.println("βΉοΈ Could not apply theme to alert");
}
alert.showAndWait();
}
/**
* β ACTION: Cancel feedback
*/
@FXML
private void cancel() {
closeDialog();
}
/**
* πͺ UTILITY: Close dialog
*/
private void closeDialog() {
if (dialogStage != null) {
dialogStage.close();
}
}
/**
* π― SETTER: Set dialog stage reference
*/
public void setDialogStage(Stage dialogStage) {
this.dialogStage = dialogStage;
}
}