mercredi 28 janvier 2015

No scrollbars in ScrolledComposite in RCP/RAP


Vote count:

0




I have a piece of code with a ScrolledComposite where the vertical scrollbar does not appear even if the content of the widgets is much more then the space in the widget. I was able to reproduce this behaviour in a simple example. I need this to work as RAP application but the code also does not work if I run it in a view in RCP application.


Here is the code for a simple view (I omit the imports part):



public class View extends ViewPart {
public static final String ID = "View_spike.view";

List<String> inputData = new ArrayList<String>();
final String LONG_TEXT = "Some long text. Some long text. Some long text. Some long text. Some long text. Some long text. Some long text.";


private TableViewer viewer;

/**
* The content provider class is responsible for providing objects to the
* view. It can wrap existing objects in adapters or simply return objects
* as-is. These objects may be sensitive to the current input of the view,
* or ignore it and always show the same content (like Task List, for
* example).
*/
class ViewContentProvider implements IStructuredContentProvider {
public void inputChanged(Viewer v, Object oldInput, Object newInput) {
}

public void dispose() {
}

public Object[] getElements(Object parent) {
if (parent instanceof Object[]) {
return (Object[]) parent;
}
return new Object[0];
}
}

class ViewLabelProvider extends LabelProvider implements
ITableLabelProvider {
public String getColumnText(Object obj, int index) {
return getText(obj);
}

public Image getColumnImage(Object obj, int index) {
return getImage(obj);
}

public Image getImage(Object obj) {
return PlatformUI.getWorkbench().getSharedImages().getImage(
ISharedImages.IMG_OBJ_ELEMENT);
}
}

/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
populateInputData();

final SashForm container = new SashForm(parent, SWT.HORIZONTAL);
final ScrolledComposite objectViewerContainer = new ScrolledComposite(
container, SWT.H_SCROLL | SWT.V_SCROLL);
objectViewerContainer.setLayout(new GridLayout());
objectViewerContainer.setExpandHorizontal(true);
objectViewerContainer.setExpandVertical(true);
objectViewerContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

final Composite someContainer = new Composite(container, SWT.NONE);
someContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

container.setWeights(new int[] { 1, 2 });
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

createObjectViewerArea(objectViewerContainer);
}

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


private void populateInputData() {
for(int i = 0; i < 100; i++) {
inputData.add(LONG_TEXT);
}

}

private void createObjectViewerArea(final ScrolledComposite parent) {
final Composite panel = new Composite(parent, SWT.NONE);
panel.setLayout(resetMargin(new TableWrapLayout()));
createContent(panel);
panel.setLayoutData(new GridData(GridData.FILL_BOTH));
parent.setContent(panel);
}

private void createContent(final Composite parent) {
final Tree tree = new Tree(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setLayoutData(new TableWrapData());
TreeViewer treeViewer = new TreeViewer(tree);

treeViewer.setContentProvider(new ITreeContentProvider() {

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

}

@Override
public void inputChanged(Viewer viewer, Object oldInput,
Object newInput) {
viewer.refresh();
parent.layout();
}

@Override
public Object[] getElements(Object inputElement) {
// TODO Auto-generated method stub
return ((List) inputData).toArray();
}

@Override
public Object[] getChildren(Object parentElement) {
// TODO Auto-generated method stub
return null;
}

@Override
public Object getParent(Object element) {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean hasChildren(Object element) {
// TODO Auto-generated method stub
return false;
}

});
treeViewer.setInput(this.inputData);
treeViewer.setAutoExpandLevel(1); // don't expand past top level
treeViewer.setLabelProvider(new ILabelProvider() {

@Override
public void removeListener(ILabelProviderListener listener) {

}

@Override
public boolean isLabelProperty(Object element, String property) {
return false;
}

@Override
public void dispose() {

}

@Override
public void addListener(ILabelProviderListener listener) {

}

@Override
public String getText(Object element) {
// TODO Auto-generated method stub
return (String) element;
}

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

public TableWrapLayout resetMargin(final TableWrapLayout layout) {
layout.topMargin = 0;
layout.leftMargin = 0;
layout.bottomMargin = 1;
layout.rightMargin = 0;
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
return layout;
}

}


The result is following: enter image description here


Of course there are more rows in the tree widget than what is displayed (the code adds 100 objects). I would expect scrollbars to appear (both vertical and horizontal) in the tree widget. Any ideas what I should improve in my code?



asked 52 secs ago

Jakub

949






No scrollbars in ScrolledComposite in RCP/RAP

Aucun commentaire:

Enregistrer un commentaire