Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/HowTo/4.0/Display HTML content"

< Scout‎ | HowTo‎ | 4.0
m
(Replaced content with "The Scout documentation has been moved to https://eclipsescout.github.io/.")
 
Line 1: Line 1:
{{ScoutPage|cat=HowTo 4.0}}
+
The Scout documentation has been moved to https://eclipsescout.github.io/.
 
+
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 '''&lt;img src="" /&gt;''' 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):
+
<pre>            @Override
+
            public void importFormFieldData(AbstractFormFieldData source, boolean valueChangeTriggersEnabled) {
+
              super.importFormFieldData(source, valueChangeTriggersEnabled);
+
              if (getValue()&nbsp;!= null) {
+
                List&lt;String&gt; files = HtmlClientUtil.extractImageNames(getValue());
+
                ArrayList&lt;RemoteFile&gt; attachments = new ArrayList&lt;RemoteFile&gt;();
+
                HtmlClientUtil.createHtmlAttachments(files, attachments);
+
                if (attachments.size() &gt; 0) {
+
                  setAttachments(attachments);
+
                }
+
              }
+
            }</pre>
+
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:
+
<pre>  // 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 = "&lt;img src=\"";
+
  private static String IMG_SRC_END = "\"&gt;";
+
 
+
  public static List&lt;String&gt; extractImageNames(String html) {
+
    List&lt;String&gt; result = new ArrayList&lt;String&gt;();
+
    String search = html;
+
    int pos = 0;
+
    while (pos &gt;= 0) {
+
      pos = search.indexOf(IMG_SRC);
+
      if (pos &gt;= 0) {
+
        search = search.substring(pos + IMG_SRC.length());
+
        pos = search.indexOf(IMG_SRC_END);
+
        if (pos &gt;= 0) {
+
          result.add(search.substring(0, pos));
+
          search = search.substring(pos + IMG_SRC_END.length());
+
        }
+
      }
+
    }
+
    return result;
+
  }</pre>
+
<br> 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:
+
<pre>public static void createHtmlAttachments(List&lt;String&gt; filenames, Collection&lt;RemoteFile&gt; collection) {
+
    for (String filename&nbsp;: filenames) {
+
      RemoteFile f = new RemoteFile(filename, 0);
+
      if (f&nbsp;!= null &amp;&amp;&nbsp;!f.hasContent()) {
+
        // try to load bundle resource
+
        try {
+
          IClientSession clientSession = ClientSyncJob.getCurrentSession();
+
          String nameWithoutExtension = filename;
+
          int ext = filename.lastIndexOf('.');
+
          if (ext &gt; 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) {
+
          ScoutLogManager.getLogger(HtmlClientUtil.class).info(null, ex2);
+
          f = null;
+
        }
+
      }
+
      if (f&nbsp;!= null &amp;&amp; f.hasContent()) {
+
        collection.add(f);
+
      }
+
    }
+
  }
+
 
+
</pre>
+
<br>
+
 
+
= 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.  
+
 
+
<br>
+

Latest revision as of 07:35, 18 March 2024

The Scout documentation has been moved to https://eclipsescout.github.io/.

Back to the top