예제 #1
0
 /**
  * Use hyperlink detectors to find a text viewer's hyperlinks and return the style ranges to
  * render them.
  *
  * @param textViewer
  * @param hyperlinkDetectors
  * @return the style ranges to render the detected hyperlinks
  */
 public static StyleRange[] getHyperlinkDetectorStyleRanges(
     ITextViewer textViewer, IHyperlinkDetector[] hyperlinkDetectors) {
   List<StyleRange> styleRangeList = new ArrayList<StyleRange>();
   if (hyperlinkDetectors != null && hyperlinkDetectors.length > 0) {
     for (int i = 0; i < textViewer.getTextWidget().getText().length(); i++) {
       IRegion region = new Region(i, 0);
       for (IHyperlinkDetector hyperLinkDetector : hyperlinkDetectors) {
         IHyperlink[] hyperlinks = hyperLinkDetector.detectHyperlinks(textViewer, region, true);
         if (hyperlinks != null) {
           for (IHyperlink hyperlink : hyperlinks) {
             StyleRange hyperlinkStyleRange =
                 new StyleRange(
                     hyperlink.getHyperlinkRegion().getOffset(),
                     hyperlink.getHyperlinkRegion().getLength(),
                     Display.getDefault().getSystemColor(SWT.COLOR_BLUE),
                     Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
             hyperlinkStyleRange.underline = true;
             styleRangeList.add(hyperlinkStyleRange);
           }
         }
       }
     }
   }
   StyleRange[] styleRangeArray = new StyleRange[styleRangeList.size()];
   styleRangeList.toArray(styleRangeArray);
   return styleRangeArray;
 }
  /**
   * Map public URLs to Alt URLs. Replaces {@link URLHyperlink}s computed by default by instances
   * with the alternative URL.
   *
   * @param hyperlinks
   */
  private IHyperlink[] removeURLHyperlinksOfManagedResources(IHyperlink[] hyperlinks) {
    if (hyperlinks == null) return hyperlinks;

    List<IHyperlink> result = new ArrayList<>(hyperlinks.length);
    for (int i = 0; i < hyperlinks.length; i++) {
      IHyperlink hyperlink = hyperlinks[i];
      if (hyperlink instanceof URLHyperlink) {
        URLHyperlink urlHyperlink = (URLHyperlink) hyperlink;
        String publicUri = urlHyperlink.getURLString();
        IConfigurationManagerForIDE cmgr = null;
        try {
          // get the configuration manager for the edited resource
          IResource editedResource =
              (IResource) getEditor().getEditorInput().getAdapter(IResource.class);
          SadlModelManager visitor =
              sadlModelManagerProvider.get(URI.createURI(editedResource.getLocation().toString()));
          cmgr = visitor.getConfigurationMgr(editedResource.getLocation().toString());

          // map the public URL to the mapped URL
          String altUrl = cmgr.getAltUrlFromPublicUri(publicUri);
          result.add(new URLHyperlinkExt(hyperlink.getHyperlinkRegion(), altUrl));
          // TODO: Actually this hyperlink should not be added
          // but if left out, also no Xtext hyperlink appears. Have to check out why
          // at least this one is mapped
        } catch (ConfigurationException | URISyntaxException | IOException e) {
        }
      } else {
        result.add(hyperlink);
      }
    }
    return result.toArray(hyperlinks);
  }