use of jmri.jmrit.logix.OPath in project JMRI by JMRI.
the class BlockPathTableModel method setValueAt.
@Override
public void setValueAt(Object value, int row, int col) {
String msg = null;
if (_block.getPaths().size() == row) {
switch(col) {
case NAME_COLUMN:
String strValue = (String) value;
if (_block.getPathByName(strValue) != null) {
msg = Bundle.getMessage("DuplPathName", strValue);
tempRow[col] = strValue;
} else {
Portal fromPortal = _block.getPortalByName(tempRow[FROM_PORTAL_COLUMN]);
Portal toPortal = _block.getPortalByName(tempRow[TO_PORTAL_COLUMN]);
if (fromPortal != null || toPortal != null) {
OPath path = new OPath(strValue, _block, fromPortal, toPortal, null);
float len = 0.0f;
try {
len = IntlUtilities.floatValue(tempRow[LENGTHCOL]);
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, Bundle.getMessage("BadNumber", tempRow[LENGTHCOL]), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
}
if (tempRow[UNITSCOL].equals((Bundle.getMessage("cm")))) {
path.setLength(len * 10.0f);
} else {
path.setLength(len * 25.4f);
}
if (!_block.addPath(path)) {
msg = Bundle.getMessage("AddPathFailed", strValue);
tempRow[NAME_COLUMN] = strValue;
} else {
initTempRow();
_parent.updateOpenMenu();
fireTableDataChanged();
}
} else {
tempRow[NAME_COLUMN] = strValue;
}
}
break;
case LENGTHCOL:
try {
_tempLen = IntlUtilities.floatValue(value.toString());
if (tempRow[UNITSCOL].equals(Bundle.getMessage("cm"))) {
_tempLen *= 10f;
} else {
_tempLen *= 25.4f;
}
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, Bundle.getMessage("BadNumber", tempRow[LENGTHCOL]), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
}
return;
case UNITSCOL:
_units.set(row, (Boolean) value);
fireTableRowsUpdated(row, row);
return;
case DELETE_COL:
initTempRow();
fireTableRowsUpdated(row, row);
break;
default:
// fall through
break;
}
tempRow[col] = (String) value;
if (msg != null) {
JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE);
}
return;
}
OPath path = (OPath) _block.getPaths().get(row);
switch(col) {
case FROM_PORTAL_COLUMN:
String strValue = (String) value;
if (strValue != null) {
Portal portal = _block.getPortalByName(strValue);
PortalManager portalMgr = InstanceManager.getDefault(PortalManager.class);
if (portal == null || portalMgr.getPortal(strValue) == null) {
int response = JOptionPane.showConfirmDialog(null, Bundle.getMessage("BlockPortalConflict", value, _block.getDisplayName()), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
break;
}
portal = portalMgr.providePortal(strValue);
if (portal == null) {
msg = Bundle.getMessage("NoSuchPortalName", strValue);
break;
} else {
if (!portal.setFromBlock(_block, false)) {
response = JOptionPane.showConfirmDialog(null, Bundle.getMessage("BlockPathsConflict", value, portal.getFromBlockName()), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
break;
}
}
portal.setFromBlock(_block, true);
_parent.getPortalModel().fireTableDataChanged();
}
}
path.setFromPortal(portal);
if (!portal.addPath(path)) {
msg = Bundle.getMessage("AddPathFailed", strValue);
}
} else {
path.setFromPortal(null);
}
fireTableRowsUpdated(row, row);
break;
case NAME_COLUMN:
strValue = (String) value;
if (strValue != null) {
if (_block.getPathByName(strValue) != null) {
msg = Bundle.getMessage("DuplPathName", strValue);
}
path.setName(strValue);
fireTableRowsUpdated(row, row);
}
break;
case TO_PORTAL_COLUMN:
strValue = (String) value;
if (strValue != null) {
PortalManager portalMgr = InstanceManager.getDefault(PortalManager.class);
Portal portal = _block.getPortalByName(strValue);
if (portal == null || portalMgr.getPortal(strValue) == null) {
int response = JOptionPane.showConfirmDialog(null, Bundle.getMessage("BlockPortalConflict", value, _block.getDisplayName()), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
break;
}
portal = portalMgr.providePortal(strValue);
if (portal == null) {
msg = Bundle.getMessage("NoSuchPortalName", strValue);
break;
} else {
if (!portal.setToBlock(_block, false)) {
response = JOptionPane.showConfirmDialog(null, Bundle.getMessage("BlockPathsConflict", value, portal.getToBlockName()), Bundle.getMessage("WarningTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
break;
}
}
portal.setToBlock(_block, true);
_parent.getPortalModel().fireTableDataChanged();
}
}
path.setToPortal(portal);
if (!portal.addPath(path)) {
msg = Bundle.getMessage("AddPathFailed", strValue);
}
} else {
path.setToPortal(null);
}
fireTableRowsUpdated(row, row);
break;
case LENGTHCOL:
try {
float len = IntlUtilities.floatValue(value.toString());
if (_units.get(row)) {
path.setLength(len * 10.0f);
} else {
path.setLength(len * 25.4f);
}
fireTableRowsUpdated(row, row);
} catch (ParseException e) {
JOptionPane.showMessageDialog(null, Bundle.getMessage("BadNumber", value), Bundle.getMessage("ErrorTitle"), JOptionPane.WARNING_MESSAGE);
}
return;
case UNITSCOL:
_units.set(row, (Boolean) value);
fireTableRowsUpdated(row, row);
return;
case EDIT_COL:
_parent.openPathTurnoutFrame(_parent.makePathTurnoutName(_block.getSystemName(), path.getName()));
break;
case DELETE_COL:
if (deletePath(path)) {
_units.remove(row);
fireTableDataChanged();
}
break;
default:
// fall through
break;
}
if (msg != null) {
JOptionPane.showMessageDialog(null, msg, Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE);
}
}
Aggregations