Skip to content

Commit 4f13eef

Browse files
committed
refactor: #main controllers remaster
1 parent ebd8241 commit 4f13eef

File tree

6 files changed

+197
-167
lines changed

6 files changed

+197
-167
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
<version>2.0.0-alpha0</version>
4242
<scope>runtime</scope>
4343
</dependency>
44+
<dependency>
45+
<groupId>org.jetbrains</groupId>
46+
<artifactId>annotations</artifactId>
47+
<version>24.0.1</version>
48+
<scope>compile</scope>
49+
</dependency>
4450
</dependencies>
4551

4652
<build>
Lines changed: 18 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
11
package pl.bator.lso_list_generator.controller;
22

3-
import pl.bator.lso_list_generator.model.Group;
43
import pl.bator.lso_list_generator.repository.GroupJSONRepository;
5-
import pl.bator.lso_list_generator.service.ApplicationService;
6-
import pl.bator.lso_list_generator.service.PDFService;
74

85
import javax.swing.*;
96
import java.awt.*;
107
import java.io.IOException;
11-
import java.text.DateFormatSymbols;
12-
import java.time.Month;
13-
import java.time.Year;
14-
import java.util.Arrays;
15-
import java.util.Comparator;
16-
import java.util.Locale;
178
import java.util.Objects;
189

1910
public class ApplicationController extends JFrame {
20-
private JComboBox<String> monthComboBox;
21-
private JComboBox<Integer> yearComboBox;
22-
private JScrollPane scrollPane;
23-
private JButton generatePdfButton;
24-
private JButton addGroupButton;
25-
private JPanel groupTilesPanel;
26-
private JLabel pathLabel;
27-
private String defaultSavePath = System.getProperty("user.home") + "\\Desktop";
11+
private final NavbarController navbarController;
12+
private final GroupsPanelController groupsPanelController;
2813
private GroupJSONRepository groupJSONRepository;
29-
private ApplicationService applicationService;
3014

3115
public ApplicationController() {
3216
try {
33-
this.groupJSONRepository = new GroupJSONRepository();
34-
this.applicationService = new ApplicationService(groupJSONRepository);
35-
initWindow();
17+
groupJSONRepository = new GroupJSONRepository();
3618
} catch (IOException e) {
37-
showErrorAndExit("Błąd podczas inicjalizacji aplikacji: " + e.getMessage());
19+
showErrorAndExit("Nie można otworzyć pliku JSON.");
3820
}
39-
}
40-
41-
private void showErrorAndExit(String message) {
42-
javax.swing.JOptionPane.showMessageDialog(null, message, "Błąd krytyczny", javax.swing.JOptionPane.ERROR_MESSAGE);
43-
System.exit(1);
21+
navbarController = new NavbarController(groupJSONRepository, this);
22+
groupsPanelController = new GroupsPanelController(groupJSONRepository);
23+
initWindow();
4424
}
4525

4626
private void initWindow() {
@@ -52,138 +32,26 @@ private void initWindow() {
5232
setResizable(false);
5333

5434
JPanel mainPanel = new JPanel(new BorderLayout());
55-
JPanel monthAndYearSelector = new JPanel(new FlowLayout());
56-
JPanel pathSelector = new JPanel(new FlowLayout());
35+
JPanel monthAndYearSelectorPanel = new JPanel(new FlowLayout());
36+
JPanel pathSelectorPanel = new JPanel(new FlowLayout());
5737
JPanel buttonPanel = new JPanel(new FlowLayout());
38+
JPanel groupsPanel = new JPanel(new FlowLayout());
39+
JScrollPane scrollPane = new JScrollPane(groupsPanel);
5840

59-
initializeGroupTilesPanel();
60-
initializeMonthComboBox(monthAndYearSelector);
61-
initializeYearComboBox(monthAndYearSelector);
62-
initializeButtons();
63-
64-
displayCurrentSavePath(pathSelector);
65-
addChangePathButton(pathSelector);
66-
67-
buttonPanel.add(addGroupButton);
68-
buttonPanel.add(generatePdfButton);
41+
navbarController.initView(monthAndYearSelectorPanel, pathSelectorPanel, buttonPanel);
42+
groupsPanelController.initView(groupsPanel, scrollPane, buttonPanel);
6943

7044
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
71-
mainPanel.add(monthAndYearSelector);
72-
mainPanel.add(pathSelector);
45+
mainPanel.add(monthAndYearSelectorPanel);
46+
mainPanel.add(pathSelectorPanel);
7347
mainPanel.add(buttonPanel);
74-
7548
mainPanel.add(scrollPane);
7649

7750
add(mainPanel);
7851
}
7952

80-
// events
81-
82-
public void handleChangePathClick() {
83-
JFileChooser fileChooser = new JFileChooser(defaultSavePath);
84-
fileChooser.setDialogTitle("Wybierz folder zapisu");
85-
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
86-
int userSelection = fileChooser.showSaveDialog(this);
87-
88-
if (userSelection == JFileChooser.APPROVE_OPTION) {
89-
defaultSavePath = fileChooser.getSelectedFile().getAbsolutePath();
90-
pathLabel.setText("Aktualna ścieżka: " + defaultSavePath);
91-
}
92-
}
93-
94-
95-
public void handleGenerateClick() {
96-
try {
97-
String selectedMonth = (String) monthComboBox.getSelectedItem();
98-
Integer selectedYear = (Integer) yearComboBox.getSelectedItem();
99-
100-
int monthIndex = -1;
101-
String[] polishMonths = new DateFormatSymbols(new Locale("pl")).getMonths();
102-
for (int i = 0; i < polishMonths.length; i++) {
103-
assert selectedMonth != null;
104-
if (selectedMonth.equals(polishMonths[i])) {
105-
monthIndex = i;
106-
break;
107-
}
108-
}
109-
110-
if (monthIndex == -1) {
111-
JOptionPane.showMessageDialog(this, "Błąd: Nie można odnaleźć wybranego miesiąca.", "Błąd", JOptionPane.ERROR_MESSAGE);
112-
return;
113-
}
114-
115-
Month month = Month.of(monthIndex + 1);
116-
117-
PDFService pdfService = new PDFService();
118-
for (int i = 0; i < groupJSONRepository.getGroups().size(); i++) {
119-
pdfService.generatePdf(groupJSONRepository.getGroups().get(i), month, Year.of(selectedYear), defaultSavePath);
120-
}
121-
JOptionPane.showMessageDialog(this, "Wygenerowano pomyślnie!");
122-
} catch (IOException e) {
123-
JOptionPane.showMessageDialog(this, "Błąd generowania: " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
124-
}
125-
}
126-
127-
128-
//components
129-
130-
131-
private void initializeGroupTilesPanel() {
132-
groupTilesPanel = new JPanel();
133-
groupTilesPanel.setLayout(new BoxLayout(groupTilesPanel, BoxLayout.Y_AXIS));
134-
groupTilesPanel.setBorder(BorderFactory.createTitledBorder("Grupy"));
135-
groupTilesPanel.setMinimumSize(new Dimension(600, 400));
136-
137-
scrollPane = new JScrollPane(groupTilesPanel);
138-
scrollPane.setPreferredSize(new Dimension(600, 400));
139-
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
140-
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
141-
142-
var groups = groupJSONRepository.getGroups().stream().sorted(Comparator.comparingInt(Group::getNumber)).toList();
143-
for (Group group : groups) {
144-
JPanel tilePanel = applicationService.createGroupTile(group);
145-
groupTilesPanel.add(tilePanel);
146-
}
147-
148-
applicationService.setGroupTilesPanel(groupTilesPanel);
149-
}
150-
151-
private void initializeMonthComboBox(JPanel selectionPanel1) {
152-
int currentMonth = java.time.LocalDate.now().getMonthValue();
153-
String[] polishMonths = new DateFormatSymbols(new Locale("pl")).getMonths();
154-
monthComboBox = new JComboBox<>(Arrays.copyOf(polishMonths, polishMonths.length - 1));
155-
monthComboBox.setSelectedItem(polishMonths[currentMonth - 1]);
156-
selectionPanel1.add(new JLabel("Generuj dla miesiąca: "));
157-
selectionPanel1.add(monthComboBox);
158-
}
159-
160-
private void initializeYearComboBox(JPanel selectionPanel1) {
161-
int currentYear = Year.now().getValue();
162-
Integer[] years = new Integer[10];
163-
for (int i = 0; i < years.length; i++) {
164-
years[i] = currentYear - 5 + i;
165-
}
166-
yearComboBox = new JComboBox<>(years);
167-
yearComboBox.setSelectedItem(currentYear);
168-
selectionPanel1.add(new JLabel("roku: "));
169-
selectionPanel1.add(yearComboBox);
170-
}
171-
172-
private void displayCurrentSavePath(JPanel selectionPanel2) {
173-
pathLabel = new JLabel("Aktualna ścieżka: " + defaultSavePath);
174-
selectionPanel2.add(pathLabel);
175-
}
176-
177-
private void addChangePathButton(JPanel selectionPanel2) {
178-
JButton changePathButton = new JButton("Zmień ścieżkę");
179-
changePathButton.addActionListener(e -> handleChangePathClick());
180-
selectionPanel2.add(changePathButton);
181-
}
182-
183-
private void initializeButtons() {
184-
generatePdfButton = new JButton("Generuj");
185-
generatePdfButton.addActionListener(e -> handleGenerateClick());
186-
addGroupButton = new JButton("Dodaj nową grupę");
187-
addGroupButton.addActionListener(e -> applicationService.handleAddNewGroup(groupTilesPanel));
53+
public void showErrorAndExit(String message) {
54+
JOptionPane.showMessageDialog(null, message, "Błąd krytyczny", javax.swing.JOptionPane.ERROR_MESSAGE);
55+
System.exit(1);
18856
}
18957
}

src/main/java/pl/bator/lso_list_generator/service/ApplicationService.java renamed to src/main/java/pl/bator/lso_list_generator/controller/GroupsPanelController.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package pl.bator.lso_list_generator.service;
1+
package pl.bator.lso_list_generator.controller;
22

3-
import lombok.Setter;
3+
import lombok.RequiredArgsConstructor;
44
import pl.bator.lso_list_generator.model.Group;
55
import pl.bator.lso_list_generator.model.Person;
66
import pl.bator.lso_list_generator.model.SundayMass;
@@ -14,19 +14,36 @@
1414
import java.awt.event.ActionEvent;
1515
import java.io.IOException;
1616
import java.time.DayOfWeek;
17+
import java.util.Comparator;
1718

18-
public class ApplicationService {
19+
@RequiredArgsConstructor
20+
public class GroupsPanelController {
1921
private final String[] dayNames = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
2022
private final String[] polishDayNames = {"poniedziałek", "wtorek", "środa", "czwartek", "piątek", "sobota"};
23+
2124
private final GroupJSONRepository groupJSONRepository;
22-
@Setter
23-
private JPanel groupTilesPanel;
2425

25-
public ApplicationService(GroupJSONRepository groupJSONRepository) {
26-
this.groupJSONRepository = groupJSONRepository;
26+
public void initView(JPanel groupPanel, JScrollPane scrollPane, JPanel buttonPanel) {
27+
groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS));
28+
groupPanel.setBorder(BorderFactory.createTitledBorder("Grupy"));
29+
groupPanel.setMinimumSize(new Dimension(600, 400));
30+
31+
scrollPane.setPreferredSize(new Dimension(600, 400));
32+
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
33+
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
34+
35+
var addGroupButton = new JButton("Dodaj nową grupę");
36+
addGroupButton.addActionListener(e -> handleAddNewGroup(groupPanel));
37+
buttonPanel.add(addGroupButton);
38+
39+
var groups = groupJSONRepository.getGroups().stream().sorted(Comparator.comparingInt(Group::getNumber)).toList();
40+
for (Group group : groups) {
41+
JPanel tilePanel = createGroupTile(group, groupPanel);
42+
groupPanel.add(tilePanel);
43+
}
2744
}
2845

29-
public JPanel createGroupTile(Group group) {
46+
public JPanel createGroupTile(Group group, JPanel groupTilesPanel) {
3047
JPanel tilePanel = new JPanel();
3148
tilePanel.setLayout(new BoxLayout(tilePanel, BoxLayout.Y_AXIS));
3249
tilePanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 265));
@@ -88,7 +105,7 @@ public void changedUpdate(DocumentEvent e) {
88105
});
89106

90107
confirmButton.addActionListener(e -> {
91-
handleSaveGroupClick(acolytesTextArea.getText(), group, tilePanel, dayCheckBoxes);
108+
handleSaveGroupClick(acolytesTextArea.getText(), group, tilePanel, dayCheckBoxes, groupTilesPanel);
92109
confirmButton.setVisible(false);
93110
});
94111

@@ -102,7 +119,7 @@ public void changedUpdate(DocumentEvent e) {
102119
deleteButton.addActionListener(e -> handleDeleteGroupClick(groupTilesPanel, group, tilePanel));
103120

104121
for (JCheckBox checkBox : dayCheckBoxes) {
105-
checkBox.addActionListener(e -> ApplicationService.handleCheckBoxCLick(e, dayCheckBoxes, confirmButton));
122+
checkBox.addActionListener(e -> GroupsPanelController.handleCheckBoxCLick(e, dayCheckBoxes, confirmButton));
106123
}
107124

108125
return tilePanel;
@@ -116,7 +133,7 @@ public void handleAddNewGroup(JPanel groupTilesPanel) {
116133
Group newGroup = new Group(groupJSONRepository.getGroups().size() + 1, "Poniedziałek", "Czwartek", DayOfWeek.MONDAY, DayOfWeek.THURSDAY, sund);
117134
try {
118135
groupJSONRepository.addGroup(newGroup);
119-
JPanel newTilePanel = createGroupTile(newGroup);
136+
JPanel newTilePanel = createGroupTile(newGroup, groupTilesPanel);
120137
groupTilesPanel.add(newTilePanel);
121138
groupTilesPanel.revalidate();
122139
groupTilesPanel.repaint();
@@ -138,7 +155,7 @@ private void handleDeleteGroupClick(JPanel groupTilesPanel, Group newGroup, JPan
138155
}
139156
}
140157

141-
private void refreshGroupContainer(Group group, JPanel container, Group oldGroup) {
158+
private void refreshGroupContainer(Group group, JPanel container, Group oldGroup, JPanel groupTilePanel) {
142159
try {
143160
groupJSONRepository.removeGroup(oldGroup.getNumber());
144161
groupJSONRepository.addGroup(group);
@@ -149,7 +166,7 @@ private void refreshGroupContainer(Group group, JPanel container, Group oldGroup
149166
Group panelGroup = (Group) panel.getClientProperty("group");
150167
if (panelGroup != null && panelGroup.equals(group)) {
151168
container.remove(panel);
152-
JPanel updatedPanel = createGroupTile(group);
169+
JPanel updatedPanel = createGroupTile(group, groupTilePanel);
153170
container.add(updatedPanel);
154171
container.revalidate();
155172
container.repaint();
@@ -176,7 +193,7 @@ private static void handleCheckBoxCLick(ActionEvent e, JCheckBox[] dayCheckBoxes
176193
}
177194
}
178195

179-
private void handleSaveGroupClick(String text, Group group, JPanel container, JCheckBox[] dayCheckBoxes) {
196+
private void handleSaveGroupClick(String text, Group group, JPanel container, JCheckBox[] dayCheckBoxes, JPanel groupTilePanel) {
180197
var oldGroup = new Group(group.getNumber(), group.getDay1Name(), group.getDay2Name(), group.getDay1(), group.getDay2(), group.getSunday());
181198

182199
String[] acolytesNames = text.split("\n");
@@ -203,6 +220,6 @@ private void handleSaveGroupClick(String text, Group group, JPanel container, JC
203220
}
204221
}
205222

206-
refreshGroupContainer(group, container, oldGroup);
223+
refreshGroupContainer(group, container, oldGroup, groupTilePanel);
207224
}
208225
}

0 commit comments

Comments
 (0)