Created
May 29, 2013 02:11
-
-
Save TrentBrown/5667518 to your computer and use it in GitHub Desktop.
QuestionDialogMediator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.filethis.main.view | |
{ | |
import com.filethis.main.controller.AnswerQuestionCommand; | |
import com.filethis.main.controller.ConnectSourceConnectionCommand; | |
import com.filethis.main.controller.DeleteSourceConnectionsCommand; | |
import com.filethis.main.model.AllQuestionsProxy; | |
import com.filethis.main.model.ApplicationProxy; | |
import com.filethis.main.view.components.AskUserToFixQuestionView; | |
import com.filethis.main.view.components.ComplexQuestionView; | |
import com.filethis.main.view.components.CredentialsQuestionView; | |
import com.filethis.main.view.components.QuestionDialogView; | |
import com.filethis.main.view.components.TextQuestionView; | |
import com.filethis.main.view.components.TwoLetterStateQuestionView; | |
import com.filethis.model.vo.SourceConnectionVO; | |
import com.filethis.model.vo.QuestionVO; | |
import flash.events.Event; | |
import flash.events.KeyboardEvent; | |
import flash.ui.Keyboard; | |
import flash.events.MouseEvent; | |
import mx.resources.ResourceManager; | |
import net.expantra.smartypants.SmartyPants; | |
import org.puremvc.as3.multicore.interfaces.IMediator; | |
import org.puremvc.as3.multicore.patterns.mediator.Mediator; | |
public class QuestionDialogMediator extends Mediator implements IMediator | |
{ | |
public static const ID:String = 'QuestionDialogMediator'; | |
public static const CLOSED:String = 'AnswerQuestionDialogMediator CLOSED'; | |
public static const CONTROL_CHANGED:String = "CONTROL_CHANGED"; | |
public function get id():String | |
{ | |
return getMediatorName(); | |
} | |
protected var connection:SourceConnectionVO = null; | |
protected var question:QuestionVO = null; | |
protected var subMediatorAnswerQuestion:QuestionInterface = null; | |
protected var subMediatorClassName:String = ""; | |
[Inject] public var applicationProxy:ApplicationProxy = null; | |
[Inject] public var allQuestionsProxy:AllQuestionsProxy = null; | |
public function QuestionDialogMediator | |
( | |
inConnection:SourceConnectionVO, | |
view:QuestionDialogView, | |
id:String = ID | |
) | |
{ | |
super(id, view); | |
SmartyPants.injectInto(this); | |
connection = inConnection; | |
question = allQuestionsProxy.findWithConnectionId(connection.id); | |
view.title = ResourceManager.getInstance().getString('common', 'SOURCE_CONNECTION_ERROR_TITLE'); | |
view.descriptionField.text = ResourceManager.getInstance().getString('common', 'SOURCE_CONNECTION_ERROR_DESCRIPTION', [connection.source.name]); | |
view.deleteConnectionButton.addEventListener(MouseEvent.CLICK, onDeleteConnectionButtonClicked); | |
view.cancelButton.addEventListener(MouseEvent.CLICK, onCancelButtonClicked); | |
view.commitButton.addEventListener(MouseEvent.CLICK, onTryAgainButtonClicked); | |
view.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown); | |
view.addEventListener(CONTROL_CHANGED, onControlChanged); | |
} | |
override public function onRegister() : void | |
{ | |
const tryAgainLabel:String = ResourceManager.getInstance().getString('common', 'TRY_AGAIN'); | |
const continueLabel:String = ResourceManager.getInstance().getString('common', 'CONTINUE'); | |
var showCancelButton:Boolean = true; | |
if (connection.state == SourceConnectionVO.ERROR_STATE) | |
useSimplePanel('SOURCE_CONNECTION_ERROR_EXPLANATION', tryAgainLabel); | |
else | |
{ | |
switch (question.type) | |
{ | |
case QuestionVO.SIMPLE_TYPE: | |
var simpleView:TextQuestionView = new TextQuestionView; | |
view.content.addElement(simpleView); | |
subMediatorAnswerQuestion = new TextQuestionMediator(connection, question, question.data, simpleView); | |
subMediatorClassName = "TextQuestionMediator"; | |
view.commitButton.label = continueLabel; | |
break; | |
case QuestionVO.COMPLEX_TYPE: | |
var complexView:ComplexQuestionView = new ComplexQuestionView; | |
view.content.addElement(complexView); | |
subMediatorAnswerQuestion = new ComplexQuestionMediator(connection, question, complexView); | |
subMediatorClassName = "ComplexQuestionMediator"; | |
view.commitButton.label = continueLabel; | |
break; | |
case QuestionVO.CREDENTIALS_TYPE: | |
var credentialsView:CredentialsQuestionView = new CredentialsQuestionView; | |
view.content.addElement(credentialsView); | |
subMediatorAnswerQuestion = new CredentialsQuestionMediator(connection, question, credentialsView); | |
subMediatorClassName = "CredentialsQuestionMediator"; | |
view.commitButton.label = tryAgainLabel; | |
break; | |
case QuestionVO.TWO_LETTER_STATE_TYPE: | |
var twoLetterStateView:TwoLetterStateQuestionView = new TwoLetterStateQuestionView; | |
view.content.addElement(twoLetterStateView); | |
subMediatorAnswerQuestion = new TwoLetterStateQuestionMediator(connection, question, twoLetterStateView); | |
subMediatorClassName = "TwoLetterStateQuestionMediator"; | |
view.commitButton.label = continueLabel; | |
break; | |
case QuestionVO.BRANCH_CODE_TYPE: | |
var branchCodeView:TextQuestionView = new TextQuestionView; | |
view.content.addElement(branchCodeView); | |
var branchCodeQuestionText:String = ResourceManager.getInstance().getString('common', 'PLEASE_ENTER_BRANCH_CODE'); | |
subMediatorAnswerQuestion = new TextQuestionMediator(connection, question, branchCodeQuestionText, branchCodeView); | |
subMediatorClassName = "TextQuestionMediator"; | |
view.commitButton.label = continueLabel; | |
break; | |
case QuestionVO.PIN_TYPE: | |
var pinView:TextQuestionView = new TextQuestionView; | |
view.content.addElement(pinView); | |
var pinQuestionText:String = ResourceManager.getInstance().getString('common', 'PLEASE_ENTER_PIN'); | |
subMediatorAnswerQuestion = new TextQuestionMediator(connection, question, pinQuestionText, pinView); | |
subMediatorClassName = "TextQuestionMediator"; | |
view.commitButton.label = continueLabel; | |
break; | |
case QuestionVO.ACCOUNT_TYPE_NOT_SUPPORTED_TYPE: | |
useSimplePanel('ACCOUNT_TYPE_UNSUPPORTED', continueLabel); | |
showCancelButton = false; | |
break; | |
case QuestionVO.NOT_PAPERLESS_TYPE: | |
useSimplePanel('NOT_PAPERLESS', tryAgainLabel); | |
break; | |
case QuestionVO.USER_IS_LOCKED_OUT_TYPE: | |
useSimplePanel('USER_IS_LOCKED_OUT', tryAgainLabel); | |
break; | |
case QuestionVO.USER_MUST_SET_UP_ACCOUNT_TYPE: | |
useSimplePanel('USER_MUST_SET_UP_ACCOUNT', tryAgainLabel); | |
break; | |
case QuestionVO.TYPE_USER_ACTION_REQUIRED: | |
useSimplePanel('USER_ACTION_REQUIRED', tryAgainLabel); | |
break; | |
case QuestionVO.GENERAL_SECURITY_PROBLEM_TYPE: | |
useSimplePanel('GENERAL_SECURITY_PROBLEM', tryAgainLabel); | |
break; | |
} | |
} | |
view.cancelButton.visible = showCancelButton; | |
// Note text | |
const hasInfo:Boolean = connection.hasInfo; | |
// view.noteGroup.visible = hasInfo; | |
// view.noteGroup.includeInLayout = hasInfo; | |
// view.noteGroupSpacer.visible = hasInfo; | |
// view.noteGroupSpacer.includeInLayout = hasInfo; | |
if (hasInfo) | |
view.note.text = "Note: " + connection.info; | |
view.note.visible = hasInfo; | |
view.note.includeInLayout = hasInfo; | |
view.callLater(subMediatorAnswerQuestion.setInitialFocus); | |
validateControls(); | |
} | |
protected function useSimplePanel(textStringId:String, buttonLabel:String) : void | |
{ | |
var askUserView:AskUserToFixQuestionView = new AskUserToFixQuestionView; | |
view.content.addElement(askUserView); | |
subMediatorAnswerQuestion = new AskUserToFixQuestionMediator(connection, askUserView, textStringId); | |
subMediatorClassName = "AskUserToFixQuestionMediator"; | |
view.commitButton.label = buttonLabel; | |
} | |
protected function onKeyDown(event:KeyboardEvent) : void | |
{ | |
if (event.keyCode == Keyboard.ESCAPE) | |
dismiss(); | |
else if (event.keyCode == Keyboard.ENTER) | |
commit(); | |
} | |
protected function onControlChanged(event:Event) : void | |
{ | |
validateControls(); | |
} | |
protected function validateControls() : void | |
{ | |
const valid:Boolean = subMediatorAnswerQuestion.validateControls(); | |
view.commitButton.enabled = valid; | |
} | |
override public function onRemove() : void | |
{ | |
facade.removeMediator(subMediatorClassName); | |
} | |
protected function onDeleteConnectionButtonClicked(event:MouseEvent) : void | |
{ | |
var connections:Array = [connection]; | |
const force:Boolean = event.altKey; | |
var body:Object = { connections:connections, force:force }; | |
sendNotification(DeleteSourceConnectionsCommand.ID, body); | |
applicationProxy.removeSourceConnectionWantingImmediateFix(connection.id); | |
} | |
protected function onCancelButtonClicked(event:MouseEvent) : void | |
{ | |
dismiss(); | |
} | |
protected function onTryAgainButtonClicked(event:MouseEvent) : void | |
{ | |
commit(); | |
} | |
protected function commit() : void | |
{ | |
switch (connection.state) | |
{ | |
case SourceConnectionVO.ERROR_STATE: | |
sendNotification(ConnectSourceConnectionCommand.ID, connection); | |
break; | |
case SourceConnectionVO.QUESTION_STATE: | |
switch (question.type) | |
{ | |
case QuestionVO.ACCOUNT_TYPE_NOT_SUPPORTED_TYPE: | |
dismiss(); | |
break; | |
default: | |
var body:Object = { connection:connection, question:question, answer:subMediatorAnswerQuestion.answer }; | |
sendNotification(AnswerQuestionCommand.ID, body); | |
break; | |
} | |
break; | |
} | |
} | |
protected function dismiss() : void | |
{ | |
applicationProxy.removeSourceConnectionWantingImmediateFix(connection.id); | |
sendNotification(CLOSED); | |
} | |
protected function get view() : QuestionDialogView | |
{ | |
return viewComponent as QuestionDialogView; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment