SWT Tree on Mac - "Source List"
I am implementing views for the OS X version of our Java SWT application and would like to use the "source list" option offered by NSOutlineView in my SWT tree.
I implemented this by hacking my own version of Tree.class by adding this code to the method #createHandle()
:
long NSTableViewSelectionHighlightStyleSourceList = 1;
long hi = OS.sel_registerName("setSelectionHighlightStyle:");
OS.objc_msgSend(widget.id, hi, NSTableViewSelectionHighlightStyleSourceList);
Which essentially just puts the underlying NSOutlineView to use the NSTableViewSelectionHighlightStyleSourceList style.
It looks like this:
which works, but doesn't completely emulate the behavior of NSOutlineView. For example, when selecting root nodes, although the header has enough space, it still truncates it like this:
Another problem is that I don't know how to implement categories (or groups / folders) as you can see in Finder.app/iTunes.app/Mail.app/ in the following example:
Pay attention to the category FAVORITES
and how it is formatted. This is handled in a normal ObjC application, returning true in a method outlineView:isGroupItem
in a delegate outlineView
, but I have no idea where to hack it.
So my question is:
How do I implement category ( isGroupItem
) functionality in SWT on OS X?
source to share
I was able to make adjustments to the SWT java source and native source to get this to work as shown in the image below.
Custom SWT for mac cocoa x86_64 can be downloaded here .
The changes made to the original state at this time can be seen in this commit
Below is a snippet showing how this works. The style is SWT.SOURCE_LIST
used to denote the tree as a source list, and the style is SWT.GROUP_ITEM
used to denote the elements that are group elements.
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
public class SourceList {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Tree tree = new Tree (shell, SWT.SOURCE_LIST);
for (int i=0; i<4; i++) {
final TreeItem iItem = new TreeItem (tree, SWT.GROUP_ITEM);
display.asyncExec( new Runnable() {
public void run() {
iItem.setExpanded(true);
}
});
iItem.setText ("GROUP " + i);
for (int j = 0; j < 4; j++) {
TreeItem jItem = new TreeItem (iItem, 0);
jItem.setText ("TreeItem " + j);
}
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
source to share