Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » TreeViewer Auto Scroll
TreeViewer Auto Scroll [message #483134] Mon, 31 August 2009 05:26
Murthy Bhat is currently offline Murthy Bhat
Messages: 159
Registered: July 2009
Senior Member
Hello,

I have a treeviewer which has multiline text cell editors. I have
listeners to refresh the row size for every modify.

The problem occurs when the contents of the cell is so large that it
cannot fit in the given table size. The row selection does not work as
expectd. i.e. if a large amount of multiline text is entered into any of
the cell, and any other row is selected after that, the selected row is no
more visible.

Below is an example.

import java.util.ArrayList;

import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrate gy;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

/**
* A simple TreeViewer to demonstrate usage of inline editing
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class TreeViewerEditing {
private class MyContentProvider implements ITreeContentProvider {

/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.IStructuredContentProvider#getElem ents(
* java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
return ((MyModel) inputElement).child.toArray();
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {

}

/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.IContentProvider#inputChanged(org. eclipse
* .jface.viewers.Viewer, java.lang.Object, java.lang.Object)
*/
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {

}

/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.ITreeContentProvider#getChildren(j ava.lang
* .Object)
*/
public Object[] getChildren(Object parentElement) {
return getElements(parentElement);
}

/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.ITreeContentProvider#getParent(jav a.lang
* .Object)
*/
public Object getParent(Object element) {
if (element == null) {
return null;
}

return ((MyModel) element).parent;
}

/*
* (non-Javadoc)
*
* @see
* org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(j ava.lang
* .Object)
*/
public boolean hasChildren(Object element) {
return ((MyModel) element).child.size() > 0;
}

}

public class MyModel {
public MyModel parent;

public ArrayList child = new ArrayList();

public String counter;

public MyModel(String counter, MyModel parent) {
this.parent = parent;
this.counter = counter;
}

public String toString() {
String rv = "Item ";
if (parent != null) {
rv = parent.toString() + ".";
}

rv += counter;

return rv;
}
}

public class MyLabelProvider extends LabelProvider implements
ITableLabelProvider {
FontRegistry registry = new FontRegistry();

public Image getColumnImage(Object element, int columnIndex) {
return null;
}

public String getColumnText(Object element, int columnIndex) {
return "Column " + columnIndex + " => " + element.toString();
}
}

public TreeViewerEditing(Composite parent) {
final TreeViewer v = new TreeViewer(parent,SWT.FULL_SELECTION);


ColumnViewerEditorActivationStrategy actSupport = new
ColumnViewerEditorActivationStrategy(
v) {
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
|| event.eventType ==
ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTI ON
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED
&& event.keyCode == SWT.CR)
|| event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
};


int navigation = ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
| ColumnViewerEditor.TABBING_VERTICAL
| ColumnViewerEditor.KEYBOARD_ACTIVATION;

TreeViewerEditor.create(v, actSupport,navigation);

//v.getTree().setS.setCellSelectionEnabled(true);

addTableListener(v.getTree());

TreeColumn column = new TreeColumn(v.getTree(),SWT.NONE);
column.setWidth(200);
column.setText("Column 1");

column = new TreeColumn(v.getTree(),SWT.NONE);
column.setWidth(200);
column.setText("Column 2");

v.setCellEditors(new CellEditor[]{new TextCellEditor(v.getTree()), new
TextCellEditor(v.getTree(),SWT.WRAP|SWT.V_SCROLL)});
v.setColumnProperties(new String[]{"col1","col2"});
v.setCellModifier(new ICellModifier() {

public boolean canModify(Object element, String property) {
ISelection selection = v.getSelection();
Object obj = ((IStructuredSelection) selection) .getFirstElement();
if(!element.equals(obj))
return false;

return true;
}

public Object getValue(Object element, String property) {
//System.out.println("get Value:"+element);
return ((MyModel)element).counter+"";
}

public void modify(Object element, String property, Object value) {
//System.out.println("Selection:"+v.getSelection());
IStructuredSelection selection = (IStructuredSelection)
v.getSelection();
if(selection.getFirstElement() instanceof TreeItem)
{
TreeItem firstElement = (TreeItem)selection.getFirstElement();
if(!firstElement.equals(element))
{
modify( firstElement, property, value);
return;
}
}

//System.out.println("Modify:"+element);
((MyModel)((TreeItem)element).getData()).counter = value.toString();
v.update(((TreeItem)element).getData(), null);

}

});
v.setLabelProvider(new MyLabelProvider());
v.setContentProvider(new MyContentProvider());
v.setLabelProvider(labelProvider)
v.setInput(createModel());
v.expandAll();
}

private void addTableListener(Tree tree) {

try {
/*
* NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
* Therefore, it is critical for performance that these methods be
* as efficient as possible.
*/
Listener paintListener = new Listener() {
public void handleEvent(Event event) {
try {
switch (event.type) {
case SWT.MeasureItem: {
TreeItem item = (TreeItem) event.item;
String text = getText(item, event.index);
Point size = event.gc.textExtent(text);
event.width = size.x;
event.height = Math.max(25, Math.max(event.height,
size.y));
break;
}
case SWT.PaintItem: {
TreeItem item = (TreeItem) event.item;
String text = getText(item, event.index);
Image img = null;

Point size = event.gc.textExtent(text);
int offset2 = event.index == 0 ? Math.max(0,
(event.height - size.y) / 2) : 0;

if (event.index == 0) {
event.gc.drawText(text, event.x + 20, event.y
+ offset2, true);
img = item.getImage();
event.gc.drawImage(img, event.x, event.y
+ offset2);
} else
event.gc.drawText(text, event.x, event.y
+ offset2, true);

break;
}
case SWT.EraseItem: {
event.detail &= ~SWT.FOREGROUND;
break;
}
}
} catch (Exception e) {

}
}

String getText(TreeItem item, int column) {
String text = item.getText(column);
return text;
}
};

tree.addListener(SWT.MeasureItem, paintListener);
tree.addListener(SWT.PaintItem, paintListener);
tree.addListener(SWT.EraseItem, paintListener);

} catch (RuntimeException e) {

}

}

private MyModel createModel() {

MyModel root = new MyModel("" + 0, null);
root.counter = "" + 0;

MyModel tmp;
for (int i = 1; i < 5; i++) {
tmp = new MyModel("" + i, root);
root.child.add(tmp);
for (int j = 1; j < i; j++) {
tmp.child.add(new MyModel("" + j, tmp));
}
}

return root;
}

public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout());
new TreeViewerEditing(comp);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}

display.dispose();
}
}


Please note : The editors are activated on double click
1. In any of the cell in the second column enter the following value as
input
2
3
4
5
6
7
8
9
0
2
3
4
5
6
7
8
9
0


3.Select the row with this content.
4.The row is shifted up and is not visible

Any help would be of great help.

Thanks and Regards,
Bhat
Previous Topic:Changing the background color of the wizard dialog
Next Topic:CheckboxTreeViewer disable some elements
Goto Forum:
  


Current Time: Wed Jul 23 16:11:04 EDT 2014

Powered by FUDForum. Page generated in 0.01586 seconds