(note that swt usage questions like
this should be posted on the eclipse.platform.swt newsgroup)
You're right, all items in a Table/Tree
always have the same height, and they can never shrink. These are
constraints that arise from the underlying native widgets on some of the
platforms. I don't think there is a workaround available to you to
change this, because these behaviours are in the Table/Tree implementations
across the platforms by design.
Perhaps someone else that has tried
this can suggest an alternate approach that does not allow an item to become
too tall as a result of the user shrinking the column width too much?
Grant
"Travis D. Breaux"
<tdbreaux@xxxxxxxxxxxx> Sent by: platform-swt-dev-bounces@xxxxxxxxxxx
08/14/2006 01:32 PM
Please respond to
"Eclipse Platform SWT component developers list." <platform-swt-dev@xxxxxxxxxxx>
To
platform-swt-dev@xxxxxxxxxxx
cc
Subject
[platform-swt-dev] [Fwd: [Fwd: SWT Tree
and Eclipse 3.2 Milestone M6]]
I was hoping someone on this list could help me solve a problem I'm
having with the SWT Tree widget. I need to support multi-line display
and editing for long lines of text in a TreeItem -- meaning, I need
word-wrap in both display and edit and resizing of the tree is
preferable at times. Ideally, one could supply the SWT.WRAP bit to the
appropriate components. Here is the reference material I've found to-date:
1. Eclipse 3.2 Milestone 6: Custom Draw Tree
http://www.eclipse.org/swt/R3_2/new_and_noteworthy.html#m6
2. "Multiple lines in a TreeItem" code snippet
http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet227.java?rev=HEAD&content-type=text/vnd.viewcvs-markup
3. [Cell Editors] Support for multiline text cell editor, opened in
October 2002 (still unresolved?)
https://bugs.eclipse.org/bugs/show_bug.cgi?id=25356
I've attached a modified version of Snippet227 that includes some
support for images, word wrap and resizing events -- note Milestone M6
in 3.2 did not include image support. I've only run the code on Windows
and I'm having two problems:
1) For each row in the tree, the allotted row height equals the tallest
row (e.g., if the tallest row is three text lines tall, then rows with
only one line of text will have the same height). Ideally, each row only
requires the height reported by the MeasureItem event handler.
2) The height of a row will increase during resizing events that reduce
the components width -- to accommodate the additional rows added by word
wrap -- but the height of the rows will *not* decrease when the
components width decreases -- when word wrap contracts the text into
fewer lines. Ideally, the row height would decrease when the component's
width also decreases.
I'd like to solve at least one of the two problems; preferably the
first. I traced the code with the Eclipse debugger and it appears the
'y' attribute in the 'event' object (responsible for placing the image
and text in the custom listener) is being set in some platform-specific
SWT API code. I can give references to that platform specific code, if
necessary.
Any ideas? Cheers! Travis
/*******************************************************************************
* Copyright (c) 2000, 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
//package org.eclipse.swt.snippets;
/*
* Tree example snippet: Multiple lines in a TreeItem
*
* For a list of all SWT example snippets see
* http://www.eclipse.org/swt/snippets/
*
* @since 3.2
*/
public class Snippet227B {
public static void main(String [] args) {
Display
display = new Display();
Shell
shell = new Shell (display);
shell.setText("Multiple
lines in a TreeItem");
shell.setLayout
(new FillLayout());
final
Tree tree = new Tree(shell, SWT.MULTI);
PaletteData
paletteData = new PaletteData(new RGB[] {new RGB(255,0,0)});
ImageData
imageData = new ImageData(16,16,1,paletteData);
Image
image = new Image(display, imageData);
TreeItem
item1 = new TreeItem(tree, SWT.NONE);
item1.setText("One");
item1.setImage(image);
TreeItem
item2 = new TreeItem(tree, SWT.NONE);
item2.setText("Two");
item2.setImage(image);
TreeItem
item3 = new TreeItem(tree, SWT.NONE);
item3.setText("Three
is the successor to two, a crowd and not company, and the first word in
a rather long line of text.");
item3.setImage(image);
/*
* 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) {
switch(event.type)
{
case SWT.MeasureItem: {
TreeItem
item = (TreeItem)event.item;
String
text = getText(item, event.index, event.gc);
Point
size = event.gc.textExtent(text);
Image
image = item.getImage(event.index);
event.width
= size.x + image.getBounds().x + 5;
event.height
= Math.max(image.getBounds().y, Math.max(event.height, size.y));
break;
}
case SWT.PaintItem: {
TreeItem
item = (TreeItem)event.item;
String
text = getText(item, event.index, event.gc);
Point
size = event.gc.textExtent(text);
Image
image = item.getImage(event.index);
int
offset1 = image.getBounds().width + 5;
int
offset2 = event.index == 0 ? Math.max(0, (event.height - size.y) / 2) :
0;
int
offset3 = event.index == 0 ? Math.max(0, (event.height - image.getBounds().height)
/ 2) : 0;
event.gc.drawImage(image,
event.x, event.y + offset3);
event.gc.drawText(text,
event.x + offset1, event.y + offset2, true);
break;
}
case SWT.EraseItem: {
event.detail
&= ~SWT.FOREGROUND;
break;
}
case SWT.Resize: {
Tree
tree = (Tree) event.widget;
tree.redraw();
break;
}
}
}
String getText(TreeItem item, int column, GC gc) {
String
text = item.getText(column);
if
(column == 0) {
String[] word = text.split("\\s+");
int maxWidth = tree.getBounds().width - 5
//
Adjust for the image width.
-
item.getImageBounds(column).width
//
Adjust for the vertical scrollbar width.
-
(item.getParent().getVerticalBar().isVisible() ? 20 : 0);
text = "";
for (int i = 0, j = 0, k; i < word.length; i++) {
k
= gc.textExtent(word[i] + " ").x;
if
(j + k >= maxWidth) {
j = k;
text += "\n";
}
else {
j += k;
}
text
+= word[i] + " ";
}
}
return
text;
}
};
tree.addListener(SWT.MeasureItem,
paintListener);
tree.addListener(SWT.PaintItem,
paintListener);
tree.addListener(SWT.EraseItem,
paintListener);
tree.addListener(SWT.Resize,
paintListener);
shell.setSize(300,
300);
shell.open();
while
(!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
_______________________________________________
platform-swt-dev mailing list
platform-swt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/platform-swt-dev