Action.NamedActionDictionary namedActions = Action.getNamedActions();
namedActions.put("configureCell", new Action() {
@Override
public void perform(Component source) {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Sheet sheet;
// Make the cell component available to script blocks
int rowIndex = tablePane.getRowAt(contextMenuHandler.getY());
int columnIndex = tablePane.getColumnAt(contextMenuHandler.getX());
Component component = tablePane.getCellComponent(rowIndex, columnIndex);
bxmlSerializer.getNamespace().put("component", component);
try {
sheet = (Sheet)bxmlSerializer.readObject(TablePanes.class,
"table_panes_configure_cell.bxml");
} catch (SerializationException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
sheet.open(TablePanes.this);
}
});
namedActions.put("configureRow", new Action() {
@Override
public void perform(Component source) {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Sheet sheet;
// Make the selected row available to script blocks
int rowIndex = tablePane.getRowAt(contextMenuHandler.getY());
TablePane.Row row = tablePane.getRows().get(rowIndex);
bxmlSerializer.getNamespace().put("row", row);
try {
sheet = (Sheet)bxmlSerializer.readObject(TablePanes.class,
"table_panes_configure_row.bxml");
} catch (SerializationException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
sheet.open(TablePanes.this);
}
});
namedActions.put("insertRow", new Action() {
@Override
public void perform(Component source) {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Sheet sheet;
// Create and insert a new row
TablePane.Row row = new TablePane.Row();
int rowIndex = tablePane.getRowAt(contextMenuHandler.getY());
tablePane.getRows().insert(row, rowIndex);
// Populate the row with the expected content
row.add(new Label("-1"));
for (int i = 1, n = tablePane.getColumns().getLength(); i < n; i++) {
Panel panel = new Panel();
panel.getStyles().put("backgroundColor", "#dddcd5");
row.add(panel);
}
// Make the new row available to script blocks
bxmlSerializer.getNamespace().put("row", row);
try {
sheet = (Sheet)bxmlSerializer.readObject(TablePanes.class,
"table_panes_configure_row.bxml");
} catch (SerializationException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
sheet.open(TablePanes.this);
}
});
namedActions.put("removeRow", new Action() {
@Override
public void perform(Component source) {
ArrayList<String> options = new ArrayList<String>("OK", "Cancel");
String message = "Remove Row?";
Label body = new Label("Are you sure you want to remove the row?");
body.getStyles().put("wrapText", true);
final Prompt prompt = new Prompt(MessageType.QUESTION, message, options, body);
prompt.setSelectedOptionIndex(0);
prompt.open(TablePanes.this, new SheetCloseListener() {
@Override
public void sheetClosed(Sheet sheet) {
if (prompt.getResult() && prompt.getSelectedOptionIndex() == 0) {
int rowIndex = tablePane.getRowAt(contextMenuHandler.getY());
tablePane.getRows().remove(rowIndex, 1);
}
}
});
}
});
namedActions.put("configureColumn", new Action() {
@Override
public void perform(Component source) {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Sheet sheet;
// Make the selected column available to script blocks
int columnIndex = tablePane.getColumnAt(contextMenuHandler.getX());
TablePane.Column column = tablePane.getColumns().get(columnIndex);
bxmlSerializer.getNamespace().put("column", column);
try {
sheet = (Sheet)bxmlSerializer.readObject(TablePanes.class,
"table_panes_configure_column.bxml");
} catch (SerializationException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
sheet.open(TablePanes.this);
}
});
namedActions.put("insertColumn", new Action() {
@Override
public void perform(Component source) {
BXMLSerializer bxmlSerializer = new BXMLSerializer();
Sheet sheet;
// Create and insert a new column
TablePane.Column column = new TablePane.Column();
int columnIndex = tablePane.getColumnAt(contextMenuHandler.getX());
tablePane.getColumns().insert(column, columnIndex);
// Populate the column with the expected content
TablePane.RowSequence rows = tablePane.getRows();
rows.get(0).insert(new Label("-1"), columnIndex);
for (int i = 1, n = rows.getLength(); i < n; i++) {
Panel panel = new Panel();
panel.getStyles().put("backgroundColor", "#dddcd5");
rows.get(i).insert(panel, columnIndex);
}
// Make the new column available to script blocks
bxmlSerializer.getNamespace().put("column", column);
try {
sheet = (Sheet)bxmlSerializer.readObject(TablePanes.class,
"table_panes_configure_column.bxml");
} catch (SerializationException exception) {
throw new RuntimeException(exception);
} catch (IOException exception) {
throw new RuntimeException(exception);