/** * Checks and answers whether the given list of validation messages * includes message with the specified Severity. * * @param messages the List of ValidationMessages to check * @param severity the Severity to check * @return true if the given messages list includes error messages, * false if not */ private static boolean hasSeverity( List<ValidationMessage> messages, Severity severity) { for (ValidationMessage message : messages) { if (message.severity() == severity) { return true; } } return false; }
/** * Checks and answers whether the given list of validation messages * includes message with the specified Severity. * * @param messages the List of ValidationMessages to check * @param severity the Severity to check * @return true if the given messages list includes error messages, * false if not */ private static boolean hasSeverity( List<ValidationMessage> messages, Severity severity) { for (ValidationMessage message : messages) { if (message.severity() == severity) { return true; } } return false; }
/** * Returns an unmodifiable List of ValidationMessage that * that is the sublist of message with the given Severity. * * @param messages the List of ValidationMessages to iterate * @param severity the Severity to look for * @return the sublist of error messages */ private static List<ValidationMessage> getMessagesWithSeverity( List<ValidationMessage> messages, Severity severity) { List<ValidationMessage> errorMessages = new ArrayList<ValidationMessage>(); for (ValidationMessage message : messages) { if (message.severity() == severity) { errorMessages.add(message); } } return Collections.unmodifiableList(errorMessages); }
/** * Returns the highest severity of this result's messages, * <code>Severity.OK</code> if there are no messages. * A single validation message can have only the severity * error or warning. Hence, this method returns the error severity * if there's at least one error message; and it returns * the warning severity, otherwise - assuming that there are * no other severities.<p> * * TODO: Consider changing the iteration to build the * maximum of the message severities. This would make it * easier for users that want to change this library's semantics. * For example if someone adds Severity.FATAL, this algorithm * may return an unexpected result. * * @param messages the List of ValidationMessages to check * @return the highest severity of this result's messages, * <code>Severity.OK</code> if there are no messages */ private static Severity getSeverity(List<ValidationMessage> messages) { if (messages.isEmpty()) { return Severity.OK; } for (ValidationMessage message : messages) { if (message.severity() == Severity.ERROR) { return Severity.ERROR; } } return Severity.WARNING; }
/** * Returns an unmodifiable List of ValidationMessage that * that is the sublist of message with the given Severity. * * @param messages the List of ValidationMessages to iterate * @param severity the Severity to look for * @return the sublist of error messages */ private static List<ValidationMessage> getMessagesWithSeverity( List<ValidationMessage> messages, Severity severity) { List<ValidationMessage> errorMessages = new ArrayList<ValidationMessage>(); for (ValidationMessage message : messages) { if (message.severity() == severity) { errorMessages.add(message); } } return Collections.unmodifiableList(errorMessages); }
/** * Adds a new ValidationMessage to the list of messages. * * @param validationMessage the message to add * * @throws NullPointerException if the message is {@code null} * @throws UnsupportedOperationException if the result is unmodifiable * @throws IllegalArgumentException if the severity is <code>OK</code> * * @see #addError(String) * @see #addWarning(String) */ public void add(ValidationMessage validationMessage) { assertModifiable(); if (validationMessage == null) throw new NullPointerException("The validation message must not be null."); if (validationMessage.severity() == Severity.OK) throw new IllegalArgumentException("You must not add a validation message with severity OK."); messageList.add(validationMessage); }
/** * Adds all messages from the given list to this validation result. * * @param messages the messages to be added * * @throws NullPointerException if the messages list is {@code null} * @throws UnsupportedOperationException if the result is unmodifiable * @throws IllegalArgumentException if the messages list contains * a message with severity <code>OK</code> * * @see #addAllFrom(ValidationResult) */ public void addAll(List<ValidationMessage> messages) { assertModifiable(); if (messages == null) throw new NullPointerException("The messages list must not be null."); for (ValidationMessage message : messages) { if (message.severity() == Severity.OK) { throw new IllegalArgumentException("You must not add a validation message with severity OK."); } } messageList.addAll(messages); }
private static void updateText(JLabel label, ValidationResult result) { if (result.isEmpty()) { label.setText(""); return; } Severity severity = result.getSeverity(); for (ValidationMessage message : result) { if (message.severity() == severity) { label.setText(message.formattedText()); return; } } }
/** * Returns the highest severity of this result's messages, * {@code Severity.OK} if there are no messages. * A single validation message can have only the severity * error or warning. Hence, this method returns the error severity * if there's at least one error message; and it returns * the warning severity, otherwise - assuming that there are * no other severities.<p> * * @param messages the List of ValidationMessages to check * @return the highest severity of this result's messages, * {@code Severity.OK} if there are no messages */ private static Severity getSeverity(List<ValidationMessage> messages) { Severity severity = Severity.OK; for (ValidationMessage message : messages) { severity = Severity.max(severity, message.severity()); if (severity.ordinal() == 0) { // we found the maximum severity. break; } } return severity; }
/** * Adds all messages from the given list to this validation result. * * @param messages the messages to be added * * @throws NullPointerException if the messages list is {@code null} * @throws UnsupportedOperationException if the result is unmodifiable * @throws IllegalArgumentException if the messages list contains * a message with severity {@code OK} * * @see #addAllFrom(ValidationResult) */ public void addAll(List<ValidationMessage> messages) { checkModifiable(); checkNotNull(messages, "The messages list must not be null."); for (ValidationMessage message : messages) { checkArgument(message.severity() != Severity.OK, "You must not add a validation message with severity OK."); } messageList.addAll(messages); }
/** * Adds a new ValidationMessage to the list of messages. * * @param validationMessage the message to add * * @return this validation result * * @throws NullPointerException if the message is {@code null} * @throws UnsupportedOperationException if the result is unmodifiable * @throws IllegalArgumentException if the severity is {@code OK} * * @see #addError(String) * @see #addWarning(String) */ public ValidationResult add(ValidationMessage validationMessage) { checkModifiable(); checkNotNull(validationMessage, "The validation message must not be null."); checkArgument(validationMessage.severity() != Severity.OK, "You must not add a validation message with severity OK."); messageList.add(validationMessage); return this; }
/** * In addition to the superclass behavior, this method * assumes that the value is a ValidationMessage. * It sets the renderer's icon to the one associated with the * ValidationMessage's severity and sets the renderer's text * to the message's formatted text. */ @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent( list, value, index, false, // Ignore the selection state false); // Ignore the cell focus state ValidationMessage message = (ValidationMessage) value; this.setIcon(ValidationResultViewFactory.getIcon(message.severity())); this.setText(message.formattedText()); return this; } }
/** * In addition to the superclass behavior, this method * assumes that the value is a ValidationMessage. * It sets the renderer's icon to the one associated with the * ValidationMessage's severity and sets the renderer's text * to the message's formatted text. */ @Override public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { super.getListCellRendererComponent( list, value, index, false, // Ignore the selection state false); // Ignore the cell focus state ValidationMessage message = (ValidationMessage) value; this.setIcon(ValidationResultViewFactory.getIcon(message.severity())); this.setText(message.formattedText()); return this; } }
private JDefaultToolTip createToolTip(ValidationMessage vm) { JDefaultToolTip res = null; switch (vm.severity()) { case ERROR: res = new JRedToolTip(vm.formattedText()); break; case OK: res = new JGreenToolTip(vm.formattedText()); break; case WARNING: res = new JOrangeToolTip(vm.formattedText()); break; default: res = new JRedToolTip(vm.formattedText()); break; } res.setInsetsToolTip(new Insets(2, 5, 2, 5)); return res; }