views:

540

answers:

3

Hi,

I have a TileList with thumbnail images. Under each thumbnail images I display the name of the image. I want to build rename functionality. So under the TileList is a "Rename selected image" button.

When this button is clicked, I would like to change the state of the itemRenderer component. The label underneath the image will change into a TextInput so that the user can type a new name. This is much like the Windows rename file functionality.

How can I access the itemRenderer of the selected image? How can I make it happen that it listens to the click event of the Rename button?

Some code:

<mx:TileList id="imageTileList" width="100%" height="100%" doubleClickEnabled="true"
 itemsChangeEffect="{tileListEffect}" dataProvider="{images}" 
 keyDown="{tileListKeyDownHandler(event)}"
 itemRenderer="com.n200.components.htmlElement.ImageTile" 
 columnWidth="128" rowHeight="128" itemDoubleClick="{insertImage()}" 
 horizontalScrollPolicy="off" verticalScrollPolicy="auto" />

<mx:LinkButton label="Rename selected image" labelPlacement="left"
    enabled="{imageTileList.selectedIndex>0}"
    styleName="rename24" click="{renameSelectedImage()}" />


<mx:Script>
<![CDATA[
    private function renameSelectedImage():void
    {
        // Need to access itemRenderer of selected image here
    }
]]>
</mx:Script>

The itemRenderer is just a mx:VBox with an mx:Image and a mx:Text. In there is another mx:State where the mx:Text changes into a mx:TextInput:

<mx:states>
    <mx:State name="rename">
        <mx:RemoveChild target="{imageName}" />
        <mx:AddChild>
            <mx:TextInput id="newName" text="{originalName}" keyDown="{textInputKeyDownHandler(event)}" 
                width="100%" focusOut="{commit()}" focusThickness="0" />
        </mx:AddChild>
    </mx:State>
</mx:states>

<enterComponents:P200Image source="{imgFunction?imgFunction.fileId:null}" width="82" height="82" verticalAlign="bottom" stretch="true" />
<mx:Text id="imageName" text="{imgFunction.name}" selectable="false" truncateToFit="true" 
    textAlign="center" width="82" toolTip="{imgFunction.name}" />
A: 

I don't think this is the best way to go.

What I would do is get the SelectedItem from the TileList. This is the data-model for this image (= an item from your images-collection). I guess this object has a property like Name, or Title. Try setting this value with the new value. When you make the object [Bindable], the correct value should appear in your ItemRenderer.

Pbirkoff
Hi, thanks for the answer. It is not exactly what I am looking for. I try to clarify. What I am after is to make a rename function like the rename function of Windows File explorer. If you click on a listed item and then hit F2, the name of a file changes into a TextInput. And the user can type the new name in the list. This is what I am trying to achieve, which means I need to change the state of my itemRenderer, so that the text changes into an input and the name becomes editable.
Mad Oxyn
Ok, I understand better what you mean. Still, I don't think this is the best way to go. It is not recommended to directly access a itemrenderer (as described here: http://www.adobe.com/devnet/flex/articles/itemrenderers_pt1.html). You should let the action take place from inside the itemrenderer. Hope you find a way to do this!
Pbirkoff
A: 

Take a look at this example--the in place editing controls might give you a place to start with your controls.

Michael Brewer-Davis
A: 

Ok, thanks Pbirkoff, your answer led me in the correct direction. The way I do it now is that I set the name property of the data object to "" as soon as F2 or the rename button is clicked on the TileList selected item.

I have implemented an Observe in the itemRenderer on that data.name property (http://blogs.adobe.com/paulw/archives/2006/05/the_worlds_smal.html). As soon as this property changes I change the state of the itemRenderer to show an input box rather than a label.

This works just like Windows file explorer now.

Some code with some relevant functions of the itemRenderer:

<mx:states>
    <mx:State name="rename">
        <mx:RemoveChild target="{imageName}" />
        <mx:AddChild>
            <mx:TextInput id="newName" text="{originalName}" keyDown="{textInputKeyDownHandler(event)}" 
                width="100%" focusOut="{commit()}" focusThickness="0" />
        </mx:AddChild>
    </mx:State>
</mx:states>

<enterComponents:P200Image source="{imgFunction?imgFunction.fileId:null}" width="82" height="82" verticalAlign="bottom" stretch="true" />
<mx:Text id="imageName" text="{imgFunction.name}" selectable="false" truncateToFit="true" 
    textAlign="center" width="82" toolTip="{imgFunction.name}" />

<util:Observe source="{imgFunction.name}" handler="{nameChangedHandler}" />

<mx:Script>
    <![CDATA[
        [Bindable]
        private var imgFunction:ImageFunctionRecord;

        [Bindable]
        public override function set data(value:Object):void
        {
            super.data = value;
            if(value)
            {
                imgFunction = ImageFunctionRecord(value);
                originalName = imgFunction.name;
            }
            else
            {
                imgFunction = null;
            }
        }
        public override function get data():Object
        {
            return imgFunction;
        }

        private function nameChangedHandler():void
        {
            if (imgFunction.name.length == 0)
                // when rename is clicked, the name property will be set to ""
                renameImage();
            else
                originalName = imgFunction.name;
        }


        private function renameImage():void
        {
            currentState = "rename";
            newName.setFocus();
            selectAllText();
        }

    ]]>
</mx:Script>
Mad Oxyn