Please help
I can save the data from the three components as xml and it works, but now I am struggling with the code to read that data back into the components when the user opens. This is a local file that is created by the user. I need help with the open event handler.
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="734" height="389"
creationComplete="init();">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var xmlData:XML=<ROOTS></ROOTS>;
private function fnAddItem():void
{
var fr:FileReference = new FileReference();
var ba:ByteArray = new ByteArray();
var newXmlRow:XML=<ROOTS>
<TXT>{txt1.text}</TXT>
<TXTA>{txt2.text}</TXTA>
<DTF>{txt3.text}</DTF>
</ROOTS>;
ba.writeMultiByte(newXmlRow, 'utf-8');
fr.save(ba);
}
protected function oped_clickHandler(event:MouseEvent):void
{
var fr:FileReference = new FileReference();
var ba:ByteArray = new ByteArray();
var newXmlRow:XML=<ROOTS>
<TXT>{txt1.text}</TXT>
<TXTA>{txt2.text}</TXTA>
<DTF>{txt3.text}</DTF>
</ROOTS>;
ba.readMultiByte(xmlData, 'utf-8');
fr.load(ba);
}
]]>
</fx:Script>
<s:Label x="108" y="80" text="Name"/>
<s:Label x="91" y="222" text="Remarks"/>
<s:Label x="108" y="116" text="text"/>
<s:TextInput id="txt1" x="167" y="78"/>
<s:TextArea id="txt2" x="167" y="218" height="86"/>
<s:TextArea id="txt3" x="167" y="108" height="77"/>
<s:Button x="53" y="242" label="save" width="90" click="fnAddItem()"/>
<s:Button id="oped" x="73" y="271" label="open" click="oped_clickHandler(event)"/>
</s:WindowedApplication>
first if you want a dialog you have to wait for the user to make a selection. The selection throws an event that you can catch. Within the handler you can do the file handling. Try if the following code works for you.
private var openedFile:File;
private function oped_clickHandler(event:MouseEvent):void {
openedFile = new File();
openedFile.addEventListener(Event.SELECT, file_select);
openedFile.browseForOpen("Please select a file...");
}
private function file_select(event:Event):void {
if(openedFile != null && openedFile.exists){
var fileStream:FileStream = new FileStream();
fileStream.open(openedFile, FileMode.READ);
var readXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
fileStream.close();
trace(readXML.toString());
txt1.text = readXML.TXT;
txt2.text = readXML.TXTA;
txt3.text = readXML.DTF;
}
trace(event);
}
cheers,
rob
Related
I want to show the busyIndicator before launching a HTTPService , and hide it when the rpc is finished :
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
title="Saisie et envoi"
creationComplete="creationCompleteHandler(event)" >
<fx:Declarations>
<!-- Placer ici les éléments non visuels (services et objets de valeur, par exemple). -->
<mx:HTTPService id="userRequest" url="{url}/crr.php" resultFormat="text" result="userRequest_resultHandler(event)" useProxy="false" method="POST" />
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
import model.Config;
import model.Db;
var conf:Config = new Config();
public var db:Db = new Db();
[Bindable]
public var url:String = conf.getConfigSo("url"); // example : http://localhost/myfolder
protected function enregistrer(event:MouseEvent):void
{
var param:Object = new Object();
try {
if (db.avoirAttribut("enreg")) {
db.setDbSo("enreg", db.getDbSo("enreg")+chp1.text+";"+chp2.text+"\n");
}
else {
db.setDbSo("enreg", chp1.text+";"+chp2.text+"\n");
}
param.text = "Enregistrement effectué avec succès !";
navigator.pushView(Message, param);
}
catch (e:Error) {
param.text = "Enregistrement non effectué !";
navigator.pushView(Message, param);
}
}
protected function lire(event:MouseEvent):void
{
area.text = db.getDbSo("enreg");
}
protected function send(event:MouseEvent):void
{
busy.visible = true;
userRequest.cancel();
var params:Object = new Object();
params.col1 = db.getDbSo("enreg");
userRequest.send(params);
busy.visible = false;
}
protected function userRequest_resultHandler(event:ResultEvent):void
{
resultHTTP.text = userRequest.lastResult.toString();
}
protected function creationCompleteHandler(event:FlexEvent):void
{
chp1.setFocus();
}
]]>
</fx:Script>
<s:Scroller width="100%" height="100%">
<s:VGroup width="100%" height="100%" paddingTop="5" paddingBottom="5" verticalAlign="middle" horizontalAlign="center" gap="5">
<s:TextInput id="chp1" width="50%"/>
<s:TextInput id="chp2" width="50%"/>
<s:Button label="Enregistrer" click="enregistrer(event)"/>
<s:Button label="Lire" click="lire(event)"/>
<s:TextArea id="area" editable="false"/>
<s:Button label="Envoyer" click="send(event)"/>
<s:BusyIndicator id="busy" symbolColor="blue" visible="false" />
<s:Label id="resultHTTP"/>
</s:VGroup>
</s:Scroller>
</s:View>
The problem is that at runtime the BusyIndicator is not shown , though I realise that it took some seconds for the entire operation to complete. So why is not the BusyIndicator shown in this case ?
The HTTPService request send() returns when the request is sent, not when the response is received. So you are showing the busy indicator only for a short period of time (and actually not at all, since the rendering is done in the next display cycle...)
What you need to do is to make the indicator visible in the send function as you're doing, but hide the indicator on the result handler. (You should do this for the error handler as well, otherwise the indicator will still be visible if there was an error in the request)
Hello I'm building a chat using Flex. The problem is how to do that new user get the list off all users online and added to the lists of all users. I try to put this information in DataGrid through dataProvider "callerns":
<s:DataGrid x="10" y="125" width="238" height="125" alternatingRowColors="[ #67676767, #555555]"
borderVisible="true" chromeColor="#555555" color="#CCCCCC"
contentBackgroundColor="#555555" dataProvider="{callerns}" fontWeight="bold"
requestedRowCount="4" rollOverColor="#08700D" selectionColor="#08700D"
symbolColor="#CCCCCC">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="name" headerText="USER ONLINE"></s:GridColumn>
<s:GridColumn dataField="peerID" headerText="USER ID"></s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
Here is the part of code:
[Bindable]
private var callerns:ArrayCollection = new ArrayCollection();
...........
private function netStatusEvent(event:NetStatusEvent):void{
trace('NetConnection status event (1): ' + event.info.code);
//writeText(event.info.code);
switch(event.info.code){
case "NetConnection.Connect.Success":
log('Connected (NearID: '+nc.nearID+')', 'debug');
log('Connection sucsessful');
MyPeerID = nc.nearID;
txtFingerprint.text = MyPeerID;
initSendNetStream();
callerns.addItem({peerID: MyPeerID, name: myName});
setupGroup();
break;
case "NetGroup.Posting.Notify":
receiveMessage(event.info.message);
log('Message posted');
break;
case "NetGroup.Connect.Success":
log('Net Group connection sucsessful');
connected = true;
break;
case 'NetStream.Connect.Success':
log('Peer Connected (FarID: '+event.info.stream.farID+')', 'debug');
break;
case "NetGroup.Neighbor.Connect":
log('New user connected');
break;
}
}
private function initSendNetStream():void{
trace("initSendStream");
sendStream = new NetStream(nc,NetStream.DIRECT_CONNECTIONS);
sendStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
var clientObject:Object = new Object();
clientObject.onPeerConnect = function(callerns:NetStream):Boolean{
initRecvStream(callerns.farID);
callerns.send('onPeerNameUpdate', MyPeerID, myName);
return true;
}
sendStream.client = clientObject;
sendStream.publish('video');
log('Net Stream publish start');
}
private function initRecvStream(peerID:String):void {
//log('initRecvStream', 'debug');
var stream:NetStream = new NetStream(nc, peerID);
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusEvent);
stream.play('video');
var client:Object = new Object();
client.onPeerNameUpdate = onPeerNameUpdate;
stream.client = client;
var peer:Object = new Object();
peer.stream = stream;
recvStreams[peerID] = peer;
}
private function onPeerNameUpdate(peerID:String, name:String):void {
//log('onPeerNameUpdate received: '+peerID+':'+name, 'debug');
//log(name+' connected to your channel', 'debug');
callerns.addItem({peerID: peerID, name: name});
}
Unfortunately this doesn't work and new users aren't added to the grid. Could you please help me to solve this problem?
Removed original post seems I'm wrong, this works:
<?xml version="1.0" encoding="utf-8"?>
<s:Group creationComplete="group1_creationCompleteHandler(event)"
height="100%"
width="100%"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
[Bindable]
private var callerns : ArrayCollection;
protected function group1_creationCompleteHandler(event : FlexEvent) : void
{
callerns = new ArrayCollection();
}
protected function addItemClickHandler(event : MouseEvent) : void
{
callerns.addItem({peerID: "somePeerID", name: "someName"});
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:Button click="addItemClickHandler(event)"
label="Add Item"/>
<s:Button click="propertyChangeClickHandler(event)"
label="Dispatch Property Change"/>
<s:DataGrid alternatingRowColors="[ #67676767, #555555]"
borderVisible="true"
chromeColor="#555555"
color="#CCCCCC"
contentBackgroundColor="#555555"
dataProvider="{callerns}"
fontWeight="bold"
height="125"
requestedRowCount="4"
rollOverColor="#08700D"
selectionColor="#08700D"
symbolColor="#CCCCCC"
width="238"
x="10"
y="125">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="name"
headerText="USER ONLINE">
</s:GridColumn>
<s:GridColumn dataField="peerID"
headerText="USER ID">
</s:GridColumn>
</s:ArrayList>
</s:columns>
</s:DataGrid>
</s:Group>
So I was originally wrong, hitting the button does cause an update to the grid. It would have been extremely helpful if you had posted a link to the original tutorial you were building off of, I found it and got it working with spark just fine. I'm assuming you got this part figured out already but here's the basic chat client:
<?xml version="1.0" encoding="utf-8"?>
<s:Application minHeight="600"
minWidth="955"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private const SERVER_ADDRESS : String = "rtmfp://p2p.rtmfp.net/";
private const DEVELOPER_KEY: String = PUT_YOUR_DEV_KEY_HERE;
private var nc : NetConnection;
private var myPeerID : String;
private var farPeerID : String;
// streams
private var sendStream : NetStream;
private var recvStream : NetStream;
private function initConnection() : void
{
if (txtFingerprint.text)
{
farPeerID = txtFingerprint.text;
}
nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
nc.connect(SERVER_ADDRESS + DEVELOPER_KEY);
}
private function ncStatus(event : NetStatusEvent) : void
{
trace(event.info.code);
myPeerID = nc.nearID;
txtFingerprint.text = myPeerID;
}
private function initSendStream() : void
{
trace("initSendStream");
sendStream = new NetStream(nc, NetStream.DIRECT_CONNECTIONS);
sendStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
sendStream.publish("media");
var sendStreamClient : Object = new Object();
sendStreamClient.onPeerConnect = function(callerns : NetStream) : Boolean
{
farPeerID = callerns.farID;
trace("onPeerConnect " + farPeerID);
return true;
}
sendStream.client = sendStreamClient;
}
private function initRecvStream() : void
{
recvStream = new NetStream(nc, farPeerID);
recvStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
recvStream.play("media");
recvStream.client = this;
}
public function receiveSomeData(str : String) : void
{
txtReceiveData.text = str;
}
private function sendSomeData() : void
{
sendStream.send("receiveSomeData", txtSendData.text);
}
private function netStatusHandler(event : NetStatusEvent) : void
{
trace(event.info.code);
}
]]>
</fx:Script>
<mx:TextInput id="txtFingerprint"
width="391"
x="10"
y="10"/>
<mx:Button click="initConnection()"
label="Connect"
x="409"
y="10"/>
<mx:TextInput id="txtSendData"
x="10"
y="40"/>
<mx:TextInput id="txtReceiveData"
width="251"
x="10"
y="70"/>
<mx:Button click="sendSomeData()"
label="Send data"
x="178"
y="40"/>
<mx:Button click="initSendStream()"
label="initSendStream"
x="10"
y="100"/>
<mx:Button click="initRecvStream();"
label="initReceiveStream"
x="132"
y="100"/>
<mx:Text height="122"
text="Hint: First running Flash app - click Connect to get Fingerprint PeerID. Copy and paste this PeerID to second running Flash app to the same field and click Connect. Then initSendStream and initReceiveStream on both of them and finally you can write some text and click Send data."
width="391"
x="10"
y="130"/>
</s:Application>
Get a developer key here:
http://www.adobe.com/cfusion/entitlement/index.cfm?e=cirrus
I'm not sure what's going wrong but as the commenter above says you need to include how exactly it's breaking, that is what happens when you debug (be sure to install the debug flash player for whatever browser your using, beware chrome manages it's own plugin by default) what do you see in the console, do you get any errors etc. The tutorial app works fine for me, having this deal with two way communication and maintaining a list of all connected peers seems to be the goal correct? I also don't understand the question about NetGroup, there's more on P2P that covers that here though:
http://www.adobe.com/devnet/flashmediaserver/articles/p2p_rtmfp_groups.html
I wish you would have included more information about where you started and where you're headed with all this though (and code, I always want more code :)
I don't even know how to explain this behavior but I'll try. I am loading images from an external url that requires basic auth so I am using URLLoader to load the image from a unique ID. The ID gets passed to the itemrenderer which then proceeds to load the image. But the images switch around on their own when I scroll. If I load more than 7 images or so it starts repeating images....
Youtube video of error:
http://www.youtube.com/watch?v=ZYoqlS14gWQ
Relevant code:
<s:ItemRenderer name="RandomItemRenderer" creationComplete="init();"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="false">
<s:states>
<s:State name="normal" />
<s:State name="hovered" />
<s:State name="selected" />
</s:states>
<fx:Script>
<![CDATA[
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.net.URLRequestHeader;
import flash.net.URLRequestMethod;
import mx.utils.ObjectProxy;
import customclasses.Settings;
[Bindable] private var coverArtImage:Image;
private var myCoverArtLoader:URLLoader;
[Bindable] private var coverArtSource:String;
private function init():void {
get_coverArt();
}
private function get_coverArt(): void {
if (!data.coverArt) {
set_nullCoverArt();
} else {
var requestString:String = "/rest/getCoverArt.view?v=1.5.0&c=AirSub&id=" + data.coverArt;
var requestURL:String = Settings.ServerURL + requestString;
myCoverArtLoader = new URLLoader();
var myRequest:URLRequest = new URLRequest();
var authHeader:URLRequestHeader = new URLRequestHeader();
authHeader.name = 'Authorization';
authHeader.value = 'Basic ' + Settings.EncryptedCreds();
myRequest.requestHeaders.push(authHeader);
myRequest.url = requestURL;
myRequest.method = URLRequestMethod.GET;
myCoverArtLoader.dataFormat = URLLoaderDataFormat.BINARY;
myCoverArtLoader.addEventListener(Event.COMPLETE, set_coverArt);
myCoverArtLoader.addEventListener(IOErrorEvent.IO_ERROR, set_failedCoverArt);
myCoverArtLoader.load(myRequest);
}
}
private function set_coverArt(evt:Event) : void {
coverArtImage = new Image();
coverArtImage.source = myCoverArtLoader.data;
myCoverArtLoader.removeEventListener(Event.COMPLETE, set_coverArt);
}
private function set_nullCoverArt() : void {
coverArtImage = new Image();
coverArtImage.source = "assets/nullCoverArt.jpg";
}
private function set_failedCoverArt() : void {
coverArtImage = new Image();
coverArtImage.source = "assets/nullCoverArt.jpg";
myCoverArtLoader.addEventListener(IOErrorEvent.IO_ERROR, set_nullCoverArt);
}
]]>
</fx:Script>
<s:Image source.normal="assets/coverOutline.png" source.selected="assets/coverOutlineYellow.png" source.hovered="assets/coverOutlineYellow.png"
height="226" width="226" />
<s:VGroup top="4.5" bottom="5" width="200" horizontalAlign="center" letterSpacing="10"
paddingBottom="5" paddingTop="9" verticalAlign="middle" x="13.5">
<s:Image id="ui_imgCoverArt" width="200" height="200" source="{coverArtImage.source}"/>
<s:Label text="{data.title}" width="160" styleName="RandomList" />
</s:VGroup>
ItemRenderers are reusable and cached, i.e. there are only limited count created in List to fill its area (rowCount +- couple). And when you scroll, new renderers are not instantiated, instead the one renderer that was scrolled out goes up or down and is filled with new data.
That's why you can not rely on creationComplete event, it will be fired only once for each instance of renderer.
The solution is to override data setter and build there the behaviour needed:
override public function set data(value:Object):void
{
super.data = value;
get_coverArt();
}
Useful link: How flex itemRenderer works ? (their life cycle)
I load image to control than I applie some effects, and when I save image it's saving without effects. What should i do?
Here is the code:
private var byteArr2:ByteArray;
private var fileRef:FileReference = new FileReference();
public function process():void
{
var ct:ColorTransform = new ColorTransform();
ct.redOffset = 99;
ct.blueOffset = 11;
ct.greenOffset = 22;
currImg.transform.colorTransform = ct;
callLater(toByteArray);
}
public function toByteArray():void
{
var data:BitmapData = new BitmapData(currImg.width, currImg.width);
data.draw(currImg);
var encod:JPEGEncoder = new JPEGEncoder(100);
byteArr2 = encod.encode(data);
}
public function saveFile():void
{
fileRef.save(byteArr2,"NewFileName1.jpg");
}
<mx:HBox>
<mx:VBox>
<s:Button x="69" y="98" label="open" click="open()()"/>
<s:Button label="show" click="show()"/>
<s:Button label="process" click="process()"/>
<s:Button label="save" click="saveFile()"/>
</mx:VBox>
<mx:Image id="currImg" width="200" height="300"/>
</mx:HBox>
UPDATE Appears new problem as I am using var data:BitmapData = new BitmapData(currImg.width, currImg.width); saved image is small(size like image control) but I need to save image with original size.
With var data:BitmapData = Bitmap(currImg.content).bitmapData; it worked
I would draw the component into a new BitmapData object rather than use the content of the currImg. This should give you what's drawn on the screen rather than the unmodified content. Something like so:
var data:BitmapData = new BitmapData(currImg.width, currImg.width);
data.draw(currImg);
Hope that helps.
Alright this isn't a great solution cause I don't know why it works but if you put a container around the image then save the results of drawing that it seems to work.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.graphics.codec.JPEGEncoder;
private var byteArr2:ByteArray;
private var fileRef:FileReference = new FileReference();
public function process():void
{
var ct:ColorTransform = new ColorTransform();
ct.redOffset = 99;
ct.blueOffset = 11;
ct.greenOffset = 22;
currImg.transform.colorTransform = ct;
callLater(toByteArray);
}
public function toByteArray():void
{
var data:BitmapData = new BitmapData(everything.width, everything.width);
data.draw(everything);
var encod:JPEGEncoder = new JPEGEncoder(100);
byteArr2 = encod.encode(data);
}
public function saveFile():void
{
fileRef.save(byteArr2,"NewFileName1.jpg");
}
]]>
</fx:Script>
<mx:HBox>
<mx:VBox>
<!--<s:Button x="69" y="98" label="open" click="open()"/>-->
<!--<s:Button label="show" click="show()"/> -->
<s:Button label="process" click="process()"/>
<s:Button label="save" click="saveFile()"/>
</mx:VBox>
<mx:Box id="everything">
<mx:Image id="currImg" width="200" height="300" source="http://www.google.com/images/logos/ps_logo2.png"/>
</mx:Box>
</mx:HBox>
</s:Application>
Shaun
I have a DataGrid with a custom itemRenderer(Canvas) which has a context menu on its right click. I am trying to get the data of the itemRenderer.
I tried to find something in event & variables. I also tried with FlexNativeMenu on RIGHT_MOUSE_CLICK. But I didn't find any way out.
Please help me in getting the data of the itemrenderer or its instance.
contextMenuOwner property of the ContextMenuEvent dispatched by the ContextMenu would point to the itemRenderer of interest.
var renderer:Canvas = Canvas(event.contextMenuOwner);
trace(renderer.data);
trace(renderer.data.something);
Use ContextMenuEvent.mouseTarget:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" initialize="init();">
<fx:Script>
<![CDATA[
import mx.core.IDataRenderer;
private function init():void
{
var dataGridContextMenu:ContextMenu = new ContextMenu();
dataGridContextMenu.hideBuiltInItems();
dataGridContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,
menuSelectHandler);
dataGrid.contextMenu = dataGridContextMenu;
}
private function menuSelectHandler(event:ContextMenuEvent):void
{
var displayObject:DisplayObject = event.mouseTarget as DisplayObject;
while (!(displayObject is IDataRenderer) && !(displayObject == dataGrid))
{
displayObject = displayObject.parent;
}
var data:Object;
if (displayObject is IDataRenderer)
data = IDataRenderer(displayObject).data;
var customItems:Array = [];
if (data)
{
var contextMenuItem:ContextMenuItem = new ContextMenuItem(data.name, false, false);
customItems.push(contextMenuItem);
}
var menu:ContextMenu = ContextMenu(event.target);
menu.customItems = customItems;
}
]]>
</fx:Script>
<mx:DataGrid id="dataGrid">
<mx:dataProvider>
<s:ArrayCollection>
<fx:Object name="Mike" age="21"/>
<fx:Object name="Juss" age="19"/>
</s:ArrayCollection>
</mx:dataProvider>
</mx:DataGrid>
</s:Application>