// TODO load images
JButton addnationButton = Button.SmallAdd.create();
JButton removenationButton = Button.SmallDelete.create();
JButton changenationButton = Button.SmallEdit.create();
ButtonBar nbar = new ButtonBar();
nbar.add(addnationButton, removenationButton, changenationButton);
// list
nationsList = new JList<>();
nationsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
nationsList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
boolean adjusting = e.getValueIsAdjusting();
if (!adjusting) {
XList<Nation> model = (XList<Nation>) nationsList.getModel();
int row = nationsList.getSelectedIndex();
if (row != -1) {
Nation nation = model.getElementAt(row);
XList<Province> provinces = nation.getProvinces();
// TODO tell the province panel all about it
provinceList.setModel(provinces);
}
}
}
});
// set button actions
addnationButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = FrameManager.getInstance().showInputDialog("Enter new Nation's name:");
if (name != null) {
// TODO test if already existing
XList<Nation> model = (XList<Nation>) nationsList.getModel();
Nation nation = new Nation();
nation.setProperty(Nation.KEY_NAME, name);
model.addElement(nation);
}
}
});
removenationButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = nationsList.getSelectedIndex();
if (row != -1) {
// TODO are you sure?
XList<Nation> model = (XList<Nation>) nationsList.getModel();
model.removeElementAt(row);
}
}
});
changenationButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int row = nationsList.getSelectedIndex();
if (row != -1) {
// TODO implement
}
}
});
// scrollpane
JScrollPane nationScrollPane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
nationScrollPane.setViewportView(nationsList);
// layout of nations panel
panel.setLayout(new MigLayout("wrap 1, fill", "", "[][][grow]"));
panel.add(nationsInfoLabel);
panel.add(nbar.get());
panel.add(nationScrollPane, "height 400!, width 300!");
return panel;
}