Vaadin Web应用开发教程(21):UI组件-Embedded组件

jerry VaadinWeb 2015年11月25日 收藏

Embedded组件支持者浏览器中嵌入媒体对象,如图像,动画等其它浏览器支持的媒体类型。Embedded组件内容在Vaadin中是作为资源来管理的。

  1. Embedded image = new Embedded("Yes, logo:",
  2. new ClassResource("vaadin-logo.png", this));
  3. main.addComponent(image);

Embedded组件支持多种显示内容,可以通过setType() 来设置嵌入对象的类型。
Embedded.TYPE_OBJECT 缺省内容类型,实现渲染时为HTML

The Embedded.TYPE_OBJECT 支持浏览器嵌入媒体,目前只支持显示Flash动画,它的MIME类型为application/x-shockwave-flash.

  1. // Create a Shockware Flash resource
  2. final ClassResource flashResource =
  3. new ClassResource("itmill_spin.swf", getApplication());
  4.  
  5. // Display the resource in a Embedded compoant
  6. final Embedded embedded =
  7. new Embedded("Embedded Caption", flashResource);
  8.  
  9. // This is the default type, but we set it anyway.
  10. embedded.setType(Embedded.TYPE_OBJECT);
  11.  
  12. // This is recorgnized automatically, but set it anyway.
  13. embedded.setMimeType("application/x-shockwave-flash");

可以通过方法setParameter 为对象设置参数。

Embedded.TYPE_IMAGE 用来显示图像,通常无需明确指定其类型。 Embedded 组件缺省未定义宽度和高度,因此可以自动适应所显示图像的大小,如果需要使用滚动条,可以在Panel中嵌入Embedded组件。
如果需要显示动态生成的图像,比如从StreamResource显示图像并且显示对象发生变化,就需要在浏览器重新加载图像,不同浏览器处理缓存Cache的方法不同,因此保险的方法是为动态生成的图像使用不同的文件名,
并在创建图像使用setCacheTime 将Cache时间设为0.

  1. // Create the stream resource with some initial filename.
  2. StreamResource imageResource =
  3. new StreamResource(imageSource, "initial-filename.png",
  4. getApplication());
  5.  
  6. // Instruct browser not to cache the image.
  7. imageResource.setCacheTime(0);
  8.  
  9. // Display the image in an Embedded component.
  10. Embedded embedded = new Embedded("", imageResource);

刷新图像使用requestRepaint() 方法。

  1. // This needs to be done, but is not sufficient.
  2. embedded.requestRepaint();
  3.  
  4. // Generate a filename with a timestamp.
  5. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
  6. String filename = "myfilename-" + df.format(new Date()) + ".png";
  7.  
  8. // Replace the filename in the resource.
  9. imageResource.setFilename(makeImageFilename());

Embedded.TYPE_BROWSER在iframe中显示一个外部链接。

  1. URL url = new URL("http://dev.vaadin.com/");
  2. Embedded browser = new Embedded("", new ExternalResource(url));
  3. browser.setType(Embedded.TYPE_BROWSER);
  4. main.addComponent(browser);