Exemplo n.º 1
0
  private void deliverPostRestoreStateEvent(FacesContext facesContext) throws FacesException {
    UIViewRoot root = facesContext.getViewRoot();
    final PostRestoreStateEvent postRestoreStateEvent = new PostRestoreStateEvent(root);
    try {
      Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_ITERATION);
      VisitContext visitContext = VisitContext.createVisitContext(facesContext, null, hints);
      root.visitTree(
          visitContext,
          new VisitCallback() {

            public VisitResult visit(VisitContext context, UIComponent target) {
              postRestoreStateEvent.setComponent(target);
              target.processEvent(postRestoreStateEvent);
              //noinspection ReturnInsideFinallyBlock
              return VisitResult.ACCEPT;
            }
          });
    } catch (AbortProcessingException e) {
      facesContext
          .getApplication()
          .publishEvent(
              facesContext,
              ExceptionQueuedEvent.class,
              new ExceptionQueuedEventContext(facesContext, e, null, PhaseId.RESTORE_VIEW));
    }
  }
Exemplo n.º 2
0
  /** Method process chart tags, it collects chart options and data. */
  @Override
  public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    super.encodeBegin(context, component);

    AbstractChart chart = (AbstractChart) component;

    VisitChart visitCallback = new VisitChart(chart);
    // copy attributes to parent tag and process data
    chart.visitTree(
        VisitContext.createVisitContext(FacesContext.getCurrentInstance()), visitCallback);

    // store data to parent tag
    component.getAttributes().put("chartData", visitCallback.getData());

    if (!visitCallback.isDataEmpty()) {
      component.getAttributes().put("charttype", visitCallback.getChartType());
      component.getAttributes().put("xtype", axisDataTypeToString(visitCallback.getKeyType()));
      component.getAttributes().put("ytype", axisDataTypeToString(visitCallback.getValType()));
    }

    // set flag whether request to server should be sent
    boolean anyServerSideListener = chart.getPlotClickListener() != null ? true : false;
    if (!anyServerSideListener) {
      // check if there is particular series listener
      List<MethodExpression> listeners = visitCallback.getParticularSeriesListeners();
      for (MethodExpression methodExpression : listeners) {
        if (methodExpression != null) {
          anyServerSideListener = true;
          break;
        }
      }
    }
    component.getAttributes().put("serverSideListener", anyServerSideListener);

    // client-side handlers for particular series
    component.getAttributes().put("handlers", visitCallback.getSeriesSpecificHandlers());

    // server-side listeners for particular series
    component
        .getAttributes()
        .put("particularSeriesListeners", visitCallback.getParticularSeriesListeners());
  }
Exemplo n.º 3
0
  public void testSpecificIdTraversal() throws Exception {
    buildTree();
    UIViewRoot root = getFacesContext().getViewRoot();
    final StringBuilder builder = new StringBuilder();

    HashSet ids = new HashSet();
    ids.add("form:panel0:data:3:output0");
    ids.add("form:panel1:data:0:output0");
    root.visitTree(
        VisitContext.createVisitContext(getFacesContext(), ids, null),
        new VisitCallback() {
          public VisitResult visit(VisitContext context, UIComponent target) {
            builder.append(target.getClientId(context.getFacesContext()) + " ");
            return VisitResult.ACCEPT;
          }
        });
    System.out.println(builder);
    String result = builder.toString().trim();
    assertEquals(result, "form:panel0:data:3:output0 form:panel1:data:0:output0");
  }
Exemplo n.º 4
0
  /**
   * PRECONDITION: the necessary factories have been installed in the ServletContext attr set.
   *
   * <p>
   *
   * <p>POSTCONDITION: The facesContext has been initialized with a tree.
   */
  public void execute(FacesContext facesContext) throws FacesException {

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Entering RestoreViewPhase");
    }
    if (null == facesContext) {
      throw new FacesException(
          MessageUtils.getExceptionMessageString(MessageUtils.NULL_CONTEXT_ERROR_MESSAGE_ID));
    }

    // If an app had explicitely set the tree in the context, use that;
    //
    UIViewRoot viewRoot = facesContext.getViewRoot();
    if (viewRoot != null) {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Found a pre created view in FacesContext");
      }
      facesContext.getViewRoot().setLocale(facesContext.getExternalContext().getRequestLocale());

      // do per-component actions
      UIViewRoot root = facesContext.getViewRoot();
      final PostRestoreStateEvent event = new PostRestoreStateEvent(root);
      try {
        root.visitTree(
            VisitContext.createVisitContext(facesContext),
            new VisitCallback() {

              public VisitResult visit(VisitContext context, UIComponent target) {
                event.setComponent(target);
                target.processEvent(event);
                return VisitResult.ACCEPT;
              }
            });
      } catch (AbortProcessingException e) {
        facesContext
            .getApplication()
            .publishEvent(
                ExceptionQueuedEvent.class, new ExceptionQueuedEventContext(facesContext, e));
      }

      if (!facesContext.isPostback()) {
        facesContext.renderResponse();
      }
      return;
    }

    // Reconstitute or create the request tree
    Map requestMap = facesContext.getExternalContext().getRequestMap();
    String viewId = (String) requestMap.get("javax.servlet.include.path_info");
    if (viewId == null) {
      viewId = facesContext.getExternalContext().getRequestPathInfo();
    }

    // It could be that this request was mapped using
    // a prefix mapping in which case there would be no
    // path_info.  Query the servlet path.
    if (viewId == null) {
      viewId = (String) requestMap.get("javax.servlet.include.servlet_path");
    }

    if (viewId == null) {
      viewId = facesContext.getExternalContext().getRequestServletPath();
    }

    if (viewId == null) {
      if (LOGGER.isLoggable(Level.WARNING)) {
        LOGGER.warning("viewId is null");
      }
      throw new FacesException(
          MessageUtils.getExceptionMessageString(MessageUtils.NULL_REQUEST_VIEW_ERROR_MESSAGE_ID));
    }

    ViewHandler viewHandler = Util.getViewHandler(facesContext);

    boolean isPostBack = (facesContext.isPostback() && !isErrorPage(facesContext));
    if (isPostBack) {
      // try to restore the view
      viewRoot = viewHandler.restoreView(facesContext, viewId);
      if (viewRoot == null) {
        if (is11CompatEnabled(facesContext)) {
          // 1.1 -> create a new view and flag that the response should
          //        be immediately rendered
          if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Postback: recreating a view for " + viewId);
          }
          viewRoot = viewHandler.createView(facesContext, viewId);
          facesContext.renderResponse();

        } else {
          Object[] params = {viewId};
          throw new ViewExpiredException(
              MessageUtils.getExceptionMessageString(
                  MessageUtils.RESTORE_VIEW_ERROR_MESSAGE_ID, params),
              viewId);
        }
      }

      facesContext.setViewRoot(viewRoot);

      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("Postback: restored view for " + viewId);
      }
    } else {
      if (LOGGER.isLoggable(Level.FINE)) {
        LOGGER.fine("New request: creating a view for " + viewId);
      }

      ViewDeclarationLanguage vdl =
          facesContext
              .getApplication()
              .getViewHandler()
              .getViewDeclarationLanguage(facesContext, viewId);

      if (vdl != null) {
        // If we have one, get the ViewMetadata...
        ViewMetadata metadata = vdl.getViewMetadata(facesContext, viewId);

        if (metadata != null) { // perhaps it's not supported
          // and use it to create the ViewRoot.  This will have, at most
          // the UIViewRoot and its metadata facet.
          viewRoot = metadata.createMetadataView(facesContext);

          // Only skip to render response if there are no view parameters
          Collection<UIViewParameter> params = ViewMetadata.getViewParameters(viewRoot);
          if (params.isEmpty()) {
            facesContext.renderResponse();
          }
        }
      } else {
        facesContext.renderResponse();
      }

      if (null == viewRoot) {
        viewRoot = (Util.getViewHandler(facesContext)).createView(facesContext, viewId);
      }
      facesContext.setViewRoot(viewRoot);
      assert (null != viewRoot);
      facesContext.getApplication().publishEvent(PostAddToViewEvent.class, viewRoot);
    }

    if (LOGGER.isLoggable(Level.FINE)) {
      LOGGER.fine("Exiting RestoreViewPhase");
    }
  }
Exemplo n.º 5
0
    @Override
    public VisitResult visit(VisitContext context, UIComponent target) {

      if (target instanceof AbstractLegend) {
        copyAttrs(target, chart, "", asList("position", "sorting"));
      } else if (target instanceof AbstractSeries) {
        AbstractSeries s = (AbstractSeries) target;
        ChartDataModel model = s.getData();
        particularSeriesListeners.add(s.getPlotClickListener());

        // Collect Series specific handlers
        Map<String, Object> optMap = new HashMap<String, Object>();
        RenderKitUtils.Attributes seriesEvents =
            attributes()
                .generic("onplothover", "onplothover", "plothover")
                .generic("onplotclick", "onplotclick", "plotclick");

        addToScriptHash(
            optMap,
            context.getFacesContext(),
            target,
            seriesEvents,
            RenderKitUtils.ScriptHashVariableWrapper.eventHandler);

        if (optMap.get("onplotclick") != null) {
          plotClickHandlers.put(new RawJSONString(optMap.get("onplotclick").toString()));
        } else {
          plotClickHandlers.put(s.getOnplotclick());
        }

        if (optMap.get("onplothover") != null) {
          plothoverHandlers.put(new RawJSONString(optMap.get("onplothover").toString()));
        } else {
          plothoverHandlers.put(s.getOnplothover());
        }
        // end collect series specific handler

        if (model == null) {
          /**
           * data model priority: if there is data model passed through data attribute use it.
           * Otherwise nested point tags are expected.
           */
          VisitSeries seriesCallback = new VisitSeries(s.getType());
          s.visitTree(
              VisitContext.createVisitContext(FacesContext.getCurrentInstance()), seriesCallback);
          model = seriesCallback.getModel();

          // if series has no data create empty model
          if (model == null) {
            switch (s.getType()) {
              case line:
                model = new NumberChartDataModel(ChartType.line);
                break;
              case bar:
                model = new NumberChartDataModel(ChartType.bar);
                break;
              case pie:
                model = new StringChartDataModel(ChartType.pie);
                break;
              default:
                break;
            }
          } else {
            nodata = false;
          }
        } else {
          nodata = false;
        }
        model.setAttributes(s.getAttributes());

        try {
          // Check model/series compatibility

          if (chartType == null && (!nodata)) {
            // if series is empty do not set types
            chartType = model.getType();
            keyType = model.getKeyType();
            valType = model.getValueType();
          } else {
            if (chartType == ChartDataModel.ChartType.pie) {
              throw new IllegalArgumentException("Pie chart supports only one series.");
            }
          }
          if (keyType != model.getKeyType() || valType != model.getValueType()) {
            throw new IllegalArgumentException("Data model is not valid for this chart type.");
          }

          data.put(model.export());
        } catch (IOException ex) {
          throw new FacesException(ex);
        }

      } else if (target instanceof AbstractXAxis) {
        copyAttrs(target, chart, "x", asList("min", "max", "pad", "label", "format"));
      } else if (target instanceof AbstractYAxis) {
        copyAttrs(target, chart, "y", asList("min", "max", "pad", "label", "format"));
      }
      return VisitResult.ACCEPT;
    }