Skip to content

Commit 20feda5

Browse files
author
Cliff Hall
committed
WidgetShell and Widget communicating
1 parent ef2217c commit 20feda5

File tree

16 files changed

+334
-29
lines changed

16 files changed

+334
-29
lines changed

src/Modularity.mxml

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<mx:Application
3+
xmlns:view="org.puremvc.as3.multicore.demos.flex.modularity.view.components.*"
34
xmlns:mx="http://www.adobe.com/2006/mxml"
4-
xmlns:widgetmakers="com.widgetmakers.coolwidget.view.components.*"
5-
xmlns:widgetworks="com.widgetworks.superwidget.view.components.*"
6-
layout="vertical">
5+
pageTitle="Modularity - A PureMVC AS3 MultiCore Demo"
6+
paddingTop="70" paddingBottom="0" paddingRight="0" paddingLeft="0"
7+
backgroundGradientColors="[#325EC0,#FFFFFF]"
8+
viewSourceURL="srcview/index.html"
9+
backgroundColor="#FFFFFF"
10+
themeColor="HaloBlue"
11+
layout="vertical"
12+
creationComplete="facade.startup(this)"
13+
>
14+
715
<mx:Script>
816
<![CDATA[
9-
17+
import org.puremvc.as3.multicore.demos.flex.modularity.ApplicationFacade;
18+
public static const NAME:String = 'Modularity';
19+
protected var facade:ApplicationFacade = ApplicationFacade.getInstance( NAME );
1020
]]>
1121
</mx:Script>
22+
23+
<!-- Widget Shell -->
24+
<view:WidgetShell id="widgetShell"/>
1225

13-
<mx:ApplicationControlBar dock="true">
14-
<mx:Text text="Modularity"/>
15-
<widgetmakers:CoolWidget id="w1" />
16-
<widgetworks:SuperWidget id="w2" />
17-
</mx:ApplicationControlBar>
18-
26+
<!-- Widget Canvas -->
27+
<view:WidgetCanvas id="widgetCanvas"/>
28+
1929
</mx:Application>

src/com/widgetmakers/coolwidget/ApplicationFacade.as

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.widgetmakers.coolwidget
22
{
3+
import org.puremvc.as3.multicore.interfaces.IFacade;
4+
import org.puremvc.as3.multicore.patterns.facade.Facade;
5+
36
import com.widgetmakers.coolwidget.controller.StartupCommand;
47
import com.widgetmakers.coolwidget.view.components.CoolWidget;
58

6-
import org.puremvc.as3.multicore.interfaces.IFacade;
7-
import org.puremvc.as3.multicore.patterns.facade.Facade;
89

910
public class ApplicationFacade extends Facade implements IFacade
1011
{

src/com/widgetmakers/coolwidget/controller/StartupCommand.as

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ package com.widgetmakers.coolwidget.controller
1414
{
1515
facade.registerProxy( new CoolWidgetProxy() );
1616

17-
var app:CoolWidget = note.getBody() as CoolWidget;
18-
facade.registerMediator(new CoolWidgetMediator( app ));
17+
var coolWidget:CoolWidget = note.getBody() as CoolWidget;
18+
facade.registerMediator( new CoolWidgetMediator( coolWidget ) );
1919
}
2020

2121
}

src/com/widgetmakers/coolwidget/view/CoolWidgetMediator.as

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package com.widgetmakers.coolwidget.view
66

77
import flash.events.Event;
88

9-
import mx.controls.Alert;
9+
import mx.controls.Label;
1010

1111
import org.puremvc.as3.multicore.interfaces.IMediator;
1212
import org.puremvc.as3.multicore.patterns.mediator.Mediator;
@@ -29,7 +29,10 @@ package com.widgetmakers.coolwidget.view
2929

3030
protected function onProd( event:Event ):void
3131
{
32-
mx.controls.Alert.show(coolWidgetProxy.getData().toString(),"You prodded the Cool Widget!");
32+
//mx.controls.Alert.show(coolWidgetProxy.getData().toString(),"You prodded the Cool Widget!");
33+
var label:Label = new Label();
34+
label.text = 'You prodded the Cool Widget!';
35+
coolWidget.widgetShell.addComponent( label );
3336
}
3437

3538
protected function get coolWidget():CoolWidget

src/com/widgetmakers/coolwidget/view/components/CoolWidget.mxml

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml"
33
implements="org.puremvc.as3.multicore.demos.flex.modularity.interfaces.IWidget"
4-
creationComplete="initializeWidget( )"
4+
creationComplete="initializeWidget()"
55
layout="absolute">
66

77
<mx:Metadata>
88
[Event('prod')]
99
</mx:Metadata>
1010

11-
1211
<mx:Script>
1312
<![CDATA[
13+
import org.puremvc.as3.multicore.demos.flex.modularity.interfaces.IWidget;
14+
import org.puremvc.as3.multicore.demos.flex.modularity.interfaces.IWidgetShell;
1415
import com.widgetmakers.coolwidget.ApplicationFacade;
1516
17+
// URI for this widget. Must be unique
1618
protected static const WIDGET_URI:String = "http://widgetmakers.com/CoolWidget/";
19+
20+
// Constants for the events this component dispatches.
1721
public static const PROD:String='prod';
1822
23+
// This Widget's reference to the WidgetShell
24+
public var widgetShell:IWidgetShell;
25+
26+
// Set the Widget Shell
27+
public function setWidgetShell( shell:IWidgetShell ):void
28+
{
29+
widgetShell=shell;
30+
}
31+
32+
// The unique key for this widget instance
1933
public function getWidgetKey():String
2034
{
2135
return WIDGET_URI+this.id
2236
}
2337
24-
/**
25-
* Startup the widget using the multiton key passed in.
26-
*/
38+
// Initialize the widget using the multiton key passed in.
2739
public function initializeWidget( ):void
2840
{
29-
ApplicationFacade.getInstance( getWidgetKey() ).startup(this);
41+
ApplicationFacade.getInstance( this.getWidgetKey() ).startup( this );
3042
}
3143
3244
]]>

src/com/widgetworks/superwidget/ApplicationFacade.as

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.widgetworks.superwidget
22
{
3-
import com.widgetworks.superwidget.controller.StartupCommand;
4-
import com.widgetworks.superwidget.view.components.SuperWidget;
5-
63
import org.puremvc.as3.multicore.interfaces.IFacade;
74
import org.puremvc.as3.multicore.patterns.facade.Facade;
85

6+
import com.widgetworks.superwidget.controller.StartupCommand;
7+
import com.widgetworks.superwidget.view.components.SuperWidget;
8+
99
public class ApplicationFacade extends Facade implements IFacade
1010
{
1111

src/com/widgetworks/superwidget/view/components/SuperWidget.mxml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,25 @@
1010

1111
<mx:Script>
1212
<![CDATA[
13+
import org.puremvc.as3.multicore.demos.flex.modularity.interfaces.IWidgetShell;
1314
import com.widgetworks.superwidget.ApplicationFacade;
1415
16+
// URI for this widget. Must be unique
1517
public static const WIDGET_URI:String = "http://widgetworks.com/SuperWidget/";
18+
19+
// Constants for the events this component dispatches.
1620
public static const POKE:String='poke';
1721
22+
// This Widget's reference to the WidgetShell
23+
public var widgetShell:IWidgetShell;
24+
25+
// Set the Widget Shell
26+
public function setWidgetShell( shell:IWidgetShell ):void
27+
{
28+
widgetShell=shell;
29+
}
30+
31+
// The unique key for this widget instance
1832
public function getWidgetKey():String
1933
{
2034
return WIDGET_URI+this.id
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.puremvc.as3.multicore.demos.flex.modularity
2+
{
3+
4+
import org.puremvc.as3.multicore.interfaces.IFacade;
5+
import org.puremvc.as3.multicore.patterns.facade.Facade;
6+
import org.puremvc.as3.multicore.demos.flex.modularity.controller.StartupCommand;
7+
8+
public class ApplicationFacade extends Facade implements IFacade
9+
{
10+
11+
// Notification constants
12+
public static const STARTUP:String = 'startup';
13+
public static const ADD_COMP:String = 'addComp';
14+
15+
public function ApplicationFacade( key:String )
16+
{
17+
super(key);
18+
}
19+
20+
/**
21+
* Singleton ApplicationFacade Factory Method
22+
*/
23+
public static function getInstance( key:String ) : ApplicationFacade
24+
{
25+
if ( instanceMap[ key ] == null ) instanceMap[ key ] = new ApplicationFacade( key );
26+
return instanceMap[ key ] as ApplicationFacade;
27+
}
28+
29+
/**
30+
* Register Commands with the Controller
31+
*/
32+
override protected function initializeController( ) : void
33+
{
34+
super.initializeController();
35+
registerCommand( STARTUP, StartupCommand );
36+
}
37+
38+
/**
39+
* Application startup
40+
*
41+
* @param app a reference to the application component
42+
*/
43+
public function startup( app:Modularity ):void
44+
{
45+
sendNotification( STARTUP, app );
46+
}
47+
48+
49+
}
50+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.puremvc.as3.multicore.demos.flex.modularity.controller
2+
{
3+
import org.puremvc.as3.multicore.interfaces.ICommand;
4+
import org.puremvc.as3.multicore.interfaces.INotification;
5+
import org.puremvc.as3.multicore.patterns.command.SimpleCommand;
6+
7+
import org.puremvc.as3.multicore.demos.flex.modularity.view.WidgetCanvasMediator;
8+
import org.puremvc.as3.multicore.demos.flex.modularity.view.WidgetShellMediator;
9+
10+
public class StartupCommand extends SimpleCommand implements ICommand
11+
{
12+
override public function execute(note:INotification):void
13+
{
14+
15+
16+
var app:Modularity = note.getBody() as Modularity;
17+
facade.registerMediator( new WidgetShellMediator( app.widgetShell ) );
18+
facade.registerMediator( new WidgetCanvasMediator( app.widgetCanvas ) );
19+
20+
}
21+
22+
}
23+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.puremvc.as3.multicore.demos.flex.modularity.interfaces
22
{
33
/**
4-
* Define an interface for communicating with Widgets.
4+
* Interface for a Widget.
55
* <P>
6-
* This app must define an interface by which we will
7-
* communicate with the widget. What we call 'widgets'
6+
* This is the API that must be implemented by a
7+
* Widget. What we are calling 'widgets'
88
* in this demo application are Flex Modules, but they
99
* could also be Flash based apps using the PureMVC
1010
* AS3 MultiCore framework.</P>
@@ -16,8 +16,17 @@ package org.puremvc.as3.multicore.demos.flex.modularity.interfaces
1616
* <P>
1717
* This will be a unique string. Generally created
1818
* by adding a unique URI for the widget to the
19-
* id property of the IWidget instance.</P>
19+
* id property of the IWidget instance.</P>
2020
*/
2121
function getWidgetKey( ):String;
22+
23+
/**
24+
* Set the Widget's reference to the WidgetShell.
25+
* <P>
26+
* The Widget communicates with the rest of the
27+
* application via the API exposed by the
28+
* WidgetShell.
29+
*/
30+
function setWidgetShell( shell:IWidgetShell ):void;
2231
}
2332
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.puremvc.as3.multicore.demos.flex.modularity.interfaces
2+
{
3+
import flash.display.DisplayObject;
4+
5+
/**
6+
* Interface for the WidgetShell
7+
* <P>
8+
* This is the API that the WidgetShell
9+
* exposes to Widgets for communication.
10+
* Through this interface alone the IWidget
11+
* is able to communicate its wishes to the
12+
* application into which it is loaded.</P>
13+
* <P>
14+
* Since third-parties may develop widgets
15+
* to run inside the app, we want to limit
16+
* their access to the application and its
17+
* resources.</P>
18+
*
19+
*/
20+
public interface IWidgetShell
21+
{
22+
23+
/**
24+
* A widget may add a view component to the canvas.
25+
* <P>
26+
* Though the display object reference is added to
27+
* the view hierarchy of the app, it is still managed
28+
* by a mediator running inside the widget itself.</P>
29+
*
30+
* @param displayObject to add to the canvas
31+
*/
32+
function addComponent( component:DisplayObject ):void;
33+
34+
}
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.puremvc.as3.multicore.demos.flex.modularity.view
2+
{
3+
import flash.display.DisplayObject;
4+
5+
import org.puremvc.as3.multicore.demos.flex.modularity.ApplicationFacade;
6+
import org.puremvc.as3.multicore.demos.flex.modularity.view.components.WidgetCanvas;
7+
import org.puremvc.as3.multicore.interfaces.INotification;
8+
import org.puremvc.as3.multicore.patterns.mediator.Mediator;
9+
10+
public class WidgetCanvasMediator extends Mediator
11+
{
12+
public static const NAME:String = 'WidgetCanvasMediator';
13+
14+
public function WidgetCanvasMediator( viewComponent:WidgetCanvas )
15+
{
16+
super( NAME, viewComponent );
17+
}
18+
19+
override public function listNotificationInterests():Array
20+
{
21+
return [ ApplicationFacade.ADD_COMP ];
22+
}
23+
24+
override public function handleNotification(note:INotification):void
25+
{
26+
switch ( note.getName() )
27+
{
28+
case ApplicationFacade.ADD_COMP:
29+
widgetCanvas.addChild( note.getBody() as DisplayObject );
30+
break;
31+
}
32+
}
33+
34+
protected function get widgetCanvas():WidgetCanvas
35+
{
36+
return viewComponent as WidgetCanvas;
37+
}
38+
}
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.puremvc.as3.multicore.demos.flex.modularity.view
2+
{
3+
4+
import org.puremvc.as3.multicore.patterns.mediator.Mediator;
5+
6+
import org.puremvc.as3.multicore.demos.flex.modularity.ApplicationFacade;
7+
import org.puremvc.as3.multicore.demos.flex.modularity.view.components.WidgetShell;
8+
import org.puremvc.as3.multicore.demos.flex.modularity.view.events.AddComponentEvent;
9+
10+
public class WidgetShellMediator extends Mediator
11+
{
12+
public static const NAME:String = 'WidgetShellMediator';
13+
14+
public function WidgetShellMediator( viewComponent:WidgetShell )
15+
{
16+
super( NAME, viewComponent );
17+
shell.addEventListener( AddComponentEvent.NAME, onAddComponent );
18+
}
19+
20+
protected function onAddComponent( event:AddComponentEvent ):void
21+
{
22+
sendNotification( ApplicationFacade.ADD_COMP, event.component );
23+
}
24+
25+
protected function get shell():WidgetShell
26+
{
27+
return viewComponent as WidgetShell;
28+
}
29+
30+
}
31+
}

0 commit comments

Comments
 (0)