|
1 | 1 | package pl.bator.lso_list_generator.service;
|
2 | 2 |
|
3 |
| -import lombok.RequiredArgsConstructor; |
4 | 3 | import org.jetbrains.annotations.NotNull;
|
5 | 4 | import pl.bator.lso_list_generator.model.Group;
|
6 | 5 | import pl.bator.lso_list_generator.model.Person;
|
7 | 6 | import pl.bator.lso_list_generator.model.SundayMass;
|
8 | 7 | import pl.bator.lso_list_generator.repository.GroupJSONRepository;
|
| 8 | +import pl.bator.lso_list_generator.util.JSONUtil; |
9 | 9 |
|
10 | 10 | import javax.swing.*;
|
11 | 11 | import javax.swing.border.EmptyBorder;
|
|
14 | 14 | import java.awt.*;
|
15 | 15 | import java.awt.event.ActionEvent;
|
16 | 16 | import java.io.IOException;
|
| 17 | +import java.nio.file.Path; |
17 | 18 | import java.time.DayOfWeek;
|
18 | 19 | import java.util.Comparator;
|
19 | 20 |
|
20 |
| -@RequiredArgsConstructor |
| 21 | +import static pl.bator.lso_list_generator.view.NavbarView.pdfSavePath; |
| 22 | + |
21 | 23 | public class GroupsService {
|
22 | 24 | private final String[] dayNames = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
|
23 | 25 | private final String[] polishDayNames = {"poniedziałek", "wtorek", "środa", "czwartek", "piątek", "sobota"};
|
24 |
| - private final GroupJSONRepository groupJSONRepository; |
| 26 | + |
| 27 | + private GroupJSONRepository groupJSONRepository; |
| 28 | + private final JSONUtil jsonUtil = new JSONUtil(); |
| 29 | + |
| 30 | + public GroupsService(GroupJSONRepository groupJSONRepository) { |
| 31 | + this.groupJSONRepository = groupJSONRepository; |
| 32 | + } |
25 | 33 |
|
26 | 34 | public void handleAddNewGroup(JPanel groupTilesPanel) {
|
27 | 35 | SundayMass sund = SundayMass.R;
|
@@ -133,6 +141,35 @@ public void changedUpdate(DocumentEvent e) {
|
133 | 141 | return tilePanel;
|
134 | 142 | }
|
135 | 143 |
|
| 144 | + public void initButtons(Component parent, @NotNull JPanel buttonPanel, @NotNull JPanel groupTilesPanel) { |
| 145 | + var exportJsonButton = new JButton("Eksportuj listę"); |
| 146 | + exportJsonButton.addActionListener(e -> { |
| 147 | + try { |
| 148 | + jsonUtil.copyFile(GroupJSONRepository.dbFilePath, pdfSavePath.resolve("lso-groups.json")); |
| 149 | + } catch (IOException ex) { |
| 150 | + throw new RuntimeException(ex); |
| 151 | + } |
| 152 | + }); |
| 153 | + buttonPanel.add(exportJsonButton); |
| 154 | + |
| 155 | + var importJsonButton = new JButton("Importuj listę"); |
| 156 | + importJsonButton.addActionListener(e -> { |
| 157 | + try { |
| 158 | + JFileChooser fileChooser = new JFileChooser(pdfSavePath.toString()); |
| 159 | + fileChooser.setDialogTitle("Wybierz plik JSON"); |
| 160 | + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
| 161 | + int userSelection = fileChooser.showSaveDialog(parent); |
| 162 | + if (userSelection == JFileChooser.APPROVE_OPTION) { |
| 163 | + jsonUtil.copyFile(Path.of(fileChooser.getSelectedFile().getAbsolutePath()), GroupJSONRepository.dbFilePath); |
| 164 | + refreshGroups(groupTilesPanel); |
| 165 | + } |
| 166 | + } catch (IOException ex) { |
| 167 | + throw new RuntimeException(ex); |
| 168 | + } |
| 169 | + }); |
| 170 | + buttonPanel.add(importJsonButton); |
| 171 | + } |
| 172 | + |
136 | 173 | //
|
137 | 174 |
|
138 | 175 | private void handleSaveGroupClick(@NotNull String text, @NotNull Group group, JPanel container, JCheckBox[] dayCheckBoxes, JPanel groupTilePanel) throws IOException {
|
@@ -193,8 +230,9 @@ private void handleDeleteGroupClick(@NotNull JPanel groupTilesPanel, @NotNull Gr
|
193 | 230 | refreshGroups(groupTilesPanel);
|
194 | 231 | }
|
195 | 232 |
|
196 |
| - private void refreshGroups(@NotNull JPanel container) { |
| 233 | + private void refreshGroups(@NotNull JPanel container) throws IOException { |
197 | 234 | container.removeAll();
|
| 235 | + groupJSONRepository = new GroupJSONRepository(); |
198 | 236 | var groups = groupJSONRepository.getGroups().stream().sorted(Comparator.comparingInt(Group::getNumber)).toList();
|
199 | 237 | for (Group group : groups) {
|
200 | 238 | JPanel tilePanel = createGroupTile(group, container);
|
|
0 commit comments