Skip to content

Commit

Permalink
feat: change validation if manager is proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
rhwood committed Jul 5, 2019
1 parent ea4fbe1 commit 2bb8d69
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions java/src/jmri/Manager.java
Expand Up @@ -78,6 +78,10 @@ public default String getSystemNamePrefix() {
/**
* Create a SystemName by prepending the system name prefix to the name if
* not already present.
* <p>
* <strong>Note:</strong> implementations <em>must</em> call
* {@link #validateSystemNameFormat(java.lang.String)} to ensure the
* returned name is valid.
*
* @param name the item to make the system name for
* @return A system name from a user input, typically a number.
Expand Down
15 changes: 15 additions & 0 deletions java/src/jmri/ProxyManager.java
@@ -0,0 +1,15 @@
package jmri;

/**
* Interface for Managers of NamedBeans that are proxies for a collection of
* like Managers.
* <p>
* At present, this is merely an extension point that can be used to test if a
* Manager is a proxy.
*
* @author Randall Wood Copyright 2019
* @param <B> type of supported NamedBean
*/
public interface ProxyManager<B extends NamedBean> extends Manager<B> {

}
3 changes: 2 additions & 1 deletion java/src/jmri/managers/AbstractProxyManager.java
@@ -1,5 +1,6 @@
package jmri.managers;

import jmri.ProxyManager;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
Expand Down Expand Up @@ -35,7 +36,7 @@
* @param <E> the supported type of NamedBean
* @author Bob Jacobsen Copyright (C) 2003, 2010, 2018
*/
abstract public class AbstractProxyManager<E extends NamedBean> implements ProvidingManager<E>, Manager.ManagerDataListener<E> {
abstract public class AbstractProxyManager<E extends NamedBean> implements ProxyManager<E>, ProvidingManager<E>, Manager.ManagerDataListener<E> {

/**
* Number of managers available through getManager(i) and getManagerList(),
Expand Down
16 changes: 12 additions & 4 deletions java/src/jmri/swing/SystemNameValidator.java
Expand Up @@ -3,10 +3,12 @@
import com.alexandriasoftware.swing.JInputValidator;
import com.alexandriasoftware.swing.JInputValidatorPreferences;
import com.alexandriasoftware.swing.Validation;
import javax.annotation.Nonnull;
import javax.swing.JComponent;
import javax.swing.text.JTextComponent;
import jmri.Manager;
import jmri.Manager.NameValidity;
import jmri.ProxyManager;

/**
* A {@link com.alexandriasoftware.swing.JInputValidator} that validates a
Expand Down Expand Up @@ -40,7 +42,7 @@ public class SystemNameValidator extends JInputValidator {
* @param component the component to validate has a valid system name
* @param manager the manager that will be used for validation
*/
public SystemNameValidator(JComponent component, Manager<?> manager) {
public SystemNameValidator(@Nonnull JComponent component, @Nonnull Manager<?> manager) {
this(component, manager, false);
}

Expand All @@ -53,7 +55,7 @@ public SystemNameValidator(JComponent component, Manager<?> manager) {
* {@link javax.swing.InputVerifier#verify(javax.swing.JComponent)}
* must return true to allow focus change; false otherwise
*/
public SystemNameValidator(JComponent component, Manager<?> manager, boolean required) {
public SystemNameValidator(@Nonnull JComponent component, @Nonnull Manager<?> manager, boolean required) {
super(component, true, required);
this.manager = manager;
this.required = required;
Expand All @@ -67,10 +69,16 @@ protected Validation getValidation(JComponent component, JInputValidatorPreferen
if (text != null && !text.isEmpty()) {
try {
manager.validateSystemNameFormat(text);
if (manager instanceof ProxyManager) {
ProxyManager proxyManager = (ProxyManager) manager;
proxyManager.validateSystemNameFormat(text);
} else {
manager.makeSystemName(text);
}
} catch (IllegalArgumentException ex) {
if (manager.validSystemNameFormat(text) == NameValidity.VALID_AS_PREFIX_ONLY) {
return new Validation(Validation.Type.WARNING, Bundle.getMessage("SystemNameValidatorValidPrefix", text, manager.getBeanTypeHandled(),
trimHtmlTags(getToolTipText())), preferences);
return new Validation(Validation.Type.WARNING, Bundle.getMessage("SystemNameValidatorValidPrefix", text, manager.getBeanTypeHandled(),
trimHtmlTags(getToolTipText())), preferences);
}
return new Validation(Validation.Type.DANGER, Bundle.getMessage("SystemNameValidatorInvalid", ex.getMessage(), trimHtmlTags(getToolTipText())), preferences);
}
Expand Down

0 comments on commit 2bb8d69

Please sign in to comment.