Scout/HowTo/3.8/Display HTML content

From Eclipsepedia

< Scout‎ | HowTo‎ | 3.8
Jump to: navigation, search


Scout
Wiki Home
Website
DownloadGit
Community
ForumsBlogTwitter
Bugzilla
Bugzilla


This how-to describes how to display HTML content using an AbstractHtmlField and an AbstractBrowserField

AbstractHtmlField

The HtmlField takes a HTML string as value and renders it. If a link in the rendered HTML output is clicked, the execHyperlinkAction() method is called. You will need to retrieve the content of the url parameter and to set that content as the new value of the field in order to allow following links.

If you want images referenced in <img src="" /> tags without absolute URLs to be rendered properly, you need to add them as attached RemoteFiles.

For HTML code that references icons/images that are part of the client resources, the two following methods will take care of this:

Overwrite the importFormFieldData() method of your HtmlField to handle images referenced in the initial HTML content (add a changeListener to also do this for dynamic changes of content):

            @Override
            public void importFormFieldData(AbstractFormFieldData source, boolean valueChangeTriggersEnabled) {
              super.importFormFieldData(source, valueChangeTriggersEnabled);
              if (getValue() != null) {
                List<String> files = HtmlClientUtil.extractImageNames(getValue());
                ArrayList<RemoteFile> attachments = new ArrayList<RemoteFile>();
                HtmlClientUtil.createHtmlAttachments(files, attachments);
                if (attachments.size() > 0) {
                  setAttachments(attachments.toArray(new RemoteFile[attachments.size()]));
                }
              }
            }

Create a HtmlClientUtil class in your client and add the extractImageNames() method to it. This method will (roughly) parse image names from your HTML code string and return the file names in an ArrayList:

  // this method will extract image file names from HTML source code. it is only a rough'n'ready method and will probably not catch all images, especially if additional attributes are used
  private static String IMG_SRC = "<img src=\"";
  private static String IMG_SRC_END = "\">";
  
  public static List<String> extractImageNames(String html) {
    List<String> result = new ArrayList<String>(); 
    String search = html;
    int pos = 0; 
    while (pos >= 0) {
      pos = search.indexOf(IMG_SRC);
      if (pos >= 0) {
        search = search.substring(pos + IMG_SRC.length());
        pos = search.indexOf(IMG_SRC_END);
        if (pos >= 0) {
          result.add(search.substring(0, pos));
          search = search.substring(pos + IMG_SRC_END.length());
        }
      }
    } 
    return result;
  }


Add the createHtmlAttachement() method to your HtmlClientUtil class. The method iterates over the list of images and tries to retrieve them as icons and return a list of RemotFiles with the icon file contents:

public static void createHtmlAttachments(List<String> filenames, Collection<RemoteFile> collection) {
    for (String filename : filenames) {
      RemoteFile f = new RemoteFile(filename, 0);
      if (f != null && !f.hasContent()) {
        // try to load bundle resource
        try {
          IClientSession clientSession = ClientSyncJob.getCurrentSession();
          String nameWithoutExtension = filename;
          int ext = filename.lastIndexOf('.');
          if (ext > 0) {
            nameWithoutExtension = filename.substring(0, ext);
          }
          IconSpec iconSpec = clientSession.getIconLocator().getIconSpec(nameWithoutExtension);
          ByteArrayInputStream is = new ByteArrayInputStream(iconSpec.getContent());
          f.readData(is);
          is.close();
        }
        catch (Exception ex2) {
          LOG.info(null, ex2);
          f = null;
        }
      }
      if (f != null && f.hasContent()) {
        collection.add(f);
      }
    }
  } 


AbstractBrowserField

The browser field takes a URL (which can be set using BrowserField.setLocation() ) and renders the content of that URL. However, this currently only seems to work for SWT but not Swing.