Skip to content

Commit

Permalink
Updated ValidationMessage class to support ok and info severity
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonScholz committed Mar 18, 2019
1 parent afee385 commit 294825a
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 13 deletions.
Expand Up @@ -4,4 +4,8 @@

.warning {
-fx-effect: dropshadow(three-pass-box, gold, 14, 0, 0, 0);
}

.info {
-fx-effect: dropshadow(three-pass-box, rgba(196, 208, 239, 0.7), 14, 0, 0, 0);
}
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2018 ControlsFX
* Copyright (c) 2014, 2019 ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -30,6 +30,8 @@
* Defines severity of validation messages
*/
public enum Severity {
OK,
INFO,
WARNING,
ERROR
}
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2015, ControlsFX
* Copyright (c) 2014, 2019, ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -84,6 +84,35 @@ public static ValidationMessage error( Control target, String text ) {
public static ValidationMessage warning( Control target, String text ) {
return new SimpleValidationMessage(target, text, Severity.WARNING);
}

/**
* Factory method to create a simple info message
* @param target message target
* @param text message text
* @return info message
*/
public static ValidationMessage info( Control target , String text ) {
return new SimpleValidationMessage(target, text, Severity.INFO);
}

/**
* Factory method to create a simple ok message
* @param target message target
* @param text message text
* @return ok message
*/
public static ValidationMessage ok( Control target , String text ) {
return new SimpleValidationMessage(target, text, Severity.OK);
}

/**
* Factory method to create a simple ok message
* @param target message target
* @return ok message
*/
public static ValidationMessage ok( Control target ) {
return new SimpleValidationMessage(target, "", Severity.OK);
}

@Override default public int compareTo(ValidationMessage msg) {
return msg == null || getTarget() != msg.getTarget() ? -1: getSeverity().compareTo(msg.getSeverity());
Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2015, ControlsFX
* Copyright (c) 2014, 2019, ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -60,6 +60,7 @@ public class GraphicValidationDecoration extends AbstractValidationDecoration {

private static final Image ERROR_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/decoration-error.png").toExternalForm()); //$NON-NLS-1$
private static final Image WARNING_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/decoration-warning.png").toExternalForm()); //$NON-NLS-1$
private static final Image INFO_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/decoration-info.png").toExternalForm()); //$NON-NLS-1$
private static final Image REQUIRED_IMAGE = new Image(GraphicValidationDecoration.class.getResource("/impl/org/controlsfx/control/validation/required-indicator.png").toExternalForm()); //$NON-NLS-1$

private static final String SHADOW_EFFECT = "-fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.8), 10, 0, 0, 0);"; //$NON-NLS-1$
Expand All @@ -72,6 +73,9 @@ public class GraphicValidationDecoration extends AbstractValidationDecoration {
private static final String WARNING_TOOLTIP_EFFECT = POPUP_SHADOW_EFFECT + TOOLTIP_COMMON_EFFECTS
+ "-fx-background-color: FFFFCC; -fx-text-fill: CC9900; -fx-border-color: CC9900;"; //$NON-NLS-1$

private static final String INFO_TOOLTIP_EFFECT = POPUP_SHADOW_EFFECT + TOOLTIP_COMMON_EFFECTS
+ "-fx-background-color: c4d0ef; -fx-text-fill: FFFFFF; -fx-border-color: a8c8ff;"; //$NON-NLS-1$

/**
* Creates default instance
*/
Expand All @@ -80,17 +84,25 @@ public GraphicValidationDecoration() {
}

// TODO write javadoc that users should override these methods to customise
// the error / warning / success nodes to use
// the error / warning / success nodes to use
/**
* @deprecated See {@link #getGraphicBySeverity(Severity)} method
*/
@Deprecated
protected Node createErrorNode() {
return new ImageView(ERROR_IMAGE);
}

/**
* @deprecated See {@link #getGraphicBySeverity(Severity)} method
*/
@Deprecated
protected Node createWarningNode() {
return new ImageView(WARNING_IMAGE);
}

protected Node createDecorationNode(ValidationMessage message) {
Node graphic = Severity.ERROR == message.getSeverity() ? createErrorNode() : createWarningNode();
Node graphic = getGraphicBySeverity(message.getSeverity());
graphic.setStyle(SHADOW_EFFECT);
Label label = new Label();
label.setGraphic(graphic);
Expand All @@ -99,14 +111,36 @@ protected Node createDecorationNode(ValidationMessage message) {
return label;
}

protected Node getGraphicBySeverity(Severity severity) {
switch (severity) {
case ERROR:
return createErrorNode();
case WARNING:
return createWarningNode();
default:
return new ImageView(INFO_IMAGE);
}
}

protected Tooltip createTooltip(ValidationMessage message) {
Tooltip tooltip = new Tooltip(message.getText());
tooltip.setOpacity(.9);
tooltip.setAutoFix(true);
tooltip.setStyle( Severity.ERROR == message.getSeverity()? ERROR_TOOLTIP_EFFECT: WARNING_TOOLTIP_EFFECT);
tooltip.setStyle(getStyleBySeverity(message.getSeverity()));
return tooltip;
}

protected String getStyleBySeverity(Severity severity) {
switch (severity) {
case ERROR:
return ERROR_TOOLTIP_EFFECT;
case WARNING:
return WARNING_TOOLTIP_EFFECT;
default:
return INFO_TOOLTIP_EFFECT;
}
}

/**
* {@inheritDoc}
*/
Expand Down
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2014, 2015, ControlsFX
* Copyright (c) 2014, 2019, ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -34,7 +34,6 @@

import org.controlsfx.control.decoration.Decoration;
import org.controlsfx.control.decoration.StyleClassDecoration;
import org.controlsfx.validation.Severity;
import org.controlsfx.validation.ValidationMessage;

/**
Expand All @@ -48,6 +47,8 @@ public class StyleClassValidationDecoration extends AbstractValidationDecoration

private final String errorClass;
private final String warningClass;
private final String infoClass;
private final String okClass;

/**
* Creates a default instance of a decorator
Expand All @@ -62,15 +63,42 @@ public StyleClassValidationDecoration() {
* @param warningClass class name for warning decoration
*/
public StyleClassValidationDecoration(String errorClass, String warningClass) {
this(errorClass, warningClass, null, null);
}

/**
* Creates an instance of validator using custom class names
* @param errorClass class name for error decoration
* @param warningClass class name for warning decoration
* @param infoClass class name for info decoration
* @param okClass class name for ok decoration
*/
public StyleClassValidationDecoration(String errorClass, String warningClass, String infoClass, String okClass) {
this.errorClass = errorClass != null? errorClass : "error"; //$NON-NLS-1$
this.warningClass = warningClass != null? warningClass : "warning"; //$NON-NLS-1$
this.infoClass = infoClass != null? infoClass : "info"; //$NON-NLS-1$
this.okClass = okClass != null? okClass : "ok"; //$NON-NLS-1$
}


@Override
protected Collection<Decoration> createValidationDecorations(ValidationMessage message) {
return Arrays.asList(new StyleClassDecoration( Severity.ERROR == message.getSeverity()? errorClass:warningClass));
}
@Override
protected Collection<Decoration> createValidationDecorations(ValidationMessage message) {
String validationClass = infoClass;
switch (message.getSeverity()) {
case ERROR:
validationClass = errorClass;
break;
case WARNING:
validationClass = warningClass;
break;
case OK:
validationClass = okClass;
break;
default:
validationClass = infoClass;
break;
}
return Arrays.asList(new StyleClassDecoration( validationClass ));
}

@Override
protected Collection<Decoration> createRequiredDecorations(Control target) {
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 294825a

Please sign in to comment.