7.4. 加载和显示图片

我们看看加载、存储和显示员工图片的任务:

  • 员工由 Employee 实体表示。

  • 图片文件存储在 FileStorage 中。Employee 实体包含指向相应 FileDescriptor 的链接。

  • Employee 编辑界面显示图片,还支持上传,下载和清除图片。

带有图片文件链接的实体类:

  1. @Table(name = "SAMPLE_EMPLOYEE")
  2. @Entity(name = "sample$Employee")
  3. public class Employee extends StandardEntity {
  4. ...
  5. @ManyToOne(fetch = FetchType.LAZY)
  6. @JoinColumn(name = "IMAGE_FILE_ID")
  7. protected FileDescriptor imageFile;
  8. public void setImageFile(FileDescriptor imageFile) {
  9. this.imageFile = imageFile;
  10. }
  11. public FileDescriptor getImageFile() {
  12. return imageFile;
  13. }
  14. }

同时加载 EmployeeFileDescriptor视图需要包含 FileDescriptor 的所有本地属性:

  1. <view entity="demo_Employee"
  2. name="employee-edit"
  3. extends="_local">
  4. <property name="imageFile"
  5. view="_local"/>
  6. </view>

Employee 编辑界面 XML 描述的一个片段:

  1. <groupBox caption="Photo" spacing="true"
  2. height="300px" width="300px" expand="image">
  3. <image id="image"
  4. width="100%"
  5. align="MIDDLE_CENTER"
  6. scaleMode="CONTAIN"/>
  7. <hbox align="BOTTOM_LEFT"
  8. spacing="true">
  9. <upload id="uploadField"/>
  10. <button id="downloadImageBtn"
  11. caption="Download"
  12. invoke="onDownloadImageBtnClick"/>
  13. <button id="clearImageBtn"
  14. caption="Clear"
  15. invoke="onClearImageBtnClick"/>
  16. </hbox>
  17. </groupBox>

用于显示、上传和下载图片的组件包含在 groupBox 容器中。容器顶部使用 image 组件显示一张图片,而它的底部从左到右包含上传控件以及下载和清除图片的按钮。因此,界面的这一部分如下所示:

images recipe

现在,现在我们来看看编辑界面控制器

  1. import com.company.sales.entity.Employee;
  2. import com.haulmont.cuba.core.entity.FileDescriptor;
  3. import com.haulmont.cuba.core.global.DataManager;
  4. import com.haulmont.cuba.core.global.FileStorageException;
  5. import com.haulmont.cuba.gui.Notifications;
  6. import com.haulmont.cuba.gui.components.Button;
  7. import com.haulmont.cuba.gui.components.FileDescriptorResource;
  8. import com.haulmont.cuba.gui.components.FileUploadField;
  9. import com.haulmont.cuba.gui.components.Image;
  10. import com.haulmont.cuba.gui.export.ExportDisplay;
  11. import com.haulmont.cuba.gui.export.ExportFormat;
  12. import com.haulmont.cuba.gui.model.InstanceContainer;
  13. import com.haulmont.cuba.gui.screen.*;
  14. import com.haulmont.cuba.gui.upload.FileUploadingAPI;
  15. import javax.inject.Inject;
  16. @UiController("sales_Employee.edit")
  17. @UiDescriptor("employee-edit.xml")
  18. @EditedEntityContainer("employeeDc")
  19. @LoadDataBeforeShow
  20. public class EmployeeEdit extends StandardEditor<Employee> {
  21. @Inject
  22. private InstanceContainer<Employee> employeeDc;
  23. @Inject
  24. private Image image;
  25. @Inject
  26. private FileUploadField uploadField;
  27. @Inject
  28. private FileUploadingAPI fileUploadingAPI;
  29. @Inject
  30. private DataManager dataManager;
  31. @Inject
  32. private Notifications notifications;
  33. @Inject
  34. private Button downloadImageBtn;
  35. @Inject
  36. private Button clearImageBtn;
  37. @Subscribe
  38. protected void onInit(InitEvent event) { (1)
  39. uploadField.addFileUploadSucceedListener(uploadSucceedEvent -> {
  40. FileDescriptor fd = uploadField.getFileDescriptor(); (2)
  41. try {
  42. fileUploadingAPI.putFileIntoStorage(uploadField.getFileId(), fd);
  43. } catch (FileStorageException e) {
  44. throw new RuntimeException("Error saving file to FileStorage", e);
  45. }
  46. getEditedEntity().setImageFile(dataManager.commit(fd)); (3)
  47. displayImage(); (4)
  48. });
  49. uploadField.addFileUploadErrorListener(uploadErrorEvent ->
  50. notifications.create()
  51. .withCaption("File upload error")
  52. .show());
  53. employeeDc.addItemPropertyChangeListener(employeeItemPropertyChangeEvent -> { (5)
  54. if ("imageFile".equals(employeeItemPropertyChangeEvent.getProperty()))
  55. updateImageButtons(employeeItemPropertyChangeEvent.getValue() != null);
  56. });
  57. }
  58. @Subscribe
  59. protected void onAfterShow(AfterShowEvent event) { (6)
  60. displayImage();
  61. updateImageButtons(getEditedEntity().getImageFile() != null);
  62. }
  63. public void onDownloadImageBtnClick() { (7)
  64. if (getItem().getImageFile() != null)
  65. exportDisplay.show(getItem().getImageFile(), ExportFormat.OCTET_STREAM);
  66. }
  67. public void onClearImageBtnClick() { (8)
  68. getEditedEntity().setImageFile(null);
  69. displayImage();
  70. }
  71. private void updateImageButtons(boolean enable) {
  72. downloadImageBtn.setEnabled(enable);
  73. clearImageBtn.setEnabled(enable);
  74. }
  75. private void displayImage() { (9)
  76. if (getEditedEntity().getImageFile() != null) {
  77. image.setSource(FileDescriptorResource.class).setFileDescriptor(getEditedEntity().getImageFile());
  78. image.setVisible(true);
  79. } else {
  80. image.setVisible(false);
  81. }
  82. }
  83. }
1onInit() 方法首先初始化用于上传新图片的 uploadField 组件。
2在成功上传的情况下,从组件获取新的 FileDescriptor 实例,并通过调用 FileUploadingAPI.putFileIntoStorage() 将相应的文件从临时客户端存储发送到 FileStorage
3之后,通过调用 DataManager.commit()FileDescriptor 保存到数据库中,并将保存的实例分配给 Employee 实体的 imageFile 属性。
4然后,调用控制器的 displayImage() 方法显示上传的图片。
5之后,将一个监听器添加到包含 Employee 实例的数据容器中。监听器根据文件加载的状态启用或禁用下载和清除按钮。
6onAfterShow() 方法根据加载的文件是否存在来决定是否显示图片并且更新按钮状态。
7点击 downloadImageBtn 按钮时调用 onDownloadImageBtnClick(),使用 ExportDisplay 接口下载文件。
8点击 clearImageBtn 时调用 onClearImageBtnClick();它只是清除了 Employee 实体的 imageFile 属性。该文件不会从存储中删除。
9displayImage() 方法从存储中加载文件并设置 image 组件的内容。