Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSTableView and Drag and Drop - anyone gotten it to work?

NSTableView and Drag and Drop - anyone gotten it to work?
Thread Tools
Dedicated MacNNer
Join Date: Oct 2000
Location: Pasadena, CA, USA
Status: Offline
Reply With Quote
Jan 3, 2001, 09:33 AM
 
I've been trying to implement drag and drop support in a NSTableView, and I'm having problems.

What I'd like to do is allow my users re-order rows in the table via drag and drop. I've implemented tableViewWriteRowsToPasteboard(), tableViewValidateDrop(), tableViewAcceptDrop(), and tableViewSetObjectValueForLocation(). But only tableViewWriteRowsToPasteboard() is being called - the other methods never are. I suspect there's a problem with my code, and that for some reason the data isn't being put into the pasteboard. I've put the source at the end of this message.

If anyone can point me to a sample application that uses drag and drop within an NSTableView (either Obj C or Java) I would appreciate it.

public Object tableViewObjectValueForLocation(NSTableView tab,
NSTableColumn col, int i) {

if (col.identifier().toString().equals("COL1"))
return ((row)rowData.elementAt(i)).newsgroup;
else if (col.identifier().toString().equalsIgnoreCase("COL 2"))
return ((row)rowData.elementAt(i)).unread;
else
return ("unknown col id: " + col.identifier().toString());

}

Any help is appreciated.

Erik
     
Forum Regular
Join Date: Jan 2000
Location: California
Status: Offline
Reply With Quote
Jan 3, 2001, 12:07 PM
 
Ahhh, I recently tackled this with my OS X NotePad app (Notes, check on versiontracker).

Since I am fairly new to Cocoa, it didn't take me long to realize that implementing Drag&Drop in a NSTableView was far beyond me.

So, when you can't write it yourself... shamelessly rip off someone else's code!

Andrew Stone put up the source code to GIFfun, which contains a NSTableView with Drag&Drop row re-ordering. I just moved his SDTableView class into my application, as well as the formal protocol declaration (both without modification, it's very well written!), added the delegate methods to my controller class, and it was working!

Took me about half an hour, tops. His class also lets you set a key modifier that must be pressed down for the drag to start. I used this in Notes, because if the user moved the mouse at all while clicking (very likely) then it would start to drag, and then snap back when the mouse was released, and this caused wierd behavior.

I also don't know if you are allowed to create commerical software with Stone's code. Notes is freeware, so I was not really too concerned about that. If you want, I can send you the Notes source, to see how it's implemented. You should be able to just copy out the files that I used, and add (I think) 3 very simple delegate methods to the SDTableView delegate, and it'll start working.

Very cool indeed.

Also, I highly suggest you pick up the latest issue of MacTech magazine. It contains an article written by Andrew Stone which runs through a basic drag & drop implementation. (This guy is everywhere, and that's a good thing!). This issue of MacTech came out after I released Notes, so I did this on the GIFfun source alone (meaning you don't need the MacTech to do this, it will just explain how the Drag & Drop works).


Good Luck!
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
May 3, 2002, 06:53 PM
 
Does anyone have sample code for how to do this in Cocoa Java?
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
May 3, 2002, 08:48 PM
 
This honestly doesn't look that hard. You just need to implement three methods in your delegate, something like this (because someone suggested the java example, we'll go with that)

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
String myType = <font color = red>"myType"</font>;

public boolean tableViewWriteRowsToPasteboard( NSTableView tableView, NSArray rows, NSPasteboard pboard)
{
pboard.declareTypes(new NSArray(myType))
<font color = brown>//myData is up to you how you define it</font>
pboard.setDataForType(myData,myType);
return true;
}

public int tableViewValidateDrop( NSTableView tableView, NSDraggingInfo info, int row, int operation)
{
if(operation==NSDraggingInfo.DragOperationMove) return operation;
else return DragOperationNone;
}

public boolean tableViewAcceptDrop( NSTableView tableView, NSDraggingInfo info, int row, int operation)
{
<font color = brown>//this is the data that I told you to encode earlier!</font>
NSData myDaya = info.draggingPasteboard().dataForType(myType);
<font color = brown>//now insert this data into the appropriate positions</font>
}
</font>[/code]

Now, I didn't check this code yet (finals... must study...), but something like this should world.

HTH,
F-bacher
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
May 3, 2002, 08:50 PM
 
Originally posted by eyadams:
<STRONG>If anyone can point me to a sample application that uses drag and drop within an NSTableView (either Obj C or Java) I would appreciate it.</STRONG>
I believe there's an example with drag and drop with an outline view in the AppKit examples that come with the dev tools. OutlineView is just a subclass of NSTableView, so there should be some similarities.

F-bacher
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
May 5, 2002, 12:42 PM
 
F-Bacher thanks for the help, I am a beginner so please bear with me. Below is the code that I got (after looking at your example code). The tableViewAcceptDrop and tableViewValidateDrop are NOT getting called. The tableViewWriteRowsToPasteboard is getting called. If you can take a look at it and let me know what you think that would be greatly appreciated. Thanks,
Ian

public boolean tableViewWriteRowsToPasteboard(NSTableView aTable, NSArray anArray, NSPasteboard aPastboard ) {
//When I click and drag a row in the table, the strings from that row move around with the mouse arrow.
NSArray newArray = new NSArray();
newArray = newArray.arrayByAddingObject(NSPasteboard.DragPboa rd);

dataString = new String();
String[][] defaultMatrix;
defaultMatrix = getMatrix(); //returns a String[][] that holds the strings that are displayed in the table
for (int i=0; i&lt;numberOfRows; i++) {
for (int j=0; j&lt;numberOfColumns; j++) {
String f = defaultMatrix[i][j];
dataString = dataString + f; //put all of the strings together
}
}

aPastboard.declareTypes(newArray,null);
aPastboard.setStringForType(dataString, NSPasteboard.DragPboard);
return true;
}
public boolean tableViewAcceptDrop(NSTableView tableView, NSDraggingInfo info, int row, int operation) {
//don't know why this method doesn't get called
String data = info.draggingPasteboard().stringForType(dataString );
//not sure how to make it insert it where appropriate
return true;

}

public int tableViewValidateDrop(NSTableView tableView, NSDraggingInfo info, int row, int operation) {

if(operation==NSDraggingInfo.DragOperationMove) return operation;
else return NSDraggingInfo.DragOperationNone;

}

[ 05-05-2002: Message edited by: macrophyllum ]
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status: Offline
Reply With Quote
May 8, 2002, 03:03 PM
 
I think you need to call registerForDraggedTypes() on the NSTableView so it knows to accept your type.

Also, NSPasteboard.DragPboard is the string name of a pasteboard, not a pastboard type. You normally use those to get an NSPasteboard instance (the pasteboardWithName() method), but in this case it's passed to you already. The constants ending in "PboardType" are the ones normally used. Any string is OK, and since this is a custom operation (i.e. nothing is being dragged to other applications) you probably want to use a custom string (can be anything, really). In this sense, NSPasteboard.DragPboard probably would work as a random string, but it's confusing to see in the code.
     
Mac Enthusiast
Join Date: Sep 2000
Location: Vermont, USA
Status: Offline
Reply With Quote
May 9, 2002, 02:13 AM
 
lindberg,

Thanks for replying. I will implement the method you suggested when I get a chance (I just upgraded). I can't even look at the documentation now (download in progress) to see what to put in that method. I am sure I will be posting again in this thread.

Thanks,
Ian
     
Fresh-Faced Recruit
Join Date: May 2002
Location: oakland, CA
Status: Offline
Reply With Quote
Jun 12, 2002, 05:52 PM
 
I don't know if you got this working or not, but I was struggling with it last night and made it work in java. Things to note:

--------------------
In tableViewWriteRowsToPasteboard(), you need to call:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
pboard.declareTypes(new NSArray(&quot;DragData&quot,null);</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">and either:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">pboard.setStringForType(someString,&am p;quot;DragData&quot;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">or:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">pboard.setDataForType(someData,&qu ot;DragData&quot;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Assuming your just moving rows within one table, it's not necessary to drag all the data from the selected rows. Just put a comma delimeted string of the row numbers on the pasteboard and then parse it to see which rows to copy, insert, and then delete as they get added at the new drop location.

---------------
Lindberg wrote:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif"> I think you need to call registerForDraggedTypes() on the NSTableView so it knows to accept your type. </font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">YES YES YES - I have a TableViewController class (set as the delegate for the NSTableView in my nib file) which contains:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">public void awakeFromNib() {
myTableView.registerForDraggedTypes(new NSArray(&quot;DragData&quot);
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Once this was in I started getting suggestions for drop locations in the table.

-------------
In 'tableViewValidateDrop', the 'int operation' values are 1=betweenrows and 0=overrow. I just wanted to be able to drop between rows so:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">public int tableViewValidateDrop (NSTableView tV, NSDraggingInfo info, int row, int operation) {
if (operation == 1) {
return NSDraggingInfo.DragOperationMove;
} else {
return NSDraggingInfo.DragOperationNone;
}
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Hope this helps
GW
gw
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:23 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2