]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | package { |
2 | import flash.display.Sprite; | |
3 | import flash.text.TextField; | |
4 | import flash.text.TextFieldType; | |
5 | import flash.events.MouseEvent; | |
6 | import flash.system.Security; | |
7 | ||
8 | import org.apache.thrift.transport.TSocket; | |
9 | import org.apache.thrift.transport.TTransport; | |
10 | import org.apache.thrift.protocol.TProtocol; | |
11 | import org.apache.thrift.protocol.TBinaryProtocol; | |
12 | ||
13 | /** | |
14 | * Simple interface and connection logic implementation for tutorial. | |
15 | */ | |
16 | public class CalculatorUI extends Sprite { | |
17 | public static const BUTTON_PADDING:uint = 5; | |
18 | ||
19 | private var mCalculatorClient:Calculator; // we use calculator through interface | |
20 | private var mTransport:TTransport; // Transport, used to comunicate with server | |
21 | ||
22 | private var mAddButton:Sprite; | |
23 | private var mLeft:TextField; | |
24 | private var mRight:TextField; | |
25 | private var mResult:TextField; | |
26 | ||
27 | private var pingButton:Sprite; | |
28 | ||
29 | public function CalculatorUI() { | |
30 | buildInterface(); | |
31 | initSecurity(); | |
32 | initConnection(); | |
33 | } | |
34 | ||
35 | private function initSecurity():void { | |
36 | Security.loadPolicyFile("xmlsocket://127.0.0.1:9092"); | |
37 | } | |
38 | ||
39 | /** | |
40 | * Example of initializing connection. | |
41 | */ | |
42 | private function initConnection():void { | |
43 | mTransport = new TSocket("127.0.0.1", 9090); // we connect to server | |
44 | mTransport.open(); | |
45 | // initialize protocol: | |
46 | var protocol:TProtocol = new TBinaryProtocol(mTransport, false, false); | |
47 | mCalculatorClient = new CalculatorImpl(protocol); // finally, we create calculator client instance | |
48 | } | |
49 | ||
50 | private function onPingClick(me:MouseEvent):void { | |
51 | if(!mTransport.isOpen()) return; | |
52 | mCalculatorClient.ping(onPingError, onPingSuccess); | |
53 | } | |
54 | ||
55 | private function onPingError(error:Error):void { | |
56 | trace("Error, while requesting ping."); | |
57 | throw error; | |
58 | } | |
59 | ||
60 | private function onPingSuccess():void { | |
61 | trace("Ping returned successfully"); | |
62 | } | |
63 | ||
64 | private function onAddClick(me:MouseEvent):void { | |
65 | if(!mTransport.isOpen()) return; | |
66 | var num1:Number = Number(mLeft.text); | |
67 | var num2:Number = Number(mRight.text); | |
68 | mResult.text = "Processing..."; | |
69 | mCalculatorClient.add(num1, num2, onAddError, onAddSuccess); | |
70 | } | |
71 | ||
72 | private function onAddError(error:Error):void { | |
73 | trace("Error, while requesting add."); | |
74 | throw error; | |
75 | } | |
76 | ||
77 | private function onAddSuccess(res:Number):void { | |
78 | mResult.text = String(res); | |
79 | } | |
80 | ||
81 | private function buildInterface():void { | |
82 | addChild(pingButton = buildButton("PING")); | |
83 | pingButton.x = (stage.stageWidth - pingButton.width) / 2; | |
84 | pingButton.y = 10; | |
85 | pingButton.addEventListener(MouseEvent.CLICK, onPingClick); | |
86 | ||
87 | var top:Number = pingButton.y + pingButton.height + 20; | |
88 | addChild(mLeft = buildDigitInput()); | |
89 | mLeft.x = 15; | |
90 | mLeft.y = top + BUTTON_PADDING; | |
91 | addChild(mRight = buildDigitInput()); | |
92 | mRight.x = mLeft.x + mLeft.width + 15; | |
93 | mRight.y = top + BUTTON_PADDING; | |
94 | addChild(mAddButton = buildButton("ADD")); | |
95 | mAddButton.x = mRight.x + mRight.width + 15; | |
96 | mAddButton.y = top; | |
97 | mAddButton.addEventListener(MouseEvent.CLICK, onAddClick); | |
98 | addChild(mResult = buildDigitInput()); | |
99 | mResult.x = mAddButton.x + mAddButton.width + 15; | |
100 | mResult.y = top + BUTTON_PADDING; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Simple digit-only input field. | |
105 | */ | |
106 | private function buildDigitInput():TextField { | |
107 | var textField:TextField = new TextField; | |
108 | textField.width = 75; | |
109 | textField.height = 20; | |
110 | textField.restrict = "0987654321."; | |
111 | textField.type = TextFieldType.INPUT; | |
112 | textField.background = true; | |
113 | textField.backgroundColor = 0xaaaaff; | |
114 | textField.textColor = 0xffff00; | |
115 | return textField; | |
116 | } | |
117 | ||
118 | /** | |
119 | * Simple button drawing. | |
120 | */ | |
121 | private function buildButton(text:String):Sprite { | |
122 | var button:Sprite = new Sprite; | |
123 | var textField:TextField = new TextField; | |
124 | textField.width = 4000; | |
125 | textField.text = text; | |
126 | textField.textColor = 0xffff00; | |
127 | textField.width = textField.textWidth + 4; | |
128 | textField.height = textField.textHeight + 4; | |
129 | textField.mouseEnabled = false; | |
130 | button.graphics.beginFill(0x0000ff); | |
131 | button.graphics.lineStyle(0, 0x000000); | |
132 | button.graphics.drawRoundRect(0, 0, textField.width + BUTTON_PADDING * 2, | |
133 | textField.height + BUTTON_PADDING * 2, BUTTON_PADDING); | |
134 | button.graphics.endFill(); | |
135 | button.addChild(textField); | |
136 | textField.x = BUTTON_PADDING; | |
137 | textField.y = BUTTON_PADDING; | |
138 | button.useHandCursor = button.buttonMode = true; | |
139 | return button; | |
140 | } | |
141 | } | |
142 | } |