-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainGUI.java
More file actions
405 lines (340 loc) · 13.5 KB
/
MainGUI.java
File metadata and controls
405 lines (340 loc) · 13.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* MainGUI is the main window of the Project Organizer.
* It lets the user create, edit, delete, save, and load projects.
*/
public class MainGUI extends JFrame {
// GUI components
private DefaultListModel<Project> projectListModel;
private JList<Project> lstProjects;
private JTextField txtTitle;
private JTextField txtStatus;
private JTextField txtDueDate; // yyyy-MM-dd
private JTextArea txtDescription;
private JTextArea txtCollaborators;
private JTextArea txtNotes;
private JButton btnNew;
private JButton btnUpdate;
private JButton btnDelete;
private JButton btnSaveToFile;
private JButton btnLoadFromFile;
// Control object
private ProjectManager projectManager;
private static final DateTimeFormatter DATE_FMT =
DateTimeFormatter.ofPattern("yyyy-MM-dd");
public MainGUI() {
super("Project Organizer Application");
projectManager = new ProjectManager();
projectListModel = new DefaultListModel<>();
initComponents();
layoutComponents();
attachHandlers();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(900, 600);
setLocationRelativeTo(null);
}
private void initComponents() {
lstProjects = new JList<>(projectListModel);
lstProjects.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
txtTitle = new JTextField(20);
txtStatus = new JTextField(10);
txtDueDate = new JTextField(10);
txtDescription = new JTextArea(5, 30);
txtDescription.setLineWrap(true);
txtDescription.setWrapStyleWord(true);
txtCollaborators = new JTextArea(4, 30);
txtCollaborators.setLineWrap(true);
txtCollaborators.setWrapStyleWord(true);
txtNotes = new JTextArea(4, 30);
txtNotes.setLineWrap(true);
txtNotes.setWrapStyleWord(true);
btnNew = new JButton("Add Project");
btnUpdate = new JButton("Update Project");
btnDelete = new JButton("Delete Project");
btnSaveToFile = new JButton("Save All");
btnLoadFromFile = new JButton("Load");
}
private void layoutComponents() {
setLayout(new BorderLayout(10, 10));
// ===== LEFT: project list =====
JPanel leftPanel = new JPanel(new BorderLayout(5, 5));
leftPanel.setBorder(BorderFactory.createTitledBorder("Projects"));
leftPanel.add(new JScrollPane(lstProjects), BorderLayout.CENTER);
JPanel leftButtons = new JPanel(new GridLayout(3, 1, 5, 5));
leftButtons.add(btnNew);
leftButtons.add(btnUpdate);
leftButtons.add(btnDelete);
leftPanel.add(leftButtons, BorderLayout.SOUTH);
add(leftPanel, BorderLayout.WEST);
// ===== RIGHT: details =====
JPanel rightPanel = new JPanel();
rightPanel.setBorder(BorderFactory.createTitledBorder("Project Details"));
rightPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1.0;
// make fields visually larger
txtTitle.setColumns(30);
txtStatus.setColumns(20);
txtDueDate.setColumns(15);
txtDescription.setRows(4);
txtCollaborators.setRows(3);
txtNotes.setRows(3);
txtDescription.setPreferredSize(new Dimension(400, 80));
txtCollaborators.setPreferredSize(new Dimension(400, 60));
txtNotes.setPreferredSize(new Dimension(400, 60));
int row = 0;
// Title
gbc.gridx = 0; gbc.gridy = row; gbc.weightx = 0; gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
rightPanel.add(new JLabel("Title:"), gbc);
gbc.gridx = 1; gbc.gridy = row; gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
rightPanel.add(txtTitle, gbc);
row++;
// Status
gbc.gridx = 0; gbc.gridy = row; gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
rightPanel.add(new JLabel("Status (To-Do / In-Progress / Completed):"), gbc);
gbc.gridx = 1; gbc.gridy = row; gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
rightPanel.add(txtStatus, gbc);
row++;
// Due date
gbc.gridx = 0; gbc.gridy = row; gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
rightPanel.add(new JLabel("Due Date (yyyy-MM-dd, optional):"), gbc);
gbc.gridx = 1; gbc.gridy = row; gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
rightPanel.add(txtDueDate, gbc);
row++;
// Short Description (big text area)
gbc.gridx = 0; gbc.gridy = row; gbc.weightx = 0; gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
rightPanel.add(new JLabel("Short Description:"), gbc);
gbc.gridx = 1; gbc.gridy = row; gbc.weightx = 1.0; gbc.weighty = 0.3;
gbc.fill = GridBagConstraints.BOTH;
rightPanel.add(new JScrollPane(txtDescription), gbc);
row++;
// Collaborators (big text area)
gbc.gridx = 0; gbc.gridy = row; gbc.weightx = 0; gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
rightPanel.add(new JLabel("Collaborators (one per line, name <email>):"), gbc);
gbc.gridx = 1; gbc.gridy = row; gbc.weightx = 1.0; gbc.weighty = 0.2;
gbc.fill = GridBagConstraints.BOTH;
rightPanel.add(new JScrollPane(txtCollaborators), gbc);
row++;
// Research Notes (big text area)
gbc.gridx = 0; gbc.gridy = row; gbc.weightx = 0; gbc.weighty = 0;
gbc.fill = GridBagConstraints.NONE;
rightPanel.add(new JLabel("Research Notes / Links (one per line):"), gbc);
gbc.gridx = 1; gbc.gridy = row; gbc.weightx = 1.0; gbc.weighty = 0.2;
gbc.fill = GridBagConstraints.BOTH;
rightPanel.add(new JScrollPane(txtNotes), gbc);
row++;
// Bottom buttons (Load / Save All)
gbc.gridx = 0; gbc.gridy = row; gbc.gridwidth = 2;
gbc.weightx = 1.0; gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
JPanel bottomButtons = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomButtons.add(btnLoadFromFile);
bottomButtons.add(btnSaveToFile);
rightPanel.add(bottomButtons, gbc);
add(rightPanel, BorderLayout.CENTER);}
private void attachHandlers() {
lstProjects.addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
Project selected = lstProjects.getSelectedValue();
if (selected != null) {
populateDetails(selected);
}
}
});
btnNew.addActionListener(e -> handleAddProject());
btnUpdate.addActionListener(e -> handleUpdateProject());
btnDelete.addActionListener(e -> handleDeleteProject());
btnSaveToFile.addActionListener(e -> handleSave());
btnLoadFromFile.addActionListener(e -> handleLoad());
}
private void populateDetails(Project p) {
txtTitle.setText(p.getTitle());
txtStatus.setText(p.getStatus());
txtDueDate.setText(p.getDueDate() != null
? p.getDueDate().format(DATE_FMT) : "");
txtDescription.setText(p.getDescription());
StringBuilder collabText = new StringBuilder();
for (Collaborator c : p.getCollaborators()) {
collabText.append(c.getName());
if (c.getEmail() != null && !c.getEmail().isEmpty()) {
collabText.append(" <").append(c.getEmail()).append(">");
}
collabText.append("\n");
}
txtCollaborators.setText(collabText.toString().trim());
StringBuilder noteText = new StringBuilder();
for (ResearchNote n : p.getNotes()) {
noteText.append(n.getContent());
if (n.getLink() != null && !n.getLink().isEmpty()) {
noteText.append(" (").append(n.getLink()).append(")");
}
noteText.append("\n");
}
txtNotes.setText(noteText.toString().trim());
}
/** Build a Project object based on the current form fields. */
private Project buildProjectFromFields() {
String title = txtTitle.getText().trim();
if (title.isEmpty()) {
JOptionPane.showMessageDialog(this,
"Title is required.",
"Validation Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
String status = txtStatus.getText().trim();
String dueStr = txtDueDate.getText().trim();
LocalDate due = null;
if (!dueStr.isEmpty()) {
try {
due = LocalDate.parse(dueStr, DATE_FMT);
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Invalid date format. Use yyyy-MM-dd.",
"Validation Error",
JOptionPane.ERROR_MESSAGE);
return null;
}
}
String desc = txtDescription.getText().trim();
Project p = new Project(title);
p.setStatus(status.isEmpty() ? "To-Do" : status);
p.setDueDate(due);
p.setDescription(desc);
// collaborators
String[] collabLines = txtCollaborators.getText().split("\\r?\\n");
for (String line : collabLines) {
line = line.trim();
if (line.isEmpty()) continue;
String name = line;
String email = "";
int ltIndex = line.indexOf('<');
int gtIndex = line.indexOf('>');
if (ltIndex >= 0 && gtIndex > ltIndex) {
name = line.substring(0, ltIndex).trim();
email = line.substring(ltIndex + 1, gtIndex).trim();
}
p.addCollaborator(new Collaborator(name, email));
}
// notes
String[] noteLines = txtNotes.getText().split("\\r?\\n");
for (String line : noteLines) {
line = line.trim();
if (line.isEmpty()) continue;
String content = line;
String link = "";
int openParen = line.indexOf('(');
int closeParen = line.lastIndexOf(')');
if (openParen >= 0 && closeParen > openParen) {
content = line.substring(0, openParen).trim();
link = line.substring(openParen + 1, closeParen).trim();
}
p.addNote(new ResearchNote(content, link));
}
return p;
}
private void handleAddProject() {
Project newProject = buildProjectFromFields();
if (newProject == null) return;
projectManager.addProject(newProject);
refreshProjectList();
lstProjects.setSelectedValue(newProject, true);
}
private void handleUpdateProject() {
Project selected = lstProjects.getSelectedValue();
if (selected == null) {
JOptionPane.showMessageDialog(this,
"Select a project to update.",
"No Selection",
JOptionPane.WARNING_MESSAGE);
return;
}
Project updated = buildProjectFromFields();
if (updated == null) return;
projectManager.updateProject(selected, updated);
refreshProjectList();
lstProjects.setSelectedValue(updated, true);
}
private void handleDeleteProject() {
Project selected = lstProjects.getSelectedValue();
if (selected == null) {
JOptionPane.showMessageDialog(this,
"Select a project to delete.",
"No Selection",
JOptionPane.WARNING_MESSAGE);
return;
}
int confirm = JOptionPane.showConfirmDialog(
this,
"Delete selected project?",
"Confirm Delete",
JOptionPane.YES_NO_OPTION
);
if (confirm == JOptionPane.YES_OPTION) {
projectManager.deleteProject(selected);
refreshProjectList();
clearDetailFields();
}
}
private void clearDetailFields() {
txtTitle.setText("");
txtStatus.setText("");
txtDueDate.setText("");
txtDescription.setText("");
txtCollaborators.setText("");
txtNotes.setText("");
}
private void refreshProjectList() {
projectListModel.clear();
List<Project> projects = projectManager.getProjects();
for (Project p : projects) {
projectListModel.addElement(p);
}
}
private void handleSave() {
try {
projectManager.saveAll();
JOptionPane.showMessageDialog(this, "Projects saved successfully.");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Error saving projects: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void handleLoad() {
try {
projectManager.loadAll();
refreshProjectList();
JOptionPane.showMessageDialog(this, "Projects loaded successfully.");
} catch (Exception ex) {
JOptionPane.showMessageDialog(this,
"Error loading projects: " + ex.getMessage(),
"Error",
JOptionPane.ERROR_MESSAGE);
}
}
/** Entry point – you can also make a separate Main class that just creates MainGUI. */
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainGUI gui = new MainGUI();
gui.setVisible(true);
});
}
}