Esempio n. 1
0
  /** @see FacesContext#addMessage(String, javax.faces.application.FacesMessage) */
  public void addMessage(String clientId, FacesMessage message) {
    assertNotReleased();
    // Validate our preconditions
    Util.notNull("message", message);

    if (maxSeverity == null) {
      maxSeverity = message.getSeverity();
    } else {
      Severity sev = message.getSeverity();
      if (sev.getOrdinal() > maxSeverity.getOrdinal()) {
        maxSeverity = sev;
      }
    }

    if (componentMessageLists == null) {
      componentMessageLists = new LinkedHashMap<String, List<FacesMessage>>();
    }

    // Add this message to our internal queue
    List<FacesMessage> list = componentMessageLists.get(clientId);
    if (list == null) {
      list = new ArrayList<FacesMessage>();
      componentMessageLists.put(clientId, list);
    }
    list.add(message);
    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine(
          "Adding Message[sourceId="
              + (clientId != null ? clientId : "<<NONE>>")
              + ",summary="
              + message.getSummary()
              + ")");
    }
  }
 protected void encodeScript(FacesContext facesContext, UIComponent component) throws IOException {
   JavaScriptService javaScriptService = ServiceTracker.getService(JavaScriptService.class);
   JSFunction messageObject = new JSObject(getJSClassName(), component.getClientId(facesContext));
   Map<String, Object> attributes = component.getAttributes();
   Builder<String, Object> parametersBuilder = ImmutableMap.builder();
   String forId = (String) attributes.get("for");
   RendererUtils rendererUtils = RendererUtils.getInstance();
   if (!Strings.isNullOrEmpty(forId)) {
     UIComponent target = rendererUtils.findComponentFor(component, forId);
     if (null != target) {
       parametersBuilder.put("forComponentId", target.getClientId(facesContext));
     }
   }
   Severity level = getLevel(component);
   if (FacesMessage.SEVERITY_INFO != level) {
     parametersBuilder.put("level", level.getOrdinal());
   }
   if (!rendererUtils.isBooleanAttribute(component, "showSummary")) {
     parametersBuilder.put("showSummary", false);
   }
   if (rendererUtils.isBooleanAttribute(component, "showDetail")) {
     parametersBuilder.put("showDetail", true);
   }
   if (rendererUtils.isBooleanAttribute(component, "tooltip")) {
     parametersBuilder.put("tooltip", true);
   }
   if (isComponentMessages(component)) {
     parametersBuilder.put("isMessages", true);
   }
   messageObject.addParameter(parametersBuilder.build());
   // RendererUtils.getInstance().writeScript(facesContext, component, messageObject);
   javaScriptService.addPageReadyScript(facesContext, messageObject);
 }
  public static String toCssFriendly(Severity severity) {
    if (severity.equals(SEVERITY_INFO)) {
      return "info";
    } else if (severity.equals(SEVERITY_WARN)) {
      return "warn";
    } else if (severity.equals(SEVERITY_ERROR)) {
      return "error";
    } else if (severity.equals(SEVERITY_FATAL)) {
      return "fatal";
    }

    throw new IllegalStateException("Unexpected message severity: " + severity.toString());
  }
Esempio n. 4
0
 /** @see javax.faces.context.FacesContext#getMaximumSeverity() */
 public Severity getMaximumSeverity() {
   assertNotReleased();
   Severity result = null;
   if (componentMessageLists != null && !(componentMessageLists.isEmpty())) {
     for (Iterator<FacesMessage> i = new ComponentMessagesIterator(componentMessageLists);
         i.hasNext(); ) {
       Severity severity = i.next().getSeverity();
       if (result == null || severity.compareTo(result) > 0) {
         result = severity;
       }
       if (result == FacesMessage.SEVERITY_FATAL) {
         break;
       }
     }
   }
   return result;
 }
  @Override
  public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
    if (!component.isRendered()) {
      return;
    }

    Growl uiGrowl = (Growl) component;
    ResponseWriter writer = facesContext.getResponseWriter();

    String clientId = uiGrowl.getClientId(facesContext);
    Iterator<FacesMessage> allMessages =
        uiGrowl.isGlobalOnly() ? facesContext.getMessages(null) : facesContext.getMessages();
    Map<String, List<FacesMessage>> messages = new HashMap<String, List<FacesMessage>>();
    messages.put("info", new ArrayList<FacesMessage>()); // Bootstrap info
    messages.put("warn", new ArrayList<FacesMessage>()); // Bootstrap warning
    messages.put("error", new ArrayList<FacesMessage>()); // Bootstrap Error
    messages.put("fatal", new ArrayList<FacesMessage>()); // Bootstrap Success

    while (allMessages.hasNext()) {

      FacesMessage message = allMessages.next();
      Severity severity = message.getSeverity();
      if (message.isRendered() && !uiGrowl.isRedisplay()) {
        continue;
      }

      if (severity.equals(FacesMessage.SEVERITY_INFO)) messages.get("info").add(message);
      else if (severity.equals(FacesMessage.SEVERITY_WARN)) messages.get("warn").add(message);
      else if (severity.equals(FacesMessage.SEVERITY_ERROR)) messages.get("error").add(message);
      else if (severity.equals(FacesMessage.SEVERITY_FATAL)) messages.get("fatal").add(message);
    }

    writer.startElement("script", uiGrowl);
    writer.writeAttribute("id", clientId, "id");
    writer.writeText("$(function() { ", null);
    for (String severity : messages.keySet()) {
      List<FacesMessage> severityMessages = messages.get(severity);
      if (severityMessages.size() > 0) {
        encodeSeverityMessages(facesContext, uiGrowl, severity, severityMessages);
      }
    }
    writer.writeText("});", null);

    writer.endElement("script");
  }
  public String getMaxSeverity() {
    Severity maxSeverity = null;
    Iterator<FacesMessage> msgs = FacesContext.getCurrentInstance().getMessages();

    while (msgs.hasNext()) {
      if (maxSeverity == null) {
        maxSeverity = FacesMessage.SEVERITY_INFO;
      }

      Severity currentSeverity = msgs.next().getSeverity();
      if (currentSeverity.getOrdinal() > maxSeverity.getOrdinal()) {
        maxSeverity = currentSeverity;
      }
    }

    if (maxSeverity != null) {
      return MessageUtil.toCssFriendly(maxSeverity);
    }

    return "";
  }