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,62 +118,91 @@ public class CSVEditor extends JFrame {
fileChooser.showOpenDialog(null); fileChooser.showOpenDialog(null);
if (fileChooser.getSelectedFile() != null) { if (fileChooser.getSelectedFile() != null) {
List<CSVRecord> records = CSVFetcher.INSTANCE.readFromFile( if (fileChooser.getSelectedFile().exists()) {
fileChooser.getSelectedFile().getAbsolutePath()); List<CSVRecord> records = CSVFetcher.INSTANCE.readFromFile(
fileChooser.getSelectedFile().getAbsolutePath());
// turn list of records into a spreadsheet // turn list of records into a spreadsheet
// first dispose of any existing data // first dispose of any existing data
((DefaultTableModel) spreadsheet.getModel()).setRowCount(0); ((DefaultTableModel) spreadsheet.getModel()).setRowCount(0);
// then work on the file // then work on the file
for (CSVRecord record : records) { for (CSVRecord record : records) {
Vector newRow = new Vector(columns.length); Vector newRow = new Vector(columns.length);
// construct newRow // construct newRow
for (String column : columns) { for (String column : columns) {
String value = record.get(column); String value = record.get(column);
if (value == null) { if (value == null) {
value = csvFormat.getNullString(); value = csvFormat.getNullString();
}
newRow.add(spreadsheet.getColumnModel().getColumnIndex(column), value);
} }
newRow.add(spreadsheet.getColumnModel().getColumnIndex(column), value); ((DefaultTableModel) spreadsheet.getModel()).addRow(newRow);
} }
((DefaultTableModel) spreadsheet.getModel()).addRow(newRow); // then add the comments
// since the Commons CSV simply ignores the comments, we have to read them on our own.
try {
StringBuilder sb = new StringBuilder();
List<String> allTheLines = Files.readAllLines(
fileChooser.getSelectedFile().toPath());
allTheLines.forEach(line -> {
if (line.startsWith("" + csvFormat.getCommentMarker().toString())) {
sb.append(line);
sb.append('\n');
}
});
comment.setText(sb.toString());
statBar.setText(lang.getProperty("STAT_LOAD_SUCCESSFUL"));
}
catch (Throwable fuck) {
displayError("ERROR_INTERNAL", fuck);
}
} }
// if file not found
// then add the comments else {
// since the Commons CSV simply ignores the comments, we have to read them on our own. displayMessage("NO_SUCH_FILE");
try {
StringBuilder sb = new StringBuilder();
List<String> allTheLines = Files.readAllLines(fileChooser.getSelectedFile().toPath());
allTheLines.forEach(line -> {
if (line.startsWith("" + csvFormat.getCommentMarker().toString())) {
sb.append(line);
sb.append('\n');
}
});
comment.setText(sb.toString());
} }
catch (Throwable fuck) { } // if opening cancelled, do nothing
throw new InternalError(fuck); } // if discard cancelled, do nothing
}
}
// if opening 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 {