public MyStatusPanel(String id, UserProfile userProfile) {
    super(id);

    log.debug("MyStatusPanel()");

    // get info
    final String displayName = userProfile.getDisplayName();
    final String userId = userProfile.getUserUuid();

    // if superUser and proxied, can't update
    boolean editable = true;
    if (sakaiProxy.isSuperUserAndProxiedToUser(userId)) {
      editable = false;
    }

    // name
    Label profileName = new Label("profileName", displayName);
    add(profileName);

    // status component
    status = new ProfileStatusRenderer("status", userId, null, "tiny");
    status.setOutputMarkupId(true);
    add(status);

    WebMarkupContainer statusFormContainer = new WebMarkupContainer("statusFormContainer");

    // setup SimpleText object to back the single form field
    StringModel stringModel = new StringModel();

    // status form
    Form form = new Form("form", new Model(stringModel));
    form.setOutputMarkupId(true);

    // status field
    final TextField statusField =
        new TextField("message", new PropertyModel(stringModel, "string"));
    statusField.setOutputMarkupPlaceholderTag(true);
    form.add(statusField);

    // link the status textfield field with the focus/blur function via this dynamic js
    // also link with counter
    StringHeaderContributor statusJavascript =
        new StringHeaderContributor(
            "<script type=\"text/javascript\">"
                + "$(document).ready( function(){"
                + "autoFill('#"
                + statusField.getMarkupId()
                + "', '"
                + defaultStatus
                + "');"
                + "countChars('#"
                + statusField.getMarkupId()
                + "');"
                + "});"
                + "</script>");
    add(statusJavascript);

    // clear link
    final AjaxFallbackLink clearLink =
        new AjaxFallbackLink("clearLink") {
          private static final long serialVersionUID = 1L;

          public void onClick(AjaxRequestTarget target) {
            // clear status, hide and repaint
            if (statusLogic.clearUserStatus(userId)) {
              status.setVisible(false); // hide status
              this.setVisible(false); // hide clear link
              target.addComponent(status);
              target.addComponent(this);
            }
          }
        };
    clearLink.setOutputMarkupPlaceholderTag(true);
    clearLink.add(new Label("clearLabel", new ResourceModel("link.status.clear")));

    // set visibility of clear link based on status and if it's editable
    if (!status.isVisible() || !editable) {
      clearLink.setVisible(false);
    }
    add(clearLink);

    // submit button
    IndicatingAjaxButton submitButton =
        new IndicatingAjaxButton("submit", form) {

          private static final long serialVersionUID = 1L;

          protected void onSubmit(AjaxRequestTarget target, Form form) {

            // get the backing model
            StringModel stringModel = (StringModel) form.getModelObject();

            // get userId from sakaiProxy
            String userId = sakaiProxy.getCurrentUserId();

            // get the status. if its the default text, do not update, although we should clear the
            // model
            String statusMessage = StringUtils.trim(stringModel.getString());
            if (StringUtils.isBlank(statusMessage)
                || StringUtils.equals(statusMessage, defaultStatus)) {
              log.warn(
                  "Status for userId: "
                      + userId
                      + " was not updated because they didn't enter anything.");
              return;
            }

            // save status from userProfile
            if (statusLogic.setUserStatus(userId, statusMessage)) {
              log.info("Saved status for: " + userId);

              // post update event
              sakaiProxy.postEvent(
                  ProfileConstants.EVENT_STATUS_UPDATE, "/profile/" + userId, true);

              // update twitter
              externalIntegrationLogic.sendMessageToTwitter(userId, statusMessage);

              // post to walls if wall enabled
              if (true == sakaiProxy.isWallEnabledGlobally()) {
                wallLogic.addNewStatusToWall(statusMessage, sakaiProxy.getCurrentUserId());
              }

              // repaint status component
              ProfileStatusRenderer newStatus =
                  new ProfileStatusRenderer("status", userId, null, "tiny");
              newStatus.setOutputMarkupId(true);
              status.replaceWith(newStatus);
              newStatus.setVisible(true);

              // also show the clear link
              clearLink.setVisible(true);

              if (target != null) {
                target.addComponent(newStatus);
                target.addComponent(clearLink);
                status = newStatus; // update reference

                // reset the field
                target.appendJavascript(
                    "autoFill('#" + statusField.getMarkupId() + "', '" + defaultStatus + "');");

                // reset the counter
                target.appendJavascript("countChars('#" + statusField.getMarkupId() + "');");
              }

            } else {
              log.error("Couldn't save status for: " + userId);
              String js =
                  "alert('Failed to save status. If the problem persists, contact your system administrator.');";
              target.prependJavascript(js);
            }
          }
        };
    submitButton.setModel(new ResourceModel("button.sayit"));
    form.add(submitButton);

    // add form to container
    statusFormContainer.add(form);

    // if not editable, hide the entire form
    if (!editable) {
      statusFormContainer.setVisible(false);
    }

    add(statusFormContainer);
  }
Exemple #2
0
 public void enableName(AjaxRequestTarget target) {
   target.appendJavascript(
       "document.getElementById('" + name.getMarkupId() + "').disabled='false'");
 }