NSOutlineView from Java or Bust
NSOutlineView from Java or Bust
- Subject: NSOutlineView from Java or Bust
- From: Dave Horlick <email@hidden>
- Date: Sun, 13 May 2001 23:24:14 -0400
Hi, everybody.
I am encountering a weird and frustrating problem while trying to use an
NSOutlineView from Java.
I wrote two distinct versions of a simple OutlineView demo, one in
Objective-C and one in Java. Both demos have hooks into interface
builder. These hooks set the OutlineView's data source delegate when a
button is pressed.
The demos are each written to display the following:
+ Heading
|---Subheading
|---Subheading
|---Subheading
and so on, with several subheadings. My Objective-C version of this demo
works, no problem.
My Java version, on the other hand, works only occasionally.
The Heading item displays after I click my load button. It is when I
click on its disclosure triangle that my demo crashes... sometimes. I am
usually okay if I set it to display only a small number of Subheadings.
But if I ask for many, it will choke on a NullPointerException.
Sometimes this will occur on the 21st subheading, sometimes on the 30th,
sometimes on the 34th. There seems to be no rhyme or reason to it.
The console will not reveal where this NullPointerException is actually
being thrown. The Stack Trace is empty. All I get is the cryptic
information that "TestOutlineView.app has exited due to signal 10
(SIGBUS)."
The Java version of my demo has three source files: a simple controller
class, and two empty public classes called Heading and Subheading. I
instantiate a data source anonymously from the NSOutlineView.DataSource
interface/protocol. (I have also tried implementing this as a separate,
named class too and it doesn't help.)
I would appreciate any insights that anyone can provide. I have included
the source for my controller class below.
Thanks,
Dave
---------
/* TestController */
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class TestController {
NSOutlineView myOutlineView;
final int howBig = 56;
public void populate(NSButton sender)
{
System.out.println("*** TestController.populate invoked ***");
System.out.println("myOutlineView="+myOutlineView+"\n");
NSOutlineView.DataSource source = new NSOutlineView.DataSource()
{
public Object outlineViewChildOfItem( NSOutlineView
outlineView, int index, Object item)
{
System.out.println("*** invoked
outlineViewChildOfItem, asked for index #" +index + " ***");
if (item==null)
{
System.out.println("\treturning Heading");
return new Heading();
}
else if (item instanceof Heading)
{
System.out.println("\treturning Subheading");
return new Subheading();
}
else
{
/* Since this outline is only one-level
deep, this should never happen. */
throw new RuntimeException("source.populate
Tried to dig too deep");
}
}
public boolean outlineViewIsItemExpandable
( NSOutlineView outlineView, Object item)
{
System.out.println("*** invoked
source.outlineViewIsItemExpandable, asked for item "
+item.toString() + " ***");
if (item == null)
{
System.out.println("\titem was null; returning true");
return true;
}
else if (item instanceof Heading)
{
System.out.println("\titem was a Heading; returning true");
return true;
}
else
{
System.out.println("\tritem was something else; returning false");
return false;
}
}
public int outlineViewNumberOfChildrenOfItem
( NSOutlineView outlineView, Object object)
{
System.out.println("***
source.outlineViewNumberOfChildrenOfItem invoked ***");
if (object == null)
{
/* It's asking about the root; let's say there's 1 item. */
System.out.println("\tobject was null; returning 1");
return 1;
}
else if (object instanceof Heading)
{
/* It's asking about the Heading; let's say
there're _howBig_ children. */
System.out.println("\tobject was Heading; returning "+howBig);
return howBig;
}
else if (object instanceof Subheading)
{
System.out.println("\tobject was Subheading; returning 0");
return 0;
}
else
{
throw new
RuntimeException("source.outlineViewNumberOfChildrenOfItem: Unfamiliar
object type encountered.");
}
}
/**
<p>Returns a String to represent the displayable
label for this item.</p>
*/
public Object outlineViewObjectValueForItem
( NSOutlineView outlineView, NSTableColumn tableColumn, Object item)
{
System.out.println("***
source.outlineViewObjectValueForItem invoked for item " +
item.toString() + "***");
if ( item == null)
{
throw new
RuntimeException("source.outlineViewObjectValueForItem: was passed
null");
}
else if (item instanceof Heading)
{
System.out.println("\titem was a Heading; returning 'Heading' ");
return "Heading";
}
else if (item instanceof Subheading)
{
System.out.println("\titem was an
Subheading; returning 'Subheading' ");
return "Subheading";
}
else
{
throw new
RuntimeException("source.outlineViewObjectValueForItem Unfamiliar item
type " + item.getClass().toString() + "encountered.");
}
}
public void outlineViewSetObjectValueForItem
( NSOutlineView outlineView, Object object, NSTableColumn tableColumn,
Object item)
{
System.out.println("*** Invoked
outlineViewSetObjectValueForItem ***");
}
public boolean outlineViewWriteItemsToPasteboard
( NSOutlineView outlineView, NSArray items, NSPasteboard pboard)
{
System.out.println("*** Invoked
outlineViewWriteItemsToPasteboard ***");
return false;
}
public boolean outlineViewAcceptDrop( NSOutlineView
outlineView, NSDraggingInfo info, Object item, int index)
{
System.out.println("*** Invoked outlineViewAcceptDrop ***");
return false;
}
public int outlineViewValidateDrop( NSOutlineView
outlineView, NSDraggingInfo info, Object item, int index)
{
System.out.println("*** Invoked outlineViewValidateDrop ***");
return 0;
}
};
System.out.println("source="+source);
myOutlineView.setDataSource(source);
}
}