ExpandBar in Eclipse View Part - eclipse

I am trying to add an expand bar to an Eclipse viewpart. When I click the expand button I would like the viewpart to move items below the expand bar down and show the expanded items. What currently happens is the expand bar items just disappear below the items below the expand bar. Any thoughts?
final ExpandBar expandBar = new ExpandBar(parent, SWT.NONE);
expandBar.setBackground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
expandBar.setSpacing(0);
fd_toolBar.top = new FormAttachment(expandBar, 6);
FormData fd_expandBar = new FormData();
fd_expandBar.top = new FormAttachment(0, 62);
fd_expandBar.left = new FormAttachment(0, 3);
expandBar.setLayoutData(fd_expandBar);
formToolkit.paintBordersFor(expandBar);
final ExpandItem xpndtmWarningDetails = new ExpandItem(expandBar, SWT.NONE);
xpndtmWarningDetails.setExpanded(true);
xpndtmWarningDetails.setText("Warning Details");
final Composite composite_1 = new Composite(expandBar, SWT.NONE);
composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
xpndtmWarningDetails.setControl(composite_1);
formToolkit.paintBordersFor(composite_1);
xpndtmWarningDetails.setHeight(xpndtmWarningDetails.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
Label lblTest = new Label(composite_1, SWT.NONE);
lblTest.setBounds(10, 10, 55, 15);
lblTest.setText("Test");
expandBar.addExpandListener(new ExpandListener(){
#Override
public void itemCollapsed(ExpandEvent e) {
expandBar.setSize(expandBar.getSize().x, xpndtmWarningDetails.getHeaderHeight());
parent.layout(true);
}
#Override
public void itemExpanded(ExpandEvent e) {
expandBar.setSize(expandBar.getSize().x, 300);
expandBar.layout(true);
parent.layout(true);
}
});

I think the ExpandBar works best when used like it is in this example...
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet343.java
... with several expand bars stacked on top of each other, and nothing else mixed in.
I think the functionality your looking for can be accomplished with an ExpandableComposite object. It depends on what else is going on in your ViewPart.
Here's a quick example of an ExpandableComposite.
package com.amx.designsuite.rcp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
public class ExpandableCompositeExample extends Composite {
/**
* Create the composite.
* #param parent
* #param style
*/
public ExpandableCompositeExample(final Composite parent, int style) {
super(parent, style);
FormToolkit toolkit;
toolkit = new FormToolkit(parent.getDisplay());
final ScrolledForm form = toolkit.createScrolledForm(parent);
form.setText("Title for Form holding Expandable Composite (optional)");
TableWrapLayout layout = new TableWrapLayout();
form.getBody().setLayout(layout);
ExpandableComposite expandableCompsite = toolkit.createExpandableComposite(form.getBody(), ExpandableComposite.TREE_NODE | ExpandableComposite.SHORT_TITLE_BAR);
toolkit.paintBordersFor(expandableCompsite);
expandableCompsite.setText("Expandable Composite Title (Optional)");
expandableCompsite.setExpanded(true);
Text txtMyNewText = toolkit.createText(expandableCompsite, "Text to show when composite is expanded", SWT.NONE);
expandableCompsite.setClient(txtMyNewText);
}
#Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
}

Related

How to bind SWT objects to the center of the application window?

I am using SWT to create an application GUI, and I don't really need to resize the components, but it does bother me that when the window is maximized, the components stay left-aligned. Is there a way to fix this with SWT or do I need to utilize a different set of GUI tools?
Thanks in advance. I am using SWT 4.8 for this application.
EDIT: Images
Small: https://imgur.com/CPbAlaZ
Maximized: https://imgur.com/4d6YXcl
Provided images are a basic application using the following code
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class TestWindow {
protected Shell shlSwtApplicationExample;
private Text text;
/**
* Launch the application.
* #param args
*/
public static void main(String[] args) {
try {
TestWindow window = new TestWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlSwtApplicationExample.open();
shlSwtApplicationExample.layout();
while (!shlSwtApplicationExample.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlSwtApplicationExample = new Shell();
shlSwtApplicationExample.setSize(705, 529);
shlSwtApplicationExample.setText("SWT Application Example");
Composite composite = new Composite(shlSwtApplicationExample, SWT.NONE);
composite.setBounds(10, 10, 669, 465);
text = new Text(composite, SWT.BORDER);
text.setBounds(22, 10, 334, 295);
Button btnNewButton = new Button(composite, SWT.NONE);
btnNewButton.setBounds(49, 384, 137, 26);
btnNewButton.setText("New Button");
Button button = new Button(composite, SWT.NONE);
button.setText("New Button");
button.setBounds(300, 384, 137, 26);
}
}
I would not recommend using setBounds since it does not resize the components when you resize the application. Use Layouts, like for example below I have used GridLayout for both the Shell and the Composite which will properly arrange the UI when resize happens.
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
public class TestWindow {
protected Shell shlSwtApplicationExample;
private Text text;
/**
* Launch the application.
* #param args
*/
public static void main(String[] args) {
try {
TestWindow window = new TestWindow();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents(display);
shlSwtApplicationExample.open();
shlSwtApplicationExample.layout();
shlSwtApplicationExample.setLayout(new GridLayout(1, false));
while (!shlSwtApplicationExample.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
* #param display
*/
protected void createContents(Display display) {
shlSwtApplicationExample = new Shell(display);
shlSwtApplicationExample.setLayout(new GridLayout(1, false));
Composite txtcomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
txtcomposite.setLayout(new GridLayout(1, false));
txtcomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Composite btncomposite = new Composite(shlSwtApplicationExample, SWT.NONE);
btncomposite.setLayout(new GridLayout(2, false));
btncomposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
text = new Text(txtcomposite, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button btnNewButton = new Button(btncomposite, SWT.NONE);
btnNewButton.setText("New Button");
Button button = new Button(btncomposite, SWT.NONE);
button.setText("New Button");
shlSwtApplicationExample.setText("SWT Application Example");
//shlSwtApplicationExample.setSize(705, 529);
}
}
Since you are using setBounds you will need to add a Control listener to the shell to be told about resize and move events. You will then have to recalculate the positions on each resize event.
shlSwtApplicationExample.addControlListener(
new ControlListener() {
#Override
public void controlMoved(ControlEvent event) {
// No action
}
#Override
public void controlResized(ControlEvent event) {
Rectangle rect = shlSwtApplicationExample.getClientArea();
// TODO Call new `setBounds` on each control based on the
// client area size
}
});
This might be a good time to learn about using Layouts instead of setBounds (see here). Layouts will automatically deal with resizes.

programmatically scroll ScrolledComposite TableViewer item

I have a dialog with large table viewer that lays on ScrolledComposite.
I need programmatically scroll ScrolledComposite to select item from TableViewer.
Looks like a easy task but I really got stack.
I tried number of thinks and non of them are working.
There is my sample code:
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
/**
* Scroll a Viewer 99th element
*
*/
public class Snippet008RevealElement {
public class MyModel {
public int counter;
public MyModel(int counter) {
this.counter = counter;
}
#Override
public String toString() {
return "Item " + this.counter;
}
}
public Snippet008RevealElement(Shell shell) {
ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
scrolledComposite.setLayoutData(data);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
Composite main = new Composite(scrolledComposite, SWT.NONE);
main.setLayout(new GridLayout());
main.setLayoutData(new GridData(GridData.FILL_BOTH));
final TableViewer v = new TableViewer(main);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(ArrayContentProvider.getInstance());
MyModel[] model = createModel();
v.setInput(model);
v.getTable().setLinesVisible(true);
// v.reveal(model[99]);
// v.getTable().setSelection(99);
TableItem[] items = v.getTable().getItems();
TableItem item = items[99];
scrolledComposite.getVerticalBar().setSelection(item.getBounds().y);
scrolledComposite.setContent(main);
scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
private MyModel[] createModel() {
MyModel[] elements = new MyModel[100];
for( int i = 0; i < 100; i++ ) {
elements[i] = new MyModel(i);
}
return elements;
}
/**
* #param args
*/
public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet008RevealElement(shell);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
the real challenge or a bug is to find real bounds for TableItem item = items[99]; that not visible on composite.
You can use the setOrigin method of ScrolledComposite for this using something like:
scrolledComposite.setContent(main);
scrolledComposite.setMinSize(main.computeSize(SWT.DEFAULT, SWT.DEFAULT));
// Need to run the calculations after all size calculations have been done
// So do asynchronously.
final Display display = scrolledComposite.getDisplay();
display.asyncExec(() ->
{
final Table table = v.getTable();
final TableItem item = table.getItem(99);
Rectangle itemBounds = item.getBounds();
// Convert to be relative to scrolled composite
itemBounds = display.map(viewer.getTable(), scrolledComposite, itemBounds);
scrolledComposite.setOrigin(0, itemBounds.y);
});
Note: The bounds calculations are not accurate if you call this code during the initialization of the controls so I have shown it being done asynchronously here. The asyncExec is not needed if you run the code from a Button or something like that.

SmartGWT - can't click into items into windows modal

I have one problem using Window with setIsModal(true).
I have this code:
#Override
public void onModuleLoad() {
Button tester = new Button("Tester");
tester.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final com.smartgwt.client.widgets.Window win = new com.smartgwt.client.widgets.Window();
win.setTitle("Ventana Tester");
win.setWidth(900);
win.setHeight(600);
win.setIsModal(true);
win.setShowModalMask(true);
win.centerInPage();
win.setMinimized(false);
win.addCloseClickHandler(new CloseClickHandler() {
#Override
public void onCloseClick(CloseClientEvent event) {
win.destroy();
}
});
PlanBoard pb = new PlanBoard();
win.addItem(pb);
win.show();
}
});
vlPrincipal.addMember(tester);
RootPanel.get("main").add(vlPrincipal);
}
and this is PlanBoard class:
public class PlanBoard extends VLayout{
private CaptionPanel contentDetallePlan = new CaptionPanel("DETALLES DEL PLAN");
private CaptionPanel contentAtributosPlan = new CaptionPanel("ATRIBUTOS DE PLAN");
private CaptionPanel contentSeccionesPlan = new CaptionPanel("SECCIONES");
public PlanBoard(){
contentDetallePlan.setStyleName("estiloCaptionPanel");
contentAtributosPlan.setStyleName("estiloCaptionPanel");
addMember(contentDetallePlan);
addMember(contentAtributosPlan);
addMember(contentSeccionesPlan);
preparaDetallePlan();
preparaAtributosPlan();
}
private void preparaDetallePlan(){
VLayout contenedorSeccion = new VLayout();
FlexTable table1 = new FlexTable();
FlexTable table2 = new FlexTable();
FlexTable table3 = new FlexTable();
Label np = new Label("Nombre de Plan:");
Label npText = new Label("Plan B");
Label tc = new Label("Tipo de Carta:");
DynamicForm tcForm = new DynamicForm();
ComboBoxItem tcBox = new ComboBoxItem();
tcBox.setWidth(250);
tcBox.setShowTitle(false);
tcForm.setItems(tcBox);
Label pr = new Label("Periodo:");
DynamicForm prForm = new DynamicForm();
ComboBoxItem prBox = new ComboBoxItem();
prBox.setWidth(150);
prBox.setShowTitle(false);
prForm.setItems(prBox);
Label dp = new Label("Descripcion:");
DynamicForm dpForm = new DynamicForm();
TextAreaItem dpText = new TextAreaItem();
dpText.setShowTitle(false);
dpText.setWidth(600);
dpForm.setItems(dpText);
table1.setWidget(0, 0, np);
table1.setWidget(0, 1, npText);
table2.setWidget(0, 0, tc);
table2.setWidget(0, 1, tcForm);
table2.setWidget(0, 2, pr);
table2.setWidget(0, 3, prForm);
table3.setWidget(0, 1, dp);
table3.setWidget(1, 1, dpForm);
contenedorSeccion.addMember(table1);
contenedorSeccion.addMember(table2);
contenedorSeccion.addMember(table3);
contentDetallePlan.add(contenedorSeccion);
}
private void preparaAtributosPlan(){
VLayout contenedorSeccion = new VLayout();
FlexTable table1 = new FlexTable();
Label fe = new Label("Firma Electornica:");
CheckboxItem feCheck = new CheckboxItem();
DateItem feFechaIni = new DateItem();
DateItem feFechaFin = new DateItem();
feFechaIni.setUseTextField(true);
feCheck.setShowTitle(false);
DynamicForm feForm = new DynamicForm();
feForm.setItems(feCheck,feFechaIni,feFechaFin);
table1.setWidget(0, 0, fe);
table1.setWidget(0, 1, feForm);
contenedorSeccion.addMember(table1);
contentAtributosPlan.add(contenedorSeccion);
}
The problem is when I try to click on CheckBoxItem or DateItem, I can't edit them, but when I don't use setIsModal(true), it works fine.
I don't know how to set the Window to modal(true), and have those items working on that window.
Here is your code cleaned (a little bit) and improved so that what you want to achieve (which is the modal window with selectable/actionable controls):
PlanBoard.java
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.StaticTextItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.layout.VLayout;
public class PlanBoard extends VLayout {
public PlanBoard(){
preparaDetallePlan();
preparaAtributosPlan();
preparaSecciones();
}
private void preparaDetallePlan(){
StaticTextItem np = new StaticTextItem("id2", "Nombre de Plan:");
StaticTextItem npText = new StaticTextItem("id2", "Plan B");
ComboBoxItem tcBox = new ComboBoxItem();
tcBox.setTitle("Tipo de Carta");
tcBox.setWidth(250);
ComboBoxItem prBox = new ComboBoxItem();
tcBox.setTitle("Periodo");
prBox.setWidth(150);
StaticTextItem dp = new StaticTextItem("id3", "Descripcion:");
TextAreaItem dpText = new TextAreaItem();
dpText.setShowTitle(false);
dpText.setWidth(600);
dpText.setStartRow(true);
dpText.setEndRow(true);
dpText.setColSpan(2);
DynamicForm form = new DynamicForm();
form.setItems(np, npText, tcBox, prBox, dp, dpText);
form.setIsGroup(true);
form.setGroupTitle("DETALLES DE PLAN");
addMember(form);
}
private void preparaAtributosPlan(){
StaticTextItem fe = new StaticTextItem("id4", "Firma Electornica:");
CheckboxItem feCheck = new CheckboxItem();
feCheck.setShowTitle(false);
DateItem feFechaIni = new DateItem();
DateItem feFechaFin = new DateItem();
feFechaIni.setUseTextField(true);
DynamicForm form = new DynamicForm();
form.setItems(fe, feCheck, feFechaIni, feFechaFin);
form.setIsGroup(true);
form.setGroupTitle("ATRIBUTOS DE PLAN");
addMember(form);
}
private void preparaSecciones(){
DynamicForm form = new DynamicForm();
form.setIsGroup(true);
form.setGroupTitle("SECCIONES");
addMember(form);
}
}
TestCases.java (rename to your EntryPoint class name, which you don't specify):
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;
import com.google.gwt.core.client.EntryPoint;
public class TestCases implements EntryPoint {
public void onModuleLoad() {
IButton tester = new IButton("Tester");
tester.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
final Window win = new Window();
win.setTitle("Ventana Tester");
win.setWidth(900);
win.setHeight(600);
win.setIsModal(true);
win.setShowModalMask(true);
win.centerInPage();
win.setMinimized(false);
win.addCloseClickHandler(new CloseClickHandler() {
#Override
public void onCloseClick(CloseClickEvent event) {
win.destroy();
}
});
PlanBoard pb = new PlanBoard();
win.addItem(pb);
win.show();
}
});
VLayout vlPrincipal = new VLayout();
vlPrincipal.addMember(tester);
vlPrincipal.draw();
}
}
Some notes:
Don't mix GWT and SmartGWT widgets unless absolutely necessary and only when you really know what you are doing (because it will generated unexpected results, as the one you are experiencing). In your case, the mixing is unnecessary!!
You can add the titles to the different controls you are using, and they will display as labels. You can specify if the title label appears above or to one side, per control.
Take a look at this SmartGWT demo to learn how to configure and use the different types of DynamicForm controls. Many of the things you were trying to do, you were doing more difficult than necessary.
On the "stylistic" aspect, don't write your code using Spanglish. I speak Spanish, so I understand, but it limits a lot the feedback you get, because your code is so much more difficult to understand. Use English (at least for code you intend to post in SO).

Cannot reduce size of RCP view when migrated to Eclipse 4

I'm currently working on migrating a set of eclipse RCP applications from 3.6 to 4.2.
I'm struggling with an issue with RCP perspectives that view height cannot be reduced below a certain size (looks like 10% from the window height).
The code works fine in eclipse 3.x and user can reduce the view height by dragging the border.
However, in 4.x the height of top most view (button view) can only be reduced upto a certain value.
Can anybody help with this, please?
public class Perspective implements IPerspectiveFactory {
public static final String ID = "im.rcpTest2.fixedperspective";
public void createInitialLayout(IPageLayout layout) {
String editorArea = layout.getEditorArea();
layout.setEditorAreaVisible(false);
layout.addStandaloneView(ButtonView.ID, false, IPageLayout.TOP,
0.1f, editorArea);
layout.addStandaloneView(View.ID, false, IPageLayout.LEFT,
0.25f, editorArea);
IFolderLayout folder = layout.createFolder("messages", IPageLayout.TOP,
0.5f, editorArea);
folder.addPlaceholder(View2.ID + ":*");
folder.addView(View2.ID+":2");
folder.addView(View2.ID+":3");
layout.getViewLayout(ButtonView.ID).setCloseable(false);
layout.getViewLayout(View.ID).setCloseable(false);
}
}
public class ButtonView extends ViewPart {
public ButtonView() {
}
public static final String ID = "im.rcptest2.ButtonView"; //$NON-NLS-1$
private Text text;
/**
* Create contents of the view part.
* #param parent
*/
#Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
container.setLayout(new GridLayout(2, false));
text = new Text(container, SWT.BORDER);
text.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
{
Button btnMybutton = new Button(container, SWT.NONE);
btnMybutton.setText("MyButton");
}
}
#Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
This looks like the value
int minSashPercent = 10;
in org.eclipse.e4.ui.workbench.renderers.swt.SashLayout
There does not seem to be a way to change this value. So the only thing you could do would be to write a custom Sash Renderer class by providing your own Renderer Factory.

GXT ToolBar is scrolled

I'm developing a simple GXT widget - it's a TreePanel with a ToolBar added using setTopComponent.
The problem is that as soon as the tree is large enough so that it can be scrolled, the scroll-bar doesn't scroll the tree only, but scrolls the ToolBar as well.
What should be change so that ToolBar remains on the top of page, and only the tree is scrolled.
public class TreePanelExample extends LayoutContainer {
#Override
protected void onRender(Element parent, int index) {
super.onRender(parent, index);
Folder model = getTreeModel();
TreeStore<ModelData> store = new TreeStore<ModelData>();
store.add(model.getChildren(), true);
final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
tree.setDisplayProperty("name");
tree.setAutoLoad(true);
ToolBar toolBar = new ToolBar();
toolBar.setBorders(true);
toolBar.add(new Button("Dummy button", new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
Info.display("Dummy button", "I'm so dumb!");
}
}));
ContentPanel panel = new ContentPanel();
panel.setHeaderVisible(false);
panel.setCollapsible(false);
panel.setFrame(false);
panel.setAutoWidth(true);
panel.setAutoHeight(true);
// setting fixed size doesn't make any difference
// panel.setHeight(100);
panel.setTopComponent(toolBar);
panel.add(tree);
add(panel);
}
The problem is that
TreePanelExample extends LayoutContainer
while instead it should extend Viewport.
Additionally I shouldn't have used
panel.setAutoWidth(true);
panel.setAutoHeight(true);
Plus it is necessary to add the main panel using
new BorderLayoutData(LayoutRegion.CENTER);
Here is the complete solution:
public class TreePanelExample extends Viewport {
public TreePanelExample() {
super();
setLayout(new BorderLayout());
Folder model = getTreeModel();
TreeStore<ModelData> store = new TreeStore<ModelData>();
store.add(model.getChildren(), true);
final TreePanel<ModelData> treePanel = new TreePanel<ModelData>(store);
treePanel.setDisplayProperty("name");
treePanel.setAutoLoad(true);
ToolBar toolBar = new ToolBar();
toolBar.setBorders(true);
toolBar.add(new Button("Dummy button", new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
Info.display("Dummy button", "I'm so dumb!");
}
}));
ContentPanel panel = new ContentPanel();
panel.setBodyBorder(false);
panel.setHeaderVisible(false);
panel.setTopComponent(toolBar);
panel.setLayout(new FitLayout());
panel.add(treePanel);
BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
centerData.setMargins(new Margins(5, 5, 5, 5));
centerData.setCollapsible(true);
panel.syncSize();
add(panel, centerData);
}