table.setAutoCreateRowSorter(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setColumnSelectionAllowed(true);
table.setRowSelectionAllowed(true);
// add model
final XTable model = new XTable();
model.insertColumn("New Column", 0);
model.insertRow(0);
table.setModel(model);
// create tool bar
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
// file chooser used in load/save dialogs
final String extension = ".xml";
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
fileChooser.setFileFilter(new FileFilterImpl(extension));
// create buttons
JButton newButton = new JButton("New Table");
JButton loadButton = new JButton("Load Table");
JButton saveButton = new JButton("Save Table");
JButton addRowButton = new JButton("Add row");
JButton delRowButton = new JButton("Remove row");
JButton addColumnButton = new JButton("Add column");
JButton delColumnButton = new JButton("Remove column");
JButton renColumnButton = new JButton("Rename column");
JButton helpButton = new JButton("Help");
// action handlers
// new table button action
newButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showConfirmDialog(TableEditorFrame.this, "Discard all changes?", "New Table", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
model.clear();
model.insertColumn("New Column", 0);
model.insertRow(0);
}
}
});
// load table button action
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// choose xml file
if (fileChooser.showOpenDialog(TableEditorFrame.this) == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
// read file and parse to xml
Node xml;
try (InputStream is = new FileInputStream(f)) {
xml = XMLHelper.read(is);
} catch (ParsingException | IOException ex) {
LOG.log(Level.SEVERE, null, ex);
// NotificationFactory.createInfoPane(TableEditorFrame.this, "Loading failed."); // TODO fix
return;
}
// initialize from xml
model.fromXML(xml);
// NotificationFactory.createInfoPane(TableEditorFrame.this, "Table loaded."); // TODO fix
}
}
});
// save table button action
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (fileChooser.showSaveDialog(TableEditorFrame.this) == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
String name = f.getAbsolutePath();
if (!name.endsWith(extension)) {
f = new File(name + extension);
}
Node xml = model.toXML();
OutputStream os;
try {
os = new FileOutputStream(f);
XMLHelper.write(os, xml);
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
// NotificationFactory.createInfoPane(TableEditorFrame.this, "Saving failed."); // TODO fix
return;
}
// NotificationFactory.createInfoPane(TableEditorFrame.this, "Table saved."); // TODO fix
}
}
});
// add row button action
addRowButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row == -1) {
row = 0;
}
model.insertRow(row);
}
});
// remove row button action
delRowButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row == -1) {
row = 0;
}
model.removeRow(row);
}
});
// add column button action
addColumnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int column = table.getSelectedColumn();
if (column == -1) {
column = 0;
}
model.insertColumn("New Column", column);
}
});
// remove column button action
delColumnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int column = table.getSelectedColumn();
if (column == -1) {
column = 0;
}
// confirmation dialog
int confirm = JOptionPane.showConfirmDialog(TableEditorFrame.this, "Are you sure?", "Remove column", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (confirm == JOptionPane.YES_OPTION) {
model.removeColumn(column);
}
}
});
// rename column button action
renColumnButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String newName = JOptionPane.showInputDialog(TableEditorFrame.this, "New column name", model.getColumnName(0));
// if user click on cancel newName will be null
if (newName != null) {
int column = table.getSelectedColumn();
if (column == -1) {
column = 0;
}
model.setColumnName(column, newName);
}
}
});
// help button action
helpButton.addActionListener(new ActionListener() {