public void dispose() {
   if (delegate != null) {
     delegate.dispose();
     delegate = null;
   }
   descriptor = null;
   context = null;
 }
  public IHyperlink[] detectHyperlinks(
      ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    if (!isEnabled()) {
      return null;
    }

    if (!createFailed && delegate == null) {
      try {
        delegate = descriptor.createHyperlinkDetector();
      } catch (CoreException ex) {
        createFailed = true;
      }
      if (delegate != null && context != null) {
        delegate.setContext(context);
      }
    }
    if (delegate != null) {
      return delegate.detectHyperlinks(textViewer, region, canShowMultipleHyperlinks);
    }

    return null;
  }
  public static void checkRegions(
      IProject project,
      String fileName,
      List<TestRegion> regionList,
      AbstractHyperlinkDetector elDetector)
      throws Exception {
    IFile file = project.getFile(fileName);

    assertNotNull("The file \"" + fileName + "\" is not found", file);
    assertTrue("The file \"" + fileName + "\" is not found", file.isAccessible());

    FileEditorInput editorInput = new FileEditorInput(file);

    IDocumentProvider documentProvider = null;
    try {
      documentProvider = DocumentProviderRegistry.getDefault().getDocumentProvider(editorInput);
    } catch (Exception x) {
      x.printStackTrace();
      fail("An exception caught: " + x.getMessage());
    }

    assertNotNull(
        "The document provider for the file \"" + fileName + "\" is not loaded", documentProvider);

    try {
      documentProvider.connect(editorInput);
    } catch (Exception x) {
      x.printStackTrace();
      fail(
          "The document provider is not able to be initialized with the editor input\nAn exception caught: "
              + x.getMessage());
    }

    IDocument document = documentProvider.getDocument(editorInput);

    assertNotNull("The document for the file \"" + fileName + "\" is not loaded", document);

    if (regionList.get(0).region == null) loadRegions(regionList, document);

    int expected = 0;
    for (TestRegion testRegion : regionList) expected += testRegion.region.getLength() + 1;

    IEditorPart part = openFileInEditor(file);
    ISourceViewer viewer = null;
    if (part instanceof JavaEditor) {
      viewer = ((JavaEditor) part).getViewer();
      elDetector.setContext(new TestContext((ITextEditor) part));
    } else if (part instanceof EditorPartWrapper) {
      if (((EditorPartWrapper) part).getEditor() instanceof WebCompoundEditor) {
        WebCompoundEditor wce = (WebCompoundEditor) ((EditorPartWrapper) part).getEditor();
        viewer = wce.getSourceEditor().getTextViewer();
        elDetector.setContext(new TestContext(wce.getSourceEditor()));
      } else if (((EditorPartWrapper) part).getEditor() instanceof XMLTextEditorStandAlone) {
        XMLTextEditorStandAlone xtesa =
            (XMLTextEditorStandAlone) ((EditorPartWrapper) part).getEditor();
        viewer = xtesa.getTextViewer();
        elDetector.setContext(new TestContext(xtesa));
      } else fail("unsupported editor type - " + ((EditorPartWrapper) part).getEditor().getClass());
    } else if (part instanceof JSPMultiPageEditor) {
      viewer = ((JSPMultiPageEditor) part).getJspEditor().getTextViewer();
      elDetector.setContext(new TestContext(((JSPMultiPageEditor) part).getJspEditor()));
    } else fail("unsupported editor type - " + part.getClass());

    int counter = 0;
    for (int i = 0; i < document.getLength(); i++) {
      int lineNumber = document.getLineOfOffset(i);
      int position = i - document.getLineOffset(lineNumber) + 1;
      lineNumber++;

      TestData testData = new TestData(document, i);
      IHyperlink[] links = elDetector.detectHyperlinks(viewer, testData.getHyperlinkRegion(), true);

      boolean recognized = links != null;

      if (recognized) {
        counter++;
        TestRegion testRegion = findOffsetInRegions(i, regionList);
        if (testRegion == null) {
          String information = findRegionInformation(document, i, regionList);
          fail(
              "Wrong detection for offset - "
                  + i
                  + " (line - "
                  + lineNumber
                  + " position - "
                  + position
                  + ") "
                  + information);
        } else {
          checkTestRegion(links, testRegion);
        }
      } else {
        for (TestRegion testRegion : regionList) {
          if (i >= testRegion.region.getOffset()
              && i <= testRegion.region.getOffset() + testRegion.region.getLength()) {
            fail(
                "Wrong detection for region - "
                    + getRegionInformation(document, testRegion)
                    + " offset - "
                    + i
                    + " (line - "
                    + lineNumber
                    + " position - "
                    + position
                    + ")");
          }
        }
      }
    }

    assertEquals("Wrong recognized region count: ", expected, counter);

    documentProvider.disconnect(editorInput);
  }