Create a Composite that adapt to my data in Eclipse SWT - java

My First try is:
public MultiLangugeText(Composite parent, int style) {
super(parent, style);
setLayout(new RowLayout(SWT.HORIZONTAL));
Map<Locale, String> texts = new HashMap<Locale, String>();
texts.put(Locale.GERMAN, "Hey du");
texts.put(Locale.ENGLISH, "Hey you");
texts.put(Locale.FRENCH, "Bon Jour");
setTexts(texts);
public void setTexts(Map<Locale, String> texts) {
for (Locale locale : texts.keySet()) {
createTextComposite(locale, texts.get(locale));
}
}
private void createTextComposite(Locale locle, String localizedString) {
Composite composite = formToolkit.createComposite(this, SWT.BORDER);
formToolkit.paintBordersFor(composite);
composite.setLayout(new RowLayout(SWT.HORIZONTAL));
CLabel label = new CLabel(composite, SWT.NONE);
label.setText(locle.toString());
formToolkit.adapt(label);
formToolkit.paintBordersFor(label);
Text txtString = new Text(composite, SWT.BORDER);
txtString.setText(localizedString);
formToolkit.adapt(txtString, true, true);
}
But this code will not draw anything on my UI. At least when I try to render it in the Deisigner and in the Preview. I have to tests if this works, when I use it in my application. But I don't get where error is.

Okay it only fails in the designer, not when you actually use the composite elsewehre.

Related

How to display a composite on the top of another composite which contains a table viewer?

In the createControl(parent) method of the Wizard Page, the code is as below-
top = new Composite(parent, SWT.NONE);
top.setLayout(new FillLayout());
setControl(top);
setPageComplete(false);
createViewer(top);
I want to add a row above the composite denoted by top.
It doesn't allows me. If I add it, the composite denoted by top gets lost.
The below is rendered for the top composite -
The moment I place the below code before the top composite, the top composite gets lost -
upper = new Composite(parent, SWT.NONE);
upper.setLayout(new FillLayout());
setControl(upper);
Label label = new Label(page, SWT.NONE);
label.setText("Some text to disply");
Please suggest on how to achieve the same.
The below is the createControl snippet in my case. It breaks the scrolling and size of the table viewer if add a row.
#Override
public void createControl(Composite parent) {
/*top = new Composite(parent, SWT.NONE);
top.setLayout(new FillLayout());
setControl(top);*/
setPageComplete(false);
top = new Composite(parent, SWT.NONE);
top.setLayout(new GridLayout());
setControl(top);
Label label = new Label(top, SWT.NONE);
label.setText("Some text to disply");
label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
createViewer(top);
}
private void createViewer(Composite parent) {
tableLayout = new TableColumnLayout();
// A separate composite containing just the table viewer is required
Composite tableComp = new Composite(parent, SWT.NONE);
tableComp.setLayout(tableLayout);
viewer = new TableViewer(tableComp, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
createColumns(parent, viewer);
final Table table = viewer.getTable();
table.setHeaderVisible(true);
table.setLinesVisible(true);
viewer.setContentProvider(new ArrayContentProvider());
// Layout the viewer
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
}
public TableViewer getViewer() {
return viewer;
}
// This will create the columns for the table
private void createColumns(final Composite parent, final TableViewer viewer) {
String[] titles = { "Node Status", "Node Type", "Node Name" };
int[] bounds = { 100, 100, 100 };
Image HEADER = CommonUtility.HEADER;
TableViewerColumn col = null;
// First column is for type
col = createTableViewerColumn(HEADER, titles[0], bounds[0], 0);
col.setLabelProvider(new CentredImageCellLabelProvider() {
#Override
public Image getImage(Object element) {
GenerateSkeletonComponentsStatusModel model = (GenerateSkeletonComponentsStatusModel) element;
if (model.isNew()) {
return CommonUtility.CHECKED;
} else {
return CommonUtility.UNCHECKED;
}
}
});
// Weight for column
tableLayout.setColumnData(col.getColumn(), new ColumnWeightData(50));
// now the newly created
col = createTableViewerColumn(HEADER, titles[1], bounds[1], 1);
col.setLabelProvider(new CentredImageCellLabelProvider() {
#Override
public Image getImage(Object element) {
GenerateSkeletonComponentsStatusModel model = (GenerateSkeletonComponentsStatusModel) element;
if ("ns_flow".equals(model.getIcon())) {
return CommonUtility.FLOW_SERVICE;
}
if ("ns_open_interface".equals(model.getIcon())) {
return CommonUtility.FOLDER;
}
if ("ns_record".equals(model.getIcon())) {
return CommonUtility.DOCUMENT_TYPE;
}
if ("ns_package".equals(model.getIcon())) {
return CommonUtility.PACKAGE;
}
if("RestResource".equals(model.getIcon())){
return CommonUtility.RESTFUL_FOLDER;
}
return null;
}
});
// Weight for column
tableLayout.setColumnData(col.getColumn(), new ColumnWeightData(50));
// First column is for the component name
col = createTableViewerColumn(HEADER, titles[2], bounds[2], 2);
col.setLabelProvider(new ColumnLabelProvider() {
#Override
public String getText(Object element) {
GenerateSkeletonComponentsStatusModel model = (GenerateSkeletonComponentsStatusModel) element;
return model.getComponentName();
}
});
// Weight for column
tableLayout.setColumnData(col.getColumn(), new ColumnWeightData(100));
}
private TableViewerColumn createTableViewerColumn(Image image, String title, int bound, final int colNumber) {
final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.LEAD);
final TableColumn column = viewerColumn.getColumn();
column.setImage(image);
column.setText(title);
column.setWidth(bound);
column.setResizable(true);
column.setMoveable(true);
column.setAlignment(SWT.CENTER);
return viewerColumn;
}
A wizard page must only have one top level composite. You can add multiple controls (including nested composites) to that composite.
So for a label above to table:
top = new Composite(parent, SWT.NONE);
top.setLayout(new GridLayout());
setControl(top);
Label label = new Label(top, SWT.NONE);
label.setText("Some text to disply");
label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
TableViewer viewer = new TableViewer(top, .... flags
viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

ScrolledComposite rendering incorrectly when adding a large amount of content

When trying to add a lot of labels on to a Composite that is contained in a ScrolledComposite after a certain number (1638) for me, it just seems to give up and stop drawing the components after it. Is this a hard limit on a number of things that can be displayed or something I'm doing wrong.
This also happens if I just add one label with 2000 lines of text in.
public class LoadsOfLabelsTestDialog extends Dialog
{
List<Label> displaylabels = new ArrayList<>();
Composite content, list;
ScrolledComposite scroll;
public LoadsOfLabelsTestDialog(Shell parentShell)
{
super(parentShell);
}
#Override
protected void configureShell(Shell shell)
{
super.configureShell(shell);
shell.setSize(new Point(700, 500));
shell.setText("FML"); //$NON-NLS-1$
}
#Override
public Control createDialogArea(final Composite comp)
{
content = (Composite) super.createDialogArea(comp);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button set1 = new Button(content, SWT.PUSH);
set1.setText("Display List 1");
set1.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
List<String> rows = new ArrayList<>();
for (int i = 0; i < 2000; i++) {
rows.add(i +" row");
}
updateList(rows);
}
});
scroll = new ScrolledComposite(content, SWT.V_SCROLL);
list = new Composite(scroll, SWT.NONE);
list.setLayout(new GridLayout(1, true));
scroll.setContent(list);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
new Label(content, SWT.HORIZONTAL | SWT.SEPARATOR);
setScrollSize();
return content;
}
private void setScrollSize() {
scroll.setMinSize(list.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
private void updateList(List<String> rows) {
if (this.displaylabels == null) {
this.displaylabels = new ArrayList<>();
}
for (Label l : displaylabels) {
l.dispose();
}
this.displaylabels.clear();
for (String item : rows) {
addListLabel(item);
}
content.layout(true, true);
setScrollSize();
}
private void addListLabel(String whoText) {
Label a = new Label(list, SWT.NONE);
a.setText(whoText);
this.displaylabels.add(a);
}
public static void main(String[] args)
{
Display d = new Display();
Shell s = new Shell();
LoadsOfLabelsTestDialog fml = new LoadsOfLabelsTestDialog(s);
fml.open();
}
}
You hit a hard limit, probably the maximum size of a control. While this limit may differ slightly on other platforms, you can't size a control arbitrarily.
As #greg-449 suggested, prefer using a Table. If the content per table row is more than just an image and text, you can add a paint listener to draw the row contents yourself.

Eclipse RCP tableViewer not displaying correctly

I have two jface TableViewers in my view, both defined two occupy the same amount of space vertically, or in other words, both defined to grab the Excess vertical space and to FILL the vertical alignment.
This way, the view is correctly displayed, in a small screen the table with more input gets more space, however, in a bigger screen both tables get the same amount of space, that's fine:
However, since the input from the bottom table is the same everytime, what I really want is to make the bottom table to get only the amount of space necessary to display that input.. But I can't do it, I set the bottom table to not grab the excess vertical alignement, but what it does is this:
And then.. When I resize the window.. BAM, it displays correctly:
I can't understand this behavior.. Can somebody help?
This is my code:
private void createTableViewers(Composite parent) {
// TABLE 1
Composite compositeTableImpostos = new Composite(parent, SWT.NONE);
compositeTableImpostos.setLayout(new FillLayout());
compositeTableImpostos.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
tableImpostos = createTableViewer(compositeTableImpostos,
InterfaceVARS.REGISTOCOLABORADORES_TABLEVIEWER_COLUMNSTYLE,
LabelVARS.REGISTOCOLABORADORES_TABLEVIEWER_COLUMN_TITLES,
InterfaceVARS.REGISTOCOLABORADORES_TABLEVIEWER_COLUMNSIZE,
InterfaceVARS.STYLE_TABLE_SINGLE);
tableImpostos.setContentProvider(contentProvider = new ImpostosContentProvider(tableImpostos));
tableImpostos.setLabelProvider(new ImpostosLabelProvider());
// TABLE 2
Composite compositeTableLinhasDespesa = new Composite(parent, SWT.NONE);
compositeTableLinhasDespesa.setLayout(new FillLayout());
final GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
compositeTableLinhasDespesa.setLayoutData(gridData2);
tableLinhasDespesa = createTableViewer(compositeTableLinhasDespesa,
InterfaceVARS.REGISTODESPESAS_TABLEVIEWER_COLUMNSTYLE,
LabelVARS.REGISTODESPESA_TABLEVIEWER_COLUMN_TITLES,
InterfaceVARS.REGISTODESPESAS_TABLEVIEWER_COLUMNSIZE,
InterfaceVARS.STYLE_TABLE_MULTI);
tableLinhasDespesa.setContentProvider(linhasContentProvider = new LinhasDespesaContentProvider(tableLinhasDespesa, contentProvider));
tableLinhasDespesa.setLabelProvider(new LinhasDespesaLabelProvider());
}
public class CustomTableViewer {
//Viewer
private TableViewer tableViewer;
private TableColumnLayout tableViewerLayout;
//Columns
private String titles[];
private int columnSize[];
private int columnStyle[];
public CustomTableViewer(Composite composite, int[] columnStyle, String titles[], int[] columnSize, int STYLE){
this.titles = titles;
this.columnStyle = columnStyle;
this.columnSize = columnSize;
tableViewerLayout = new TableColumnLayout();
Composite compositeTable = new Composite(composite, SWT.NONE);
compositeTable.setLayout(tableViewerLayout);
tableViewer = new TableViewer(compositeTable, STYLE);
Table table = tableViewer.getTable();
table.setLinesVisible(true);
table.setHeaderVisible(true);
addTableViewerColumns();
addTableViewerListeners();
}
private void addTableViewerColumns() {
for(int index=0 ; index<titles.length ; index++)
createTableColumn(columnStyle[index], titles[index], columnSize[index], columnSize[index], true, true);
}
private TableViewerColumn createTableColumn(int STYLE, String title, int weight, int minWidth, boolean moveable, boolean resizable) {
TableViewerColumn viewerColumn = new TableViewerColumn(tableViewer, STYLE);
TableColumn column = viewerColumn.getColumn();
column.setText(title);
column.setMoveable(moveable);
tableViewerLayout.setColumnData(column, new ColumnWeightData(weight, minWidth, resizable));
return viewerColumn;
}
public void addTableViewerListeners() {
tableViewer.getTable().addControlListener(new ResizeListener(tableViewer));
}
public TableViewer getTableViewer(){
return tableViewer;
}
public class ResizeListener implements ControlListener {
private TableViewer tableViewer;
public ResizeListener(TableViewer tableViewer) {
this.tableViewer = tableViewer;
}
#Override
public void controlMoved(ControlEvent e) {
}
#Override
public void controlResized(ControlEvent e) {
TableColumn[] colunas = tableViewer.getTable().getColumns();
for(int i=0; i<colunas.length; i++) {
tableViewerLayout.setColumnData(colunas[i], new ColumnWeightData(
columnSize[i],columnSize[i],
true));
}
}
}
}
Calling the method layout(new Control[] {tableViewer2.getControl()}) in the parent composite of the tables after setInput() was called did the job!

Using wizard and ScrolledForm(jface and forms api)

I'm new to eclipse plugin - SWT development. I'm trying to create wizard page having number of text fields and combo boxes. For better look n feel I'm trying to use FormToolkit for creating components and add them in ScrolledForm. But with this nothing is rendered on wizard page at runtime and there is no error also.
Questions:
Is it possible to have scrolled container inside wizard page?
Can we mix JFace and forms api?
(removed unwanted code)
Here is wizard page code:
public class ContactWizardPage extends WizardPage {
private static int counter;
private Form form;
public ContactWizardPage() {
super("New Contact Wizard" + ++counter, "New Contact Wizard" + counter, null);
setMessage("Please enter contact info." + counter);
}
public void createControl(final Composite parent) {
createControlWithoutToolkit(parent);
// commenting out toolkit code
// createControlWithToolkit(parent);
}
public void createControlWithoutToolkit(final Composite parent) {
Composite composite = new Composite(parent, SWT.DEFAULT);
composite.setLayout(new GridLayout(2, true));
Label lblFirstName = new Label(composite, SWT.FLAT);
lblFirstName.setText("First Name");
Label lblLastName = new Label(composite, SWT.FLAT);
lblLastName.setText("Last Name");
Text txtFirstName = new Text(composite, SWT.FLAT);
Text txtLastName = new Text(composite, SWT.FLAT);
Label lblEmail = new Label(composite, SWT.FLAT);
lblEmail.setText("Email");
GridDataFactory.swtDefaults().span(2, 1).align(
SWT.FILL,
SWT.BEGINNING).applyTo(lblEmail);
Text txtEmail = new Text(composite, SWT.FLAT);
GridDataFactory.swtDefaults().span(2, 1).align(
SWT.FILL,
SWT.BEGINNING).applyTo(txtEmail);
setControl(composite);
}
public void createControlWithToolkit(final Composite parent) {
FormToolkit toolkit = new FormToolkit(Display.getCurrent());
ScrolledForm form = toolkit.createScrolledForm(parent);
Composite composite = form.getBody();
composite.setLayout(new GridLayout(2, true));
Label lblFirstName = toolkit.createLabel(composite, "First Name");
Label lblLastName = toolkit.createLabel(composite, "Last Name");
Text txtFirstName = toolkit.createText(composite, "");
Text txtLastName = toolkit.createText(composite, "");
Label lblEmail = toolkit.createLabel(composite, "Email");
GridDataFactory.swtDefaults().span(2, 1).align(
SWT.FILL,
SWT.BEGINNING).applyTo(lblEmail);
Text txtEmail = toolkit.createText(composite, "");
GridDataFactory.swtDefaults().span(2, 1).align(
SWT.FILL,
SWT.BEGINNING).applyTo(txtEmail);
setControl(composite);
}
}
Here is Wizard code:
public class SampleNewWizard extends Wizard implements INewWizard {
public SampleNewWizard() {
super();
setNeedsProgressMonitor(true);
}
#Override
public IWizardPage getNextPage(IWizardPage page) {
return super.getNextPage(page);
}
public void addPages() {
addPage(new ContactWizardPage());
addPage(new ContactWizardPage());
addPage(new ContactWizardPage());
addPage(new ContactWizardPage());
}
public boolean performFinish() {
return true;
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
}
}
With this code first page of wizard shows fine but second page is never rendered properly. :(
here are screenshots:
first page:
second page:
The line
parent.getShell().setSize(240, 320);
is upsetting something (and is not something you should do in a wizard page as the Wizard class deals with sizing). Testing here it works fine without it (and I get your problem with it).
Make sure that you do not dispose the FormToolKit as long as the wizard is open. Paint events need the toolkit after createControl() has finished.
My solution is to create and dispose the FormToolKit in the wizard:
#Override
public void createPageControls(Composite pageContainer) {
toolkit = new FormToolkit(pageContainer.getDisplay());
super.createPageControls(pageContainer);
}
#Override
public void dispose() {
super.dispose();
if (toolkit != null) {
try { toolkit.dispose(); } catch (Exception e) {}
}
}
public FormToolkit getToolkit() {
return toolkit;
}
In the wizard pages you can than use ((FormWizard) getWizard).getToolkit() to get a reference to the wizard's form toolkit.
#Override
public void createControl(Composite parent) {
FormToolkit toolkit = ((FormWizard) getWizard()).getToolkit();
...
Hope this helps ...

How to update/refresh a view in an eclipse plug-in?

I have an eclipse plug-in with a single view (like the eclipse helloworld-view-plugin-project). In the view-file I get an event when I want to update the view.
In this view I have a GridData in a Group with multiple labels. I have several services which register to the programe and whose status should be shown in this GridData.
Edit: In order to better show my problem I updated this post and added the whole code:
CreatePartControl():
public void createPartControl(Composite _parent) {
parent = _parent;
createContents();
addBindings();
makeActions();
contributeToActionBars();
}
CreateContents():
protected void createContents() {
// fixed
checkIcon = //...
errorIcon = //...
smallFont = SWTResourceManager.getFont("Segoe UI", 7, SWT.NORMAL);
// content
gl_shell = new GridLayout(1, false);
//margins, etc. for gl_shell
parent.setLayout(gl_shell);
final Label lblGreeting = new Label(parent, SWT.NONE);
lblGreeting.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 1, 1));
lblGreeting.setText("Hi " + Preferences.getPreName());
// -- GROUP YOUR STATS (show all services)
createStatusGroupBox();
}
createStatusGroupBox():
private Group grpYourStatus = null; // outside the method for access in listener (below)
private void createStatusGroupBox() {
grpYourStatus = new Group(parent, SWT.NONE);
grpYourStatus.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
grpYourStatus.setText("Your upload status");
grpYourStatus.setLayout(new GridLayout(3, false));
// add message if no service is registered
if ( r.getServiceList().size() == 0 ) {
Label status = new Label(grpYourStatus, SWT.NONE);
status.setText("No service registered.");
new Label(grpYourStatus, SWT.NONE); //empty
new Label(grpYourStatus, SWT.NONE); //empty
}
// add labels (status, message, name) for each registered service
for ( IRecorderObject service : r.getServiceList() ) {
Label name = new Label(grpYourStatus, SWT.NONE);
Label status = new Label(grpYourStatus, SWT.NONE);
Label message = new Label(grpYourStatus, SWT.NONE);
message.setFont(smallFont);
message.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
service.getServiceViewItem().setLabelsAndIcons(name, status, message, checkIcon, errorIcon); //this also sets the values of the labels (label.setText(...) via data binding)
}
Unfortunately, I don't know what the right way is to update/reset it. I tried the following:
listener (which should update the view / the services-list):
r.addPropertyChangeListener(BindingNames.SERVICE_ADDED, new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// This "redraws" the view, but just places the whole content (generated in createStatusGroupBox()) above the other parts.
//Display.getCurrent().update();
//createStatusGroupBox();
//parent.layout(true);
//parent.redraw();
// disposes grpYourStatus-Group but doesn't show anything then
grpYourStatus.dispose();
createStatusGroupBox();
grpYourStatus.layout();
grpYourStatus.redraw();
}
});
}
});
I also tried the following statements (individually); all without success:
parent.redraw();
parent.update();
parent.layout();
parent.layout(true);
parent.refresh();
In this post I read the following:
createPartControls is that time of the life cycle of a view, where its
contained widgets are created (when the view becomes visible
initially). This code is only executed once during the view life
cycle, therefore you cannot add anything here directly to refresh your
view.
Eclipse parts typically update their content as a reaction to a
changed selection inside of the workbench (e.g. the user might click
on another stack frame in the debug view).
Unfortunately, I don't know what to else I could try and I didn't find anything helpful with searches... thank's for your help and suggestions!
I finally found the answer (together with AndreiC's help!):
my listener now looks like this:
r.addPropertyChangeListener(BindingNames.SERVICE_ADDED, new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
// remove grpYourStatus from parent
grpYourStatus.dispose();
// add grpYourStatus (with updated values) to parent
createStatusGroupBox();
// refresh view
parent.pack();
parent.layout(true);
}
});
}
});
The rest is the same like the code above.
I do not know the API by heart, but have you tried parent.update()?

Categories