Example usage for org.eclipse.jface.wizard ProgressMonitorPart internalWorked

List of usage examples for org.eclipse.jface.wizard ProgressMonitorPart internalWorked

Introduction

In this page you can find the example usage for org.eclipse.jface.wizard ProgressMonitorPart internalWorked.

Prototype

@Override
    public void internalWorked(double work) 

Source Link

Usage

From source file:org.eclipse.wst.internet.cache.internal.LicenseAcceptanceDialog.java

License:Open Source License

/**
 * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
 *//*from w  w  w .j a  va 2s  . co  m*/
protected Control createContents(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout layout = new GridLayout();
    composite.setLayout(layout);
    GridData gd = new GridData(SWT.FILL);
    gd.widthHint = 500;
    composite.setLayoutData(gd);

    // Display a statement about the license.
    Label licenseText1 = new Label(composite, SWT.NONE);
    licenseText1.setText(CacheMessages._UI_CACHE_DIALOG_LICENSE_STATEMENT1);
    Label urlText = new Label(composite, SWT.WRAP);
    gd = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
    urlText.setLayoutData(gd);
    urlText.setText(url);
    new Label(composite, SWT.NONE); // Spacing label.
    Label licenseText2 = new Label(composite, SWT.WRAP);
    gd = new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1);
    licenseText2.setLayoutData(gd);

    // Display the license in a browser.
    try {
        final Composite licenseTextComposite = new Composite(composite, SWT.NONE);
        final StackLayout stackLayout = new StackLayout();
        licenseTextComposite.setLayout(stackLayout);
        gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd.heightHint = 400;
        licenseTextComposite.setLayoutData(gd);
        // Create the loading progress monitor composite and part.
        Composite monitorComposite = new Composite(licenseTextComposite, SWT.NONE);
        monitorComposite.setLayout(new GridLayout());
        gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        gd.heightHint = 400;
        monitorComposite.setLayoutData(gd);
        final ProgressMonitorPart monitor = new ProgressMonitorPart(monitorComposite, new GridLayout());
        gd = new GridData(SWT.FILL, SWT.BOTTOM, true, true);
        monitor.setLayoutData(gd);
        monitor.beginTask(CacheMessages._UI_LOADING_LICENSE, 100);
        stackLayout.topControl = monitorComposite;
        // Create the browser.
        final Browser browser = new Browser(licenseTextComposite, SWT.BORDER);
        gd = new GridData(SWT.FILL, SWT.FILL, true, true);
        //gd.heightHint = 400;

        // It's important that the license URL is set even if we read 
        // the contents of the license file ourselves (see below) as
        // otherwise the progress monitor will not be called on certain
        // linux systems with certain browsers.
        browser.setUrl(licenseURL);

        // The browser widget has problems loading files stored in jars
        // so we read from the jar and set the browser text ourselves.
        // See bug 154721.
        if (licenseURL.startsWith("jar:")) {
            InputStream licenseStream = null;
            InputStreamReader isreader = null;
            BufferedReader breader = null;
            try {
                URL browserURL = new URL(licenseURL);
                licenseStream = browserURL.openStream();
                isreader = new InputStreamReader(licenseStream);
                breader = new BufferedReader(isreader);
                String str;
                StringBuffer sb = new StringBuffer();
                while ((str = breader.readLine()) != null) {
                    sb.append(str);
                }
                browser.setText(sb.toString());
            } finally {
                if (licenseStream != null) {
                    licenseStream.close();
                }
                if (isreader != null) {
                    isreader.close();
                }
                if (breader != null) {
                    breader.close();
                }
            }
        }

        browser.setLayoutData(gd);
        browser.addProgressListener(new ProgressListener() {

            /* (non-Javadoc)
             * @see org.eclipse.swt.browser.ProgressListener#changed(org.eclipse.swt.browser.ProgressEvent)
             */
            public void changed(ProgressEvent event) {
                if (event.total != 0) {
                    monitor.internalWorked(event.current * 100 / event.total);
                }
            }

            /* (non-Javadoc)
             * @see org.eclipse.swt.browser.ProgressListener#completed(org.eclipse.swt.browser.ProgressEvent)
             */
            public void completed(ProgressEvent event) {
                monitor.done();

                stackLayout.topControl = browser;
                agreeButton.setEnabled(true);
                licenseTextComposite.layout();
            }
        });

        licenseText2.setText(CacheMessages._UI_CACHE_DIALOG_LICENSE_STATEMENT2);
    } catch (Exception e) {
        // The browser throws an exception on platforms that do not support it. 
        // In this case we need to create an external browser.
        try {
            CachePlugin.getDefault().getWorkbench().getBrowserSupport().getExternalBrowser()
                    .openURL(new URL(licenseURL));
            licenseText2.setText(CacheMessages._UI_CACHE_DIALOG_LICENSE_STATEMENT2_NO_INTERNAL);
        } catch (Exception ex) {
            // In this case the license cannot be display. Inform the user of this and give them the license location.
            licenseText2.setText(MessageFormat.format(
                    CacheMessages._UI_CACHE_DIALOG_LICENSE_STATEMENT2_NO_BROWSER, new Object[] { licenseURL }));
        }
    }

    createButtonBar(composite);

    return composite;
}