Skip to content

Commit 1b8d339

Browse files
committed
feat: #main groups import and export
1 parent 96d1f40 commit 1b8d339

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

src/main/java/pl/bator/lso_list_generator/repository/GroupJSONRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class GroupJSONRepository {
1919
@Setter
2020
private List<Group> groups;
2121
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
22-
private final Path dbFilePath = Path.of(System.getProperty("user.home"), "lso-groups.json");
22+
public static final Path dbFilePath = Path.of(System.getProperty("user.home"), "lso-groups.json");
2323

2424
public GroupJSONRepository() throws IOException {
2525
groups = readGroups();

src/main/java/pl/bator/lso_list_generator/service/GroupsService.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package pl.bator.lso_list_generator.service;
22

3-
import lombok.RequiredArgsConstructor;
43
import org.jetbrains.annotations.NotNull;
54
import pl.bator.lso_list_generator.model.Group;
65
import pl.bator.lso_list_generator.model.Person;
76
import pl.bator.lso_list_generator.model.SundayMass;
87
import pl.bator.lso_list_generator.repository.GroupJSONRepository;
8+
import pl.bator.lso_list_generator.util.JSONUtil;
99

1010
import javax.swing.*;
1111
import javax.swing.border.EmptyBorder;
@@ -14,14 +14,22 @@
1414
import java.awt.*;
1515
import java.awt.event.ActionEvent;
1616
import java.io.IOException;
17+
import java.nio.file.Path;
1718
import java.time.DayOfWeek;
1819
import java.util.Comparator;
1920

20-
@RequiredArgsConstructor
21+
import static pl.bator.lso_list_generator.view.NavbarView.pdfSavePath;
22+
2123
public class GroupsService {
2224
private final String[] dayNames = {"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};
2325
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+
}
2533

2634
public void handleAddNewGroup(JPanel groupTilesPanel) {
2735
SundayMass sund = SundayMass.R;
@@ -133,6 +141,35 @@ public void changedUpdate(DocumentEvent e) {
133141
return tilePanel;
134142
}
135143

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+
136173
//
137174

138175
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
193230
refreshGroups(groupTilesPanel);
194231
}
195232

196-
private void refreshGroups(@NotNull JPanel container) {
233+
private void refreshGroups(@NotNull JPanel container) throws IOException {
197234
container.removeAll();
235+
groupJSONRepository = new GroupJSONRepository();
198236
var groups = groupJSONRepository.getGroups().stream().sorted(Comparator.comparingInt(Group::getNumber)).toList();
199237
for (Group group : groups) {
200238
JPanel tilePanel = createGroupTile(group, container);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pl.bator.lso_list_generator.util;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.StandardCopyOption;
7+
8+
public class JSONUtil {
9+
public void copyFile(Path sourceFilePath, Path targetFilePath) throws IOException {
10+
Files.copy(sourceFilePath, targetFilePath, StandardCopyOption.REPLACE_EXISTING);
11+
}
12+
}

src/main/java/pl/bator/lso_list_generator/view/ApplicationView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ private void initWindow() throws IOException {
3939
JScrollPane scrollPane = new JScrollPane(groupsPanel);
4040

4141
navbarView.initView(monthAndYearSelectorPanel, pathSelectorPanel, buttonPanel);
42-
groupsView.initView(groupsPanel, scrollPane, buttonPanel);
42+
groupsView.initView(this, groupsPanel, scrollPane, buttonPanel);
4343

4444
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
4545
mainPanel.add(monthAndYearSelectorPanel);

src/main/java/pl/bator/lso_list_generator/view/GroupsView.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public GroupsView(GroupJSONRepository groupJSONRepository) {
1818
this.groupJSONRepository = groupJSONRepository;
1919
}
2020

21-
public void initView(@NotNull JPanel groupPanel, @NotNull JScrollPane scrollPane, @NotNull JPanel buttonPanel) {
21+
public void initView(Component parent, @NotNull JPanel groupPanel, @NotNull JScrollPane scrollPane, @NotNull JPanel buttonPanel) {
2222
groupPanel.setLayout(new BoxLayout(groupPanel, BoxLayout.Y_AXIS));
2323
groupPanel.setBorder(BorderFactory.createTitledBorder("Grupy"));
2424
groupPanel.setMinimumSize(new Dimension(600, 400));
@@ -36,5 +36,7 @@ public void initView(@NotNull JPanel groupPanel, @NotNull JScrollPane scrollPane
3636
JPanel tilePanel = groupsService.createGroupTile(group, groupPanel);
3737
groupPanel.add(tilePanel);
3838
}
39+
40+
groupsService.initButtons(parent, buttonPanel, groupPanel);
3941
}
4042
}

src/main/java/pl/bator/lso_list_generator/view/NavbarView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class NavbarView {
1818
private JComboBox<String> monthComboBox;
1919
private JComboBox<Integer> yearComboBox;
2020
private JLabel pathLabel;
21-
private Path pdfSavePath = Path.of(System.getProperty("user.home"), "Desktop");
21+
public static Path pdfSavePath = Path.of(System.getProperty("user.home"), "Desktop");
2222

2323
private final PDFGenerationService pdfGenerationService;
2424
private final Component parent;

0 commit comments

Comments
 (0)