-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15: Add validation support for input fields
- Loading branch information
Benjamin Geisselmeier
committed
Feb 5, 2016
1 parent
08e25c4
commit 128c450
Showing
3 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ava/org/levigo/jadice/server/converterclient/gui/clusterhealth/AddInstanceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...n/java/org/levigo/jadice/server/converterclient/util/validation/EmptyStringValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters