<s> """<STR_LIT>""" <EOL> from pyjamas import DOM <EOL> from pyjamas import Factory <EOL> from pyjamas . ui import Event <EOL> from ButtonBase import ButtonBase <EOL> from pyjamas . ui import Focus <EOL> from UIObject import UIObject <EOL> class Face : <EOL> STYLENAME_HTML_FACE = "<STR_LIT>" <EOL> def __init__ ( self , button , delegateTo = None ) : <EOL> """<STR_LIT>""" <EOL> self . button = button <EOL> self . delegateTo = delegateTo <EOL> self . face = None <EOL> self . id = None <EOL> self . name = "<STR_LIT>" <EOL> def getName ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . name <EOL> def getFaceID ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . id <EOL> def setName ( self , name ) : <EOL> """<STR_LIT>""" <EOL> self . name = name <EOL> return <EOL> def setFaceID ( self , face_id ) : <EOL> """<STR_LIT>""" <EOL> self . id = face_id <EOL> return <EOL> def getHTML ( self ) : <EOL> """<STR_LIT>""" <EOL> return DOM . getInnerHTML ( self . getFace ( ) ) <EOL> def getText ( self ) : <EOL> """<STR_LIT>""" <EOL> return DOM . getInnerText ( self . getFace ( ) ) <EOL> def setHTML ( self , html ) : <EOL> """<STR_LIT>""" <EOL> self . face = DOM . createDiv ( ) <EOL> UIObject . setStyleName ( self . button , self . face , self . STYLENAME_HTML_FACE , <EOL> True ) <EOL> DOM . setInnerHTML ( self . face , html ) <EOL> self . button . updateButtonFace ( ) <EOL> def setImage ( self , image ) : <EOL> """<STR_LIT>""" <EOL> self . face = image . getElement ( ) <EOL> self . button . updateButtonFace ( ) <EOL> def setText ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . face = DOM . createDiv ( ) <EOL> UIObject . setStyleName ( self . button , self . face , self . STYLENAME_HTML_FACE , <EOL> True ) <EOL> DOM . setInnerText ( self . face , text ) <EOL> self . button . updateButtonFace ( ) <EOL> def toString ( self ) : <EOL> return self . getName ( ) <EOL> def getFace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . face is not None : <EOL> return self . face <EOL> if self . delegateTo is not None : <EOL> return self . delegateTo . getFace ( ) <EOL> self . face = DOM . createDiv ( ) <EOL> return self . face <EOL> class CustomButton ( ButtonBase ) : <EOL> """<STR_LIT>""" <EOL> STYLENAME_DEFAULT = "<STR_LIT>" <EOL> DOWN_ATTRIBUTE = <NUM_LIT:1> <EOL> HOVERING_ATTRIBUTE = <NUM_LIT:2> <EOL> DISABLED_ATTRIBUTE = <NUM_LIT:4> <EOL> UP = <NUM_LIT:0> <EOL> DOWN = DOWN_ATTRIBUTE <EOL> UP_HOVERING = HOVERING_ATTRIBUTE <EOL> DOWN_HOVERING = DOWN_ATTRIBUTE | HOVERING_ATTRIBUTE <EOL> UP_DISABLED = DISABLED_ATTRIBUTE <EOL> DOWN_DISABLED = DOWN | DISABLED_ATTRIBUTE <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , upImageText = None , downImageText = None , listener = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not kwargs . has_key ( '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = self . STYLENAME_DEFAULT <EOL> if kwargs . has_key ( '<STR_LIT>' ) : <EOL> element = kwargs . pop ( '<STR_LIT>' ) <EOL> else : <EOL> element = Focus . createFocusable ( ) <EOL> ButtonBase . __init__ ( self , element , ** kwargs ) <EOL> self . curFace = None <EOL> self . curFaceElement = None <EOL> self . up = None <EOL> self . down = None <EOL> self . downHovering = None <EOL> self . upHovering = None <EOL> self . upDisabled = None <EOL> self . downDisabled = None <EOL> self . isCapturing = False <EOL> self . isFocusing = False <EOL> self . allowClick = False <EOL> self . setUpFace ( self . createFace ( None , "<STR_LIT>" , self . UP ) ) <EOL> if downImageText is None and listener is None : <EOL> listener = upImageText <EOL> upImageText = None <EOL> if upImageText and isinstance ( upImageText , basestring ) : <EOL> upText = upImageText <EOL> upImage = None <EOL> else : <EOL> upImage = upImageText <EOL> upText = None <EOL> if downImageText and isinstance ( downImageText , basestring ) : <EOL> downText = downImageText <EOL> downImage = None <EOL> else : <EOL> downImage = downImageText <EOL> downText = None <EOL> if upImage is not None : <EOL> self . getUpFace ( ) . setImage ( upImage ) <EOL> if upText is not None : <EOL> self . getUpFace ( ) . setText ( upText ) <EOL> if downImage is not None : <EOL> self . getDownFace ( ) . setImage ( downImage ) <EOL> if downText is not None : <EOL> self . getDownFace ( ) . setText ( downText ) <EOL> self . sinkEvents ( Event . ONCLICK | Event . MOUSEEVENTS | Event . FOCUSEVENTS <EOL> | Event . KEYEVENTS ) <EOL> if listener is not None : <EOL> self . addClickListener ( listener ) <EOL> def updateButtonFace ( self ) : <EOL> if self . curFace is not None and self . curFace . getFace ( ) == self . getFace ( ) : <EOL> self . setCurrentFaceElement ( self . face ) <EOL> def getDownDisabledFace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . downDisabled is None : <EOL> self . setDownDisabledFace ( self . createFace ( self . getDownFace ( ) , <EOL> "<STR_LIT>" , self . DOWN_DISABLED ) ) <EOL> return self . downDisabled <EOL> def getDownFace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . down is None : <EOL> self . setDownFace ( self . createFace ( self . getUpFace ( ) , <EOL> "<STR_LIT>" , self . DOWN ) ) <EOL> return self . down <EOL> def getDownHoveringFace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . downHovering is None : <EOL> self . setDownHoveringFace ( self . createFace ( self . getDownFace ( ) , <EOL> "<STR_LIT>" , self . DOWN_HOVERING ) ) <EOL> return self . downHovering <EOL> def getHTML ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getCurrentFace ( ) . getHTML ( ) <EOL> def getTabIndex ( self ) : <EOL> return Focus . getTabIndex ( self . getElement ( ) ) <EOL> def getText ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getCurrentFace ( ) . getText ( ) <EOL> def getUpDisabledFace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . upDisabled is None : <EOL> self . setUpDisabledFace ( self . createFace ( self . getUpFace ( ) , <EOL> "<STR_LIT>" , self . UP_DISABLED ) ) <EOL> return self . upDisabled <EOL> def getUpFace ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . up <EOL> def getUpHoveringFace ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . upHovering is None : <EOL> self . setUpHoveringFace ( self . createFace ( self . getUpFace ( ) , <EOL> "<STR_LIT>" , self . UP_HOVERING ) ) <EOL> return self . upHovering <EOL> def onBrowserEvent ( self , event ) : <EOL> if not self . isEnabled ( ) : <EOL> return <EOL> event_type = DOM . eventGetType ( event ) <EOL> if event_type == "<STR_LIT>" : <EOL> if not self . allowClick : <EOL> DOM . eventStopPropagation ( event ) <EOL> return <EOL> elif event_type == "<STR_LIT>" : <EOL> if DOM . eventGetButton ( event ) == Event . BUTTON_LEFT : <EOL> self . setFocus ( True ) <EOL> self . onClickStart ( ) <EOL> DOM . setCapture ( self . getElement ( ) ) <EOL> self . isCapturing = True <EOL> DOM . eventPreventDefault ( event ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if self . isCapturing : <EOL> self . isCapturing = False <EOL> DOM . releaseCapture ( self . getElement ( ) ) <EOL> if self . isHovering ( ) and DOM . eventGetButton ( event ) == Event . BUTTON_LEFT : <EOL> self . onClick ( ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if self . isCapturing : <EOL> DOM . eventPreventDefault ( event ) <EOL> elif event_type == "<STR_LIT>" : <EOL> to = DOM . eventGetToElement ( event ) <EOL> if ( DOM . isOrHasChild ( self . getElement ( ) , DOM . eventGetTarget ( event ) ) <EOL> and ( to is None or not DOM . isOrHasChild ( self . getElement ( ) , to ) ) ) : <EOL> if self . isCapturing : <EOL> self . onClickCancel ( ) <EOL> self . setHovering ( False ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if DOM . isOrHasChild ( self . getElement ( ) , DOM . eventGetTarget ( event ) ) : <EOL> self . setHovering ( True ) <EOL> if self . isCapturing : <EOL> self . onClickStart ( ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if self . isFocusing : <EOL> self . isFocusing = False <EOL> self . onClickCancel ( ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if self . isCapturing : <EOL> self . isCapturing = False <EOL> self . onClickCancel ( ) <EOL> ButtonBase . onBrowserEvent ( self , event ) <EOL> if ( DOM . eventGetTypeInt ( event ) & Event . KEYEVENTS ) == <NUM_LIT:0> : <EOL> return <EOL> keyCode = DOM . eventGetKeyCode ( event ) <EOL> if event_type == "<STR_LIT>" : <EOL> if keyCode == '<STR_LIT:U+0020>' : <EOL> self . isFocusing = True <EOL> self . onClickStart ( ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if self . isFocusing and keyCode == '<STR_LIT:U+0020>' : <EOL> self . isFocusing = False <EOL> self . onClick ( ) <EOL> elif event_type == "<STR_LIT>" : <EOL> if keyCode == '<STR_LIT:\n>' or keyCode == '<STR_LIT:\r>' : <EOL> self . onClickStart ( ) <EOL> self . onClick ( ) <EOL> def setAccessKey ( self , key ) : <EOL> pass <EOL> def setEnabled ( self , enabled ) : <EOL> """<STR_LIT>""" <EOL> if self . isEnabled ( ) == enabled : <EOL> return <EOL> self . toggleDisabled ( ) <EOL> ButtonBase . setEnabled ( self , enabled ) <EOL> if enabled : <EOL> self . setAriaPressed ( self . getCurrentFace ( ) ) <EOL> else : <EOL> self . cleanupCaptureState ( ) <EOL> def setFocus ( self , focused ) : <EOL> if focused : <EOL> Focus . focus ( self . getElement ( ) ) <EOL> else : <EOL> Focus . blur ( self . getElement ( ) ) <EOL> def setHTML ( self , html ) : <EOL> """<STR_LIT>""" <EOL> self . getCurrentFace ( ) . setHTML ( html ) <EOL> def setTabIndex ( self , index ) : <EOL> Focus . setTabIndex ( self . getElement ( ) , index ) <EOL> def setText ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . getCurrentFace ( ) . setText ( text ) <EOL> def isDown ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . DOWN_ATTRIBUTE & self . getCurrentFace ( ) . getFaceID ( ) ) > <NUM_LIT:0> <EOL> def onAttach ( self ) : <EOL> """<STR_LIT>""" <EOL> self . finishSetup ( ) <EOL> ButtonBase . onAttach ( self ) <EOL> def onClick ( self , sender = None ) : <EOL> """<STR_LIT>""" <EOL> self . allowClick = True <EOL> evt = None <EOL> DOM . buttonClick ( self . getElement ( ) ) <EOL> self . allowClick = False <EOL> def onClickCancel ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def onClickStart ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def onDetach ( self ) : <EOL> ButtonBase . onDetach ( self ) <EOL> self . cleanupCaptureState ( ) <EOL> def setDown ( self , down ) : <EOL> """<STR_LIT>""" <EOL> if down != self . isDown ( ) : <EOL> self . toggleDown ( ) <EOL> def finishSetup ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . curFace is None : <EOL> self . setCurrentFace ( self . getUpFace ( ) ) <EOL> def fireClickListeners ( self , nativeEvent ) : <EOL> self . fireEvent ( ClickEvent ( ) ) <EOL> def fireEvent ( self ) : <EOL> pass <EOL> def getCurrentFace ( self ) : <EOL> """<STR_LIT>""" <EOL> self . finishSetup ( ) <EOL> return self . curFace <EOL> def isHovering ( self ) : <EOL> """<STR_LIT>""" <EOL> return ( self . HOVERING_ATTRIBUTE & self . getCurrentFace ( ) . getFaceID ( ) ) > <NUM_LIT:0> <EOL> def setHovering ( self , hovering ) : <EOL> """<STR_LIT>""" <EOL> if hovering != self . isHovering ( ) : <EOL> self . toggleHover ( ) <EOL> def toggleDown ( self ) : <EOL> """<STR_LIT>""" <EOL> newFaceID = self . getCurrentFace ( ) . getFaceID ( ) ^ self . DOWN_ATTRIBUTE <EOL> self . setCurrentFaceFromID ( newFaceID ) <EOL> def cleanupCaptureState ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . isCapturing and not self . isFocusing : <EOL> return <EOL> DOM . releaseCapture ( self . getElement ( ) ) <EOL> self . isCapturing = False <EOL> self . isFocusing = False <EOL> self . onClickCancel ( ) <EOL> def createFace ( self , delegateTo , name , faceID ) : <EOL> face = Face ( self , delegateTo ) <EOL> face . setName ( name ) <EOL> face . setFaceID ( faceID ) <EOL> return face <EOL> def getFaceFromID ( self , face_id ) : <EOL> if ( face_id == self . DOWN ) : <EOL> return self . getDownFace ( ) <EOL> elif ( face_id == self . UP ) : <EOL> return self . getUpFace ( ) <EOL> elif ( face_id == self . DOWN_HOVERING ) : <EOL> return self . getDownHoveringFace ( ) <EOL> elif ( face_id == self . UP_HOVERING ) : <EOL> return self . getUpHoveringFace ( ) <EOL> elif ( face_id == self . UP_DISABLED ) : <EOL> return self . getUpDisabledFace ( ) <EOL> elif ( face_id == self . DOWN_DISABLED ) : <EOL> return self . getDownDisabledFace ( ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % str ( face_id ) ) <EOL> def setAriaPressed ( self , newFace ) : <EOL> pressed = ( newFace . getFaceID ( ) & self . DOWN_ATTRIBUTE ) == <NUM_LIT:1> <EOL> def setCurrentFace ( self , newFace ) : <EOL> """<STR_LIT>""" <EOL> if self . curFace == newFace : <EOL> return <EOL> if self . curFace is not None : <EOL> self . removeStyleDependentName ( self . curFace . getName ( ) ) <EOL> self . curFace = newFace <EOL> self . setCurrentFaceElement ( newFace . getFace ( ) ) ; <EOL> self . addStyleDependentName ( self . curFace . getName ( ) ) <EOL> if self . isEnabled : <EOL> self . setAriaPressed ( newFace ) <EOL> self . style_name = self . getStyleName ( ) <EOL> def setCurrentFaceFromID ( self , faceID ) : <EOL> """<STR_LIT>""" <EOL> newFace = self . getFaceFromID ( faceID ) <EOL> self . setCurrentFace ( newFace ) <EOL> def setCurrentFaceElement ( self , newFaceElement ) : <EOL> if self . curFaceElement == newFaceElement : <EOL> return <EOL> if self . curFaceElement is not None : <EOL> DOM . removeChild ( self . getElement ( ) , self . curFaceElement ) <EOL> self . curFaceElement = newFaceElement <EOL> DOM . appendChild ( self . getElement ( ) , self . curFaceElement ) <EOL> def setDownDisabledFace ( self , downDisabled ) : <EOL> """<STR_LIT>""" <EOL> self . downDisabled = downDisabled <EOL> def setDownFace ( self , down ) : <EOL> """<STR_LIT>""" <EOL> self . down = down <EOL> def setDownHoveringFace ( self , downHovering ) : <EOL> """<STR_LIT>""" <EOL> self . downHovering = downHovering <EOL> def setUpDisabledFace ( self , upDisabled ) : <EOL> """<STR_LIT>""" <EOL> self . upDisabled = upDisabled <EOL> def setUpFace ( self , up ) : <EOL> """<STR_LIT>""" <EOL> self . up = up <EOL> def setUpHoveringFace ( self , upHovering ) : <EOL> """<STR_LIT>""" <EOL> self . upHovering = upHovering <EOL> def toggleDisabled ( self ) : <EOL> """<STR_LIT>""" <EOL> newFaceID = self . getCurrentFace ( ) . getFaceID ( ) ^ self . DISABLED_ATTRIBUTE <EOL> newFaceID &= ~ self . HOVERING_ATTRIBUTE <EOL> self . setCurrentFaceFromID ( newFaceID ) <EOL> def toggleHover ( self ) : <EOL> """<STR_LIT>""" <EOL> newFaceID = self . getCurrentFace ( ) . getFaceID ( ) ^ self . HOVERING_ATTRIBUTE <EOL> newFaceID &= ~ self . DISABLED_ATTRIBUTE <EOL> self . setCurrentFaceFromID ( newFaceID ) <EOL> Factory . registerClass ( '<STR_LIT>' , '<STR_LIT>' , CustomButton ) </s>
