package greenfoot.guifx;
import bluej.Config;
import bluej.extensions.SourceType;
import bluej.pkgmgr.Package;
import bluej.utility.JavaNames;
import java.util.Properties;
import javafx.beans.property.ReadOnlyObjectProperty;
import javafx.beans.property.StringProperty;
| Class that verifies a class name typed into a TextField. It checks that the
| class name is a legal name of a Java class and that the class does not
| already exist. It is also possible to listen for changes in the validity of
| the TextField.
|
| @author Poul Henriksen
| @author Amjad Altadmri
|
public class ClassNameVerifier
{
| The package the class will be added to
|
private Package pkg;
private String message;
private String illegalClassName;
private Properties localProperties = new Properties();
private final String classExists = Config.getString("newclass.dialog.err.classExists");
| The text field to listen for DocumentEvents on
|
private final StringProperty textProperty;
private final ReadOnlyObjectProperty<SourceType> sourceTypeProperty;
| Create new class name verifier. This will add the appropate listeners to
| the textField.
|
| If using a Cancel button it might be useful to set
| {}link JComponent.#setVerifyInputWhenFocusTarget(boolean)} to false on
| the cancel button.
|
| @param pkg Package containing the user classes for which to check against.
|
public ClassNameVerifier(Package pkg, StringProperty textProperty, ReadOnlyObjectProperty<SourceType> sourceTypeProperty)
{
this.pkg = pkg;
this.textProperty = textProperty;
this.sourceTypeProperty = sourceTypeProperty;
checkValidity();
}
public String getMessage()
{
return message;
}
private void buildTheErrorMessage()
{
SourceType sourceType = sourceTypeProperty.get();
localProperties.put("LANGUAGE", sourceType !=null ? sourceType.toString() : "");
illegalClassName = Config.getString("newclass.dialog.err.classNameIllegal", null, localProperties);
}
| Checks whether the text field contains a valid class name. It will send
| the relevant events to notify ValidityListeners.
| Verifies that the TextField doesn't contain the name of an existing class.
|
| @return True if the class name is valid, false otherwise.
|
public boolean checkValidity()
{
buildTheErrorMessage();
String className = textProperty.get();
boolean valid = !className.isEmpty() && JavaNames.isIdentifier(className) && !classNameExist(className) && !isGreenfootClassName(className);
message = valid || className.isEmpty() ? "" :
(classNameExist(className) || isGreenfootClassName(className) ? classExists : illegalClassName);
return valid;
}
private boolean isGreenfootClassName(String className)
{
return className.equals("Actor") || className.equals("World");
}
| Returns true if a class with the given name already exists.
|
| @param className
| @return
|
private boolean classNameExist(String className)
{
return pkg.getTarget(className) != null;
}
}
top,
use,
map,
class ClassNameVerifier
. ClassNameVerifier
. getMessage
. buildTheErrorMessage
. checkValidity
. isGreenfootClassName
. classNameExist
84 neLoCode
+ 22 LoComm