public void open() { // Create the browser IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); IWebBrowser browser; try { browser = support.createBrowser(null); } catch (PartInitException e) { SadlActivatorExt.getInstance() .getLog() .log( new Status( IStatus.ERROR, SadlActivatorExt.getInstance().getBundle().getSymbolicName(), "Could not create Web browser for URLHyperlink", e)); //$NON-NLS-1$ super.open(); return; } try { browser.openURL(new URL(getURLString())); } catch (PartInitException e) { super.open(); } catch (MalformedURLException e) { super.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); }
@Test public void testDetectHyperlinks() throws PartInitException { String testText = "Name: eclipse\nURL: http://www.%{name}.org/"; newFile(testText); URLHyperlinkWithMacroDetector macroDetector = new URLHyperlinkWithMacroDetector(); macroDetector.setSpecfile(specfile); IRegion region = new Region(20, 0); IEditorPart openEditor = IDE.openEditor( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), testFile, "org.eclipse.linuxtools.rpm.ui.editor.SpecfileEditor"); editor = (SpecfileEditor) openEditor; editor.doRevertToSaved(); IHyperlink[] returned = macroDetector.detectHyperlinks(editor.getSpecfileSourceViewer(), region, false); URLHyperlink url = (URLHyperlink) returned[0]; assertEquals("http://www.eclipse.org/", url.getURLString()); }