views:

27

answers:

1

Hi All,

I've been banging away at this for a while now and I can't seem to get anywhere. I've tried all of the examples I can find online and nothing seems to work! I haven't been able to find much on this problem which leads me to think I'm missing something basic. . .

In my Eclipse RCP program I want to display a dialog that will show a list of errors that occurred while loading a data file. I have overridden TitleAreaDialog and simply want to display a scrollable Text containing the list of errors and an OK button.

The problem is that the Text vertical scroll bars don't become active - the Text just grows taller to fit the text. This makes the dialog window height increases until it either fits the Text box or until it reaches the height of the screen - and then it just cuts off the bottom of the Text box.

How do I prevent the Dialog/Text box from growing too large? What am I missing?

Thanks for your help!!
-Christine

...

Here is a simple program showing my Dialog:

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;


public class ScrollableDialogRunner {

    public static void main(String[] args) {
        System.out.println("starting");
        Display display = new Display ();
        Shell shell = new Shell (display);

        String errors = "one\ntwo\nthree\nfour\nfive\n";
        for(int i = 0; i < 5; i++){
            errors += errors;
        }

        ScrollableDialog dialog = new ScrollableDialog(shell, "Errors occurred during load", "The following errors occurred while loaded file 'x.data'", errors);
        dialog.open();
    }
}


class ScrollableDialog extends TitleAreaDialog {
    private String title;
    private String text;
    private String scrollableText;

    public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
        super(parentShell);
        this.title = title;
        this.text = text;
        this.scrollableText = scrollableText;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        parent.setLayout(layout);

        GridData gridData = new GridData();
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalAlignment = GridData.FILL;

        Text scrollable = new Text(parent, SWT.BORDER | SWT.V_SCROLL);
        scrollable.setLayoutData(gridData);
        scrollable.setText(scrollableText);

        return parent;
    }

    @Override
    public void create() {
        super.create();

        setTitle(title);
        setMessage(text, IMessageProvider.ERROR);
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        Button okButton = createButton(parent, OK, "OK", true);
        okButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                close();
            }
        });
    }

    @Override
    protected boolean isResizable() {
        return false;
    }
}
A: 

Assign a size to the dialog; otherwise, the dialog will layout the children asking them for their "preferred" size (which is infinite for the text widget) and will resize itself accordingly.

[EDIT] This version works. See my comments for details.

class ScrollableDialog extends TitleAreaDialog {
    private String title;
    private String text;
    private String scrollableText;

    public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
        super(parentShell);
        this.title = title;
        this.text = text;
        this.scrollableText = scrollableText;
    }

    @Override
    protected Control createDialogArea(Composite parent) {
        Composite composite = (Composite) super.createDialogArea (parent); // Let the dialog create the parent composite

        GridData gridData = new GridData();
        gridData.grabExcessHorizontalSpace = true;
        gridData.horizontalAlignment = GridData.FILL;
        gridData.grabExcessVerticalSpace = true; // Layout vertically, too! 
        gridData.verticalAlignment = GridData.FILL;

        Text scrollable = new Text(composite, SWT.BORDER | SWT.V_SCROLL);
        scrollable.setLayoutData(gridData);
        scrollable.setText(scrollableText);

        return composite;
    }

    @Override
    public void create() {
        super.create();

        // This is not necessary; the dialog will become bigger as the text grows but at the same time,
        // the user will be able to see all (or at least more) of the error message at once
        //getShell ().setSize (300, 300);
        setTitle(title);
        setMessage(text, IMessageProvider.ERROR);

    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        Button okButton = createButton(parent, OK, "OK", true);
        okButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                close();
            }
        });
    }

    @Override
    protected boolean isResizable() {
        return true; // Allow the user to change the dialog size!
    }
}
Aaron Digulla
Thanks for the reply Aaron. What you say makes sense and it is along the lines of what I was thinking my problem was.How/where can I set the size of the dialog? `TitleAreaDialog` doesn't expose any setSize (or similar) method that I can find. I tried setting the size of the parent inside of ScrollableDialog at every place I had access to it but each time it just seems to be ignored. Any suggestions?Thanks again!-Christine
Christine
In `createDialogArea()`, call `getShell()` and set its size.
Aaron Digulla
Hmm, that resized the dialog but again it just cut off the bottom of the Text box. The Text box continues way below the bottom of the window and the scroll bar is still grayed out (I can't see the bottom of it either). Any other ideas?
Christine
Sorry! I replied too quickly. I was just able to get it to work by also setting the size of the Text object after setting the shell size. It seems weird to have to do this. If I am missing something else, please let me know. But this works for me. :) Thanks for your help Aaron!!
Christine
You must allow the GridLayout to layout both dimensions. See my edit.
Aaron Digulla