public AddTurnoutAction() {
super("Add", ImageTools.createImageIcon("add.png"));
}
public void actionPerformed(ActionEvent e) {
TurnoutGroup selectedTurnoutGroup = (TurnoutGroup) (turnoutGroupList
.getSelectedValue());
if (selectedTurnoutGroup == null) {
JOptionPane.showMessageDialog(TurnoutConfigurationDialog.this,
"Please select a switch group", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
int nextNumber = 0;
TurnoutPersistenceIface turnoutPersistence = AdHocRailway
.getInstance().getTurnoutPersistence();
if (Preferences.getInstance().getBooleanValue(
PreferencesKeys.USE_FIXED_TURNOUT_AND_ROUTE_GROUP_SIZES)) {
nextNumber = turnoutPersistence
.getNextFreeTurnoutNumberOfGroup(selectedTurnoutGroup);
if (nextNumber == -1) {
JOptionPane.showMessageDialog(
TurnoutConfigurationDialog.this,
"No more free numbers in this group", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
} else {
nextNumber = turnoutPersistence.getNextFreeTurnoutNumber();
}
Turnout newTurnout = new Turnout();
newTurnout.setNumber(nextNumber);
newTurnout.setBus1(Preferences.getInstance().getIntValue(
PreferencesKeys.DEFAULT_TURNOUT_BUS));
newTurnout.setBus2(Preferences.getInstance().getIntValue(
PreferencesKeys.DEFAULT_TURNOUT_BUS));
newTurnout.setTurnoutGroup(selectedTurnoutGroup);
newTurnout.setDefaultStateEnum(SRCPTurnoutState.STRAIGHT);
newTurnout.setOrientationEnum(TurnoutOrientation.EAST);
newTurnout.setTurnoutType(turnoutPersistence
.getTurnoutType(SRCPTurnoutTypes.DEFAULT));
TurnoutConfig switchConfig = new TurnoutConfig(
TurnoutConfigurationDialog.this, newTurnout);
if (switchConfig.isOkPressed()) {
List<Turnout> turnouts = new ArrayList<Turnout>(
selectedTurnoutGroup.getTurnouts());
turnoutModel.setList(turnouts);
}
}