Search in sources :

Example 51 with Browser

use of org.eclipse.swt.browser.Browser in project portfolio by buchen.

the class EmbeddedBrowser method createControl.

public Control createControl(Composite parent, Consumer<Browser> functions) {
    Composite container = new Composite(parent, SWT.NONE);
    GridLayoutFactory.fillDefaults().applyTo(container);
    try {
        browser = new Browser(container, SWT.NONE);
        browser.setJavascriptEnabled(true);
        GridDataFactory.fillDefaults().grab(true, true).applyTo(browser);
        if (functions != null)
            functions.accept(browser);
        browser.setText(loadHTML(htmlpage));
        Menu menu = new Menu(browser);
        browser.setMenu(menu);
    } catch (SWTError e) {
        // if creation of embedded browser fails, provide some hints
        PortfolioPlugin.log(e);
        String stacktrace = ExceptionUtils.getStackTrace(e);
        Text text = new Text(container, SWT.WRAP);
        GridDataFactory.fillDefaults().grab(true, true).applyTo(text);
        text.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
        text.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_DARK_RED));
        text.setText(MessageFormat.format(Messages.MsgEmbeddedBrowserError, stacktrace));
    }
    return container;
}
Also used : SWTError(org.eclipse.swt.SWTError) Composite(org.eclipse.swt.widgets.Composite) Text(org.eclipse.swt.widgets.Text) Menu(org.eclipse.swt.widgets.Menu) Browser(org.eclipse.swt.browser.Browser)

Example 52 with Browser

use of org.eclipse.swt.browser.Browser in project epp.mpc by eclipse.

the class NewsViewer method showUrl.

protected void showUrl(String url) {
    if (browser instanceof Browser) {
        ((Browser) browser).setUrl(url);
    } else {
        Link link = (Link) browser;
        // $NON-NLS-1$
        link.setData("href", url);
        // $NON-NLS-1$
        link.setText(NLS.bind("<a href=\"{0}\">{1}</a>", url, url));
        link.setEnabled(true);
        link.getParent().layout();
    }
}
Also used : Link(org.eclipse.swt.widgets.Link) Browser(org.eclipse.swt.browser.Browser)

Example 53 with Browser

use of org.eclipse.swt.browser.Browser in project epp.mpc by eclipse.

the class NewsViewer method createBrowser.

protected Control createBrowser(Composite parent) {
    final Browser browser;
    try {
        if (!WorkbenchBrowserSupport.getInstance().isInternalWebBrowserAvailable()) {
            return null;
        }
        browser = new Browser(parent, SWT.NONE);
    } catch (Throwable t) {
        // embedded browser not available
        return null;
    }
    browser.addProgressListener(new ProgressRunnable());
    browser.addLocationListener(new NewsUrlHandler(this));
    this.browser = browser;
    return browser;
}
Also used : Browser(org.eclipse.swt.browser.Browser)

Example 54 with Browser

use of org.eclipse.swt.browser.Browser in project epp.mpc by eclipse.

the class OverviewToolTip method createToolTipContentArea.

@Override
protected Composite createToolTipContentArea(Event event, final Composite parent) {
    Shell shell = parent.getShell();
    setData(Shell.class.getName(), shell);
    AbstractMarketplaceDiscoveryItem.setWidgetId(shell, DiscoveryItem.WIDGET_ID_OVERVIEW);
    GridLayoutFactory.fillDefaults().applyTo(parent);
    Color backgroundColor = parent.getDisplay().getSystemColor(SWT.COLOR_WHITE);
    final Composite container = new Composite(parent, SWT.NULL);
    container.setBackground(backgroundColor);
    boolean hasImage = false;
    if (overview.getScreenshot() != null) {
        hasImage = true;
    }
    final boolean addLearnMoreLink = browser != null && overview.getUrl() != null && overview.getUrl().length() > 0;
    final int borderWidth = 1;
    final int heightHint = SCREENSHOT_HEIGHT + (borderWidth * 2);
    final int widthHint = SCREENSHOT_WIDTH;
    final int containerWidthHintWithImage = 650;
    final int containerWidthHintWithoutImage = 500;
    GridDataFactory.fillDefaults().grab(true, true).hint(hasImage ? containerWidthHintWithImage : containerWidthHintWithoutImage, SWT.DEFAULT).applyTo(container);
    GridLayoutFactory.fillDefaults().numColumns((leftImage != null) ? 3 : 2).margins(5, 5).spacing(3, 0).applyTo(container);
    if (leftImage != null) {
        Label imageLabel = new Label(container, SWT.NONE);
        imageLabel.setImage(leftImage);
        imageLabel.setBackground(backgroundColor);
        int imageWidthHint = leftImage.getBounds().width + 5;
        GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.BEGINNING).hint(imageWidthHint, SWT.DEFAULT).applyTo(imageLabel);
    }
    String summary = overview.getSummary();
    Composite summaryContainer = new Composite(container, SWT.NULL);
    summaryContainer.setBackground(backgroundColor);
    GridLayoutFactory.fillDefaults().applyTo(summaryContainer);
    GridDataFactory gridDataFactory = GridDataFactory.fillDefaults().grab(true, true).span(hasImage ? 1 : 2, 1);
    if (hasImage) {
        gridDataFactory.hint(widthHint, heightHint);
    }
    gridDataFactory.applyTo(summaryContainer);
    Browser summaryLabel = new Browser(summaryContainer, SWT.NULL);
    Font dialogFont = JFaceResources.getDialogFont();
    FontData[] fontData = dialogFont.getFontData();
    // $NON-NLS-1$
    String attr = "";
    // $NON-NLS-1$
    String fontSizeUnitOfMeasure = "pt";
    if (Platform.OS_MACOSX.equals(Platform.getOS())) {
        // $NON-NLS-1$
        fontSizeUnitOfMeasure = "px";
    } else if (Platform.OS_WIN32.equals(Platform.getOS())) {
        // $NON-NLS-1$
        attr = "overflow: auto; ";
    }
    String cssStyle = // $NON-NLS-1$ //$NON-NLS-2$
    "body, p, div, *  {" + attr + "font-family:\"" + fontData[0].getName() + "\",Arial,sans-serif !important;font-size:" + fontData[0].getHeight() + fontSizeUnitOfMeasure + // $NON-NLS-1$ //$NON-NLS-2$
    " !important;" + // $NON-NLS-1$
    "} body { margin: 0px; background-color: white;}";
    summaryLabel.setFont(dialogFont);
    String html = // $NON-NLS-1$//$NON-NLS-2$
    "<html><style>" + cssStyle + "</style><body>" + TextUtil.cleanInformalHtmlMarkup(summary) + // $NON-NLS-1$
    "</body></html>";
    summaryLabel.setText(html);
    summaryLabel.setBackground(backgroundColor);
    // instead of opening links in the tooltip, open a new browser window
    summaryLabel.addLocationListener(new LocationListener() {

        public void changing(LocationEvent event) {
            if (event.location.equals("about:blank")) {
                // $NON-NLS-1$
                return;
            }
            event.doit = false;
            OverviewToolTip.this.hide();
            WorkbenchUtil.openUrl(event.location, IWorkbenchBrowserSupport.AS_EXTERNAL);
        }

        public void changed(LocationEvent event) {
        }
    });
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).hint(SWT.DEFAULT, hasImage ? SWT.DEFAULT : SCREENSHOT_HEIGHT).applyTo(summaryLabel);
    if (hasImage) {
        final Composite imageContainer = new Composite(container, SWT.BORDER);
        GridLayoutFactory.fillDefaults().applyTo(imageContainer);
        GridDataFactory.fillDefaults().grab(false, false).align(SWT.CENTER, SWT.BEGINNING).hint(widthHint + (borderWidth * 2), heightHint).applyTo(imageContainer);
        Label imageLabel = new Label(imageContainer, SWT.NULL);
        GridDataFactory.fillDefaults().hint(widthHint, SCREENSHOT_HEIGHT).indent(borderWidth, borderWidth).applyTo(imageLabel);
        imageLabel.setBackground(backgroundColor);
        imageLabel.setSize(widthHint, SCREENSHOT_HEIGHT);
        provideImage(imageLabel, source, overview.getScreenshot());
        final Cursor handCursor = new Cursor(imageLabel.getDisplay(), SWT.CURSOR_HAND);
        imageLabel.setCursor(handCursor);
        imageLabel.addDisposeListener(new DisposeListener() {

            public void widgetDisposed(DisposeEvent e) {
                handCursor.dispose();
            }
        });
        imageLabel.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseDown(MouseEvent e) {
                OverviewToolTip.this.hide();
                WorkbenchUtil.openUrl(overview.getScreenshot(), IWorkbenchBrowserSupport.AS_EXTERNAL);
            }
        });
        // creates a border
        imageContainer.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY));
    }
    if (addLearnMoreLink) {
        Link link = new Link(summaryContainer, SWT.NULL);
        AbstractMarketplaceDiscoveryItem.setWidgetId(link, DiscoveryItem.WIDGET_ID_LEARNMORE);
        GridDataFactory.fillDefaults().grab(false, false).align(SWT.BEGINNING, SWT.CENTER).applyTo(link);
        link.setText(Messages.OverviewToolTip_learnMoreLink);
        link.setBackground(backgroundColor);
        link.setToolTipText(NLS.bind(Messages.OverviewToolTip_openUrlInBrowser, overview.getUrl()));
        link.addSelectionListener(new SelectionListener() {

            public void widgetSelected(SelectionEvent e) {
                OverviewToolTip.this.hide();
                browser.openUrl(overview.getUrl());
            }

            public void widgetDefaultSelected(SelectionEvent e) {
                widgetSelected(e);
            }
        });
    }
    if (!hasImage) {
        // prevent overviews with no image from providing unlimited text.
        Point optimalSize = summaryContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
        if (optimalSize.y > (heightHint + 10)) {
            ((GridData) summaryContainer.getLayoutData()).heightHint = heightHint;
            container.layout(true);
        }
    }
    // hack: cause the tooltip to gain focus so that we can capture the escape key
    // this must be done async since the tooltip is not yet visible.
    Display.getCurrent().asyncExec(new Runnable() {

        public void run() {
            if (!parent.isDisposed()) {
                parent.setFocus();
            }
        }
    });
    return container;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) Label(org.eclipse.swt.widgets.Label) Cursor(org.eclipse.swt.graphics.Cursor) DisposeEvent(org.eclipse.swt.events.DisposeEvent) Font(org.eclipse.swt.graphics.Font) Shell(org.eclipse.swt.widgets.Shell) LocationListener(org.eclipse.swt.browser.LocationListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) LocationEvent(org.eclipse.swt.browser.LocationEvent) MouseEvent(org.eclipse.swt.events.MouseEvent) Composite(org.eclipse.swt.widgets.Composite) Color(org.eclipse.swt.graphics.Color) FontData(org.eclipse.swt.graphics.FontData) MouseAdapter(org.eclipse.swt.events.MouseAdapter) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) GridDataFactory(org.eclipse.jface.layout.GridDataFactory) Link(org.eclipse.swt.widgets.Link) Browser(org.eclipse.swt.browser.Browser) SelectionListener(org.eclipse.swt.events.SelectionListener)

Example 55 with Browser

use of org.eclipse.swt.browser.Browser in project jbosstools-openshift by jbosstools.

the class BrowserBasedLoginDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    Composite container = (Composite) super.createDialogArea(parent);
    container.setLayout(new GridLayout());
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(container);
    browser = new Browser(container, SWT.BORDER);
    browser.setText("Loading");
    Browser.clearSessions();
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).grab(true, true).applyTo(browser);
    final ProgressBar progressBar = new ProgressBar(container, SWT.NONE);
    GridDataFactory.fillDefaults().align(SWT.FILL, SWT.FILL).applyTo(progressBar);
    ProgressListener progressListener = new ProgressListener() {

        @Override
        public void changed(ProgressEvent event) {
            if (event.total <= 0)
                return;
            int ratio = event.current * 100 / event.total;
            progressBar.setSelection(ratio);
        }

        @Override
        public void completed(ProgressEvent event) {
            progressBar.setSelection(0);
            info = processor.getRequestInfo(browser, browser.getUrl(), browser.getText());
            if (null != info) {
                close();
            }
        }
    };
    browser.addProgressListener(progressListener);
    setURL(startURL);
    return container;
}
Also used : GridLayout(org.eclipse.swt.layout.GridLayout) Composite(org.eclipse.swt.widgets.Composite) ProgressListener(org.eclipse.swt.browser.ProgressListener) ProgressEvent(org.eclipse.swt.browser.ProgressEvent) ProgressBar(org.eclipse.swt.widgets.ProgressBar) Point(org.eclipse.swt.graphics.Point) Browser(org.eclipse.swt.browser.Browser)

Aggregations

Browser (org.eclipse.swt.browser.Browser)117 Shell (org.eclipse.swt.widgets.Shell)40 FillLayout (org.eclipse.swt.layout.FillLayout)29 Composite (org.eclipse.swt.widgets.Composite)29 Display (org.eclipse.swt.widgets.Display)29 GridData (org.eclipse.swt.layout.GridData)28 Test (org.junit.Test)26 GridLayout (org.eclipse.swt.layout.GridLayout)20 LocationEvent (org.eclipse.swt.browser.LocationEvent)18 ProgressEvent (org.eclipse.swt.browser.ProgressEvent)18 Point (org.eclipse.swt.graphics.Point)18 IOException (java.io.IOException)16 SWTError (org.eclipse.swt.SWTError)16 BrowserFunction (org.eclipse.swt.browser.BrowserFunction)14 WindowEvent (org.eclipse.swt.browser.WindowEvent)14 Label (org.eclipse.swt.widgets.Label)14 URL (java.net.URL)13 LocationAdapter (org.eclipse.swt.browser.LocationAdapter)13 LocationListener (org.eclipse.swt.browser.LocationListener)13 ProgressAdapter (org.eclipse.swt.browser.ProgressAdapter)13