6 Replies Latest reply on Feb 26, 2008 4:52 PM by ctzn99

    NativeDragEvent and List and calculateDropIndex

    Driggett
      In regular drag and drop I use the calculateDropIndex to find the item within the list. This seems to be missing from NativeDragEvent. Is there another way to get the item of a List and Tree class that the drop happened over?
      Thanks,
      Chris

        • 1. NativeDragEvent and List and calculateDropIndex
          ctzn99 Level 1
          I just noticed this as well... i've got a tree call "myTree", and normally I use it's method "calculateDropIndex" to figure out where I'm dropping stuff. I'm now adding desktop drag and drop support to the tree but, calculateDropIndex ONLY takes a DragEvent as a parameter, NOT a NativeDragEvent.

          Seeing as both event types derive from mouse event wouldn't it make more sense for the calculateDropIndex method to accept a MouseEvent thus allowing DragEvents and NativeDragEvents to be resolved.

          Anyone know of another way to calculate where a file from the desktop is being dragged onto a list item (in this case a tree).
          • 2. Re: NativeDragEvent and List and calculateDropIndex
            ctzn99 Level 1
            Here's what I just created that seems to work fine, I imagine that it wouldn't work in all circumstances. I created a new SuperTree class that extends Tree and added a new method call "calculateNativeDropIndex()"

            package
            {
            import flash.events.NativeDragEvent;
            import flash.geom.Point;

            import mx.controls.Tree;

            public class SuperTree extends Tree
            {
            public function SuperTree()
            {
            super();
            }

            public function calculateNativeDropIndex(event:NativeDragEvent):int
            {
            // setup a default return value.
            var index:int = 0;

            // calculate the y value of the mouse over the tree (stored in point)
            var point:Point = this.globalToLocal(new Point(event.stageX, event.stageY));

            // divide the y value by the height of the rows (and check to make sure its not zero),
            // the interger value should be our index
            if(this.rowHeight != 0)
            index = point.y / this.rowHeight;

            return index;
            }
            }
            }

            Alternatively you could just create a standalone function but I like to encapsulate stuff like this for reuse elsewhere:


            public function calculateNativeDropIndex(tree:Tree, event:NativeDragEvent):int
            {
            // setup a default return value.
            var index:int = 0;

            // calculate the y value of the mouse over the tree (stored in point)
            var point:Point = tree.globalToLocal(new Point(event.stageX, event.stageY));

            // divide the y value by the height of the rows (and check to make sure its not zero),
            // the interger value should be our index
            if(tree.rowHeight != 0)
            index = point.y / tree.rowHeight;

            return index;
            }

            So... those both work for me, its pretty simple so I imagine if there were some funky stuff going on then it would have to be a bit more complicated (like variable row heights).
            • 3. Re: NativeDragEvent and List and calculateDropIndex
              Arno Gourdol Adobe Employee
              If you are using Flex components, you probably should stick to using Flex DragEvents. Think of NativeDragEvent as a more primitive (lower level) type. Flex uses the NativeDragManager when appropriate (i.e. when running inside an AIR app).


              • 4. Re: NativeDragEvent and List and calculateDropIndex
                ctzn99 Level 1
                As you (Arno) pointed out <a href=" http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=75&catid=697&threadid =1340643&enterthread=y">here</a> the dragSource property will contain the array of files being dragged BUT I don't believe it's obvious.

                I actually read the part in the API docs where it says that the NativeDragEvent is a more "primitive" version of the DragEvent but for the life of me couldn't figure out how to get file info out of it. Not only that, I found probably at least 10 - 15 other people on different forums asking the same question as the OP and not one single answer, and I couldn't find a single example of using DragEvent in flex builder to handle desktop draging.

                Anyway, you helped me out with my problem previously, hopefully Chris' problem has been solved as well.
                • 5. Re: NativeDragEvent and List and calculateDropIndex
                  Driggett Level 1
                  So here is how I got it to work. I use regular drag and drop except for the start event. I register a function to handle the nativeDragStart event and grab the clipboard contents from that save it to a var and then use the native drag and drop for the rest. This is because I could bot get valid clipboard contents from the regular drag and drop event handlers!
                  • 6. Re: NativeDragEvent and List and calculateDropIndex
                    ctzn99 Level 1
                    In the link i provided above it talked about how to get the clipboard contents with just using a DragEvent, it sounds like you're doing more work than you need to.

                    This will probably do what you want, getting the "clipboard" from a DragEvent object.

                    var files:Array = event.dragSource.dataForFormat("air:file list") as Array;

                    Tada, a reference to the file dragged onto your list.