Skip to content
Permalink
Browse files

Change Add sequential user naming behaviour

For Block, Light, Memory, Reporter, Sensor & Turnout tables.
  • Loading branch information...
geowar1 committed Aug 14, 2018
1 parent e2b049e commit 0a02407cf13b36f5d7d74a8296b5ec58bb962770
@@ -139,6 +139,26 @@ public void dispose() {
// should this also dispose of the frame f?
}

/**
* Increments trailing digits of a system/user name (string)
* I.E. "Geo7" ==> "Geo8"
* Note: preserves leading zeros: "Geo007" ==> "Geo008"
* Also: if no trailing digits appends "1": "Geo" ==> "Geo1"
*
* @param name the system or user name string
* @return the same name with incremented trailing digits
*/
protected @Nonnull String nextName(@Nonnull 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);
}

/**
* Specify the JavaHelp target for this specific panel.
*
@@ -987,12 +987,12 @@ void okPressed(ActionEvent e) {
return;
}
}
String user = userName.getText();
String uName = user; // keep result separate to prevent recursive manipulation
user = NamedBean.normalizeUserName(user);
String user = NamedBean.normalizeUserName(userName.getText());
if (user == null || user.isEmpty()) {
user = null;
}
String uName = user; // keep result separate to prevent recursive manipulation

String system = sysName.getText();
String sName = system; // keep result separate to prevent recursive manipulation
sName = InstanceManager.getDefault(BlockManager.class).normalizeSystemName(sName);
@@ -1017,7 +1017,7 @@ void okPressed(ActionEvent e) {
while (true) {
system = nextName(system);
// log.warn("Trying " + system);
Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getByUserName(system);
Block blk = InstanceManager.getDefault(jmri.BlockManager.class).getBySystemName(system);
if (blk == null) {
sName = system;
break;
@@ -1100,17 +1100,6 @@ void okPressed(ActionEvent e) {
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) {
JOptionPane.showMessageDialog(addFrame,
Bundle.getMessage("ErrorBlockAddFailed", sysName) + "\n" + Bundle.getMessage("ErrorAddFailedCheck"),
@@ -12,9 +12,9 @@
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -931,7 +931,7 @@ void createPressed(ActionEvent e) {
}
String suName = lightPrefix + curAddress;
String uName = userName.getText().trim(); // N11N
if (uName.equals("")) {
if (uName.isEmpty()) {
uName = null; // a blank field means no user name
}
// Does System Name have a valid format
@@ -965,7 +965,7 @@ void createPressed(ActionEvent e) {
}
// check if Light exists under an alternate name if an alternate name exists
String altName = InstanceManager.getDefault(LightManager.class).convertSystemNameToAlternate(suName);
if (!altName.equals("")) {
if (!altName.isEmpty()) {
g = InstanceManager.getDefault(LightManager.class).getBySystemName(altName);
if (g != null) {
// Light already exists
@@ -978,7 +978,7 @@ void createPressed(ActionEvent e) {
}
}
// check if a Light with the same user name exists
if (uName != null && !uName.equals("")) {
if (uName != null && !uName.isEmpty()) {
g = InstanceManager.getDefault(LightManager.class).getByUserName(uName);
if (g != null) {
// Light with this user name already exists
@@ -1113,11 +1113,13 @@ void createPressed(ActionEvent e) {
String uxName = "";
if (uName == null) {
uxName = null;
} else {
uxName = uName;
}
for (int i = 1; i < numberOfLights; i++) {
sxName = lightPrefix + (startingAddress + i);
if (uName != null) {
uxName = uName + ":" + i; // behaves like the TurnoutTable > Add multiple naming
if (uxName != null) {
uxName = nextName(uxName);
}
try {
g = InstanceManager.getDefault(LightManager.class).newLight(sxName, uxName);
@@ -1145,7 +1147,7 @@ void editPressed() {
// check if a Light with this name already exists
String suName = fixedSystemName.getText();
String sName = InstanceManager.getDefault(LightManager.class).normalizeSystemName(suName);
if (sName.equals("")) {
if (sName.isEmpty()) {
// Entered system name has invalid format
status1.setText(Bundle.getMessage("LightError3"));
status1.setForeground(Color.red);
@@ -1159,7 +1161,7 @@ void editPressed() {
if (g == null) {
// check if Light exists under an alternate name if an alternate name exists
String altName = InstanceManager.getDefault(LightManager.class).convertSystemNameToAlternate(sName);
if (!altName.equals("")) {
if (!altName.isEmpty()) {
g = InstanceManager.getDefault(LightManager.class).getBySystemName(altName);
if (g != null) {
sName = altName;
@@ -1232,7 +1234,7 @@ void updatePressed(ActionEvent e) {
Light g = curLight;
// Check if the User Name has been changed
String uName = userName.getText().trim(); // N11N
if (uName.equals("")) {
if (uName.isEmpty()) {
uName = null; // a blank field means no user name
}
String prevUName = g.getUserName();
@@ -1454,18 +1456,18 @@ private void addEditControlWindow() {

sensor1Box.setFirstItemBlank(true);
sensor1Box.setToolTipText(Bundle.getMessage("LightSensorHint"));

sensor2Box.setFirstItemBlank(true);
sensor2Box.setToolTipText(Bundle.getMessage("LightTwoSensorHint"));

fastHourSpinner1.setValue(0); // reset needed
fastHourSpinner1.setVisible(false);
fastMinuteSpinner1.setValue(0); // reset needed
fastMinuteSpinner1.setVisible(false);

sensorOnBox.setFirstItemBlank(true);
sensorOnBox.setVisible(false);

clockSep1.setVisible(false);

turnoutBox.setFirstItemBlank(true);
@@ -1485,17 +1487,17 @@ private void addEditControlWindow() {
fastMinuteSpinner2.setEditor(ne2a1);
panel33.add(fastMinuteSpinner2); // minutes OFF
panel33.add(timedOnSpinner);

fastHourSpinner2.setValue(0); // reset needed
fastHourSpinner2.setVisible(false);
fastMinuteSpinner2.setValue(0); // reset needed
fastMinuteSpinner2.setVisible(false);

timedOnSpinner.setValue(0); // reset needed
timedOnSpinner.setVisible(false);

clockSep2.setVisible(false);

panel3.add(panel31);
panel3.add(panel32);
panel3.add(panel33);
@@ -160,15 +160,15 @@ protected String helpTarget() {
}

JmriJFrame addFrame = null;
JTextField sysName = new JTextField(20);
JTextField userName = new JTextField(20);
JTextField sysNameField = new JTextField(20);
JTextField userNameField = new JTextField(20);
JLabel sysNameLabel = new JLabel(Bundle.getMessage("LabelSystemName"));
JLabel userNameLabel = new JLabel(Bundle.getMessage("LabelUserName"));
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"));
JLabel statusBar = new JLabel(Bundle.getMessage("AddBeanStatusEnter"), JLabel.LEADING);
JSpinner numberToAddSpinner = new JSpinner(rangeSpinner);
JCheckBox rangeBox = new JCheckBox(Bundle.getMessage("AddRangeBox"));
JCheckBox autoSystemNameBox = new JCheckBox(Bundle.getMessage("LabelAutoSysName"));
JLabel statusBarLabel = new JLabel(Bundle.getMessage("AddBeanStatusEnter"), JLabel.LEADING);
jmri.UserPreferencesManager p;

@Override
@@ -185,15 +185,15 @@ protected void addPressed(ActionEvent e) {
ActionListener cancelListener = (ActionEvent e1) -> {
cancelPressed(e1);
};
addFrame.add(new AddNewBeanPanel(sysName, userName, numberToAdd, range, autoSystemName, "ButtonCreate", okListener, cancelListener, statusBar));
sysName.setToolTipText(Bundle.getMessage("SysNameToolTip", "M")); // override tooltip with bean specific letter
addFrame.add(new AddNewBeanPanel(sysNameField, userNameField, numberToAddSpinner, rangeBox, autoSystemNameBox, "ButtonCreate", okListener, cancelListener, statusBarLabel));
sysNameField.setToolTipText(Bundle.getMessage("SysNameToolTip", "M")); // override tooltip with bean specific letter
}
sysName.setBackground(Color.white);
// reset statusBar text
statusBar.setText(Bundle.getMessage("AddBeanStatusEnter"));
statusBar.setForeground(Color.gray);
sysNameField.setBackground(Color.white);
// reset statusBarLabel text
statusBarLabel.setText(Bundle.getMessage("AddBeanStatusEnter"));
statusBarLabel.setForeground(Color.gray);
if (p.getSimplePreferenceState(systemNameAuto)) {
autoSystemName.setSelected(true);
autoSystemNameBox.setSelected(true);
}
addFrame.pack();
addFrame.setVisible(true);
@@ -211,8 +211,8 @@ void okPressed(ActionEvent e) {

int numberOfMemory = 1;

if (range.isSelected()) {
numberOfMemory = (Integer) numberToAdd.getValue();
if (rangeBox.isSelected()) {
numberOfMemory = (Integer) numberToAddSpinner.getValue();
}

if (numberOfMemory >= 65) { // limited by JSpinnerModel to 100
@@ -224,90 +224,86 @@ void okPressed(ActionEvent e) {
}
}

String user = NamedBean.normalizeUserName(userName.getText());
if (user.equals("")) {
user = null;
String uName = NamedBean.normalizeUserName(userNameField.getText());
if (uName.isEmpty()) {
uName = null;
}
String sName = jmri.InstanceManager.memoryManagerInstance().normalizeSystemName(sysName.getText());
String sName = jmri.InstanceManager.memoryManagerInstance().normalizeSystemName(sysNameField.getText());
// initial check for empty entry
if (sName.length() < 1 && !autoSystemName.isSelected()) {
statusBar.setText(Bundle.getMessage("WarningSysNameEmpty"));
statusBar.setForeground(Color.red);
sysName.setBackground(Color.red);
if (sName.isEmpty() && !autoSystemNameBox.isSelected()) {
statusBarLabel.setText(Bundle.getMessage("WarningSysNameEmpty"));
statusBarLabel.setForeground(Color.red);
sysNameField.setBackground(Color.red);
return;
} else {
sysName.setBackground(Color.white);
sysNameField.setBackground(Color.white);
}

// Add some entry pattern checking, before assembling sName and handing it to the memoryManager
String statusMessage = Bundle.getMessage("ItemCreateFeedback", Bundle.getMessage("BeanNameMemory"));
String errorMessage = null;
StringBuilder b;
for (int x = 0; x < numberOfMemory; x++) {

if (x != 0) {
if (user != null) {
b = new StringBuilder(NamedBean.normalizeUserName(userName.getText())); // N11N
b.append(":");
b.append(Integer.toString(x));
user = b.toString(); // add :x to user name starting with 2nd item
}
if (!autoSystemName.isSelected()) {
b = new StringBuilder(jmri.InstanceManager.memoryManagerInstance().normalizeSystemName(sysName.getText()));
b.append(":");
b.append(Integer.toString(x));
sName = b.toString();
}
}

if (user != null && !user.equals("") && jmri.InstanceManager.memoryManagerInstance().getByUserName(user) != null && !p.getPreferenceState(getClassName(), "duplicateUserName")) {
if (uName != null && !uName.isEmpty() && jmri.InstanceManager.memoryManagerInstance().getByUserName(uName) != null && !p.getPreferenceState(getClassName(), "duplicateUserName")) {
jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).
showErrorMessage(Bundle.getMessage("ErrorTitle"), Bundle.getMessage("ErrorDuplicateUserName", user), getClassName(), "duplicateUserName", false, true);
user = null; // new Memory objects always receive a valid system name using the next free index, but user names must not be in use so use none in that case
showErrorMessage(Bundle.getMessage("ErrorTitle"), Bundle.getMessage("ErrorDuplicateUserName", uName), getClassName(), "duplicateUserName", false, true);
uName = null; // new Memory objects always receive a valid system name using the next free index, but uName names must not be in use so use none in that case
// show in status bar
errorMessage = Bundle.getMessage("ErrorDuplicateUserName", user);
statusBar.setText(errorMessage);
statusBar.setForeground(Color.red);
errorMessage = Bundle.getMessage("ErrorDuplicateUserName", uName);
statusBarLabel.setText(errorMessage);
statusBarLabel.setForeground(Color.red);
}
if (sName != null && !sName.equals("") && jmri.InstanceManager.memoryManagerInstance().getBySystemName(sName) != null && !p.getPreferenceState(getClassName(), "duplicateSystemName")) {
if (sName != null && !sName.isEmpty() && jmri.InstanceManager.memoryManagerInstance().getBySystemName(sName) != null && !p.getPreferenceState(getClassName(), "duplicateSystemName")) {
jmri.InstanceManager.getDefault(jmri.UserPreferencesManager.class).
showErrorMessage(Bundle.getMessage("ErrorTitle"), Bundle.getMessage("ErrorDuplicateSystemName", sName), getClassName(), "duplicateSystemName", false, true);
// show in status bar
errorMessage = Bundle.getMessage("ErrorDuplicateSystemName", sName);
statusBar.setText(errorMessage);
statusBar.setForeground(Color.red);
statusBarLabel.setText(errorMessage);
statusBarLabel.setForeground(Color.red);
return; // new Memory objects are always valid, but system names must not be in use so skip in that case
}
try {
if (autoSystemName.isSelected()) {
InstanceManager.memoryManagerInstance().newMemory(user);
if (autoSystemNameBox.isSelected()) {
InstanceManager.memoryManagerInstance().newMemory(uName);
} else {
InstanceManager.memoryManagerInstance().newMemory(sName, user);
InstanceManager.memoryManagerInstance().newMemory(sName, uName);
}
} catch (IllegalArgumentException ex) {
// user input no good
// uName input no good
handleCreateException(sName);
errorMessage = "An error has occurred";
statusBar.setText(errorMessage);
statusBar.setForeground(Color.red);
statusBarLabel.setText(errorMessage);
statusBarLabel.setForeground(Color.red);
return; // without creating
}
// add first and last names to statusMessage user feedback string
if (x == 0 || x == numberOfMemory - 1) statusMessage = statusMessage + " " + sName + " (" + user + ")";
if (x == numberOfMemory - 2) statusMessage = statusMessage + " " + Bundle.getMessage("ItemCreateUpTo") + " ";
// only mention first and last of range added
} // end of for loop creating range of Memories

// provide feedback to user
// add first and last names to statusMessage uName feedback string
// only mention first and last of rangeBox added
if (x == 0 || x == numberOfMemory - 1) {
statusMessage = statusMessage + " " + sName + " (" + uName + ")";
}
if (x == numberOfMemory - 2) {
statusMessage = statusMessage + " " + Bundle.getMessage("ItemCreateUpTo") + " ";
}

// bump system & uName names
if (!autoSystemNameBox.isSelected()) {
sName = nextName(sName);
}
if (uName != null) {
uName = nextName(uName);
}
} // end of for loop creating rangeBox of Memories

// provide feedback to uName
if (errorMessage == null) {
statusBar.setText(statusMessage);
statusBar.setForeground(Color.gray);
statusBarLabel.setText(statusMessage);
statusBarLabel.setForeground(Color.gray);
} else {
statusBar.setText(errorMessage);
// statusBar.setForeground(Color.red); // handled when errorMassage is set to differentiate urgency
statusBarLabel.setText(errorMessage);
// statusBarLabel.setForeground(Color.red); // handled when errorMassage is set to differentiate urgency
}

p.setSimplePreferenceState(systemNameAuto, autoSystemName.isSelected());
p.setSimplePreferenceState(systemNameAuto, autoSystemNameBox.isSelected());
}

void handleCreateException(String sysName) {

0 comments on commit 0a02407

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