Bug 63931 - [browser] setVisible(true) does not work
Summary: [browser] setVisible(true) does not work
Status: RESOLVED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.0   Edit
Hardware: PC Linux-GTK
: P1 major (vote)
Target Milestone: 3.0 RC2   Edit
Assignee: Christophe Cornu CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-05-25 13:53 EDT by Akmal CLA
Modified: 2004-06-03 16:47 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Akmal CLA 2004-05-25 13:53:11 EDT
taken from the Multipageeditor example , the folllowing code does not display
the browser widget under Linux as expected: it _ONLY_ shows it when the maximize
or minimize icon is pressed. Under Windows platform there is no problem:
My settings:
debian 3.0 sarge
gnome 2.4
kernel 2.6.6

#######code starts##############
package ss.editors;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Vector;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;

/**
 * An example showing how to create a multi-page editor. This example has 3
 * pages:
 * <ul>
 * <li>page 0 contains a nested text editor.
 * <li>page 1 allows you to change the font used in page 2
 * <li>page 2 shows the words in page 0 in sorted order *
 * <li>page 3 contains the html preview
 * </ul>
 */
public class MultiPageEditor extends MultiPageEditorPart implements
IResourceChangeListener
{

    /**
     * Creates page 1 of the multi-page vEditor, which allows you to change the
     * font used in page 2.
     */
    List               tmpFiles = null;

    public Browser     browser  = null;

    /** The text editor used in page 0. */
    private TextEditor editor;

    /** The font chosen in page 1. */
    private Font       font;

    /** The text widget used in page 2. */
    private StyledText text;

    private String     mytext   = "<html><body>test</body></html>";

    /**
     * Creates a multi-page editor example.
     */
    public MultiPageEditor()
    {
        super();
        ResourcesPlugin.getWorkspace().addResourceChangeListener(this);
    }

    /**
     * Creates page 0 of the multi-page editor, which contains a text editor.
     */
    void createPage0()
    {
        try
        {
            editor = new TextEditor();
            int index = addPage(editor, getEditorInput());
            setPageText(index, editor.getTitle());
        }
        catch (PartInitException e)
        {
            ErrorDialog.openError(getSite().getShell(), "Error creating nested
text editor", null, e.getStatus());
        }
    }

    /**
     * Creates page 1 of the multi-page editor, which allows you to change the
     * font used in page 2.
     */
    void createPage1()
    {

        Composite composite = new Composite(getContainer(), SWT.NONE);
        GridLayout layout = new GridLayout();
        composite.setLayout(layout);
        layout.numColumns = 2;

        Button fontButton = new Button(composite, SWT.NONE);
        GridData gd = new GridData(GridData.BEGINNING);
        gd.horizontalSpan = 2;
        fontButton.setLayoutData(gd);
        fontButton.setText("Change Font...");

        fontButton.addSelectionListener(new SelectionAdapter() {

            public void widgetSelected(SelectionEvent event)
            {
                setFont();
            }
        });

        int index = addPage(composite);
        setPageText(index, "Properties");
    }

    /**
     * Creates page 2 of the multi-page editor, which shows the sorted text.
     */
    void createPage2()
    {
        Composite composite = new Composite(getContainer(), SWT.NONE);
        FillLayout layout = new FillLayout();
        composite.setLayout(layout);
        text = new StyledText(composite, SWT.H_SCROLL | SWT.V_SCROLL);
        text.setEditable(false);

        int index = addPage(composite);
        setPageText(index, "Preview");
    }

    /**
     * Creates the pages of the multi-page editor.
     */
    protected void createPages()
    {
        createPage0();
        createPage1();
        createPage2();
        createPage3();
    }

    /**
     * The <code>MultiPageEditorPart</code> implementation of this
     * <code>IWorkbenchPart</code> method disposes all nested editors.
     * Subclasses may extend.
     */
    public void dispose()
    {
        ResourcesPlugin.getWorkspace().removeResourceChangeListener(this);
        super.dispose();
    }

    /**
     * Saves the multi-page editor's document.
     */
    public void doSave(IProgressMonitor monitor)
    {
        getEditor(0).doSave(monitor);
    }

    /**
     * Saves the multi-page editor's document as another file. Also updates the
     * text for page 0's tab, and updates this multi-page editor's input to
     * correspond to the nested editor's.
     */
    public void doSaveAs()
    {
        IEditorPart editor = getEditor(0);
        editor.doSaveAs();
        setPageText(0, editor.getTitle());
        setInput(editor.getEditorInput());
    }

    /*
     * (non-Javadoc) Method declared on IEditorPart
     */
    public void gotoMarker(IMarker marker)
    {
        setActivePage(0);
        IDE.gotoMarker(getEditor(0), marker);
    }

    /**
     * The <code>MultiPageEditorExample</code> implementation of this method
     * checks that the input is an instance of <code>IFileEditorInput</code>.
     */
    public void init(IEditorSite site, IEditorInput editorInput) throws
PartInitException
    {
        if (!(editorInput instanceof IFileEditorInput)) throw new
PartInitException("Invalid Input: Must be IFileEditorInput");
        super.init(site, editorInput);
    }

    /*
     * (non-Javadoc) Method declared on IEditorPart.
     */
    public boolean isSaveAsAllowed()
    {
        return true;
    }

    /**
     * Calculates the contents of page 2 when the it is activated.
     */
    protected void pageChange(int newPageIndex)
    {
        super.pageChange(newPageIndex);
        if (newPageIndex == 2)
        {
            sortWords();
        }
        if (newPageIndex == 3)
        {
            showPreview(newPageIndex);
        }
    }

    /**
     * Closes all project files on project close.
     */
    public void resourceChanged(final IResourceChangeEvent event)
    {
        if (event.getType() == IResourceChangeEvent.PRE_CLOSE)
        {
            Display.getDefault().asyncExec(new Runnable() {

                public void run()
                {
                    IWorkbenchPage[] pages =
getSite().getWorkbenchWindow().getPages();
                    for (int i = 0; i < pages.length; i++)
                    {
                        if (((FileEditorInput)
editor.getEditorInput()).getFile().getProject().equals(event.getResource()))
                        {
                            IEditorPart editorPart =
pages[i].findEditor((FileEditorInput) editor.getEditorInput());
                            pages[i].closeEditor(editorPart, true);
                        }
                    }
                }
            });
        }
    }

    /**
     * Sets the font related data to be applied to the text in page 2.
     */
    void setFont()
    {
        FontDialog fontDialog = new FontDialog(getSite().getShell());
        fontDialog.setFontList(text.getFont().getFontData());
        FontData fontData = fontDialog.open();
        if (fontData != null)
        {
            if (font != null) font.dispose();
            font = new Font(text.getDisplay(), fontData);
            text.setFont(font);
        }
    }

    /**
     * Sorts the words in page 0, and shows them in page 2.
     */
    void sortWords()
    {

        String editorText =
editor.getDocumentProvider().getDocument(editor.getEditorInput()).get();

        StringTokenizer tokenizer = new StringTokenizer(editorText, "
\t\n\r\f!@#\u0024%^&*()-_=+`~[]{};:'\",.<>/?|\\");
        ArrayList editorWords = new ArrayList();
        while (tokenizer.hasMoreTokens())
        {
            editorWords.add(tokenizer.nextToken());
        }

        Collections.sort(editorWords, Collator.getInstance());
        StringWriter displayText = new StringWriter();
        for (int i = 0; i < editorWords.size(); i++)
        {
            displayText.write(((String) editorWords.get(i)));
            displayText.write(System.getProperty("line.separator"));
        }
        text.setText(displayText.toString());
    }

    private void showPreview(int newPageIndex)
    {
        if (newPageIndex == 3)
        {
            //                                 browser.refresh();
            try
            {
                File file = File.createTempFile("temp", ".html");
                OutputStream stream = new FileOutputStream(file);
                PrintWriter writer = new PrintWriter(stream);

                writer.println(text);
                writer.flush();
                stream.close();

                if (tmpFiles == null)
                {
                    tmpFiles = new Vector();
                }

                tmpFiles.add(file.getAbsolutePath());

                browser.setText(mytext);

            }
            catch (Exception e)
            {

            }
        }

    }

    protected void createControl(Composite parent, int style)
    {

        browser = new Browser(parent, SWT.NONE);

        browser.setUrl("http://eclipse.org");

        int index = addPage(browser);
        setPageText(index, "Html Preview");

    }

    void createPage3()
    {
        createControl(getContainer(), SWT.BOLD);

    }

}
###code ends ####
Comment 1 Christophe Cornu CLA 2004-05-26 12:28:40 EDT
Previous jface plugin sample can be reduced to the following SWT test:
	static int cnt = 0;
	public static void main0(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("PRBrowser");
		shell.setLayout(new FillLayout());
		TabFolder t = new TabFolder(shell, SWT.NONE);
		TabItem i1 = new TabItem(t, SWT.NONE);
		TabItem i2 = new TabItem(t, SWT.NONE);
		Button b = new Button(t, SWT.PUSH);
		final Browser browser = new Browser(t, SWT.NONE);
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				browser.setText("<html><body>hello "+cnt+"</body></html>");
				cnt++;
			}
		});
		i1.setControl(b);
		i2.setControl(browser);

		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}		
	}


The bottom of the problem lies in setVisible which does not cause the browser to
reappear and redraw without further resizing.

	public static void main(String[] args) {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setText("PRBrowser");

		Button b = new Button(shell, SWT.PUSH);
		b.setBounds(0,0,100,50);
		final Button b2 = new Button(shell, SWT.TOGGLE);
		b2.setBounds(200,0,100,50);
		final Browser browser = new Browser(shell, SWT.NONE);
		browser.setBounds(100,100,200,200);
		b.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				browser.setText("<html><body>hello "+cnt+"</body></html>");
				cnt++;
			}
		});
		b2.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				boolean flag = b2.getSelection();
				if (!flag)
					System.out.println("make browser visible "+flag);
				browser.setVisible(flag);
			}
		});
		shell.setSize(400,400);
		shell.open();
		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}		
	}
Comment 2 Christophe Cornu CLA 2004-06-03 16:47:16 EDT
fixed v>20040603