Friday, September 29, 2006

Programatically adding appender to Log4j

Wanted to use Log4j without configuration file.
So here is the clean code.

//Java

FileAppender fa = new FileAppender(new SimpleLayout(),filename,true);


fa.activateOptions();
Logger logger = Logger.getLogger(filename);
logger.addAppender(fa);
logger.info(data);
logger.removeAllAppenders();

Thursday, September 28, 2006

SWFLoader : Dynamically load / unload my 'sub applications' in Flex 2.0

Dynamically load / unload my 'sub applications' in Flex 2.0 (compiled swf) but user feel should be as if only one mxml has been loaded.

One problem in flex...

You have 3 .mxmls that get compiled into .swf ( Say A , B , C . )

step1 You have to load B inside A
step2 In B there is a button on click you have to unload B from A and load C
step3 Also in C there is a button on click you have to unload C from A and load B

---

My approach

for Step1 : cool!
I am loading B in A using SWFLoader.

for step2 : stuck!
I am not able get hold of the instance A from which i can change source property of swfLoader (instance) to "C.swf"

for step : whatever works for step 2 :-(


--
Solution ;)

Two days after making this post I figured out a simple way to achieve this.
Code below demonstrates the my approach.

Thanks,
Vishwajit Girdhari
Flexblog : http://flexiness.blogspot.com

Main.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"

creationComplete="init(event)" >


 <mx:Script>

  <![CDATA[   

   import mx.controls.Alert;

         import flash.events.Event;

         private function init(e:Event):void
{

          //doing nothing

           }

   public function loadSWF (filename : String) : void
{       

          swfLoader.source=filename;

         }    


       ]]>

 </mx:Script> 


   <mx:SWFLoader id="swfLoader"  source="one.swf"  
></mx:SWFLoader>

</mx:Application>

 

one.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">

<mx:Script>

 <![CDATA[

  import mx.controls.Alert;

  public function handleOne ( event : Event ) :void {

      mx.core.Application.application.loadSWF("two.swf");     
 

  }

   

 ]]>

</mx:Script>

<mx:Button id="btnOne" label="Button One" click="{handleOne(event)}"
y="200"></mx:Button> 

</mx:Application>

 

two.mxml

<?xml
version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute">

<mx:Script>

 <![CDATA[

  import flash.profiler.showRedrawRegions;

  import mx.controls.Alert;

  

  public function handleTwo ( event : Event ) :void {

   mx.core.Application.application.loadSWF("one.swf");     
 

  }

 ]]>

</mx:Script><mx:Button id="btnTwo" label="Button Two"
click="handleTwo(event)" x="300"></mx:Button> 

</mx:Application>



Friday, September 08, 2006

Printing a hash map in flex

A small piece of AS code that saves a lot of my debug time and frustrations...

Mostly use while understanding the remote object structure.

//code:
//Alerting the hashmap contents

var str = "";
for(var k:String in hashMap)
{
str += k + ":" + hashMap[k] + "\n";
}
Alert.show("Printing hash map : \n" + str);