Drag and Drop in Spark tilelist - actionscript-3

I am trying to initiate a drag&drop on a spark tilelist so the user can re-order the list manually.
The problem is : each time i drap and drop, the dropped/dragged entry is duplicated.
My main application is :
<?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"
initialize="initApp()">
<fx:Script source="com/init.as" />
</s:Application>
Included as file is :
import com.Sequence;
import mx.collections.ArrayCollection;
import mx.core.ClassFactory;
import mx.core.FlexGlobals;
import spark.components.List;
import spark.layouts.TileLayout;
public var main:List=new List()
public var ok: ArrayCollection = new ArrayCollection( [
{label:"1-redblue", data:"redblue"},
{label:"2-ncview", data:"ncview"},
{label:"3-greyscale", data:"greyscale"},
{label:"4-alg2", data:"alg2"},
{label:"5-alg", data:"alg"},
{label:"6-occam", data:"occam"},
{label:"7-rainbow", data:"rainbow"},
{label:"8-sst_36", data:"sst_36"},
{label:"9-occam_pastel-30", data:"occam_pastel-30"},
{label:"10-ferret", data:"ferret"}
]);
// ActionScript file
public function initApp(){
var lay:TileLayout=new TileLayout()
var ae:ClassFactory=new ClassFactory(Sequence)
main.layout=lay
main.dataProvider=ok
main.dragEnabled=true
main.dropEnabled=true
main.width=FlexGlobals.topLevelApplication.width
main.height=FlexGlobals.topLevelApplication.height
main.itemRenderer=ae
this.addElement(main);
}
And my item renderer looks like :
package com
{
import mx.controls.TextArea;
import mx.events.FlexEvent;
import spark.components.BorderContainer;
import spark.components.supportClasses.ItemRenderer;
public class Sequence extends ItemRenderer
{
private var borderC:BorderContainer=new BorderContainer()
private var labeli:TextArea=new TextArea()
private var d:Object
public function Sequence()
{
super();
this.addElement(borderC)
this.addEventListener(FlexEvent.CREATION_COMPLETE,doIT)
borderC.width=borderC.height=100
labeli.width=labeli.height=100
borderC.addElement(labeli)
}
override public function set data(d:Object):void{
this.d=d
}
override public function get data():Object{
return d;
}
private function doIT(e:FlexEvent):void{
labeli.text =String(d.label);
}
}
}

The property that allows to drag/drop within the same component (without duplicating) is :
allowDragMove
if set to true, the duplication does not occurs.

#gpasse got me on the right track.
The following fields need to be set to true for a Spark List in Flex 4.6.
dragEnabled="true"
dragMoveEnabled="true"
dropEnabled="true"

Related

Code behind for a flex application

I'm working on Flex project and having problems with "connecting" the code-behind to the mxml file (It actually worked before in another project). Both files are in the default package.
Hydw.mxml:
<?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" xmlns="*">
<s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</s:Application>
Hydw.as:
package
{
import flash.events.*;
import flash.external.*;
import flash.media.*;
import mx.controls.TextArea;
import mx.core.*;
import mx.events.*;
import spark.components.*;
public class Hydw extends spark.components.Application
{
public var txt_log:spark.components.TextArea;
public function Hydw ()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler);
}
private function creationCompleteHandler(param1:FlexEvent) : void
{
WriteToLog("creationCompleteHandler");
}
public function WriteToLog(s:String) : void
{
txt_log.text += s + "\n";
}
I run the application (after releasing) and I see nothing in the TextArea. Why?
By the way, I'm having trouble with the debugging for now, so I can't tell where's the failure exactly.
Obviously it didn't work. There need to make some changes in ActionScript and mxml file.
First: Remove package and class from ActionScript file like:
import mx.events.FlexEvent;
public function creationCompleteHandler(param1:FlexEvent) : void
{
WriteToLog("creationCompleteHandler");
}
public function WriteToLog(s:String) : void
{
txt_log.text += s + "\n";
}
Because It is in default package there isn't required to defined package and class.
Second:
Remove public var txt_log:spark.components.TextArea; from as file. Because it will conflict txt_log with id of textArea in mxml file.
Third:
Remove addEventListener(FlexEvent.CREATION_COMPLETE, this.creationCompleteHandler); from as file and give creation complete event in mxml file. like:
<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"
creationComplete="creationCompleteHandler(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script source="Hydw.as" />
<s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</s:Application>
And another thing is you forget to include as file inside mxml. like:
<fx:Script source="Hydw.as" />
Hope you understand and help to move forward.
This is what you want
Hydw.mxml
<?xml version="1.0" encoding="utf-8"?>
<abstract:Hydw xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:abstract="test.pack.abstract.*"
minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextArea id="txt_log" x="34" y="171" width="225" height="217"/>
</abstract:Hydw>
and your Hydw.as:
package test.pack.abstract
{
import mx.events.FlexEvent;
import spark.components.Application;
import spark.components.TextArea;
[Bindable]
public class Hydw extends Application
{
public var txt_log:TextArea;
public function Hydw()
{
super();
addEventListener(FlexEvent.CREATION_COMPLETE, init);
}
public function init(evt:FlexEvent):void
{
}
}
}
any visual component used in .mxml code you want to use in .as class
must be declared as public binded variable in your .as class or simply declare your .as class as [Bindable]
That's All

Actionscript Event Error

I try to dispatch event from actionscript class. But I get the error: "Error #1034: Type Coercion failed: cannot convert flash.events::Event#9f849c1 to mx.events.FlexEvent."
Here is my code:
MXML Code:
<?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" applicationComplete="init(event)">
<fx:Script>
<![CDATA[
import com.testing.package.MyASClass;
import mx.events.FlexEvent;
public var sv:MyASClass;
protected function init(event:FlexEvent):void
{
sv = new MyASClass();
sv.addEventListener("myevent",mainfunc);
sv.myfunc();
}
protected function mainfunc(event:FlexEvent):void
{
trace("receive event");
}
]]>
</fx:Script>
</s:WindowedApplication>
Actionscript file:
package com.testing.package
{
import flash.display.Sprite;
import flash.events.Event;
public class MyASClass extends Sprite
{
public function MyASClass()
{
}
public function myfunc():void
{
dispatch();
}
private function dispatch():void
{
dispatchEvent(new Event("myevent"));
}
}
}
Why the error occur? How can I fix it?
Thank you.
//Solution is need replace FlexEvent into Event
protected function mainfunc(event:Event):void{
trace("receive event");
}

How to Create Alert Dialog in ActionScript3?

I'm working on a project with Actionscript 3.0 in Flash Pro (CS5). I want to create a confirmation box. If I were using the Flex SDK I could do this using the Alert class in the mx.controls package. However, it seems that no similar control exists in the standard Flash library and any amount of Googling just leads me to Flex references.
Try this Class
package com.whatever {
//Imports
import flash.display.Shape;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.events.MouseEvent;
//Class
public class AlertBox extends Sprite {
//Vars
protected var box:Shape;
protected var yesBtn:Sprite;
//Constructor
public function AlertBox($:Rectangle):void {
//Initialise
box = new Shape()
yesBtn = new Sprite()
addChild(box)
addChild(yesBtn)
//Render
with (box.graphics) {
lineStyle(1)
beginFill(0, 0.4)
drawRect($.x, $.y, $.width, $.height)
endFill()
}
with (yesBtn.graphics) {
lineStyle(1, 0x00FF00)
beginFill(0x00FF00, 0.4)
drawRect($.x+$.width-100, $.y$.height-40, 80, 20)
endFill()
}
//Events
yesBtn.addEventListener(MouseEvent.CLICK, yesClickHandler, false, 0, true)
yesBtn.addEventListener(MouseEvent.MOUSE_OVER, yesOverHandler, false, 0, true)
}
//Handlers
protected function yesClickHandler($):void {}
protected function yesOverHandler($):void {}
You said that you can't import mx.Controls in AS3 but the following should work in a flex 4 project:
<?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"
creationComplete="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
private function init():void
{
Alert.show("This is an Alert!!!");
}// end function
]]>
</fx:Script>
</s:Application>
[UPDATE]
After realizing that I misunderstood the question, I looked on the internet for a Alert component for AS3 projects and found the following:
http://developer.yahoo.com/flash/astra-flash/alertmanager/
I'm going try my hand at creating a replica of the flex framework's Alert control and then update my answer again.
If your final swf is going to run in the browser and have script access, you could just use one of the JavaScript PopUp Boxes:
if(ExternalInterface.available) {
if (ExternalInterface.call("confirm", "Should I trace 'Yes'?")) {
trace("Yes"); // user clicked Okay
} else {
trace("User canceled or the call failed");
}
}
I'm pretty sure this will freeze the Flash UI loop until the JavaScript function returns, so make sure you call it when it's all you want to be doing.
I believe that you can continue to use the same Alert class
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/Alert.html

connecting to remote webservice using SOAP and WSDL using flex or actionscript

The requirement is to get the method named getIncidentList() from web service using Soap and wsdl using Flex or Actionscript.
Can anyone help me i have a code which is not working:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:WebService id="DirectoryService"
wsdl="http://cmuicds.rutgers.edu/uicds/core/ws/services/DirectoryService.wsdl"
useProxy="false"
showBusyCursor="true"
result="onResult(event)"
fault="onFault(event)">
</mx:WebService>
<mx:ApplicationControlBar dock="true">
<mx:Button id="button"
label="get incidents from web service"
click="button_click()"/>
<mx:ComboBox id="cmb" dataProvider="{zipfls}" labelField="name" width="241" height="24"/>
</mx:ApplicationControlBar>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.rpc.events.FaultEvent;
import mx.utils.ObjectUtil;
import mx.collections.ArrayCollection;
[Bindable] private var zipfls:ArrayCollection;
private var flag:Boolean;
private function button_click():void
{
//Alert.show("Hi");
//DirectoryService.GetIncidentList.send();
DirectoryService.GetIncidentList();
flag = DirectoryService.canLoadWSDL();
//flag = DirectoryService.hasOwnProperty();
//Alert.show("Testing....." + flag);
//Alert.show("Description " +DirectoryService.operations);
}
private function onResult(evt:ResultEvent):void
{
zipfls = new ArrayCollection();
//textArea.text = ObjectUtil.toString(evt.result);
zipfls = evt.result as ArrayCollection;
Alert.show("Is data comming in?" + zipfls.length);
}
private function onFault(evt:FaultEvent):void
{
Alert.show(evt.type);
}
]]>
</mx:Script>
</mx:Application>
I think you are not calling method properly
and event button_click should be
private function button_click():void
{
DirectoryService.GetIncidentList();
}
i.e. send should not be used.
also see Using WebService components
Hopes that helps

Failing to import a class in ActionScript

Here is the error I get:
1046: Type was not found or was not a compile-time constant: fbAPI.
Here is my MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="startGame();">
<mx:Script>
<![CDATA[
import fbAPI;
public function startGame():void {
var fbAPI:fbAPI = new fbAPI(); // breaks on this line
fbAPI.fbLogin();
}
]]>
</mx:Script>
</mx:Application>
And here is my fbAPI.as stub that doesn't seem to get imported:
package {
public class fbAPI {
import mx.controls.Alert;
public function fbLogin():void {
Alert.show('test');
}
}
}
Try putting your import statements above your class and also just rename the instance name of the fbapi in your mxml real quick.
Edit: nevermind, I forgot in AS3 you don't need a constructor.
Make sure you put the fbAPI.as file in the same location as your mxml file.