write to the actual file

This commit is contained in:
minjaesong
2019-01-03 13:02:54 +09:00
parent fcc0403c93
commit a8cb95ed74

View File

@@ -11,6 +11,8 @@ import javax.swing.table.DefaultTableModel;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.List; import java.util.List;
@@ -46,7 +48,7 @@ public class CSVEditor extends JFrame {
private JTable spreadsheet = new JTable(new DefaultTableModel(columns, INITIAL_ROWS)); // it MUST be DefaultTableModel because that's what I'm using private JTable spreadsheet = new JTable(new DefaultTableModel(columns, INITIAL_ROWS)); // it MUST be DefaultTableModel because that's what I'm using
private JTextPane caption = new JTextPane(); private JTextPane caption = new JTextPane();
private JTextPane comment = new JTextPane(); private JTextPane comment = new JTextPane();
private JLabel statBar = new JLabel("Creating a new CSV. You can still open existing file."); private JLabel statBar = new JLabel("null.");
private Properties props = new Properties(); private Properties props = new Properties();
private Properties lang = new Properties(); private Properties lang = new Properties();
@@ -116,6 +118,7 @@ public class CSVEditor extends JFrame {
fileChooser.showOpenDialog(null); fileChooser.showOpenDialog(null);
if (fileChooser.getSelectedFile() != null) { if (fileChooser.getSelectedFile() != null) {
if (fileChooser.getSelectedFile().exists()) {
List<CSVRecord> records = CSVFetcher.INSTANCE.readFromFile( List<CSVRecord> records = CSVFetcher.INSTANCE.readFromFile(
fileChooser.getSelectedFile().getAbsolutePath()); fileChooser.getSelectedFile().getAbsolutePath());
@@ -145,7 +148,8 @@ public class CSVEditor extends JFrame {
// since the Commons CSV simply ignores the comments, we have to read them on our own. // since the Commons CSV simply ignores the comments, we have to read them on our own.
try { try {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
List<String> allTheLines = Files.readAllLines(fileChooser.getSelectedFile().toPath()); List<String> allTheLines = Files.readAllLines(
fileChooser.getSelectedFile().toPath());
allTheLines.forEach(line -> { allTheLines.forEach(line -> {
if (line.startsWith("" + csvFormat.getCommentMarker().toString())) { if (line.startsWith("" + csvFormat.getCommentMarker().toString())) {
@@ -155,23 +159,50 @@ public class CSVEditor extends JFrame {
}); });
comment.setText(sb.toString()); comment.setText(sb.toString());
statBar.setText(lang.getProperty("STAT_LOAD_SUCCESSFUL"));
} }
catch (Throwable fuck) { catch (Throwable fuck) {
throw new InternalError(fuck); displayError("ERROR_INTERNAL", fuck);
} }
} }
// if file not found
// if opening cancelled, do nothing else {
displayMessage("NO_SUCH_FILE");
} }
} // if opening cancelled, do nothing
// if discard cancelled, do nothing } // if discard cancelled, do nothing
} }
}); });
add("Save…").addMouseListener(new MouseAdapter() { add("Save…").addMouseListener(new MouseAdapter() {
@Override @Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
System.out.println(toCSV()); JFileChooser fileChooser = new JFileChooser() {
{
setFileSelectionMode(JFileChooser.FILES_ONLY);
setMultiSelectionEnabled(false);
}
};
fileChooser.showSaveDialog(null);
if (fileChooser.getSelectedFile() != null) {
try {
FileOutputStream fos = new FileOutputStream(fileChooser.getSelectedFile());
fos.write(toCSV().getBytes());
fos.flush();
fos.close();
statBar.setText(lang.getProperty("STAT_SAVE_SUCCESSFUL"));
}
catch (IOException iofuck) {
displayError("WRITE_FAIL", iofuck);
}
} // if saving cancelled, do nothing
} }
}); });
@@ -188,6 +219,9 @@ public class CSVEditor extends JFrame {
// then add some columns // then add some columns
((DefaultTableModel) spreadsheet.getModel()).setRowCount(rows); ((DefaultTableModel) spreadsheet.getModel()).setRowCount(rows);
// notify the user as well
statBar.setText(lang.getProperty("STAT_NEW_FILE"));
} }
} }
} }
@@ -237,6 +271,8 @@ public class CSVEditor extends JFrame {
} }
}); });
statBar.setText(lang.getProperty("STAT_INIT"));
} }
public static void main(String[] args) { public static void main(String[] args) {
@@ -291,7 +327,18 @@ public class CSVEditor extends JFrame {
private boolean discardAgreed() { private boolean discardAgreed() {
return 0 == JOptionPane.showOptionDialog(null, return 0 == JOptionPane.showOptionDialog(null,
lang.getProperty("WARNING_YOUR_DATA_WILL_GONE"), lang.getProperty("WARNING_YOUR_DATA_WILL_GONE") + " " + lang.getProperty("WARNING_CONTINUE"),
null,
JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
new String[]{"OK", "Cancel"},
"Cancel"
);
}
private boolean confirmedContinue(String messageKey) {
return 0 == JOptionPane.showOptionDialog(null,
lang.getProperty(messageKey) + " " + lang.getProperty("WARNING_CONTINUE"),
null, null,
JOptionPane.DEFAULT_OPTION, JOptionPane.DEFAULT_OPTION,
JOptionPane.WARNING_MESSAGE, JOptionPane.WARNING_MESSAGE,
@@ -311,6 +358,17 @@ public class CSVEditor extends JFrame {
"Cancel" "Cancel"
); );
} }
private void displayError(String messageKey, Throwable cause) {
JOptionPane.showOptionDialog(null,
lang.getProperty(messageKey) + "\n" + cause.toString(),
null,
JOptionPane.DEFAULT_OPTION,
JOptionPane.ERROR_MESSAGE,
null,
new String[]{"OK", "Cancel"},
"Cancel"
);
}
/** /**
* *
@@ -358,10 +416,18 @@ public class CSVEditor extends JFrame {
*/ */
private String translations = private String translations =
"" + "" +
"WARNING_YOUR_DATA_WILL_GONE=Existing edits will be lost, continue?\n" + "WARNING_CONTINUE=Continue?\n" +
"WARNING_YOUR_DATA_WILL_GONE=Existing edits will be lost.\n" +
"OPERATION_CANCELLED=Operation cancelled.\n" + "OPERATION_CANCELLED=Operation cancelled.\n" +
"NEW_ROWS=Enter the number of rows to initialise the new CSV.¤Remember, you can always add or delete rows.\n" + "NO_SUCH_FILE=No such file exists, operation cancelled.\n" +
"ADD_ROWS=Enter the number of rows to add:\n"; "NEW_ROWS=Enter the number of rows to initialise the new CSV.¤Remember, you can always add or delete rows later.\n" +
"ADD_ROWS=Enter the number of rows to add:\n" +
"WRITE_FAIL=Writing to file has failed:\n" +
"STAT_INIT=Creating a new CSV. You can still open existing file.\n" +
"STAT_SAVE_SUCCESSFUL=File saved successfully.\n" +
"STAT_NEW_FILE=New CSV created.\n" +
"STAT_LOAD_SUCCESSFUL=File loaded successfully.\n" +
"ERROR_INTERNAL=Something went wrong.\n";
} }
class OptionSize { class OptionSize {