Originally posted by IamBob
I guess my problem is that I don't know how to get a handle to the table besides through the events it sends. I need to be able to populate the table
I'm not a programmer, and know very little about Cocoa. But here is a bit of code that uses an OutlineView (subclass of TableView). I don't know how directly to get a handle to the table, either -- but if you use interface builder, then it should give you the handle automatically.
To populate the table, you use the delegate methods. For an outline view, this is done in the outlineViewChildOfItem method (which gives an item (null if top-level) and the index of the child requested). Another delegate method is outlineViewObjectValueForItem, which gives you a table column and the item representing the row being displayed; you need to extract the column info from the item.
That's probably unclear. I agree that the documentation is pretty awful, especially compared to that for Java. But the sample ObjC code is useful for understanding the API through Java as well.
--
<pre>
/* ArticlesList */
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class ArticlesList {
Vector threads = new Vector(); // initialize for outline view
NSOutlineView outline;
NSWindow articlesWindow;
Client client;
private void addArticle(Article art, Hashtable threads) {
Vector refs = art.header.references;
if (null != refs) {
for (Enumeration e = refs.elements(); e.hasMoreElements(); ) {
String ref = (String)e.nextElement();
NSMutableArray thread = (NSMutableArray)threads.get(ref); // make Thread
if (thread != null) {
thread.addObject(art);
return;
}
}
}
// no thread has fit
NSMutableArray thread = new NSMutableArray();
thread.addObject(art);
if (art.header.id != null) {
threads.put(art.header.id, thread);
} else {
threads.put(art.header, thread); // doesn't need to be in thread list
}
}
public void loadArticles(Client client, Newsgroup group) {
this.client = client;
Hashtable threads = new Hashtable();
try {
client.selectGroup(group);
for (int i = group.start; i <= group.end; i++) {
String header = client.getHeader(i);
if (null != header) {
Article article = new Article(header);
addArticle(article, threads);
}
}
} catch (IOException e) {
System.out.println("Error: lost connection");
return;
}
this.threads = new Vector(threads.values());
System.out.println("Fetched " + threads.size() + " threads");
outline.reloadData();
if (!articlesWindow.isVisible()) {
articlesWindow.setFrame(new NSRect(183, 75, 556, 422), true);
}
articlesWindow.orderWindow(NSWindow.Above, 0);
articlesWindow.makeKeyWindow();
}
// Returns the number of child items encompassed by item. If item is null, this method should return the number of children for the top-level item.
// only called on objects which are expandable (Vectors)
public int outlineViewNumberOfChildrenOfItem(NSOutlineView outlineView, Object object) {
if (null == object) {
return threads.size();
} else {
return (((NSMutableArray)object).count() - 1);
}
}
// Returns the data object associated with the specified item. The item is located in the specified tableColumn of the view
public Object outlineViewObjectValueForItem(NSOutlineView outlineView, NSTableColumn tableColumn, Object item) {
if (null == item) {
return null;
}
Article article;
//if ("java.util.Vector".equals(item.getClass().getName())) {
if (item.getClass().getName().endsWith("NSMutableArray")) {
article = (Article)((NSMutableArray)item).objectAtIndex(0);
//return ("" + ((NSMutableArray)item).count());
} else {
article = (Article)item;
}
String response = null;
if (tableColumn.identifier().equals("author")) {
response = article.header.from.name;
} else if (tableColumn.identifier().equals("subject")) {
response = article.header.subject;
} else if (tableColumn.identifier().equals("date")) {
response = DateFormat.getDateInstance().format(article.header.date);
}
return response;
}
// This method should return true if item can be expanded to display its children.
public boolean outlineViewIsItemExpandable(NSOutlineView outlineView, Object item) {
if (null == item) {
return false;
} else {
//if ("java.util.Vector".equals(item.getClass().getName())) {
if (item.getClass().getName().endsWith("NSMutableArray")) {
// Vector v = (Vector)item;
NSMutableArray v = (NSMutableArray)item;
return (v.count() > 1);
} else {
return false;
}
}
}
// Returns the child item at the specified index. Children of a given parent item are accessed sequentially. If item is null, this method should return the appropriate child item of the root object.
public Object outlineViewChildOfItem( NSOutlineView outlineView, int index, Object item) {
if (null == item) {
return threads.elementAt(index);
} else if (item.getClass().getName().endsWith("NSMutableArray")) {
NSMutableArray v = (NSMutableArray)item;
return v.objectAtIndex(index + 1);
} else return null;
}
}
</pre>
One last thing: I wasn't able to use Vectors as the row representation item for some reason; that's why I'm using NSMutableArrays.
[Edited by tie on 12-02-2000 at 09:38 PM]