Search in sources :

Example 6 with ProgressAdapter

use of org.eclipse.swt.browser.ProgressAdapter in project xtext-eclipse by eclipse.

the class XbaseInformationControl method createContent.

/**
 * Xbase - modification+ added detailPane
 */
@Override
protected void createContent(Composite parent) {
    fSashForm = new SashForm(parent, parent.getStyle());
    fSashForm.setOrientation(SWT.VERTICAL);
    fBrowser = new Browser(fSashForm, SWT.NONE);
    fBrowser.setJavascriptEnabled(false);
    Display display = getShell().getDisplay();
    ColorRegistry registry = JFaceResources.getColorRegistry();
    // $NON-NLS-1$
    Color foreground = registry.get("org.eclipse.ui.workbench.HOVER_FOREGROUND");
    // $NON-NLS-1$
    Color background = registry.get("org.eclipse.ui.workbench.HOVER_BACKGROUND");
    if (background != null && foreground != null) {
        fBrowser.setForeground(foreground);
        fBrowser.setBackground(background);
    } else {
        fBrowser.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
        fBrowser.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    }
    fBrowser.addProgressListener(new ProgressAdapter() {

        @Override
        public void completed(ProgressEvent event) {
            fCompleted = true;
        }
    });
    fBrowser.addOpenWindowListener(new OpenWindowListener() {

        @Override
        public void open(WindowEvent event) {
            // Cancel opening of new windows
            event.required = true;
        }
    });
    // Replace browser's built-in context menu with none
    fSashForm.setMenu(new Menu(getShell(), SWT.NONE));
    detailPaneComposite = createComposite(fSashForm, 1, 1, GridData.FILL_BOTH);
    Layout layout = detailPaneComposite.getLayout();
    if (layout instanceof GridLayout) {
        GridLayout gl = (GridLayout) layout;
        gl.marginHeight = 0;
        gl.marginWidth = 0;
        gl.numColumns = 1;
    }
    if (background != null && foreground != null) {
        detailPaneComposite.setForeground(foreground);
        detailPaneComposite.setBackground(background);
    } else {
        detailPaneComposite.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
        detailPaneComposite.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
    }
    resourceProvider = new HoverEditedResourceProvider();
    embeddedEditor = xbaseHoverConfiguration.getEditorFactory().newEditor(resourceProvider).readOnly().processIssuesBy(new IValidationIssueProcessor() {

        @Override
        public void processIssues(List<Issue> issues, IProgressMonitor monitor) {
        }
    }).withParent(detailPaneComposite);
    Control viewerControl = embeddedEditor.getViewer().getControl();
    if (background != null && foreground != null) {
        viewerControl.setForeground(foreground);
        viewerControl.setBackground(background);
    } else {
        viewerControl.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
        viewerControl.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
    }
    embeddedEditor.getDocument().setValidationJob(null);
    createTextLayout();
    parent.addDisposeListener(e -> {
        if (fTextLayout != null && !fTextLayout.isDisposed()) {
            fTextLayout.dispose();
        }
    });
}
Also used : Issue(org.eclipse.xtext.validation.Issue) Color(org.eclipse.swt.graphics.Color) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) IValidationIssueProcessor(org.eclipse.xtext.ui.editor.validation.IValidationIssueProcessor) SashForm(org.eclipse.swt.custom.SashForm) GridLayout(org.eclipse.swt.layout.GridLayout) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IXtextBrowserInformationControl(org.eclipse.xtext.ui.editor.hover.html.IXtextBrowserInformationControl) AbstractInformationControl(org.eclipse.jface.text.AbstractInformationControl) Control(org.eclipse.swt.widgets.Control) ColorRegistry(org.eclipse.jface.resource.ColorRegistry) TextLayout(org.eclipse.swt.graphics.TextLayout) Layout(org.eclipse.swt.widgets.Layout) GridLayout(org.eclipse.swt.layout.GridLayout) WindowEvent(org.eclipse.swt.browser.WindowEvent) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Menu(org.eclipse.swt.widgets.Menu) OpenWindowListener(org.eclipse.swt.browser.OpenWindowListener) Browser(org.eclipse.swt.browser.Browser) Display(org.eclipse.swt.widgets.Display)

Example 7 with ProgressAdapter

use of org.eclipse.swt.browser.ProgressAdapter in project eclipse-integration-commons by spring-projects.

the class StsBrowserEditor method createPartControl.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
@Override
public void createPartControl(Composite parent) {
    parent.setLayout(new FillLayout());
    browserViewer = new StsBrowserViewer(parent, hasToolbar() ? StsBrowserViewer.BUTTON_BAR | StsBrowserViewer.LOCATION_BAR : SWT.NONE);
    final Browser browser = browserViewer.getBrowser();
    if (getEditorInput() instanceof WebBrowserEditorInput) {
        homeUrl = ((WebBrowserEditorInput) getEditorInput()).getURL().toString();
        browserViewer.setVisible(true);
    }
    if (homeUrl != null) {
        browserViewer.setHomeUrl(homeUrl);
        browserViewer.setURL(homeUrl);
    } else {
        browser.setText("<h1>URL not set</h1>" + "<p>Url should be provided via the setInitializationData method</p>");
    }
    if (getName() == null) {
        browser.addTitleListener(new TitleListener() {

            @Override
            public void changed(TitleEvent event) {
                String newValue = event.title;
                setName(newValue);
                setPartName(newValue);
            }
        });
    }
    browser.addProgressListener(new ProgressAdapter() {

        @Override
        public void completed(ProgressEvent event) {
            StsBrowserViewer browserViewer = getBrowserViewer();
            if (browserViewer != null) {
                if (browserManager == null) {
                    browserManager = new StsBrowserManager();
                }
                browserManager.setClient(browserViewer.getBrowser());
                if (getEditorInput() instanceof WebBrowserEditorInput) {
                    String url = browserViewer.getURL();
                    try {
                        setInput(new WebBrowserEditorInput(new URL(url), SWT.NONE, url));
                    } catch (MalformedURLException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    });
}
Also used : TitleListener(org.eclipse.swt.browser.TitleListener) MalformedURLException(java.net.MalformedURLException) FillLayout(org.eclipse.swt.layout.FillLayout) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) URL(java.net.URL) TitleEvent(org.eclipse.swt.browser.TitleEvent) WebBrowserEditorInput(org.eclipse.ui.internal.browser.WebBrowserEditorInput) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) Browser(org.eclipse.swt.browser.Browser)

Example 8 with ProgressAdapter

use of org.eclipse.swt.browser.ProgressAdapter in project mylyn.docs by eclipse.

the class MarkupEditor method updatePreview.

/**
 * updates the preview and optionally reveal the section that corresponds to the given outline item.
 *
 * @param outlineItem
 *            the outline item, or null
 */
private void updatePreview(final OutlineItem outlineItem) {
    if (previewDirty && browser != null) {
        Object result = null;
        try {
            result = browser.evaluate(JAVASCRIPT_GETSCROLLTOP);
        } catch (SWTException e) {
            // bug 517281 javascript fails for some Linux configurations
            logPreviewScrollingFailure(e);
        }
        final int verticalScrollbarPos = result != null ? ((Number) result).intValue() : 0;
        String xhtml = null;
        if (document == null) {
            // $NON-NLS-1$
            xhtml = "<?xml version=\"1.0\" ?><html xmlns=\"http://www.w3.org/1999/xhtml\"><body></body></html>";
        } else {
            try {
                IFile file = getFile();
                // $NON-NLS-1$
                String title = file == null ? "" : file.getName();
                if (title.lastIndexOf('.') != -1) {
                    title = title.substring(0, title.lastIndexOf('.'));
                }
                StringWriter writer = new StringWriter();
                HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer) {

                    @Override
                    protected void emitAnchorHref(String href) {
                        if (href != null && href.startsWith("#")) {
                            // $NON-NLS-1$
                            // $NON-NLS-1$
                            writer.writeAttribute(// $NON-NLS-1$
                            "onclick", // $NON-NLS-1$
                            String.format("javascript: window.location.hash = '%s'; return false;", href));
                            // $NON-NLS-1$//$NON-NLS-2$
                            writer.writeAttribute("href", "#");
                        } else {
                            super.emitAnchorHref(href);
                        }
                    }

                    @Override
                    public void beginHeading(int level, Attributes attributes) {
                        attributes.appendCssClass(CSS_CLASS_EDITOR_PREVIEW);
                        super.beginHeading(level, attributes);
                    }

                    @Override
                    public void beginBlock(BlockType type, Attributes attributes) {
                        attributes.appendCssClass(CSS_CLASS_EDITOR_PREVIEW);
                        super.beginBlock(type, attributes);
                    }
                };
                builder.setTitle(title);
                IPath location = file == null ? null : file.getLocation();
                if (location != null) {
                    builder.setBaseInHead(true);
                    builder.setBase(location.removeLastSegments(1).toFile().toURI());
                }
                String css = WikiTextUiPlugin.getDefault().getPreferences().getMarkupViewerCss();
                if (css != null && css.length() > 0) {
                    builder.addCssStylesheet(new HtmlDocumentBuilder.Stylesheet(new StringReader(css)));
                }
                MarkupLanguage markupLanguage = getMarkupLanguage();
                if (markupLanguage != null) {
                    markupLanguage = markupLanguage.clone();
                    if (markupLanguage instanceof AbstractMarkupLanguage) {
                        ((AbstractMarkupLanguage) markupLanguage).setEnableMacros(true);
                    }
                    if (markupLanguage instanceof AbstractMarkupLanguage) {
                        AbstractMarkupLanguage language = (AbstractMarkupLanguage) markupLanguage;
                        language.setFilterGenerativeContents(false);
                        language.setBlocksOnly(false);
                    }
                    MarkupParser markupParser = new MarkupParser();
                    markupParser.setBuilder(builder);
                    markupParser.setMarkupLanguage(markupLanguage);
                    markupParser.parse(document.get());
                } else {
                    builder.beginDocument();
                    builder.beginBlock(BlockType.PREFORMATTED, new Attributes());
                    builder.characters(document.get());
                    builder.endBlock();
                    builder.endDocument();
                }
                xhtml = writer.toString();
            } catch (Exception e) {
                StringWriter stackTrace = new StringWriter();
                PrintWriter writer = new PrintWriter(stackTrace);
                e.printStackTrace(writer);
                writer.close();
                StringWriter documentWriter = new StringWriter();
                HtmlDocumentBuilder builder = new HtmlDocumentBuilder(documentWriter);
                builder.beginDocument();
                builder.beginBlock(BlockType.PREFORMATTED, new Attributes());
                builder.characters(stackTrace.toString());
                builder.endBlock();
                builder.endDocument();
                xhtml = documentWriter.toString();
            }
        }
        browser.addProgressListener(new ProgressAdapter() {

            @Override
            public void completed(ProgressEvent event) {
                browser.removeProgressListener(this);
                if (outlineItem != null) {
                    revealInBrowser(outlineItem);
                } else {
                    // $NON-NLS-1$
                    browser.execute(String.format("window.scrollTo(0,%d);", verticalScrollbarPos));
                }
            }
        });
        browser.setText(xhtml);
        previewDirty = false;
    } else if (outlineItem != null && browser != null) {
        revealInBrowser(outlineItem);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) Attributes(org.eclipse.mylyn.wikitext.parser.Attributes) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) HtmlDocumentBuilder(org.eclipse.mylyn.wikitext.parser.builder.HtmlDocumentBuilder) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) SWTException(org.eclipse.swt.SWTException) SWTException(org.eclipse.swt.SWTException) StringWriter(java.io.StringWriter) BlockType(org.eclipse.mylyn.wikitext.parser.DocumentBuilder.BlockType) StringReader(java.io.StringReader) AbstractMarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.AbstractMarkupLanguage) MarkupLanguage(org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage) ProgressAdapter(org.eclipse.swt.browser.ProgressAdapter) MarkupParser(org.eclipse.mylyn.wikitext.parser.MarkupParser) PrintWriter(java.io.PrintWriter)

Aggregations

ProgressAdapter (org.eclipse.swt.browser.ProgressAdapter)8 Browser (org.eclipse.swt.browser.Browser)7 ProgressEvent (org.eclipse.swt.browser.ProgressEvent)7 WindowEvent (org.eclipse.swt.browser.WindowEvent)4 FillLayout (org.eclipse.swt.layout.FillLayout)4 Display (org.eclipse.swt.widgets.Display)4 OpenWindowListener (org.eclipse.swt.browser.OpenWindowListener)3 Shell (org.eclipse.swt.widgets.Shell)3 Test (org.junit.Test)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SWTException (org.eclipse.swt.SWTException)2 TitleListener (org.eclipse.swt.browser.TitleListener)2 VisibilityWindowAdapter (org.eclipse.swt.browser.VisibilityWindowAdapter)2 Menu (org.eclipse.swt.widgets.Menu)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1