• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Irfancpv99 / Note-Manager / #37

05 Jan 2026 03:35PM UTC coverage: 99.353% (-0.6%) from 100.0%
#37

push

web-flow
SonarCloud Fix (#187)

* Conflict Remove (#166)

* Merge with main Branch - Note Mongo  Repository (#152)

* Delete NoteSwingViewTest.java

Double file By mistake

* Test and main file of GUI (#114)

* Complete Note (Model file and Test file )

* RED: Add failing test for Category getId returns null

* GREEN: Add id field and getId method to Category

* RED: Add failing test for Category setId

* GREEN: Add setId method to Category

* RED: Add failing test for Category setName

* GREEN: Add setName method with validation to Category

* Add test for Category setName null validation - this will pass

* RED: Add failing test for Category equals by id

* GREEN: Add equals method to Category

* Add test for Category equals different id

* Add equals edge case tests

* RED: Add failing test for Category hashCode

* GREEN: Add hashCode method to Category

* RED: Add failing test for Category toString

* GREEN: Add toString method to Category

* REFACTOR: Extract validateName method

* RED: Add failing test for Note constructor sets text and categoryId

* GREEN: Add Note with constructor, getText and getCategoryId

* RED: Add failing test for Note null text validation

* GREEN: Add text validation to Note constructor

* Add test for Note empty text validation

* RED: Add failing test for Note null categoryId validation

* GREEN: Add categoryId validation to Note constructor (#28)

* Add test for Note empty categoryId validation (#29)

* GREEN: Add categoryId validation to Note constructor

* Add test for Note empty categoryId validation

* RED: Add failing test for Note getId returns null (#30)

* GREEN: Add categoryId validation to Note constructor

* Add test for Note empty categoryId validation

* RED: Add failing test for Note getId returns null

* GREEN: Add id field and getId method to Note (#31)

* GREEN: Add categoryId validation to Note constructor

* Add test for Note empty categoryId validation

* RED: Add failing test for Note getId returns null

... (continued)

307 of 309 relevant lines covered (99.35%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.26
/src/main/java/com/notemanager/view/swing/NoteSwingView.java
1
package com.notemanager.view.swing;
2

3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.FlowLayout;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9

10
import javax.swing.DefaultComboBoxModel;
11
import javax.swing.DefaultListModel;
12
import javax.swing.JButton;
13
import javax.swing.JComboBox;
14
import javax.swing.JFrame;
15
import javax.swing.JLabel;
16
import javax.swing.JList;
17
import javax.swing.JPanel;
18
import javax.swing.JScrollPane;
19
import javax.swing.JTextArea;
20
import javax.swing.ListSelectionModel;
21

22
import com.notemanager.controller.NoteController;
23
import com.notemanager.model.Category;
24
import com.notemanager.model.Note;
25
import com.notemanager.view.NoteView;
26

27
public class NoteSwingView extends JFrame implements NoteView {
28

29
        private static final long serialVersionUID = 1L;
30

31
        private JComboBox<CategoryItem> categoryComboBox;
32
        private DefaultComboBoxModel<CategoryItem> categoryComboBoxModel;
33
        private JTextArea noteTextArea;
34
        private JButton saveButton;
35
        private JList<Note> notesList;
36
        private DefaultListModel<Note> notesListModel;
37
        private JButton editButton;
38
        private JButton deleteButton;
39
        private JLabel errorLabel;
40
        private Map<String, String> categoryIdToName = new HashMap<>();
1✔
41

42
        private NoteController noteController;
43
        private boolean editMode = false;
1✔
44
        private String editingNoteId = null;
1✔
45

46
        public NoteSwingView() {
1✔
47
                setTitle("Note Manager");
1✔
48
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1✔
49
                setSize(500, 600);
1✔
50
                setLayout(new BorderLayout());
1✔
51

52
                categoryComboBoxModel = new DefaultComboBoxModel<>();
1✔
53
                categoryComboBox = new JComboBox<>(categoryComboBoxModel);
1✔
54
                categoryComboBox.setName("categoryComboBox");
1✔
55

56
                noteTextArea = new JTextArea(5, 30);
1✔
57
                noteTextArea.setName("noteTextArea");
1✔
58

59
                saveButton = new JButton("Save");
1✔
60
                saveButton.setName("saveButton");
1✔
61
                saveButton.addActionListener(e -> {
1✔
62
                        String text = noteTextArea.getText();
1✔
63
                        CategoryItem selectedCategory = (CategoryItem) categoryComboBox.getSelectedItem();
1✔
64
                        String categoryId = selectedCategory != null ? selectedCategory.getId() : null;
1✔
65

66
                        if (editMode && editingNoteId != null) {
1✔
67
                                noteController.updateNote(editingNoteId, text, categoryId);
1✔
68
                        } else {
69
                                noteController.newNote(text, categoryId);
1✔
70
                        }
71
                });
1✔
72

73
                notesListModel = new DefaultListModel<>();
1✔
74
                notesList = new JList<>(notesListModel);
1✔
75
                notesList.setName("notesList");
1✔
76
                notesList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
1✔
77
                notesList.addListSelectionListener(e -> {
1✔
78
                        if (!e.getValueIsAdjusting()) {
1✔
79
                                boolean hasSelection = notesList.getSelectedIndex() != -1;
1✔
80
                                editButton.setEnabled(hasSelection);
1✔
81
                                deleteButton.setEnabled(hasSelection);
1✔
82
                        }
83
                });
1✔
84
                
85
                notesList.setCellRenderer((list, note, index, isSelected, cellHasFocus) -> {
1✔
86
                    String categoryName = categoryIdToName.getOrDefault(note.getCategoryId(), "Unknown");
1✔
87
                    JLabel label = new JLabel("Note : " + note.getText() + " | Category : " + categoryName);
1✔
88
                    if (isSelected) {
1✔
89
                        label.setBackground(list.getSelectionBackground());
1✔
90
                        label.setForeground(list.getSelectionForeground());
1✔
91
                        label.setOpaque(true);
1✔
92
                    }
93
                    return label;
1✔
94
                });
95

96
                editButton = new JButton("Edit");
1✔
97
                editButton.setName("editButton");
1✔
98
                editButton.setEnabled(false);
1✔
99
                editButton.addActionListener(e -> {
1✔
100
                        Note selected = notesList.getSelectedValue();
1✔
101
                        if (selected != null) {
1✔
102
                                editMode = true;
1✔
103
                                editingNoteId = selected.getId();
1✔
104
                                noteTextArea.setText(selected.getText());
1✔
105
                                saveButton.setText("Update");
1✔
106
                        }
107
                });
1✔
108

109
                deleteButton = new JButton("Delete");
1✔
110
                deleteButton.setName("deleteButton");
1✔
111
                deleteButton.setEnabled(false);
1✔
112
                deleteButton.addActionListener(e -> {
1✔
113
                        Note selected = notesList.getSelectedValue();
1✔
114
                        if (selected != null) {
1✔
115
                                noteController.deleteNote(selected.getId());
1✔
116
                        }
117
                });
1✔
118

119
                errorLabel = new JLabel(" ");
1✔
120
                errorLabel.setName("errorLabel");
1✔
121
                errorLabel.setForeground(Color.RED);
1✔
122

123
                JPanel topPanel = new JPanel(new BorderLayout());
1✔
124
                topPanel.add(categoryComboBox, BorderLayout.NORTH);
1✔
125
                topPanel.add(new JScrollPane(noteTextArea), BorderLayout.CENTER);
1✔
126
                topPanel.add(saveButton, BorderLayout.SOUTH);
1✔
127

128
                JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
1✔
129
                buttonPanel.add(editButton);
1✔
130
                buttonPanel.add(deleteButton);
1✔
131

132
                JPanel centerPanel = new JPanel(new BorderLayout());
1✔
133
                centerPanel.add(new JScrollPane(notesList), BorderLayout.CENTER);
1✔
134
                centerPanel.add(buttonPanel, BorderLayout.SOUTH);
1✔
135

136
                add(topPanel, BorderLayout.NORTH);
1✔
137
                add(centerPanel, BorderLayout.CENTER);
1✔
138
                add(errorLabel, BorderLayout.SOUTH);
1✔
139
        }
1✔
140

141
        public void setNoteController(NoteController noteController) {
142
                this.noteController = noteController;
1✔
143
        }
1✔
144

145
        @Override
146
        public void showAllCategories(List<Category> categories) {
147
            categoryComboBoxModel.removeAllElements();
1✔
148
            categoryIdToName.clear();
1✔
149
            for (Category category : categories) {
1✔
150
                categoryComboBoxModel.addElement(new CategoryItem(category));
1✔
151
                categoryIdToName.put(category.getId(), category.getName());
1✔
152
            }
1✔
153
        }
1✔
154

155
        @Override
156
        public void showAllNotes(List<Note> notes) {
157
                notesListModel.clear();
1✔
158
                for (Note note : notes) {
1✔
159
                        notesListModel.addElement(note);
1✔
160
                }
1✔
161
        }
1✔
162

163
        @Override
164
        public void noteAdded(Note note) {
165
                notesListModel.addElement(note);
1✔
166
                noteTextArea.setText("");
1✔
167
        }
1✔
168

169
        @Override
170
        public void noteUpdated(Note note) {
171
                for (int i = 0; i < notesListModel.size(); i++) {
1✔
172
                        if (notesListModel.get(i).getId().equals(note.getId())) {
1✔
173
                                notesListModel.set(i, note);
1✔
174
                                break;
1✔
175
                        }
176
                }
177
                editMode = false;
1✔
178
                editingNoteId = null;
1✔
179
                saveButton.setText("Save");
1✔
180
                noteTextArea.setText("");
1✔
181
        }
1✔
182

183
        @Override
184
        public void noteDeleted(Note note) {
185
                for (int i = 0; i < notesListModel.size(); i++) {
1✔
186
                        if (notesListModel.get(i).getId().equals(note.getId())) {
1✔
187
                                notesListModel.remove(i);
1✔
188
                                break;
1✔
189
                        }
190
                }
191
        }
1✔
192

193
        @Override
194
        public void showError(String message) {
195
                errorLabel.setText(message);
×
196
        }
×
197

198
        private static class CategoryItem {
199
                private final Category category;
200

201
                public CategoryItem(Category category) {
1✔
202
                        this.category = category;
1✔
203
                }
1✔
204

205
                public String getId() {
206
                        return category.getId();
1✔
207
                }
208

209
                @Override
210
                public String toString() {
211
                        return category.getName();
1✔
212
                }
213
        }
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc