benbjohnson / melomel

External ActionScript Interface.
https://github.com/benbjohnson/melomel/wiki
Other
42 stars 8 forks source link

Recursive itemsToLabels #42

Closed laran closed 13 years ago

laran commented 13 years ago

I'm trying to get data from a tree. I've added some code to the action script to do this in itemsToLabels, but the data isn't making it back to ruby.

Here's the code I've added to UI.as. I just added a little extra code to itemsToLabels:

    /**
     *  Generates a list of labels from a data control.
     *
     *  @param component  A data control or column which has an itemToLabel()
     *                    method.
     *  @param data       The dataset to generate labels from.
     *
     *  @return           A list of labels generated by the component.
     */
    static public function itemsToLabels(component:Object, data:Object):Array
    {
        // Use source property if this is an ILists or IHierarchicalData.
        if( data as mx.collections.IList != null || data as mx.collections.IHierarchicalData != null ) {
            data = data.source;
                }

        // Loop over collection and generate labels
        var labels:Array = [];

        for each(var item:Object in data) {
            var label:Object = component.itemToLabel(item);
            labels.push(label);

            try {
                // Try to handle trees
                var descriptor:ITreeDataDescriptor = component.dataDescriptor;
                labels.concat(itemsToLabels(component, descriptor.getChildren(item)))
            } catch(e:Error) { // Swallow it. It's probably just not a tree.
            }
        }

        return labels;
    }

I'll mention that I tried both returning a multidimensional array (just replace the labels.concat with labels.push inside the try block) as well as just one long single-dimensional array. Neither objects were returned intact into the ruby code. I always get a nil object in the ruby code as the return value from the call to Melomel::Bridge.itemsToLabels(tree,tree.dataProvider).

Are there any limitations I should know about? Not being able to work with Trees is driving me bonkers!

laran commented 13 years ago

I'll also mention that I tried (and failed) getting the data out exclusively in the ruby code.

benbjohnson commented 13 years ago

One issue that I see is that you need to assign the result of the concat() method:

Change this:

labels.concat(itemsToLabels(component, descriptor.getChildren(item)));

to:

labels = labels.concat(itemsToLabels(component, descriptor.getChildren(item)));

Although it seems like you should at least be getting the root level items in your dataProvider. If you change the body of your method to:

static public function itemsToLabels(component:Object, data:Object):Array
{
  return ['foo'];
}

Does it still return nil? Also, can you post your Ruby code? Have you tried adding trace statements before your return statement to see if there is data being returned?

The data transfer code in Melomel is fairly simple. It mainly just passes around primitive data types and pointers. There are not really any limitations (except for instantiating classes with arguments) that I can think of.

laran commented 13 years ago

return ['foo'] returns nil.

Here's my ruby code:

  report_tree_types = find(:tree, :name => "Report Types")
  puts "#{report_tree_types.dataProvider.length} elements" # This prints '5', which is correct.
  labels = Melomel.items_to_labels!(report_tree_types, report_tree_types.dataProvider) # This prints just an empty line.

The flex code for this returns ['foo'], so I would expect items_to_labels to return ['foo'], but it doesn't.

laran commented 13 years ago

OK. I'm trying something different. I'm trying to pull the data out from the ruby side instead of from the Flex side. I've based this code on the code in cucumber.rb.

def get_tree_elements(tree)
  elements = []
  unless tree.dataProvider.nil?
    cursor = tree.dataProvider.createCursor()
    while !cursor.afterLast do
      elements.push cursor.current
      break if !cursor.moveNext()
    end
  end
  elements
end

I'm trying to get a multi-dimensional array from a tree.

When I call this method on a tree I get an array of 5 nils.

So it can tell that there are 5 elements, but it doesn't return them.

This is really a blocker for me. Can you suggest a way to accomplish this? I'd love to get this integrated into cucumber.rb. I'm just not getting any traction against it.

benbjohnson commented 13 years ago

I tried to get this working and I have the AS3 side implemented but I couldn't quite get the Ruby side. I checked in what I have to the "tree" branch on the Melomel.rb project.

Here's the checkin:

https://github.com/benbjohnson/melomel.rb/commit/3bd27dfcf8cc62c721f2b71c6be1600f4b0dcebc

The code will actually work when selecting an item that is visible but not one that is not visible. That's what I'm trying to figure out now. Hopefully this code gets you somewhere. I'll try to finish this up soon but I'm slammed with work right now.

The AS3 code is in v0.6.13 of the SWC.

laran commented 13 years ago

This is awesome Ben! It works! Thanks for putting in the time on this.

On Tue, Sep 20, 2011 at 7:25 PM, Ben Johnson < reply@reply.github.com>wrote:

I tried to get this working and I have the AS3 side implemented but I couldn't quite get the Ruby side. I checked in what I have to the "tree" branch on the Melomel.rb project.

Here's the checkin:

https://github.com/benbjohnson/melomel.rb/commit/3bd27dfcf8cc62c721f2b71c6be1600f4b0dcebc

The code will actually work when selecting an item that is visible but not one that is not visible. That's what I'm trying to figure out now. Hopefully this code gets you somewhere. I'll try to finish this up soon but I'm slammed with work right now.

The AS3 code is in v0.6.13 of the SWC.

Reply to this email directly or view it on GitHub: https://github.com/benbjohnson/melomel/issues/42#issuecomment-2152037

benbjohnson commented 13 years ago

Awesome. I wasn't sure if it would get you far enough. I'm glad to hear it worked.