diff -r ea9bf60acd97 css.visual/src/org/netbeans/modules/css/visual/ui/preview/CssPreviewPanel.java --- a/css.visual/src/org/netbeans/modules/css/visual/ui/preview/CssPreviewPanel.java Fri Mar 26 06:52:24 2010 +0300 +++ b/css.visual/src/org/netbeans/modules/css/visual/ui/preview/CssPreviewPanel.java Fri Mar 26 10:07:37 2010 +0100 @@ -41,7 +41,9 @@ package org.netbeans.modules.css.visual.ui.preview; import java.awt.Graphics; +import java.io.File; import java.io.InputStream; +import java.net.URL; import java.util.concurrent.FutureTask; import java.util.logging.Handler; import java.util.logging.Level; @@ -49,10 +51,18 @@ import java.util.logging.Logger; import javax.swing.JComponent; import javax.swing.SwingUtilities; +import org.netbeans.api.project.FileOwnerQuery; +import org.netbeans.api.project.Project; +import org.netbeans.modules.web.common.spi.ProjectWebRootQuery; import org.openide.awt.StatusDisplayer; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileStateInvalidException; +import org.openide.filesystems.FileUtil; import org.openide.util.Exceptions; import org.openide.util.RequestProcessor; +import org.xhtmlrenderer.extend.UserAgentCallback; import org.xhtmlrenderer.simple.XHTMLPanel; +import org.xhtmlrenderer.swing.NaiveUserAgent; /** * JPanel wrapping XHTMLPanel, the Flying Saucer's rendering area. @@ -80,7 +90,7 @@ @Override public void run() { //create outside of AWT - final XHTMLPanel panel = new PatchedXHTMLPanel(); + final XHTMLPanel panel = new PatchedXHTMLPanel(new PreviewUserAgent()); //and set the panel to this component in AWT SwingUtilities.invokeLater(new Runnable() { @Override @@ -196,6 +206,16 @@ //workaround for FlyingSaucer bug (reported as netbeans issue #117499 (NullPointerException for unreachable url)) private static class PatchedXHTMLPanel extends XHTMLPanel { + /** + * Instantiates a panel with a custom {@link org.xhtmlrenderer.extend.UserAgentCallback} + * implementation. + * + * @param uac The custom UserAgentCallback implementation. + */ + public PatchedXHTMLPanel(UserAgentCallback uac) { + super(uac); + } + @Override public void paintComponent(Graphics g) { try { @@ -208,5 +228,33 @@ } } } - + + //workaround for specifying Base URL + private static class PreviewUserAgent extends NaiveUserAgent { + @Override + public String resolveURI(String uri) { + if (uri == null) { + return null; + } + if (uri.startsWith("/")) { + // Absolute path now we will look to the webRoot of the project + FileObject edited = FileUtil.toFileObject(FileUtil.normalizeFile(new File(getBaseURL()))); + if (edited != null) { + FileObject webRoot = ProjectWebRootQuery.getWebRoot(edited); + if(webRoot == null) { + //no web root, use project's root + Project project = FileOwnerQuery.getOwner(edited); + webRoot = project.getProjectDirectory(); + } + try { + return webRoot.getFileObject(uri).getURL().toExternalForm(); + } catch (FileStateInvalidException ex) { + // ignore it and falling back to flysaucer implementation + } + } + } + // falling back to FlyingSaucer implementation + return super.resolveURI(uri); + } + } }