I have a TextInput control which has a search functionality for the people in the system.
It works fine. All I need is to style it in such a way that, it will be having search image on the right, which when clicked, will search. Its actually for look and feel part of the application, which will make the search box look much better.
It is exactly similar behavior implemented in search box embedded in Firefox.
Any solution to this?
Thanks :)
Ack, avoid subclassing. Think outside the Box, as it were, and use a Canvas:
<mx:Canvas>
<mx:TextInput change="doSearchFor(event.currentTarget.text)" />
<mx:Image source="search_icon.png" verticalCenter="0" right="5" />
</mx:Canvas>
Then make that a component itself if you want to make it neater. Favour composition over inheritance, in MXML as elsewhere.
<mx:HBox>
<mx:TextInput id = "txtSearch"/>
<mx:Image source = "yourSearchIcon.png"
click = "doSearch()"
buttonMode = "true"/>
</mx:HBox>
That's all!
You could write a subclass of TextInput Class which has as an image for "yourSearchIcon" image such as:
[Embed(source='../../libs/graphic_elements.swf#search_ico')]
private var searchIcon:Class;
private var searchImg:Image = new Image();
private function onCreationComplete(event:Event) : void {
searchImg.source = searchIcon;
searchImg.x = this.width - 40;
this.addChild(searchImg);
this.addEventListener(ResizeEvent.RESIZE, onResize);
}
obviously you have to handle the resize event
private function onResize(event:ResizeEvent) : void {
searchImg.x = event.currentTarget.width - 40;
}
That's your custom component
Hope this code will help you. This code adds a search icon to the left of the TextInput.
public class SearchInputBox extends TextInput
{
[Embed(source='../../../../assets/images/icons/searchIcon.png')]
private var searchIcon:Class;
private var searchImg:Image;
override protected function createChildren():void
{
super.createChildren();
searchImg = new Image();
searchImg.source = searchIcon;
searchImg.width=15;
searchImg.height=15;
searchImg.x = 2;
searchImg.y = 3;
setStyle("paddingLeft",searchImg.width+2);
addChild(searchImg);
}
}
Related
I am trying to embed an image using an item renderer in my Flex project.
The image path however is a String passed in as a bound variable.
I am aware that
<s:BitmapImage source="#Embed('/../assets/image.png')" />
works because the image is embedded at runtime? (Could someone please clarify this)
How would i go about embedding my bound string, somewhat like this:
<s:BitmapImage source="#Embed('/../assets/{data.image}')" />
Many Thanks
I think a better choice if you'd like to embed the image but find it dynamically at runtime is: Embed all of the images it could be and then grab a reference to it dynamically. We generally use a pattern like this:
public class Icons {
[Embed(source="icons/icon1.png")]
public var icon1:Class;
[Embed(source="icons/icon2.png")]
public var icon2:Class;
}
Then you can dynamically grab the embedded images from your Icons instance at run-time.
Edit - self contained example - I'll use an item renderer since I think that's what you're doing.
Let's assume data.image can be 'plane' 'train' or 'automobile'
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
[Embed(source="/assets/icons/plane.png")]
public var plane : Class;
[Embed(source="/assets/icons/train.png")]
public var train : Class;
[Embed(source="/assets/icons/automobile.png")]
public var automobile : Class;
]]>
</fx:Script>
<s:Image source="{this[data.image]}"/>
</s:ItemRenderer>
This is a really simple example and not the BEST way to implement, but you get the idea.
I like embed icons with css files. Then in ItemRenderer you can set css class and get image which you want.
css file or mxml css block:
.icons
{
bender: Embed(source="/assets/bender.png");
/* other icons */
}
In renderer, when you override set data method:
override public function set data(value:Object):void
{
super.data = value;
var iconName:String = data.image;
if ( iconName )
{
var cssDecl2:CSSStyleDeclaration = styleManager.getStyleDeclaration(".icons");
var IconClass:Class = cssDecl2.getStyle( iconName );
bmImage.source = new IconClass();
}
}
and bmImage as id s:BitmapImage:
<s:BitmapImage id="bmImage" />
I want to have a tooltip that shows different things when the mouse goes over a different part of a component. For example if it is the top half of a component it will show one tooltip. If the mouse is on the bottom half of the segment then the tooltip will be another. I have some code I have written that returns a panel with string in. This code is on another computer so I'll post code tomorrow.
Is it possible in ActionScript to have different tooltips (or rather differnt values in a tooltip) for different parts of a segment?
The code I have so far is:
MyToolTip.mxml
<?xml version="1.0"?>
<mx:Panel xmlns:mx="http://www.adobe.com/2006/mxml"
implements="mx.core.IToolTip"
alpha=".9" width="325" borderColor="black" borderStyle="solid"
cornerRadius="10" horizontalAlign="center">
<mx:Script><![CDATA[
[Bindable]
public var toolTipText:String = "";
public var _text:String;
[Bindable]
public function get text():String { return _text; }
public function set text(value:String):void {}
]]></mx:Script>
<mx:HBox width="100%" height="100%">
<mx:Text text = "Text here" width = "50%"/>
<mx:Text text = "{toolTipText}" width = "50%"/>
</mx:HBox>
</mx:Panel>
And then my action script class component that I want the tooltip to be against.
public class MyComponent extends mx.containers.VBox {
private var tt:MyToolTip
public function MyComponent() {
this.addEventListener(ToolTipEvent.TOOL_TIP_CREATE, toolTipCreateHandler);
this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
tt = new MyToolTip();
}
override protected function drawFigure():void {
//Need to kick the TOOL_TIP_CREATE event...and needs to be a value (eg a SPACE).
//If blank then no tooltip is created
this.toolTip = " ";
super.drawFigure();
}
private function toolTipCreateHandler(event:ToolTipEvent):void {
var toolTipText:String = "tooltip1";
eventToolTip.toolTipText = toolTipText;
event.toolTip = tt;
}
private function mouseOverHandler(event:MouseEvent):void {
//perhaps I need to be more efficient here and only fire
//when the mouse goes into top half or bottom half
//This does not appear to update the toolTipText in the view
var halfwayUp:Number = getBounds(this).height / 2;
if (event.localY < halfwayUp) {
eventToolTip.toolTipText = "tooltip2";
}
else {
eventToolTip.toolTipText = "tooltip1";
}
}
}
}
Any help or pointers in how to update the tooltip when it is already displaying would be great.
Yes, its possible, the trick is to know how tooltips work:
Tooltips get created, if you mouse over a component, and are destroyed if you mouse out. So if you change the text on a tooltip while its displayed, then you wont see the change, because the set toolTip() function does not creates a new tooltip, if one already exists. So the solution is to destroy the currently showing tooltip, and make a new one. To destroy a tooltip, you can set its value to an empty string.
Here is a sample code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
mouseMove="application1_mouseMoveHandler(event)">
<mx:Script>
<![CDATA[
import mx.managers.ToolTipManager;
protected function application1_mouseMoveHandler(event:MouseEvent):void{
if (mouseX < 100) {
testButton.toolTip = ""
testButton.toolTip = "on the left side";
} else {
testButton.toolTip = ""
testButton.toolTip = "on the right side";
}
}
]]>
</mx:Script>
<mx:Button id="testButton" label="test" width="200" height="200" />
</mx:Application>
Note: If you want to mess more with tooltips in Flex, you can get the current tooltip with ToolTipManager.currentToolTip (and modify its properties without destroying it).
I've trawled the net trying to find a solution, but everything seems to be mxml-centric. What I want is to dynamically create a series of Graphics objects each with a child BitmapImage. However, this doesn't seem to work:
var bmi:BitmapImage = new BitmapImage();
bmi.source="#Embed('custom-case.png')";
var gr:Graphic = new Graphic( );
gr.addElement( bmi );
gr.x = 50;
gr.y = 50;
this.addElement( gr );
Whereas, this does:
<s:Graphic x="250" y="250">
<s:BitmapImage source="#Embed('custom-case.png')">
</s:BitmapImage>
</s:Graphic>
Thanks in advance for any ideas.
Paul
it is quite different in AS3, you have to define a variable class type like shown below.
[Embed("custom-case.png")]
private var someImage:Class;
...
bmi.source=someImage;
To follow up Shruti's comment/question (I am unable to post comment since my current reputation insufficent):
The requirement for dynamic updating of images with mxml is the same as indicated with the original answer, which is that any images you might want to dynamically change to must be pre-embeded in your mxml:
[Embed(source="image.png")] private var theImage:Class;
which can be later used to update an image source as such:
<fx:Script>
<![CDATA[
[Embed(source="image.png")] private var theImage:Class;
private function updateImage():void {
image.source = theImage;
}
]]>
</fx:Script>
<s:BitmapImage id="image" source="#Embed('defaultImage.png')"/>
The concept of this seems easy, but I'm having trouble getting it right and can't find anything to help me on this.
I have a panel I need to perform a drag and drop operation on, but I only want to perform that if the user mouses down on a particular area of the panel. I can add an Icon to the panel by doing this:
[Embed("/img/icon.png")]
[Bindable]
public var dragIcon:Class;
newPanel.titleIcon = dragIcon;
But what I really want to add is a box, which I can then add my listeners to for the drag and mouse down like I do on some canvases created in actionscript like so
var tempBox:Box = new Box;
tempBox.x=0;
tempBox.y=0;
tempBox.width = 20;
tempBox.height = 44;
tempBox.setStyle("horizontalAlign","center");
tempBox.setStyle("verticalAlign","middle");
tempBox.addEventListener(MouseEvent.ROLL_OVER,over);
tempBox.addEventListener(MouseEvent.ROLL_OUT,out);
tempBox.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownAnswer);
var tempImg:Image = new Image();
tempImg.source = grabbableItem;
tempBox.addChild(tempImg);
myCanvas.addChild(tempBox);
So what do I need to do to use that tempBox and turn it into a class to be used as my panels titleIcon?
Edit 12/29/09:
So I came up with something where I'm extending the panel class (shown below) but all this is really doing is covering up the icon with something I can access publicly. I'm sure there's a better way out there right?
package custClass
{
import mx.containers.Box;
import mx.containers.Panel;
import mx.controls.Image;
public class DragPanel extends Panel
{
[Bindable] public var iconBox:Box = new Box();
[Embed("../img/doc_page.png")] [Bindable] public var grabbableItem:Class;
public function DragPanel()
{
super();
}
override protected function createChildren():void{
super.createChildren();
iconBox.x = 10
iconBox.y = 4
iconBox.width = 20;
iconBox.height = 20;
iconBox.setStyle("horizontalAlign","center");
iconBox.setStyle("verticalAlign","middle");
iconBox.setStyle("borderStyle","solid");
iconBox.setStyle("backgroundColor",0x000000);
var tempImg:Image = new Image();
tempImg.source = grabbableItem;
iconBox.addChild(tempImg);
this.rawChildren.addChild(iconBox);
}
}
}
EDIT 1/7/10 (or 16 according to my windows mobile phones text messages):
Using Chaims help from below here is my new answer.
Create a box mxml component like Chaim says but also add the following script block to it.
<mx:Script>
<![CDATA[
import mx.core.Application;
[Embed("/img/doc_page.png")]
[Bindable]
public var grabbableItem:Class;
public function init():void{
this.addEventListener(MouseEvent.MOUSE_DOWN,Application.application.mouseDownSection);
this.addEventListener(MouseEvent.ROLL_OVER,Application.application.over);
this.addEventListener(MouseEvent.ROLL_OUT,Application.application.out);
}
]]>
</mx:Script>
This adds in all the event listeners I want on the Box that will be used as my icon. Now just add the box as an Icon and it's good to go.
panel.titleIcon = DraggableBox;
I guess since it's a separate mxml component it is now a class, though I don't think I understand why.
The Panel expecting titleIcon property value to be a IFactory and create an instance by himself.
Make your box a component (lets name it DraggableBox.mxml):
<?xml version="1.0" encoding="utf-8"?>
<mx:Box xmlns:mx="http://www.adobe.com/2006/mxml"
x="0" y="0" width="20" height="44"
horizontalAlign="center" verticalAlign="middle">
<mx:Image source="{grabbableItem}"/>
</mx:Box>
And assign it to titleIcon:
<mx:Panel titleIcon="{DraggableBox}" >
...
</mx:Panel>
If you want do it in ActionScript use ClassFactory:
panel.titleIcon = new ClassFactory(DraggableBox);
I need to bind a property to an edit control and have the control write its value back to the same property. The problem is, I'm setting the source value before the control is created:
<mx:Panel>
<mx:Script>
<![CDATA[
[Bindable] public var editedDocument: XML;
]]>
</mx:Script>
<mx:TextInput id="docLabel" text="{editedDocument.#label}"/>
<mx:Binding source="docLabel.text" destination="editedDocument.#label"/>
</mx:Panel>
I call this like so:
var xmlDoc: XML = <document label="some label" />;
var myPanel: MyPanel = new MyPanel();
myPanel.editedDocument = xmlDoc;
parent.addChild(myPanel);
What happens is this:
the docLabel text field ends up blank (equal to "")
the xmlDoc's #label attribute is set to ""
What I want is this:
the docLabel text field should contain "some label"
the xmlDoc's #label attribute should change only when the docLabel's text property changes.
How do I accomplish this, using Flex 3?
Edit
I have also tried this:
<mx:Panel>
<mx:Script>
<![CDATA[
[Bindable] public var editedDocument: XML;
]]>
</mx:Script>
<mx:TextInput id="docLabel"/>
<mx:Binding source="editedDocument.#label" destination="docLabel.text"/>
<mx:Binding source="docLabel.text" destination="editedDocument.#label"/>
</mx:Panel>
The result is the same.
You can try using BindingUtils to programmatically create the binding after the class has been created:
http://life.neophi.com/danielr/2007/03/programmatic_bindings.html
There are many variations of this that I've used to tackle similar problems. If you can't figure it out from the link post a comment and I'll dig through my source code and see what I can find.
private function init():void
{
var xmlDoc: XML = <document label="some label" />;
var myPanel: MyPanel = new MyPanel();
myPanel.editedDocument = xmlDoc;
parent.addChild(myPanel);
BindingUtils.bindProperty(docLabel, "text", editedDocument, "label");
//or maybe it should be one of these, I have not done binding to an XML attribute before
BindingUtils.bindProperty(docLabel, "text", editedDocument, "#label");
BindingUtils.bindProperty(docLabel, "text", editedDocument, "{#label}");
}
Take a look at Two-way data binding.
Take a look at the part of the text:
In Flex 3, if you want to set
two-way binding using the
mx:Binding
tag you need to set it twice:
mx:Binding source="a.property" destination="b.property"/>
mx:Binding source="b.property" destination="a.property"/>
which becomes:
mx:Binding source="a.property" destination="b.property" twoWay="true"/>
In Flex 3 you would be better of doing something like this. Also not sure you can bind directly to XML?
Instead do something like this:
[Bindable] public var tmpString: String;
public var onChange():void {
tmpString = docLabel.text;
//set the XML string, etc.
}
]]>
</mx:Script>
<mx:TextInput id="docLabel" text="{tmpString}" change="onChange()" />
I think bidirectional data binding is a new feature in Flex 4.
This is straight from Adboe http://opensource.adobe.com/wiki/display/flexsdk/Two-way+Data+Binding, and it's Flex 3 too!
I created custom controls that programmatically create two-way bindings when given a provider object that has a suitable property whose name matches the control's id. Here's an example for a TextInput:
public class BoundTextInput extends TextInput
{
// some code omitted for brevity:
// Create getter / setter pair, call invalidateProperties()
// and set internal flag for efficiency
// create bindings in commitProperties:
override protected function commitProperties():void
{
if (this._fProviderChanged) {
this._fProviderChanged = false;
if (null != this._provider && this._provider.hasOwnProperty(this.id) && this._provider[this.id] is String) {
// this is the core bit
BindingUtils.bindProperty(this, "text", this._provider, this.id);
BindingUtils.bindProperty(this._provider, this.id, this, "text");
}
}
// Normally, you call the overridden method first,
// but we want to see the values initialized by the new
// binding right away, so we first create the bindings
// and then commit all inherited properties
super.commitProperties();
}
}
This is an example of how I use it inside one of my other components (a popup dialog). The data property is set to an instance of the appropriate model class, which is always a dumb, [Bindable] container.
<?xml version="1.0" encoding="utf-8"?>
<PopUp xmlns="com.econemon.suite.gui.components.*" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Form width="100%" height="100%" >
<mx:FormHeading label="Server-URL" />
<mx:FormItem label="URL" >
<!--
If it is available, and of type String, data.urlServer
is bound to the text property of this TextInput
-->
<BoundTextInput id="urlServer" provider="{this.data}"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="OK" click="this.submit(event)" />
</mx:FormItem>
</mx:Form>
</PopUp>