11 Replies Latest reply on Feb 12, 2009 6:06 AM by Newsgroup_User

    ItemRenderer aware of its selection state?

    Dimerman
      Hello,
      I am new to Flex, and try to learn it by comparing it to Java's Swing.
      With Swing, when you define a renderer, the interface is such that it not only gives you the value of the object to render but its index and selection state as well (boolean).
      I could not find a way to do this in Flex (my original intention was to do a TileList where the items are rendered as images with labels, and those which are selected are displayed in color whereas those deselected are displayed as blurred black&white).
      Using the debugger I know that the List holds arrays of indexes and objects selected, so I think it's a waste to create my own List and manage the objects (children) by myself. I also saw that the set data signature only includes the value to be rendered and nothing else... not even its index.

      Please, any ideas?
      many thanks,
      Dan
        • 1. Re: ItemRenderer aware of its selection state?
          JeffryHouser Level 4
          I don't know anything about Swing, so can't compare it to the Flex Framework, but..

          You can use the selectedIndex property to find the index of the selected item. If your list allows for multiple selections, I believe there is a selectedIndices property or something similar.

          There is a selectedItem property on the list class to get the data that is selected.

          The default renderers have a ListData property, which includes a rowIndex. Without testing it, I'm unsure if rowIndex is the index of the data in the dataProvider or the index of the renderer.

          The list based classes use renderer recycling so in most cases you will have significantly fewer renderers than data. As you scroll through the list the data changes, but you're still showing the same renderers.
          • 2. Re: ItemRenderer aware of its selection state?
            Dimerman Level 1
            Hi Jeff,
            Thanks for your prompt response.
            I am assuming/guessing the rowIndex (and the columnIndex) were the location of the renderer in the list viewport, not related to the actual item being rendered.
            Now, even if I can somehow get hold of the list ( event --> target --> listData --> etc. ) from within the itemrenderer, I still don't know which item I am rendering since I don't have an index at the time i'm rendering and a search in the selectedItems array can be O(n) which could be expensive.
            • 3. Re: ItemRenderer aware of its selection state?
              Level 7

              "Dimerman" <webforumsuser@macromedia.com> wrote in message
              news:gmvcp0$5kp$1@forums.macromedia.com...
              > Hello,
              > I am new to Flex, and try to learn it by comparing it to Java's Swing.
              > With Swing, when you define a renderer, the interface is such that it not
              > only
              > gives you the value of the object to render but its index and selection
              > state
              > as well (boolean).
              > I could not find a way to do this in Flex (my original intention was to do
              > a
              > TileList where the items are rendered as images with labels, and those
              > which
              > are selected are displayed in color whereas those deselected are displayed
              > as
              > blurred black&white).
              > Using the debugger I know that the List holds arrays of indexes and
              > objects
              > selected, so I think it's a waste to create my own List and manage the
              > objects
              > (children) by myself. I also saw that the set data signature only includes
              > the
              > value to be rendered and nothing else... not even its index.

              If you implement IDropInListItemRenderer or use a component that already
              implements it (for example, label), listData will give you what you need.

              HTH;

              Amy


              • 4. Re: ItemRenderer aware of its selection state?
                Dimerman Level 1
                Hi Amy,
                I'm sorry but I cannot close this conceptual loop.
                I read the docs about BaseListData and the closest I get is to use the owner property. That in my case would lead me to the List's selectedIndices array. BUT, I don't know how where to get the selected state of the object being rendered without doing a linear search on said array.
                thanks!
                Dan
                • 5. Re: ItemRenderer aware of its selection state?
                  Level 7

                  "Dimerman" <webforumsuser@macromedia.com> wrote in message
                  news:gmvjs1$dnh$1@forums.macromedia.com...
                  > Hi Amy,
                  > I'm sorry but I cannot close this conceptual loop.
                  > I read the docs about
                  > http://livedocs.adobe.com/flex/3/langref/mx/controls/listClasses/BaseListData.ht
                  > ml and the closest I get is to use the owner property. That in my
                  > case
                  > would lead me to the List's selectedIndices array. BUT, I
                  > don't
                  > know how where to get the selected state of the object being rendered
                  > without
                  > doing a linear search on said array.

                  Look at the docs on IViewCursor.

                  However, if you expand a bit more on what you're looking to do, I can
                  probably help you better.


                  • 6. Re: ItemRenderer aware of its selection state?
                    Level 7

                    "Dimerman" <webforumsuser@macromedia.com> wrote in message
                    news:gmvjs1$dnh$1@forums.macromedia.com...
                    > Hi Amy,
                    > I'm sorry but I cannot close this conceptual loop.
                    > I read the docs about
                    > http://livedocs.adobe.com/flex/3/langref/mx/controls/listClasses/BaseListData.ht
                    > ml and the closest I get is to use the owner property. That in my
                    > case
                    > would lead me to the List's selectedIndices array. BUT, I
                    > don't
                    > know how where to get the selected state of the object being rendered
                    > without
                    > doing a linear search on said array.
                    > thanks!

                    I apologize...I just went back and looked at your original post. You might
                    find this helpful:
                    http://flexdiary.blogspot.com/2008/08/tilelist-with-stylefunction.html


                    • 7. Re: ItemRenderer aware of its selection state?
                      Dimerman Level 1
                      I've looked into your example, and I'm afraid it's not quite what I'm looking for.
                      Basically what I need to do is to render items differently according to their select state in the list, independently of their data. The idea is to have several items rendered as images in a TileList: those selected by the user will be rendered in color, whereas those left deselected will be rendered black&white and blurred.

                      I still cannot find a way to get this item's select state.
                      thanks...
                      • 8. Re: ItemRenderer aware of its selection state?
                        JeffryHouser Level 4
                        I don't have time to test right now, but..

                        I wonder if you can do some math magic w/ verticalScrollPosition, rowHeight, and/or baselinePosition to figure out the index.

                        I also Googled this and my blog came up:

                        http://www.jeffryhouser.com/index.cfm/2008/11/4/How-do-you-find-an-items-dataProvider-inde x-from-inside-an-itemRenderer

                        I know that you were worried about performance issues of doing the search; but I'd suggest taking a look at how the "itemRendererToIndex" function does it.

                        • 9. Re: ItemRenderer aware of its selection state?
                          Level 7

                          "Dimerman" <webforumsuser@macromedia.com> wrote in message
                          news:gmvm0i$g74$1@forums.macromedia.com...
                          > I've looked into your example, and I'm afraid it's not quite what I'm
                          > looking
                          > for.
                          > Basically what I need to do is to render items differently according to
                          > their
                          > select state in the list, independently of their data. The idea is to have
                          > several items rendered as images in a TileList: those selected by the user
                          > will
                          > be rendered in color, whereas those left deselected will be rendered
                          > black&white and blurred.
                          >
                          > I still cannot find a way to get this item's select state.

                          Use IViewCursor on the TileList's selectedItems collection to see if the
                          data you're receiving in the styleFunction has a match in it, then send in a
                          style with the color, otherwise, send in a style that the renderer will
                          interpret and apply the appropriate filters.

                          My dad used to tell a joke where the punch line was something about leading
                          a horse to the daughter but not being able to make him wink. I wish I could
                          remember that joke...


                          • 10. Re: ItemRenderer aware of its selection state?
                            Dimerman Level 1
                            Thanks Amy for your efforts...
                            As far as I understand your example, you use styles to determine the selection state of an item, which seems to me like a hack (not that there's anything wrong with that...). I was expecting/hoping for a native support for the kind of thing I need (e.g. Java's ListCellRenderer).

                            Thanks again!
                            • 11. Re: ItemRenderer aware of its selection state?
                              Level 7

                              "Dimerman" <webforumsuser@macromedia.com> wrote in message
                              news:gn0ti1$3bf$1@forums.macromedia.com...
                              > Thanks Amy for your efforts...
                              > As far as I understand your example, you use styles to determine the
                              > selection
                              > state of an item, which seems to me like a hack (not that there's anything
                              > wrong with that...). I was expecting/hoping for a native support for the
                              > kind
                              > of thing I need (e.g.
                              > http://java.sun.com/javase/6/docs/api/javax/swing/ListCellRenderer.html#getListC
                              > ellRendererComponent(javax.swing.JList,%20java.lang.Object,%20int,%20boolean,%20
                              > boolean)).

                              No, you use the_style function_ to compare different information that you
                              have about the data and tell the renderer what style to be. That way the
                              renderer can stay really stupid and be used under a larger variety of
                              circumstances. And isn't reusability what this OO stuff is all about?

                              Check out the GroupingFunction example on my blog for an example of how you
                              can play renderers like a violin using styleFunction.

                              Ultimately I do intend to write a StatefulRenderer class that can be skinned
                              like a button, but I just haven't gotten around to it.