/*----------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Fever Framework code. The Initial Developer of the Original Code is Romain Ecarnot. Portions created by Initial Developer are Copyright (C) 2006 the Initial Developer. All Rights Reserved. Contributor(s): Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -----------------------------------------------------------------------------*/ import org.aswing.JTextArea; import org.aswing.UIManager; import com.bourre.commands.Delegate; import com.bourre.events.EventBroadcaster; import com.bourre.events.EventType; import fever.events.AsFunctionEvent; import fever.utils.Stringifier; /** * FvTextArea component extends AsWing JTextArea * adding Html AsFunction managment. * *

Asfunction call are supported like Event API. * *

Example * var txt : String = "

" + * "call 1 " + * "AND " + * "call 2" + * "

"; * * var label : FvTextArea = new FvTextArea( txt ); * * //Registers default asfunction 'call' method. * label.addAsFunctionListener( FvTextArea.onAsFunctionEVENT, Delegate.create( this, _onClicked ) ); * * //Registers custom 'onMyEvent' asfunction call. * label.addAsFunctionListener( 'onMyEvent', this, _onMyFunction ); * * * * private function _onClicked( event : AsFunctionEvent ) : Void * { * FeverDebug.INFO( event.getType() + ' with ' + event.getArguments() ); * } * * @see fever.events.AsFunctionEvent * * @author Romain Ecarnot */ class fvaswing.components.FvTextArea extends JTextArea { //------------------------------------------------------------------------- // Events definition //------------------------------------------------------------------------- /** Broadcasted when a asfunction is called. */ public static var onAsFunctionEVENT : EventType = AsFunctionEvent.onAsFunctionEVENT; //------------------------------------------------------------------------- // Private properties //------------------------------------------------------------------------- private var _bIsLabelModeEnabled : Boolean; private var _oEB : EventBroadcaster; private var _aBuffer:Array; //------------------------------------------------------------------------- // Public API //------------------------------------------------------------------------- /** * FvTextArea( text : String, rows : Number, columns : Number)
* FvTextArea( text : String, rows : Number) columns default to 0
* FvTextArea( text : String) rows and columns default to 0
* FvTextArea() text default to 0, rows and columns default to 0
*/ public function FvTextArea( text : String, rows : Number, columns : Number ) { super( text, rows, columns ); _aBuffer = new Array(); _oEB = new EventBroadcaster( this ); _bIsLabelModeEnabled = false; addEventListener( JTextArea.ON_CREATED, _initialize, this ); } /** * Transforms text area into a simple label zone if passed-in * b is true * *

No border, no background and no focus. */ public function setLabelModeEnabled( b : Boolean ) : Void { if( b == _bIsLabelModeEnabled ) return; if( b ) { setBorder( null ); setEnabled( false ); setFocusable( false ); setOpaque( false ); setEditable( false ); } else { setBorder( UIManager.getBorder( 'TextArea.border' ) ); setEnabled( true ); setFocusable( true ); setOpaque( true ); setEditable( true ); } _bIsLabelModeEnabled = b; } /** * Returns true if area is in label mode. * * @return Boolean */ public function isLabelModeEnabled( ) : Boolean { return _bIsLabelModeEnabled; } /** * Adds passed-in {@code listener} listener for receiving * passed-in {@code asFunctionName} asfunction call. * * @param asFunctionName Name of the called asfunction. * @param listener Listener object. */ public function addAsFunctionListener( asFunctionName : String, listener ) : Void { if( getTextField() )_registerCustomAsFunction.apply( this, arguments ); else _aBuffer.push( arguments ); } /** * Removes passed-in {@code listener} listener that suscribed * passed-in {@code asFunctionName} asfunction call. * * @param asFunctionName Name of the called asfunction. * @param listener Listener object. */ public function removeAsFunctionListener( asFunctionName, listener ) : Void { _oEB.removeEventListener( asFunctionName, listener ); if( !_oEB.listenerArrayExists( asFunctionName ) ) { delete textField._parent[ asFunctionName ]; } } /** * Returns string representation. */ public function toString() : String { return Stringifier.parse( this ); } //------------------------------------------------------------------------- // Private implementation //------------------------------------------------------------------------- private function _initialize( target : FvTextArea ) : Void { var l : Number = _aBuffer.length; for ( var i : Number = 0; i < l; i++ ) _registerCustomAsFunction.apply( this, _aBuffer[i] ); _aBuffer = new Array(); } private function _registerCustomAsFunction( asFunctionName : String ) : Void { _oEB.addEventListener.apply( _oEB, arguments ); if( !textField._parent[ asFunctionName ] ) { textField._parent[ asFunctionName ] = Delegate.create( this, _disptachCustomEvent, asFunctionName ); } } private function _disptachCustomEvent( ) : Void { var eventName : String = String( arguments.pop() ); var e : AsFunctionEvent = new AsFunctionEvent( eventName, this, arguments ); _oEB.broadcastEvent( e ); } }