Skip to content
Permalink
Browse files

Change "Add Block" sequential names algorithm.

  • Loading branch information...
geowar1 committed Aug 13, 2018
1 parent 410c858 commit 6637696b0abffbb7dcfafa174299698f1aec2fea
Showing with 55 additions and 31 deletions.
  1. +55 −31 java/src/jmri/jmrit/beantable/BlockTableAction.java
@@ -608,7 +608,7 @@ public Component getTableCellEditorComponent(

public JLabel updateLabel(String value, int row) {
// if (iconHeight > 0) { // if necessary, increase row height;
//table.setRowHeight(row, Math.max(table.getRowHeight(), iconHeight - 5)); // TODO adjust table row height for Block icons
//table.setRowHeight(row, Math.max(table.getRowHeight(), iconHeight - 5)); // TODO adjust table row height for Block icons
// }
if (value.equals(Bundle.getMessage("BlockUnOccupied")) && offIcon != null) {
label = new JLabel(offIcon);
@@ -859,10 +859,10 @@ protected String helpTarget() {
JTextField blockSpeed = new JTextField(7);
JCheckBox checkPerm = new JCheckBox(Bundle.getMessage("BlockPermColName"));

SpinnerNumberModel rangeSpinner = new SpinnerNumberModel(1, 1, 100, 1); // maximum 100 items
JSpinner numberToAdd = new JSpinner(rangeSpinner);
JCheckBox range = new JCheckBox(Bundle.getMessage("AddRangeBox"));
JCheckBox _autoSystemName = new JCheckBox(Bundle.getMessage("LabelAutoSysName"));
SpinnerNumberModel numberToAddSpinnerNumberModel = new SpinnerNumberModel(1, 1, 100, 1); // maximum 100 items
JSpinner numberToAddSpinner = new JSpinner(numberToAddSpinnerNumberModel);
JCheckBox addRangeCheckBox = new JCheckBox(Bundle.getMessage("AddRangeBox"));
JCheckBox _autoSystemNameCheckBox = new JCheckBox(Bundle.getMessage("LabelAutoSysName"));
JLabel statusBar = new JLabel(Bundle.getMessage("AddBeanStatusEnter"), JLabel.LEADING);
jmri.UserPreferencesManager pref;

@@ -885,16 +885,17 @@ public void actionPerformed(ActionEvent e) {
cancelPressed(e);
}
};
addFrame.add(new AddNewBeanPanel(sysName, userName, numberToAdd, range, _autoSystemName, "ButtonCreate", oklistener, cancellistener, statusBar));
addFrame.add(new AddNewBeanPanel(sysName, userName, numberToAddSpinner, addRangeCheckBox, _autoSystemNameCheckBox, "ButtonCreate", oklistener, cancellistener, statusBar));
sysName.setToolTipText(Bundle.getMessage("SysNameToolTip", "B")); // override tooltip with bean specific letter
}
sysName.setBackground(Color.white);
// reset statusBar text
statusBar.setText(Bundle.getMessage("AddBeanStatusEnter"));
statusBar.setForeground(Color.gray);
if (pref.getSimplePreferenceState(systemNameAuto)) {
_autoSystemName.setSelected(true);
_autoSystemNameCheckBox.setSelected(true);
}
addRangeCheckBox.setSelected(false);
addFrame.pack();
addFrame.setVisible(true);
}
@@ -973,14 +974,14 @@ void cancelPressed(ActionEvent e) {
*/
void okPressed(ActionEvent e) {

int NumberOfBlocks = 1;
int numberOfBlocks = 1;

if (range.isSelected()) {
NumberOfBlocks = (Integer) numberToAdd.getValue();
if (addRangeCheckBox.isSelected()) {
numberOfBlocks = (Integer) numberToAddSpinner.getValue();
}
if (NumberOfBlocks >= 65) { // limited by JSpinnerModel to 100
if (numberOfBlocks >= 65) { // limited by JSpinnerModel to 100
if (JOptionPane.showConfirmDialog(addFrame,
Bundle.getMessage("WarnExcessBeans", Bundle.getMessage("Blocks"), NumberOfBlocks),
Bundle.getMessage("WarnExcessBeans", Bundle.getMessage("Blocks"), numberOfBlocks),
Bundle.getMessage("WarningTitle"),
JOptionPane.YES_NO_OPTION) == 1) {
return;
@@ -989,14 +990,14 @@ void okPressed(ActionEvent e) {
String user = userName.getText();
String uName = user; // keep result separate to prevent recursive manipulation
user = NamedBean.normalizeUserName(user);
if (user == null || user.length() == 0) {
if (user == null || user.isEmpty()) {
user = null;
}
String system = sysName.getText();
String sName = system; // keep result separate to prevent recursive manipulation
sName = InstanceManager.getDefault(BlockManager.class).normalizeSystemName(sName);
// initial check for empty entry using the raw name
if (sName.length() < 3 && !_autoSystemName.isSelected()) { // Using 3 to catch a plain IB
if (sName.length() < 3 && !_autoSystemNameCheckBox.isSelected()) { // Using 3 to catch a plain IB
statusBar.setText(Bundle.getMessage("WarningSysNameEmpty"));
statusBar.setForeground(Color.red);
sysName.setBackground(Color.red);
@@ -1009,25 +1010,37 @@ void okPressed(ActionEvent e) {
String statusMessage = Bundle.getMessage("ItemCreateFeedback", Bundle.getMessage("BeanNameBlock"));
StringBuilder b;

for (int x = 0; x < NumberOfBlocks; x++) {
for (int x = 0; x < numberOfBlocks; x++) {
if (x != 0) { // start at 2nd Block
if (user != null) {
b = new StringBuilder(user);
b.append(":");
b.append(Integer.toString(x));
uName = b.toString();
if (!_autoSystemNameCheckBox.isSelected()) {
// Find first block with unused system name
while (true) {
system = nextName(system);
//log.warn("Trying " + system);
Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getByUserName(system);
if (blk == null) {
sName = system;
break;
}
}
}
if (!_autoSystemName.isSelected()) {
b = new StringBuilder(system);
b.append(":");
b.append(Integer.toString(x));
sName = b.toString();
if (user != null) {
// Find first block with unused user name
while (true) {
user = nextName(user);
//log.warn("Trying " + user);
Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getByUserName(user);
if (blk == null) {
uName = user;
break;
}
}
}
}
Block blk;
String xName = "";
try {
if (_autoSystemName.isSelected()) {
if (_autoSystemNameCheckBox.isSelected()) {
blk = InstanceManager.getDefault(jmri.BlockManager.class).createNewBlock(uName);
if (blk == null) {
xName = uName;
@@ -1071,20 +1084,31 @@ void okPressed(ActionEvent e) {
blk.setCurvature(Block.SEVERE);
}
// add first and last names to statusMessage user feedback string
if (x == 0 || x == NumberOfBlocks - 1) {
if (x == 0 || x == numberOfBlocks - 1) {
statusMessage = statusMessage + " " + sName + " (" + user + ")";
}
if (x == NumberOfBlocks - 2) {
if (x == numberOfBlocks - 2) {
statusMessage = statusMessage + " " + Bundle.getMessage("ItemCreateUpTo") + " ";
}
// only mention first and last of range added
} // end of for loop creating range of Blocks
// only mention first and last of addRangeCheckBox added
} // end of for loop creating addRangeCheckBox of Blocks

// provide feedback to user
statusBar.setText(statusMessage);
statusBar.setForeground(Color.gray);

pref.setSimplePreferenceState(systemNameAuto, _autoSystemName.isSelected());
pref.setSimplePreferenceState(systemNameAuto, _autoSystemNameCheckBox.isSelected());
}

private String nextName(String name) {
final String[] parts = name.split("(?=\\d+$)", 2);
String numString = "0";
if (parts.length == 2) {
numString = parts[1];
}
final int numStringLength = numString.length();
final int num = Integer.parseInt(numString) + 1;
return parts[0] + String.format("%0" + numStringLength + "d", num);
}

void handleCreateException(String sysName) {

0 comments on commit 6637696

Please sign in to comment.
You can’t perform that action at this time.