воскресенье, 7 августа 2011 г.

Presenter

Central component of MVP is presenter.
Presenter adds model data on view and handle it events.
Each presenter has view. View know nothing about presenter and model.
View is basic morph which show data on screen.

Primitive presenters change internal state of morph (view). For example, PtyTextLabelPesenter changes text contents of PtyTextMorph (view of text label presenter) to show it string model.
Composite presenters (which usually implemented in applications) add another morphs to view from child presenters. Each presenter can show other presenters on view.

To implement new presenter you should subclass PtyCompositePresenter and implement method #showViewItems. Inside this method you can add child items on view:

PtyCompositePresenter subclass: #ContactPresenter
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'PresentyExamples-ContactManager'

PtyContactPresenter>>showViewItems

    self showItem: (PtySimpleLabelPresenter on: model firstName).
    self showItem: (PtySimpleLabelPresenter on: model lastName)

This is ContactPresenter for Contact instances.
Two messages can be used to show child presenters: showItem:, showItem:on:. First message show child on own presenter view. Second message show child on concrete panel.
You can request subpanels (submorphs) from view by "view panelNamed: 'panelName'". It searchs morph with given name inside structure of view morph (recursively). If target morph absent it will be created and added to view morph.

All presenters can be created on concrete model by #on: message.

When you implement presenter you don't need to specify view for it. View of presenter is created by guide when you request guide to show presenter (method #showItems: implemented by guide). Guide search appropriate prototype for presenter and creates view based on it.
When prototype is absent default view is created. Default view is unconfigurable morph with red/yellow colors. You can design it appearance (by morphic features) and save it as prototype for presenter view (in halo menu there is "apply prototype changes" item).
Prototype is basic morph. Views are created by simple copy (#veryDeepCopy) of prototype.

Presenter can have specific prototypes for concrete context of applcation:
  • it can be specific inside different parent presenters
  • it can be specific in context of different tasks
  • it can be specific for different styles of presenter
  • it can be specific inside different parent presenter styles
Thus appearance of presenter view can be different in different parts of application. It can be configured without programming.

Each presenter can have style. You can create any presenter with style by:
  • PresenterClass on: aModel withStyle: #styleName
You can change presenter style at run time. For example, input field in Presenty change it style between  #filled and #unfilled according to filling state of field input:

PtyFieldEditorPresenter>>valueChanged
    | inputStyle |

    inputStyle := model isValid ifTrue: [#filled] ifFalse: [#unfilled].
    guide show: input withStyle: inputStyle

Last expression change style of presenter "input". It view will be replaced with new view based on new prototype.

All presenter prototypes stored in instance of PtyPrototypeManager. You can get it by:
  • guide uiBuilder prototypesManager
You can store this object to file and share between images:
  • (FileStream newFileNamed: 'yourApp.skin') fileOutClass: nil andObject: guide uiBuilder prototypesManager.
  • guide uiBuilder prototypesManager: (FileStream fileNamed: 'smallkiosk.skin') fileInObjectAndCode.

Next screencast show how implement presenters: http://www.youtube.com/watch?v=f3hk3fLBcsE&feature=player_embedded




Комментариев нет:

Отправить комментарий