Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

LinkIndexConverter



The LinkIndexConverter is an interface used in ScriptLogger, in the context of the hyperlinks.

It which allows to convert an Object to or from a link description.

Api

The LinkIndexConverter has only two methods:

For example, suppose the following class which specifies an element with a namespace and a name:
      public class TreeElement {
         public final String namespace;
         public final String name;

         public TreeElement(String namespace, String name) {
           this.namespace = namespace;
           this.name = name;
         }

         @Override
         public String toString() {
           return namespace + ":" + name;
         }

         @Override
         public int hashCode() {
           int hash = 7;
           hash = 79 * hash + Objects.hashCode(this.namespace);
           hash = 79 * hash + Objects.hashCode(this.name);
           return hash;
         }

         @Override
         public boolean equals(Object obj) {
           if (this == obj) {
             return true;
           }
           if (obj == null) {
             return false;
           }
           if (getClass() != obj.getClass()) {
             return false;
           }
           final TreeElement other = (TreeElement) obj;
           return true;
         }
      }
The following code will allow to create a link description from a TreeElement and a TreeElement from a link description:
      public class MyLinkIndexConverter implements LinkIndexConverter {
         @Override
         public String getLinkFromObject(TreeElement element) {
           return element.namespace + "$" + element.name;
         }

         @Override
         public TreeElement getObjectFromLink(String linkID) {
           int index = linkID.indexOf("$");
           if (index != -1) {
             String namespace = linkID.substring(0, index);
             String name = linkID.substring(index + 1);
             TreeElement key = new TreeElement(namespace, name);
             if (elements.containsKey(key)) {
               return elements.get(key);
             } else {
               return null;
             }
           } else {
             return null;
           }
         }
      }

Registering the converter

To register the LinkIndexConverter, you must use the ScriptLogger.registerLinkIndexConverter(LinkIndexConverter) method.

For example:
      GroovyScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      LinkIndexConverter converter = new MyLinkIndexConverter();
      ScriptLogger logger = new DefaultSwingScriptLogger();
      logger.registerLinkIndexConverter(converter);
      wrapper.setScriptLogger(logger);

Api usage

The converter is used in the following methods which allow to show hyperlinks using HyperlinkElements:
appendObjectlink
If the LinkIndexConverter has not been able to convert the Object to a link ID (the returned result is null), then by default an error message will be shown. It is possible to append a regular text without an hyperlink instead by setting the ScriptLogger.acceptNullObjectLinks(boolean) method.

See also


Categories: api

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence