private void loadUserList(List<SimpleProjectMember> memberList) {
   for (SimpleProjectMember member : memberList) {
     this.addItem(member);
     this.setItemCaption(member, StringUtils.trim(member.getDisplayName(), 25, true));
     this.setItemIcon(
         member, UserAvatarControlFactory.createAvatarResource(member.getMemberAvatarId(), 16));
   }
 }
 public void setAvatarLink(String avatarId) {
   userAvatarIcon = UserAvatarControlFactory.createAvatarResource(avatarId, 16);
 }
  private void displayUserAvatar() {
    this.userAvatar.removeAllComponents();
    this.userAvatar.addStyleName("avatar-pass-wrapper");
    this.userAvatar.setMargin(true);
    final Image cropField =
        UserAvatarControlFactory.createUserAvatarEmbeddedComponent(
            AppContext.getUserAvatarId(), 100);

    final HorizontalLayout avatarAndPass = new HorizontalLayout();
    avatarAndPass.setSpacing(true);
    avatarAndPass.setWidth("100%");
    avatarAndPass.addComponent(cropField);
    final Button btnChangePassword =
        new Button(
            LocalizationHelper.getMessage(UserI18nEnum.BUTTON_CHANGE_PASSWORD),
            new Button.ClickListener() {
              private static final long serialVersionUID = 1L;

              @Override
              public void buttonClick(final ClickEvent event) {
                UI.getCurrent().addWindow(new PasswordChangeWindow(formItem.user));
              }
            });
    final VerticalLayout passLayout = new VerticalLayout();
    passLayout.setMargin(new MarginInfo(true, false, false, false));
    final Label userName = new Label(AppContext.getSession().getDisplayName());
    userName.setStyleName("h1");
    passLayout.addComponent(userName);
    passLayout.addComponent(btnChangePassword);
    btnChangePassword.setStyleName("link");
    passLayout.setComponentAlignment(btnChangePassword, Alignment.MIDDLE_LEFT);
    avatarAndPass.addComponent(passLayout);
    avatarAndPass.setComponentAlignment(passLayout, Alignment.TOP_LEFT);
    avatarAndPass.setExpandRatio(passLayout, 1.0f);

    this.userAvatar.addComponent(avatarAndPass);

    final UploadField avatarUploadField =
        new UploadField() {
          private static final long serialVersionUID = 1L;

          @Override
          protected void updateDisplay() {
            byte[] imageData = (byte[]) this.getValue();
            String mimeType = this.getLastMimeType();
            if (mimeType.equals("image/jpeg")) {
              imageData = ImageUtil.convertJpgToPngFormat(imageData);
              if (imageData == null) {
                throw new UserInvalidInputException("Do not support image format for avatar");
              } else {
                mimeType = "image/png";
              }
            }

            if (mimeType.equals("image/png")) {
              EventBus.getInstance()
                  .fireEvent(new ProfileEvent.GotoUploadPhoto(ProfileReadViewImpl.this, imageData));
            } else {
              throw new UserInvalidInputException(
                  "Upload file does not have valid image format. The supported formats are jpg/png");
            }
          }
        };
    avatarUploadField.setButtonCaption("Change Avatar");
    avatarUploadField.setFieldType(FieldType.BYTE_ARRAY);
    this.userAvatar.addComponent(avatarUploadField);
  }
  @SuppressWarnings("serial")
  @Override
  public void editPhoto(final byte[] imageData) {
    this.removeAllComponents();
    LOG.debug("Receive avatar upload with size: " + imageData.length);
    try {
      originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
    } catch (IOException e) {
      throw new UserInvalidInputException("Invalid image type");
    }
    originalImage = ImageUtil.scaleImage(originalImage, 650, 650);

    MHorizontalLayout previewBox =
        new MHorizontalLayout()
            .withSpacing(true)
            .withMargin(new MarginInfo(false, true, true, false))
            .withWidth("100%");

    Resource defaultPhoto =
        UserAvatarControlFactory.createAvatarResource(AppContext.getUserAvatarId(), 100);
    previewImage = new Embedded(null, defaultPhoto);
    previewImage.setWidth("100px");
    previewBox.with(previewImage).withAlign(previewImage, Alignment.TOP_LEFT);

    VerticalLayout previewBoxRight = new VerticalLayout();
    previewBoxRight.setMargin(new MarginInfo(false, true, false, true));
    Label lbPreview =
        new Label(
            "<p style='margin: 0px;'><strong>To the left is what your profile photo will look like.</strong></p>"
                + "<p style='margin-top: 0px;'>To make adjustment, you can drag around and resize the selection square below. "
                + "When you are happy with your photo, click the &ldquo;Accept&ldquo; button.</p>",
            ContentMode.HTML);
    previewBoxRight.addComponent(lbPreview);

    MHorizontalLayout controlBtns = new MHorizontalLayout();
    controlBtns.setSizeUndefined();

    Button cancelBtn =
        new Button(
            AppContext.getMessage(GenericI18Enum.BUTTON_CANCEL),
            new Button.ClickListener() {
              @Override
              public void buttonClick(ClickEvent event) {
                EventBusFactory.getInstance()
                    .post(new ProfileEvent.GotoProfileView(ProfilePhotoUploadViewImpl.this, null));
              }
            });
    cancelBtn.setStyleName(UIConstants.THEME_GRAY_LINK);

    Button acceptBtn =
        new Button(
            AppContext.getMessage(GenericI18Enum.BUTTON_ACCEPT),
            new Button.ClickListener() {
              @Override
              public void buttonClick(ClickEvent event) {
                if (scaleImageData != null && scaleImageData.length > 0) {
                  try {
                    BufferedImage image = ImageIO.read(new ByteArrayInputStream(scaleImageData));
                    UserAvatarService userAvatarService =
                        ApplicationContextUtil.getSpringBean(UserAvatarService.class);
                    userAvatarService.uploadAvatar(
                        image, AppContext.getUsername(), AppContext.getUserAvatarId());
                    Page.getCurrent().getJavaScript().execute("window.location.reload();");
                  } catch (IOException e) {
                    throw new MyCollabException("Error when saving user avatar", e);
                  }
                }
              }
            });
    acceptBtn.setStyleName(UIConstants.BUTTON_ACTION);
    acceptBtn.setIcon(FontAwesome.CHECK);

    controlBtns.with(acceptBtn, cancelBtn).alignAll(Alignment.MIDDLE_LEFT);

    previewBoxRight.addComponent(controlBtns);
    previewBoxRight.setComponentAlignment(controlBtns, Alignment.TOP_LEFT);

    previewBox.addComponent(previewBoxRight);
    previewBox.setExpandRatio(previewBoxRight, 1.0f);

    this.addComponent(previewBox);

    CssLayout cropBox = new CssLayout();
    cropBox.addStyleName(UIConstants.PHOTO_CROPBOX);
    cropBox.setWidth("100%");
    VerticalLayout currentPhotoBox = new VerticalLayout();
    Resource resource =
        new ByteArrayImageResource(ImageUtil.convertImageToByteArray(originalImage), "image/png");
    CropField cropField = new CropField(resource);
    cropField.setImmediate(true);
    cropField.setSelectionAspectRatio(1.0f);
    cropField.addValueChangeListener(
        new Property.ValueChangeListener() {

          @Override
          public void valueChange(Property.ValueChangeEvent event) {
            VCropSelection newSelection = (VCropSelection) event.getProperty().getValue();
            int x1 = newSelection.getXTopLeft();
            int y1 = newSelection.getYTopLeft();
            int x2 = newSelection.getXBottomRight();
            int y2 = newSelection.getYBottomRight();
            if (x2 > x1 && y2 > y1) {
              BufferedImage subImage = originalImage.getSubimage(x1, y1, (x2 - x1), (y2 - y1));
              ByteArrayOutputStream outStream = new ByteArrayOutputStream();
              try {
                ImageIO.write(subImage, "png", outStream);
                scaleImageData = outStream.toByteArray();
                displayPreviewImage();
              } catch (IOException e) {
                LOG.error("Error while scale image: ", e);
              }
            }
          }
        });
    currentPhotoBox.setWidth("650px");
    currentPhotoBox.setHeight("650px");

    currentPhotoBox.addComponent(cropField);

    cropBox.addComponent(currentPhotoBox);

    this.addComponent(previewBox);
    this.addComponent(cropBox);
    this.setExpandRatio(cropBox, 1.0f);
  }
Example #5
0
 private void setUserInfo() {
   userAvatar.setSource(
       UserAvatarControlFactory.createAvatarResource(AppContext.getUserAvatarId(), 24));
   userName.setCaption(AppContext.getSession().getDisplayName());
 }
    @Override
    public Component generateRow(final SimpleMessage message, final int rowIndex) {
      final HorizontalLayout messageLayout = new HorizontalLayout();
      messageLayout.setStyleName("message");
      messageLayout.setSpacing(true);
      if (message.getIsstick() != null && message.getIsstick()) {
        messageLayout.addStyleName("important-message");
      }
      messageLayout.setWidth("100%");
      VerticalLayout userBlock = new VerticalLayout();
      userBlock.setDefaultComponentAlignment(Alignment.TOP_CENTER);
      userBlock.setWidth("80px");
      userBlock.setSpacing(true);
      userBlock.addComponent(
          UserAvatarControlFactory.createUserAvatarButtonLink(
              message.getPostedUserAvatarId(), message.getFullPostedUserName()));
      Label userName = new Label(message.getFullPostedUserName());
      userName.setStyleName("user-name");
      userBlock.addComponent(userName);
      messageLayout.addComponent(userBlock);

      final CssLayout rowLayout = new CssLayout();
      rowLayout.setStyleName("message-container");
      rowLayout.setWidth("100%");
      final Button title =
          new Button(
              message.getTitle(),
              new Button.ClickListener() {
                private static final long serialVersionUID = 1L;

                @Override
                public void buttonClick(final ClickEvent event) {
                  EventBus.getInstance()
                      .fireEvent(
                          new MessageEvent.GotoRead(MessageListViewImpl.this, message.getId()));
                }
              });

      title.setWidth("550px");
      title.setStyleName("link");
      title.addStyleName(UIConstants.WORD_WRAP);

      final HorizontalLayout messageHeader = new HorizontalLayout();
      messageHeader.setStyleName("message-header");
      messageHeader.setMargin(new MarginInfo(true, true, false, true));
      final VerticalLayout leftHeader = new VerticalLayout();

      title.addStyleName("message-title");
      leftHeader.addComponent(title);

      final HorizontalLayout rightHeader = new HorizontalLayout();
      rightHeader.setSpacing(true);

      final Label timePostLbl =
          new Label(DateTimeUtils.getStringDateFromNow(message.getPosteddate()));
      timePostLbl.setSizeUndefined();
      timePostLbl.setStyleName("time-post");

      final HorizontalLayout notification = new HorizontalLayout();
      notification.setStyleName("notification");
      notification.setSizeUndefined();
      notification.setSpacing(true);
      if (message.getCommentsCount() > 0) {
        final HorizontalLayout commentNotification = new HorizontalLayout();
        final Label commentCountLbl = new Label(Integer.toString(message.getCommentsCount()));
        commentCountLbl.setStyleName("comment-count");
        commentCountLbl.setSizeUndefined();
        commentNotification.addComponent(commentCountLbl);
        final Image commentIcon =
            new Image(null, MyCollabResource.newResource("icons/16/project/message.png"));
        commentNotification.addComponent(commentIcon);

        notification.addComponent(commentNotification);
      }
      ResourceService attachmentService =
          ApplicationContextUtil.getSpringBean(ResourceService.class);
      List<Content> attachments =
          attachmentService.getContents(
              AttachmentUtils.getProjectEntityAttachmentPath(
                  AppContext.getAccountId(),
                  message.getProjectid(),
                  AttachmentType.PROJECT_MESSAGE,
                  message.getId()));
      if (attachments != null && !attachments.isEmpty()) {
        final HorizontalLayout attachmentNotification = new HorizontalLayout();
        final Label attachmentCountLbl = new Label(Integer.toString(attachments.size()));
        attachmentCountLbl.setStyleName("attachment-count");
        attachmentCountLbl.setSizeUndefined();
        attachmentNotification.addComponent(attachmentCountLbl);
        final Image attachmentIcon =
            new Image(null, MyCollabResource.newResource("icons/16/attachment.png"));
        attachmentNotification.addComponent(attachmentIcon);

        notification.addComponent(attachmentNotification);
      }

      Button deleteBtn =
          new Button(
              "",
              new Button.ClickListener() {
                private static final long serialVersionUID = 1L;

                @Override
                public void buttonClick(ClickEvent event) {
                  ConfirmDialogExt.show(
                      UI.getCurrent(),
                      LocalizationHelper.getMessage(
                          GenericI18Enum.DELETE_DIALOG_TITLE, SiteConfiguration.getSiteName()),
                      LocalizationHelper.getMessage(
                          GenericI18Enum.CONFIRM_DELETE_RECORD_DIALOG_MESSAGE),
                      LocalizationHelper.getMessage(GenericI18Enum.BUTTON_YES_LABEL),
                      LocalizationHelper.getMessage(GenericI18Enum.BUTTON_NO_LABEL),
                      new ConfirmDialog.Listener() {
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void onClose(final ConfirmDialog dialog) {
                          if (dialog.isConfirmed()) {
                            final MessageService messageService =
                                ApplicationContextUtil.getSpringBean(MessageService.class);
                            messageService.removeWithSession(
                                message.getId(),
                                AppContext.getUsername(),
                                AppContext.getAccountId());
                            MessageListViewImpl.this.tableItem.setSearchCriteria(searchCriteria);
                          }
                        }
                      });
                }
              });
      deleteBtn.setIcon(MyCollabResource.newResource("icons/12/project/icon_x.png"));
      deleteBtn.addStyleName("link");
      deleteBtn.setEnabled(
          CurrentProjectVariables.canAccess(ProjectRolePermissionCollections.MESSAGES));

      rightHeader.addComponent(timePostLbl);
      rightHeader.addComponent(deleteBtn);
      rightHeader.setExpandRatio(timePostLbl, 1.0f);

      messageHeader.addComponent(leftHeader);
      messageHeader.setExpandRatio(leftHeader, 1.0f);
      messageHeader.addComponent(rightHeader);
      messageHeader.setWidth("100%");

      rowLayout.addComponent(messageHeader);

      final Label messageContent =
          new Label(StringUtils.formatExtraLink(message.getMessage()), ContentMode.HTML);
      messageContent.setStyleName("message-body");
      rowLayout.addComponent(messageContent);

      if (notification.getComponentCount() > 0) {
        VerticalLayout messageFooter = new VerticalLayout();
        messageFooter.setWidth("100%");
        messageFooter.setStyleName("message-footer");
        messageFooter.addComponent(notification);
        messageFooter.setMargin(true);
        messageFooter.setComponentAlignment(notification, Alignment.MIDDLE_RIGHT);
        rowLayout.addComponent(messageFooter);
      }

      messageLayout.addComponent(rowLayout);
      messageLayout.setExpandRatio(rowLayout, 1.0f);

      return messageLayout;
    }