You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CriticalNotifications.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.application;
  17. import java.io.IOException;
  18. import com.vaadin.server.SystemMessages;
  19. import com.vaadin.server.VaadinRequest;
  20. import com.vaadin.server.VaadinResponse;
  21. import com.vaadin.server.VaadinService;
  22. import com.vaadin.shared.JsonConstants;
  23. import com.vaadin.tests.components.AbstractReindeerTestUI;
  24. import com.vaadin.ui.Button;
  25. import com.vaadin.ui.CheckBox;
  26. public class CriticalNotifications extends AbstractReindeerTestUI {
  27. private SystemMessages systemMessages;
  28. private CheckBox includeDetails;
  29. @Override
  30. protected void setup(VaadinRequest request) {
  31. systemMessages = VaadinService.getCurrent()
  32. .getSystemMessages(getLocale(), request);
  33. includeDetails = new CheckBox("Include details");
  34. addComponent(includeDetails);
  35. Button sessionExpired = new Button("Session expired");
  36. addComponent(sessionExpired);
  37. sessionExpired.addClickListener(event ->
  38. showCriticalNotification(systemMessages.getSessionExpiredCaption(),
  39. systemMessages.getSessionExpiredMessage(), getDetailsMessage(),
  40. systemMessages.getSessionExpiredURL()));
  41. Button authenticationError = new Button("Authentication error");
  42. addComponent(authenticationError);
  43. authenticationError.addClickListener(event ->
  44. showCriticalNotification(systemMessages.getAuthenticationErrorCaption(),
  45. systemMessages.getAuthenticationErrorMessage(),
  46. getDetailsMessage(),
  47. systemMessages.getAuthenticationErrorURL()));
  48. Button communicationError = new Button("Communication error");
  49. addComponent(communicationError);
  50. communicationError.addClickListener(event ->
  51. showCriticalNotification(systemMessages.getCommunicationErrorCaption(),
  52. systemMessages.getCommunicationErrorMessage(),
  53. getDetailsMessage(),
  54. systemMessages.getCommunicationErrorURL()));
  55. Button internalError = new Button("Internal error");
  56. addComponent(internalError);
  57. internalError.addClickListener(event ->
  58. showCriticalNotification(systemMessages.getInternalErrorCaption(),
  59. systemMessages.getInternalErrorMessage(), getDetailsMessage(),
  60. systemMessages.getInternalErrorURL()));
  61. Button cookiesDisabled = new Button("Cookies disabled");
  62. addComponent(cookiesDisabled);
  63. cookiesDisabled.addClickListener(event -> showCriticalNotification(
  64. systemMessages.getCookiesDisabledCaption(),
  65. systemMessages.getCookiesDisabledMessage(), getDetailsMessage(),
  66. systemMessages.getCookiesDisabledURL()));
  67. Button custom = new Button("Custom");
  68. addComponent(custom);
  69. custom.addClickListener(
  70. event ->
  71. showCriticalNotification("Custom caption", "Custom message",
  72. "Custom details", "custom url"));
  73. }
  74. protected String getDetailsMessage() {
  75. if (includeDetails.getValue()) {
  76. return "Some details for the error";
  77. }
  78. return null;
  79. }
  80. protected void showCriticalNotification(String caption, String message,
  81. String details, String url) {
  82. VaadinService service = VaadinService.getCurrent();
  83. VaadinResponse response = VaadinService.getCurrentResponse();
  84. try {
  85. service.writeStringResponse(response,
  86. JsonConstants.JSON_CONTENT_TYPE,
  87. VaadinService.createCriticalNotificationJSON(caption,
  88. message, details, url));
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. }