package org.simantics.document.ui.graphfile; import java.net.MalformedURLException; import java.net.URL; import org.eclipse.swt.widgets.Display; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.browser.IWebBrowser; import org.eclipse.ui.browser.IWorkbenchBrowserSupport; import org.simantics.Simantics; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.document.DocumentResource; import org.simantics.document.ui.Activator; import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter; import org.simantics.ui.workbench.editor.EditorAdapter; import org.simantics.utils.ui.ExceptionUtils; public class UrlEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter { public UrlEditorAdapter() { super(Messages.UrlEditorAdapter_Browser,Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/world.png")); //$NON-NLS-2$ //$NON-NLS-3$ } @Override public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException { DocumentResource doc = DocumentResource.getInstance(g); if (!g.isInstanceOf(r, doc.UrlDocument)) return false; String url = g.getPossibleRelatedValue(r, doc.HasUrl); return url != null; } @Override protected void openEditor(final Resource input) throws Exception { Simantics.getSession().asyncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { DocumentResource doc = DocumentResource.getInstance(graph); final String urlString = graph.getPossibleRelatedValue(input, doc.HasUrl); if (urlString == null) return; Display.getDefault().asyncExec(new Runnable() { @Override public void run() { IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench().getBrowserSupport(); IWebBrowser browser; try { browser = browserSupport.createBrowser(null); URL url = new URL(urlString); browser.openURL(url); } catch (PartInitException e1) { ExceptionUtils.logAndShowError(e1); } catch (MalformedURLException e2) { ExceptionUtils.logAndShowError(e2); } } }); } }); } }