Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Nebula » GridTableViewer - layout problem
GridTableViewer - layout problem [message #57699] Tue, 12 August 2008 04:44 Go to next message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

I am trying to display a GridTableViewer in a View, together with a
Composite that contains other widgets. I am running into some formatting
problems that are driving me crazy.

The following simplified code illustrates my problem.

If I incorporate the code as is, I get a GridTableViewer that displays
scrollbars, and fills the view as I expect.

However, if I uncomment the code labelled "###UNCOMMENT" in an attempt to
display a Composite with a Label above the GridTableViewer, I run into
problems. The scrollbars disappear, and the GridTableViewer no longer
fills the entire width of the view.

Any help with this would be much appreciated.

None of the examples I've found online combine a GridTableViewer with
another Composite in a view.

Thanks
Paul

---------------------------------------------------

package my_package;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
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.Label;
import org.eclipse.ui.part.ViewPart;

import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.widgets.grid.GridColumn;


public class MyView extends ViewPart {

private GridTableViewer gridViewer;

public void createPartControl(Composite parent) {

//###UNCOMMENT
// parent.setLayout(new GridLayout(1, false));
// parent.setLayoutData(new GridData(GridData.FILL));
//
// Composite topComposite = new Composite(parent, SWT.NONE);
// topComposite.setLayout(new GridLayout(3, false));
// topComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
//
// Label label = new Label(parent, SWT.LEFT);
// label.setText("Test Label");


Composite tableComposite = new Composite(parent, SWT.NONE);
tableComposite.setLayout(new FillLayout());

gridViewer =
new GridTableViewer(
tableComposite, SWT.V_SCROLL | SWT.H_SCROLL);

gridViewer.setLabelProvider(new ColumnLabelProvider());
gridViewer.setContentProvider(new TableContentProvider());
gridViewer.getGrid().setCellSelectionEnabled(true);

gridViewer.getGrid().setLinesVisible(true);
gridViewer.getGrid().setHeaderVisible(true);

GridColumn column1 = new GridColumn(gridViewer.getGrid(), SWT.NONE);
column1.setWidth(100);
column1.setText("Column 1");

GridColumn column2 = new GridColumn(gridViewer.getGrid(), SWT.NONE);
column2.setWidth(100);
column2.setText("Column 2");

gridViewer.setInput(this.getViewSite());

}

/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
gridViewer.getControl().setFocus();
}

class ColumnLabelProvider extends LabelProvider implements
ITableLabelProvider, ITableFontProvider, ITableColorProvider {

@Override
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getColumnText(Object element, int columnIndex) {
if (element instanceof TableRow) {
switch (columnIndex) {
case 0:
return ((TableRow)element).getC1();
case 1:
return ((TableRow)element).getC2();
default:
return "TBD";
}
}
return "TBD";
}

@Override
public Font getFont(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public Color getBackground(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public Color getForeground(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

}

class TableContentProvider implements IStructuredContentProvider {

private List<TableRow> tableRows;

public TableContentProvider() {
tableRows = new ArrayList<TableRow>();

for (int row=1; row<=100; row++) {
String c1 = "R" + row + " C1";
String c2 = "R" + row + " C2";
TableRow tableRow = new TableRow(c1, c2);
tableRows.add(tableRow);
}
}

@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
System.out.println("getElements called - length = " + tableRows.size());
return tableRows.toArray();
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
// TODO Auto-generated method stub

}

}

class TableRow {
private String c1;
private String c2;

public TableRow(String c1, String c2) {
this.c1 = c1;
this.c2 = c2;
}

public String getC1() {
return c1;
}

public String getC2() {
return c2;
}


}

}
Re: GridTableViewer - layout problem [message #57724 is a reply to message #57699] Tue, 12 August 2008 04:50 Go to previous messageGo to next message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

There was a typo in my previous example.

The following line:
// Label label = new Label(parent, SWT.LEFT);

Should be:
// Label label = new Label(topComposite, SWT.LEFT);

However, the problem I describe still exists. Any help gratefully
accepted.
Re: GridTableViewer - layout problem [message #57748 is a reply to message #57724] Tue, 12 August 2008 05:02 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5277
Registered: July 2009
Senior Member
I think you only have to set the

tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

and everything works soomthly else the control will request as much
space as it needs to display itself with out scroll bars (this is no
different with SWT-Table I think)

Tom

Paul Ruthven schrieb:
> There was a typo in my previous example.
>
> The following line:
> // Label label = new Label(parent, SWT.LEFT);
>
> Should be:
> // Label label = new Label(topComposite, SWT.LEFT);
>
> However, the problem I describe still exists. Any help gratefully
> accepted.
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: GridTableViewer - layout problem [message #57825 is a reply to message #57748] Tue, 12 August 2008 20:21 Go to previous messageGo to next message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

Hi Tom,

I tried what you suggested (see following code), but unfortunately that
makes the Grid disappear altogether. The only thing that gets displayed
in the View now is the label "Test Label". Below that is a large blank
area.

Any more suggestions would be appreciated.

Thanks
Paul

------------------------------------------------------------ --------------

package my_package;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
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.Label;
import org.eclipse.ui.part.ViewPart;

import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.widgets.grid.GridColumn;


public class MyView extends ViewPart {

private GridTableViewer gridViewer;

public void createPartControl(Composite parent) {

//###UNCOMMENT
parent.setLayout(new GridLayout(1, false));
parent.setLayoutData(new GridData(GridData.FILL));

Composite topComposite = new Composite(parent, SWT.NONE);
topComposite.setLayout(new GridLayout(3, false));
topComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

Label label = new Label(topComposite, SWT.LEFT);
label.setText("Test Label");


Composite tableComposite = new Composite(parent, SWT.NONE);
// tableComposite.setLayout(new FillLayout());
tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));



gridViewer =
new GridTableViewer(
tableComposite, SWT.V_SCROLL | SWT.H_SCROLL);

gridViewer.setLabelProvider(new ColumnLabelProvider());
gridViewer.setContentProvider(new TableContentProvider());
gridViewer.getGrid().setCellSelectionEnabled(true);

gridViewer.getGrid().setLinesVisible(true);
gridViewer.getGrid().setHeaderVisible(true);

GridColumn column1 = new GridColumn(gridViewer.getGrid(), SWT.NONE);
column1.setWidth(100);
column1.setText("Column 1");

GridColumn column2 = new GridColumn(gridViewer.getGrid(), SWT.NONE);
column2.setWidth(100);
column2.setText("Column 2");

gridViewer.setInput(this.getViewSite());

}

/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
gridViewer.getControl().setFocus();
}

class ColumnLabelProvider extends LabelProvider implements
ITableLabelProvider, ITableFontProvider, ITableColorProvider {

@Override
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getColumnText(Object element, int columnIndex) {
if (element instanceof TableRow) {
switch (columnIndex) {
case 0:
return ((TableRow)element).getC1();
case 1:
return ((TableRow)element).getC2();
default:
return "TBD";
}
}
return "TBD";
}

@Override
public Font getFont(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public Color getBackground(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public Color getForeground(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

}

class TableContentProvider implements IStructuredContentProvider {

private List<TableRow> tableRows;

public TableContentProvider() {
tableRows = new ArrayList<TableRow>();

for (int row=1; row<=100; row++) {
String c1 = "R" + row + " C1";
String c2 = "R" + row + " C2";
TableRow tableRow = new TableRow(c1, c2);
tableRows.add(tableRow);
}
}

@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
System.out.println("getElements called - length = " + tableRows.size());
return tableRows.toArray();
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
// TODO Auto-generated method stub

}

}

class TableRow {
private String c1;
private String c2;

public TableRow(String c1, String c2) {
this.c1 = c1;
this.c2 = c2;
}

public String getC1() {
return c1;
}

public String getC2() {
return c2;
}


}

}
Re: GridTableViewer - layout problem [message #57846 is a reply to message #57825] Tue, 12 August 2008 20:52 Go to previous message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

Hi Tom,

After further experimentation, I discovered that if I include both the
following lines, the Grid is displayed properly with scrollbars.

tableComposite.setLayout(new FillLayout());
tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

Sorry, I mis-understood your previous email. I didn't realise that you
can call setLayoutData with a GridData instance, whilst using a FillLayout.

Thanks
Paul
Re: GridTableViewer - layout problem [message #591695 is a reply to message #57699] Tue, 12 August 2008 04:50 Go to previous message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

There was a typo in my previous example.

The following line:
// Label label = new Label(parent, SWT.LEFT);

Should be:
// Label label = new Label(topComposite, SWT.LEFT);

However, the problem I describe still exists. Any help gratefully
accepted.
Re: GridTableViewer - layout problem [message #591704 is a reply to message #57724] Tue, 12 August 2008 05:02 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5277
Registered: July 2009
Senior Member
I think you only have to set the

tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

and everything works soomthly else the control will request as much
space as it needs to display itself with out scroll bars (this is no
different with SWT-Table I think)

Tom

Paul Ruthven schrieb:
> There was a typo in my previous example.
>
> The following line:
> // Label label = new Label(parent, SWT.LEFT);
>
> Should be:
> // Label label = new Label(topComposite, SWT.LEFT);
>
> However, the problem I describe still exists. Any help gratefully
> accepted.
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: GridTableViewer - layout problem [message #591732 is a reply to message #57748] Tue, 12 August 2008 20:21 Go to previous message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

Hi Tom,

I tried what you suggested (see following code), but unfortunately that
makes the Grid disappear altogether. The only thing that gets displayed
in the View now is the label "Test Label". Below that is a large blank
area.

Any more suggestions would be appreciated.

Thanks
Paul

------------------------------------------------------------ --------------

package my_package;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
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.Label;
import org.eclipse.ui.part.ViewPart;

import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.widgets.grid.GridColumn;


public class MyView extends ViewPart {

private GridTableViewer gridViewer;

public void createPartControl(Composite parent) {

//###UNCOMMENT
parent.setLayout(new GridLayout(1, false));
parent.setLayoutData(new GridData(GridData.FILL));

Composite topComposite = new Composite(parent, SWT.NONE);
topComposite.setLayout(new GridLayout(3, false));
topComposite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

Label label = new Label(topComposite, SWT.LEFT);
label.setText("Test Label");


Composite tableComposite = new Composite(parent, SWT.NONE);
// tableComposite.setLayout(new FillLayout());
tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));



gridViewer =
new GridTableViewer(
tableComposite, SWT.V_SCROLL | SWT.H_SCROLL);

gridViewer.setLabelProvider(new ColumnLabelProvider());
gridViewer.setContentProvider(new TableContentProvider());
gridViewer.getGrid().setCellSelectionEnabled(true);

gridViewer.getGrid().setLinesVisible(true);
gridViewer.getGrid().setHeaderVisible(true);

GridColumn column1 = new GridColumn(gridViewer.getGrid(), SWT.NONE);
column1.setWidth(100);
column1.setText("Column 1");

GridColumn column2 = new GridColumn(gridViewer.getGrid(), SWT.NONE);
column2.setWidth(100);
column2.setText("Column 2");

gridViewer.setInput(this.getViewSite());

}

/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
gridViewer.getControl().setFocus();
}

class ColumnLabelProvider extends LabelProvider implements
ITableLabelProvider, ITableFontProvider, ITableColorProvider {

@Override
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getColumnText(Object element, int columnIndex) {
if (element instanceof TableRow) {
switch (columnIndex) {
case 0:
return ((TableRow)element).getC1();
case 1:
return ((TableRow)element).getC2();
default:
return "TBD";
}
}
return "TBD";
}

@Override
public Font getFont(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public Color getBackground(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

@Override
public Color getForeground(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}

}

class TableContentProvider implements IStructuredContentProvider {

private List<TableRow> tableRows;

public TableContentProvider() {
tableRows = new ArrayList<TableRow>();

for (int row=1; row<=100; row++) {
String c1 = "R" + row + " C1";
String c2 = "R" + row + " C2";
TableRow tableRow = new TableRow(c1, c2);
tableRows.add(tableRow);
}
}

@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
System.out.println("getElements called - length = " + tableRows.size());
return tableRows.toArray();
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {
// TODO Auto-generated method stub

}

}

class TableRow {
private String c1;
private String c2;

public TableRow(String c1, String c2) {
this.c1 = c1;
this.c2 = c2;
}

public String getC1() {
return c1;
}

public String getC2() {
return c2;
}


}

}
Re: GridTableViewer - layout problem [message #591745 is a reply to message #57825] Tue, 12 August 2008 20:52 Go to previous message
Eclipse User
Originally posted by: paul.ruthven.synergy.net.au

Hi Tom,

After further experimentation, I discovered that if I include both the
following lines, the Grid is displayed properly with scrollbars.

tableComposite.setLayout(new FillLayout());
tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));

Sorry, I mis-understood your previous email. I didn't realise that you
can call setLayoutData with a GridData instance, whilst using a FillLayout.

Thanks
Paul
Previous Topic:Grid: GridViewer selection service does not support cell selection
Next Topic:Grid: GridViewer selection service does not support cell selection
Goto Forum:
  


Current Time: Wed Aug 20 14:42:34 EDT 2014

Powered by FUDForum. Page generated in 0.03606 seconds