如何使用工具栏按钮作为下载链接

时间:2012-07-25 14:59:43

标签: eclipse-rap

我正在用rap 1.4编写一个RCP / RAP-App作为说唱部分的目标平台。在rcp中,我有一个通过plugin.xml中的menuContribution配置的保存按钮和一个用于save命令的相应SaveHandler。 现在我喜欢在我的说唱应用程序中使用此按钮作为下载按钮。

1 个答案:

答案 0 :(得分:2)

我知道了。
我编写了一个DownloadServiceHandler并在save-command的处理程序中创建了一个带有download-URL的不可见的浏览器。
所有的工作一步一步:
工具栏中的保存按钮在plugin.xml中配置:

    <extension
         point="org.eclipse.ui.commands">
      <command
            id="pgui.rcp.command.save"
            name="Save">
      </command>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            allPopups="false"
            locationURI="toolbar:org.eclipse.ui.main.toolbar">
         <toolbar
               id="pgui.rcp.toolbar1">
            <command
                  commandId="pgui.rcp.command.save"
                  icon="icons/filesave_16.png"
                  id="pgui.rcp.button.save"
                  style="push"
            </command>
         </toolbar>
      </menuContribution>
   </extension>
   <extension
         point="org.eclipse.ui.handlers">
      <handler
            class="pgui.handler.SaveHandler"
            commandId="pgui.rcp.command.save">
      </handler>
   </extension>

我创建了一个DownloadServiceHandler:

public class DownloadServiceHandler implements IServiceHandler
{
  public void service() throws IOException, ServletException
  {
    final String fileName = RWT.getRequest().getParameter("filename");
    final byte[] download = getYourFileContent().getBytes();
    // Send the file in the response
    final HttpServletResponse response = RWT.getResponse();
    response.setContentType("application/octet-stream");
    response.setContentLength(download.length);
    final String contentDisposition = "attachment; filename=\"" + fileName + "\"";
    response.setHeader("Content-Disposition", contentDisposition);
    response.getOutputStream().write(download);
  }
}

在ApplicationWorkbenchWindowAdvisor的postWindowCreate方法中,我注册了DownloadServiceHandler:

private void registerDownloadHandler()
  {
    final IServiceManager manager = RWT.getServiceManager();
    final IServiceHandler handler = new DownloadServiceHandler();
    manager.registerServiceHandler("downloadServiceHandler", handler);
  }

在SaveHandler的execute-method中,我创建了一个不可见的浏览器,并使用文件名和注册的DownloadServiceHandler设置了url。

  final Browser browser = new Browser(shell, SWT.NONE);
    browser.setSize(0, 0);
    browser.setUrl(createDownloadUrl(fileName));
       .
       .
  private String createDownloadUrl(final String fileName)
  {
    final StringBuilder url = new StringBuilder();
    url.append(RWT.getRequest().getContextPath());
    url.append(RWT.getRequest().getServletPath());
    url.append("?");
    url.append(IServiceHandler.REQUEST_PARAM);
    url.append("=downloadServiceHandler");
    url.append("&filename=");
    url.append(fileName);
    return RWT.getResponse().encodeURL(url.toString());
  }
相关问题