/**
  * Gets fired off when the avatar ID is changed. param inAvatarId the avatar ID.
  *
  * @param inAvatarId the avatar id.
  * @param setPersonAvatar if the person's avatar should be changed.
  */
 public void onAvatarIdChanged(final String inAvatarId, final boolean setPersonAvatar) {
   onAvatarIdChanged(inAvatarId, (inAvatarId != null), (inAvatarId != null), setPersonAvatar);
 }
  /**
   * Create an avatar upload form element.
   *
   * @param label the label of the element.
   * @param desc the description.
   * @param servletPath the path to hit to upload the image
   * @param inProcessor the processor.
   * @param inStrategy the strategy.
   */
  public AvatarUploadFormElement(
      final String label,
      final String desc,
      final String servletPath,
      final ActionProcessor inProcessor,
      final ImageUploadStrategy inStrategy) {
    description = desc;
    strategy = inStrategy;
    Boolean resizeable = strategy.isResizable();

    errorBox = new FlowPanel();
    errorBox.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formErrorBox());
    errorBox.add(
        new Label(
            "There was an error uploading your image. Please be sure "
                + "that your photo is under 4MB and is a PNG, JPG, or GIF."));

    errorBox.setVisible(false);

    this.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formAvatarUpload());
    this.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formElement());

    processor = inProcessor;
    // AvatarEntity Entity = inEntity;

    uploadForm.setAction(servletPath);

    uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
    uploadForm.setMethod(FormPanel.METHOD_POST);

    Label photoLabel = new Label(label);
    photoLabel.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formLabel());
    panel.add(photoLabel);

    FlowPanel avatarModificationPanel = new FlowPanel();
    avatarModificationPanel.addStyleName(
        StaticResourceBundle.INSTANCE.coreCss().avatarModificationPanel());

    avatarContainer.addStyleName(StaticResourceBundle.INSTANCE.coreCss().avatarContainer());
    avatarModificationPanel.add(avatarContainer);

    FlowPanel photoButtonPanel = new FlowPanel();
    photoButtonPanel.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formPhotoButtonPanel());

    if (resizeable) {
      editButton = new Anchor("Resize");
      editButton.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formResizeButton());
      editButton.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formButton());
      photoButtonPanel.add(editButton);
    }

    deleteButton = new Anchor("Delete");
    deleteButton.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formDeleteButton());
    deleteButton.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formButton());
    photoButtonPanel.add(deleteButton);

    avatarModificationPanel.add(photoButtonPanel);
    panel.add(avatarModificationPanel);

    FlowPanel uploadPanel = new FlowPanel();
    uploadPanel.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formUploadPanel());
    uploadPanel.add(errorBox);

    // Wrapping the FileUpload because otherwise IE7 shifts it way
    // to the right. I couldn't figure out why,
    // but for whatever reason, this works.
    FlowPanel fileUploadWrapper = new FlowPanel();
    FileUpload upload = new FileUpload();
    upload.setName("imageUploadFormElement");
    upload.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formAvatarUpload());
    fileUploadWrapper.add(upload);
    uploadPanel.add(fileUploadWrapper);

    uploadPanel.add(new Label(description));
    Anchor submitButton = new Anchor("");
    submitButton.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formUploadButton());
    submitButton.addStyleName(StaticResourceBundle.INSTANCE.coreCss().formButton());

    uploadPanel.add(submitButton);

    hiddenImage = new Image();
    hiddenImage.addStyleName(StaticResourceBundle.INSTANCE.coreCss().avatarHiddenOriginal());
    uploadPanel.add(hiddenImage);

    panel.add(uploadPanel);

    submitButton.addClickHandler(
        new ClickHandler() {
          public void onClick(final ClickEvent event) {
            uploadForm.submit();
          }
        });

    uploadForm.setWidget(panel);
    this.add(uploadForm);

    uploadForm.addSubmitCompleteHandler(
        new SubmitCompleteHandler() {
          public void onSubmitComplete(final SubmitCompleteEvent ev) {
            String result = ev.getResults().replaceAll("\\<.*?\\>", "");
            final boolean fail = "fail".equals(result);
            errorBox.setVisible(fail);
            if (!fail) {
              String[] results = result.split(",");
              if (results.length >= 4) {
                strategy.setX(Integer.parseInt(results[1]));
                strategy.setY(Integer.parseInt(results[2]));
                strategy.setCropSize(Integer.parseInt(results[3]));
              }
              onAvatarIdChanged(results[0], strategy.getEntityType() == EntityType.PERSON);
            }
          }
        });

    if (editButton != null) {
      editButton.addClickHandler(
          new ClickHandler() {
            public void onClick(final ClickEvent inArg0) {
              // Since the size of the image is required before we can correctly show the
              // resize dialog, this method determines the avatar url and sets image url.
              // The load event of that image being loaded will kick off the resize modal.
              AvatarUrlGenerator urlGenerator = new AvatarUrlGenerator(EntityType.PERSON);
              hiddenImage.setUrl(urlGenerator.getOriginalAvatarUrl(avatarId));
            }
          });
      hiddenImage.addLoadHandler(
          new LoadHandler() {
            public void onLoad(final LoadEvent inEvent) {
              imageCropDialog =
                  new ImageCropContent(
                      strategy,
                      processor,
                      avatarId,
                      new Command() {
                        public void execute() {
                          onAvatarIdChanged(
                              strategy.getImageId(), strategy.getEntityType() == EntityType.PERSON);
                        }
                      },
                      hiddenImage.getWidth() + "px",
                      hiddenImage.getHeight() + "px");

              Dialog dialog = new Dialog(imageCropDialog);
              dialog.showCentered();
            }
          });
    }

    if (strategy.getImageType().equals(ImageType.BANNER)) {
      onAvatarIdChanged(
          strategy.getImageId(),
          strategy.getId().equals(strategy.getImageEntityId()),
          true,
          strategy.getEntityType() == EntityType.PERSON);
    } else {
      onAvatarIdChanged(strategy.getImageId(), strategy.getEntityType() == EntityType.PERSON);
    }

    deleteButton.addClickHandler(
        new ClickHandler() {
          @SuppressWarnings("unchecked")
          public void onClick(final ClickEvent event) {
            if (new WidgetJSNIFacadeImpl()
                .confirm("Are you sure you want to delete your current photo?")) {
              strategy.getDeleteAction().delete(strategy.getDeleteParam());
            }
          }
        });

    Session.getInstance()
        .getEventBus()
        .addObserver(
            ClearUploadedImageEvent.class,
            new Observer<ClearUploadedImageEvent>() {
              public void update(final ClearUploadedImageEvent event) {
                if (event.getImageType().equals(strategy.getImageType())
                    && event.getEntityType().equals(strategy.getEntityType())) {
                  if (event.getImageType().equals(ImageType.BANNER)) {
                    onAvatarIdChanged(
                        event.getEntity().getBannerId(),
                        strategy.getId().equals(event.getEntity().getBannerEntityId()),
                        true,
                        strategy.getEntityType() == EntityType.PERSON);
                  } else {
                    onAvatarIdChanged(null, strategy.getEntityType() == EntityType.PERSON);
                  }
                }
              }
            });
  }