mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-09 13:21:51 +09:00
csv editor read file with imperfections
This commit is contained in:
@@ -2,20 +2,26 @@ package net.torvald.terrarum.debuggerapp;
|
||||
|
||||
import net.torvald.terrarum.utils.CSVFetcher;
|
||||
import org.apache.commons.csv.CSVFormat;
|
||||
import org.apache.commons.csv.CSVRecord;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.Vector;
|
||||
|
||||
/**
|
||||
* Should be made into its own artifact to build.
|
||||
*
|
||||
* Only recognisable columns are read and saved, thus this app should be update when new properties are added.
|
||||
*
|
||||
* Created by minjaesong on 2019-01-01.
|
||||
*/
|
||||
public class CSVEditor extends JFrame {
|
||||
@@ -27,26 +33,29 @@ public class CSVEditor extends JFrame {
|
||||
private final int TWO_DIGIT = 30;
|
||||
private final int ARBITRARY = 240;
|
||||
private int[] colWidth = new int[]{FOUR_DIGIT, FOUR_DIGIT, ARBITRARY, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, TWO_DIGIT, FOUR_DIGIT, FOUR_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, TWO_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT, SIX_DIGIT};
|
||||
private String[][] dummyData = new String[2][columns.length];
|
||||
|
||||
private CSVFormat csvFormat = CSVFetcher.INSTANCE.getTerrarumCSVFormat();
|
||||
|
||||
private final int INITIAL_ROWS = 2;
|
||||
|
||||
private JPanel panelSpreadSheet = new JPanel();
|
||||
private JPanel panelComment = new JPanel();
|
||||
private JSplitPane panelWorking = new JSplitPane(JSplitPane.VERTICAL_SPLIT, panelSpreadSheet, panelComment);
|
||||
|
||||
private JMenuBar menuBar = new JMenuBar();
|
||||
private JTable spreadsheet = new JTable(dummyData, columns);
|
||||
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 comment = new JTextPane();
|
||||
private JLabel statBar = new JLabel("Creating a new CSV. You can still open existing file.");
|
||||
|
||||
private Properties props = new Properties();
|
||||
private Properties lang = new Properties();
|
||||
|
||||
public CSVEditor() {
|
||||
// setup application properties //
|
||||
try {
|
||||
props.load(new StringReader(captionProperties));
|
||||
lang.load(new StringReader(translations));
|
||||
}
|
||||
catch (Throwable e) {
|
||||
|
||||
@@ -92,7 +101,54 @@ public class CSVEditor extends JFrame {
|
||||
{
|
||||
this.setMnemonic(KeyEvent.VK_F);
|
||||
|
||||
add("Open…");
|
||||
add("Open…").addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mousePressed(MouseEvent e) {
|
||||
// let's show generic warning first
|
||||
if (discardAgreed()) {
|
||||
|
||||
// actually read file
|
||||
JFileChooser fileChooser = new JFileChooser() {
|
||||
{
|
||||
setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||
setMultiSelectionEnabled(false);
|
||||
}
|
||||
};
|
||||
|
||||
fileChooser.showOpenDialog(null);
|
||||
|
||||
if (fileChooser.getSelectedFile() != null) {
|
||||
List<CSVRecord> records = CSVFetcher.INSTANCE.readFromFile(
|
||||
fileChooser.getSelectedFile().getAbsolutePath());
|
||||
|
||||
// turn list of records into a spreadsheet
|
||||
|
||||
// first dispose of any existing data
|
||||
((DefaultTableModel) spreadsheet.getModel()).setRowCount(0);
|
||||
|
||||
// then work on the file
|
||||
for (CSVRecord record : records) {
|
||||
Vector newRow = new Vector(columns.length);
|
||||
|
||||
// construct newRow
|
||||
for (String column : columns) {
|
||||
String value = record.get(column);
|
||||
|
||||
newRow.add(spreadsheet.getColumnModel().getColumnIndex(column), value);
|
||||
}
|
||||
|
||||
((DefaultTableModel) spreadsheet.getModel()).addRow(newRow);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// cancelled opening
|
||||
displayMessage("OPERATION_CANCELLED");
|
||||
}
|
||||
}
|
||||
|
||||
// if discard cancelled, do nothing
|
||||
}
|
||||
});
|
||||
|
||||
add("Save").addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
@@ -113,6 +169,8 @@ public class CSVEditor extends JFrame {
|
||||
}
|
||||
});
|
||||
|
||||
menuBar.setSize(new Dimension(100, 18));
|
||||
|
||||
// setup spreadsheet //
|
||||
|
||||
// no resize
|
||||
@@ -144,6 +202,10 @@ public class CSVEditor extends JFrame {
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// will fix some components not "updating" at init, with some minor consequences...?
|
||||
this.repaint();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -195,10 +257,33 @@ public class CSVEditor extends JFrame {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private boolean discardAgreed() {
|
||||
return 0 == JOptionPane.showOptionDialog(null,
|
||||
lang.getProperty("WARNING_YOUR_DATA_WILL_GONE"),
|
||||
null,
|
||||
JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.WARNING_MESSAGE,
|
||||
null,
|
||||
new String[]{"OK", "Cancel"},
|
||||
"Cancel"
|
||||
);
|
||||
}
|
||||
private void displayMessage(String messageKey) {
|
||||
JOptionPane.showOptionDialog(null,
|
||||
lang.getProperty(messageKey),
|
||||
null,
|
||||
JOptionPane.DEFAULT_OPTION,
|
||||
JOptionPane.INFORMATION_MESSAGE,
|
||||
null,
|
||||
new String[]{"OK", "Cancel"},
|
||||
"Cancel"
|
||||
);
|
||||
}
|
||||
|
||||
private String captionProperties =
|
||||
"" + // dummy string to make IDE happy with the auto indent
|
||||
"id=Block ID\n" +
|
||||
"drop=Drop ID\n" +
|
||||
"id=ID of this block\n" +
|
||||
"drop=ID of the block this very block should drop when mined\n" +
|
||||
"name=String identifier of the block\n" +
|
||||
"shdr=Shade Red (light absorption). Valid range 0.0-4.0\n" +
|
||||
"shdg=Shade Green (light absorption). Valid range 0.0-4.0\n" +
|
||||
@@ -218,4 +303,9 @@ public class CSVEditor extends JFrame {
|
||||
"dlfn=Dynamic Light Function. 0=Static. Please see <strong>notes</strong>\n" +
|
||||
"fv=Vertical friction when player slide on the cliff. 0 means not slide-able\n" +
|
||||
"fr=Horizontal friction. <16:slippery 16:regular >16:sticky\n";
|
||||
|
||||
private String translations =
|
||||
"" +
|
||||
"WARNING_YOUR_DATA_WILL_GONE=Existing edits will be lost, continue?\n" +
|
||||
"OPERATION_CANCELLED=Operation cancelled.\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user