1
2
3
4
5
6
7
8
9
10
11 package com.jsql.view.swing.list;
12
13 import com.jsql.util.I18nUtil;
14 import com.jsql.util.LogLevelUtil;
15 import com.jsql.view.swing.util.MediatorHelper;
16 import org.apache.logging.log4j.LogManager;
17 import org.apache.logging.log4j.Logger;
18
19 import javax.swing.*;
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.PrintStream;
25 import java.nio.charset.StandardCharsets;
26
27
28
29
30 public class MenuActionExport implements ActionListener {
31
32 private static final Logger LOGGER = LogManager.getRootLogger();
33
34
35
36
37 private final DnDList myList;
38
39
40
41
42
43 public MenuActionExport(DnDList myList) {
44 this.myList = myList;
45 }
46
47 @Override
48 public void actionPerformed(ActionEvent actionEvent) {
49 final JFileChooser importFileDialog = new JFileChooser(MediatorHelper.model().getMediatorUtils().getPreferencesUtil().getPathFile()) {
50 @Override
51 public void approveSelection() {
52 var file = this.getSelectedFile();
53 if (file.exists() && this.getDialogType() == JFileChooser.SAVE_DIALOG) {
54 int replace = JOptionPane.showConfirmDialog(
55 MediatorHelper.frame(),
56 String.format("%s %s", file.getName(), I18nUtil.valueByKey("LIST_EXPORT_CONFIRM_LABEL")),
57 I18nUtil.valueByKey("LIST_EXPORT_CONFIRM_TITLE"),
58 JOptionPane.YES_NO_OPTION
59 );
60
61 switch (replace) {
62 case JOptionPane.YES_OPTION:
63 super.approveSelection();
64 return;
65 case JOptionPane.NO_OPTION:
66 case JOptionPane.CLOSED_OPTION:
67 return;
68 case JOptionPane.CANCEL_OPTION:
69 this.cancelSelection();
70 return;
71 default:
72 break;
73 }
74 } else {
75 super.approveSelection();
76 }
77 }
78 };
79
80 importFileDialog.setDialogTitle(I18nUtil.valueByKey("LIST_EXPORT_TITLE"));
81 int choice = importFileDialog.showSaveDialog(this.myList.getTopLevelAncestor());
82 if (choice != JFileChooser.APPROVE_OPTION) {
83 return;
84 }
85
86 try (
87 var file = new FileOutputStream(importFileDialog.getSelectedFile());
88 var out = new PrintStream(file, false, StandardCharsets.UTF_8)
89 ) {
90 int len = this.myList.getModel().getSize();
91 for (var i = 0 ; i < len ; i++) {
92 out.println(this.myList.getModel().getElementAt(i).toString());
93 }
94 } catch (IOException e) {
95 LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
96 }
97 }
98 }