Skip to content

Commit

Permalink
#15: Add validation support for input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Geisselmeier committed Feb 5, 2016
1 parent 08e25c4 commit 128c450
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
package org.levigo.jadice.server.converterclient.gui.clusterhealth;

import org.apache.log4j.Logger;
import org.controlsfx.validation.ValidationSupport;
import org.levigo.jadice.server.converterclient.Preferences;
import org.levigo.jadice.server.converterclient.util.validation.EmptyStringValidator;
import org.levigo.jadice.server.converterclient.util.validation.IntegerValidator;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class AddInstanceController {

private static final Logger LOGGER = Logger.getLogger(AddInstanceController.class);

private final ValidationSupport validation = new ValidationSupport();

@FXML
private TextField hostname;

@FXML
private TextField port;

@FXML
private Button addInstance;

@FXML
protected void initialize() {
ValidationSupport.setRequired(hostname, true);
ValidationSupport.setRequired(port, true);
validation.registerValidator(hostname, new EmptyStringValidator());
validation.registerValidator(port, new IntegerValidator());

validation.invalidProperty().addListener((target, oldValue, newValue) -> {
LOGGER.debug("Invalid? " + oldValue + " -> " + newValue);
addInstance.setDisable(newValue);
});
validation.initInitialDecoration();
validation.redecorate();
}

@FXML
private void onAddInstance() {
final String jmxName = hostname.getText() + ":" + port.getText();
LOGGER.info(String.format("Adding %s to monitor cluster health", jmxName));
Preferences.clusterHealthProperty().getValue().instances.add(jmxName);
reset();
}

private void reset() {
hostname.clear();
port.clear();
hostname.requestFocus();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.levigo.jadice.server.converterclient.util.validation;

import static org.levigo.jadice.server.converterclient.util.UiUtil.getUiResources;

import org.controlsfx.validation.ValidationResult;
import org.controlsfx.validation.Validator;

import com.levigo.util.base.Strings;

import javafx.scene.control.Control;

public class EmptyStringValidator implements Validator<String> {

private static final String MSG = getUiResources().getString("validator.null-value.message");

@Override
public ValidationResult apply(Control t, String u) {
return ValidationResult.fromErrorIf(t, MSG, Strings.emptyTrim(u));
}

}
8 changes: 4 additions & 4 deletions src/main/resources/fxml/AddClusterInstance.fxml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
Expand All @@ -18,11 +18,11 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Label text="%cluster-health.add-instance.hostname" labelFor="$hostname">
<Label text="%cluster-health.add-instance.hostname">
<GridPane.margin>
<Insets />
</GridPane.margin></Label>
<Label text="%cluster-health.add-instance.port" GridPane.rowIndex="1" labelFor="$port">
<Label text="%cluster-health.add-instance.port" GridPane.rowIndex="1">
<GridPane.margin>
<Insets bottom="2.0" />
</GridPane.margin></Label>
Expand All @@ -37,7 +37,7 @@
<bottom>
<HBox alignment="CENTER_RIGHT">
<children>
<Button mnemonicParsing="false" onAction="#onAddInstance" text="%cluster-health.add-instance.add" />
<Button fx:id="addInstance" defaultButton="true" mnemonicParsing="false" onAction="#onAddInstance" text="%cluster-health.add-instance.add" />
</children>
<BorderPane.margin>
<Insets />
Expand Down

0 comments on commit 128c450

Please sign in to comment.