You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UploadConnector.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.upload;
  17. import com.vaadin.client.ApplicationConnection;
  18. import com.vaadin.client.Paintable;
  19. import com.vaadin.client.UIDL;
  20. import com.vaadin.client.annotations.OnStateChange;
  21. import com.vaadin.client.communication.StateChangeEvent;
  22. import com.vaadin.client.ui.AbstractComponentConnector;
  23. import com.vaadin.client.ui.VUpload;
  24. import com.vaadin.shared.ui.Connect;
  25. import com.vaadin.shared.ui.upload.UploadClientRpc;
  26. import com.vaadin.shared.ui.upload.UploadState;
  27. import com.vaadin.ui.Upload;
  28. @Connect(Upload.class)
  29. public class UploadConnector extends AbstractComponentConnector
  30. implements Paintable {
  31. public UploadConnector() {
  32. registerRpc(UploadClientRpc.class, () -> getWidget().submit());
  33. }
  34. @Override
  35. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  36. if (!isRealUpdate(uidl)) {
  37. return;
  38. }
  39. VUpload upload = getWidget();
  40. if (uidl.hasAttribute("notStarted")) {
  41. upload.t.schedule(400);
  42. return;
  43. }
  44. upload.setImmediateMode(getState().immediateMode);
  45. upload.client = client;
  46. upload.paintableId = uidl.getId();
  47. upload.nextUploadId = uidl.getIntAttribute("nextid");
  48. final String action = client
  49. .translateVaadinUri(uidl.getStringVariable("action"));
  50. upload.element.setAction(action);
  51. upload.fu.setName(upload.paintableId + "_file");
  52. if (!isEnabled()) {
  53. upload.disableUpload();
  54. } else if (!uidl.getBooleanAttribute("state")) {
  55. // Enable the button only if an upload is not in progress
  56. upload.enableUpload();
  57. upload.ensureTargetFrame();
  58. }
  59. }
  60. @Override
  61. public void onStateChanged(StateChangeEvent stateChangeEvent) {
  62. super.onStateChanged(stateChangeEvent);
  63. getWidget().disableTitle(hasTooltip());
  64. }
  65. /**
  66. * Updates the caption, style name, display mode, and visibility of the
  67. * submit button.
  68. * <p>
  69. * For internal use only. May be removed or replaced in the future.
  70. */
  71. @OnStateChange({ "buttonCaption", "buttonStyleName",
  72. "buttonCaptionAsHtml" })
  73. private void updateSubmitButton() {
  74. VUpload upload = getWidget();
  75. if (getState().buttonCaption != null) {
  76. if (getState().buttonCaptionAsHtml) {
  77. upload.submitButton.setHtml(getState().buttonCaption);
  78. } else {
  79. upload.submitButton.setText(getState().buttonCaption);
  80. }
  81. upload.submitButton.setStyleName(getState().buttonStyleName);
  82. upload.submitButton.setVisible(true);
  83. } else {
  84. upload.submitButton.setVisible(false);
  85. }
  86. }
  87. @Override
  88. public VUpload getWidget() {
  89. return (VUpload) super.getWidget();
  90. }
  91. @Override
  92. public UploadState getState() {
  93. return (UploadState) super.getState();
  94. }
  95. }