private void doJSPELHyperlinkTestForELInTagBodyTest( String pageName, String template, String editorName) throws BadLocationException { IEditorPart editor = WorkbenchUtils.openEditor(pageName); assertTrue(editor instanceof JSPMultiPageEditor); JSPMultiPageEditor jspMultyPageEditor = (JSPMultiPageEditor) editor; ISourceViewer viewer = jspMultyPageEditor.getSourceEditor().getTextViewer(); assertNotNull("Viewer couldn't be found for " + pageName, viewer); IDocument document = viewer.getDocument(); IRegion reg = new FindReplaceDocumentAdapter(document).find(0, template, true, true, false, false); assertNotNull("Text: " + template + " not found", reg); IHyperlink[] links = elHyperlinkDetector.detectHyperlinks( viewer, new Region(reg.getOffset() + reg.getLength() - 1, 0), true); assertNotNull("Hyperlinks for EL:#{" + template + "} are not found", links); assertTrue("Hyperlinks for EL: #{" + template + "} are not found", links.length != 0); boolean found = false; for (IHyperlink link : links) { assertNotNull(link.toString()); link.open(); IEditorPart resultEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); if (editorName.equals(resultEditor.getTitle())) { found = true; return; } } assertTrue("OpenOn have not opened " + editorName + " editor", found); }
/** * 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; }
public void clicked(MouseEvent e) { IHyperlink hyperlink = getHyperlink(e); if (hyperlink != null) { disarm(); hyperlink.open(); } }
/** * 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); }
private static TestHyperlink findTestHyperlink( List<TestHyperlink> testHyperlinks, IHyperlink link) { for (TestHyperlink testLink : testHyperlinks) { if (testLink.name.equals(link.getHyperlinkText())) return testLink; } return null; }
private static void checkTestRegion(IHyperlink[] links, TestRegion testRegion) { for (IHyperlink link : links) { TestHyperlink testLink = findTestHyperlink(testRegion.hyperlinks, link); assertNotNull("Unexpected hyperlink - " + link.getHyperlinkText(), testLink); assertEquals("Unexpected hyperlink type", testLink.hyperlink, link.getClass()); if (testLink.fileName != null) { assertTrue( "HyperLink must be inherited from AbstractHyperlink", link instanceof AbstractHyperlink); IFile f = ((AbstractHyperlink) link).getReadyToOpenFile(); assertNotNull("HyperLink must return not null file", f); assertEquals(testLink.fileName, f.getName()); } } for (TestHyperlink testLink : testRegion.hyperlinks) { IHyperlink link = findHyperlink(links, testLink); assertNotNull("Hyperlink - " + testLink.name + " not found", link); } }
private static IHyperlink findHyperlink(IHyperlink[] links, TestHyperlink testLink) { for (IHyperlink link : links) { if (testLink.name.equals(link.getHyperlinkText())) return link; } return null; }