/*----------------------------------------------------------------------------- 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/ 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.ASWingUtils; import org.aswing.border.EmptyBorder; import org.aswing.BorderLayout; import org.aswing.FlowLayout; import org.aswing.geom.Point; import org.aswing.Insets; import org.aswing.JButton; import org.aswing.JFrame; import org.aswing.JLabel; import org.aswing.JPanel; import com.bourre.commands.Delegate; import com.bourre.events.EventType; import fever.display.ModalScreen; import fever.Fever; import fever.utils.StringUtil; import fvaswing.components.FvAlertResources; import fvaswing.components.FvButton; import fvaswing.components.input.FvInputComponent; import fvaswing.components.input.FvTextFieldInput; /** * Opens a dialog with a custom component as input controller. * *
Example using a {@link fvaswing.components.input.FvSliderInput} component.
* Use {@link FvInputComponentDialog} singleton design.
* {@code
* var input : FvSliderInput = new FvSliderInput( true ); // rounded bounded range enabled.
* input.setMinimum( 1900 );
* input.setMaximum( 2100 );
* input.setValue( ( new Date() ).getFullYear() );
*
* FvInputComponentDialog.show( input, "Choose your year", this, _onInputResult );
* }
*
*
or with classic instanciation : * {@code * var input : FvSliderInput = new FvSliderInput( true ); // rounded bounded range enabled. * input.setMinimum( 1900 ); * input.setMaximum( 2100 ); * input.setValue( ( new Date() ).getFullYear() ); * * var dialog : FvInputComponentDialog = new FvInputComponentDialog( input ); * dialog.open( "Choose your year", this, _onInputResult ); * } * * @author Romain Ecarnot */ class fvaswing.components.FvInputComponentDialog { //------------------------------------------------------------------------- // Events definition //------------------------------------------------------------------------- /** Event type broadcasted when user click on submit button. */ public static var onInputResultEVENT : EventType = new EventType( "onInputResult" ); //------------------------------------------------------------------------- // Private properties //------------------------------------------------------------------------- private static var _instance : FvInputComponentDialog; private var _frame : JFrame; private var _containerPanel : JPanel; private var _label : JLabel; private var _input : FvInputComponent; private var _submitButton : FvButton; private var _cancelButton : FvButton; private var _modal : ModalScreen; private var _listener; //------------------------------------------------------------------------- // Public Properties //------------------------------------------------------------------------- /** Width of each button, in pixels. */ public static var buttonWidth : Number = 60; //------------------------------------------------------------------------- // Public API //------------------------------------------------------------------------- /** * Uses always same {@link FvInputComponentDialog} instance swapping input component * with passed-in {@code input} one. * * @param input {@link fvaswing.components.input.FvInputComponent} component * @param label Text label do display * @param listener Listener or callback context * @param ( optional ) callback function */ public static function show( input : FvInputComponent, label : String, listener, f : Function ) : FvInputComponentDialog { var o : FvInputComponentDialog = _getInstance(); o._swapInputComponent( input ); o.open.apply( o, arguments.slice( 1 ) ); return o; } /** * Constructor. */ public function FvInputComponentDialog( input : FvInputComponent ) { _input = ( input ) ? input : new FvTextFieldInput(); _configureUI(); } /** * Returns current input component. */ public function getInput() : FvInputComponent { return _input; } /** * Opens the input dialog. * *
Various way to open an input dialog : *
If omitted, {@code listener} must have a {@link #onInputResultEVENT} * event handler. */ public function open( label : String, listener, f : Function ) : Void { _listener = (f) ? _getEventProxy.apply( this, arguments.splice(1) ) : listener; _label.setText( label ); _update(); _modal.show(); _frame.show(); } //------------------------------------------------------------------------- // Private implementation //------------------------------------------------------------------------- private static function _getInstance() : FvInputComponentDialog { if( !_instance ) _instance = new FvInputComponentDialog(); return _instance; } private function _getModalTarget() : MovieClip { _modal.dispose(); _modal = new ModalScreen( 0x000000, 0 ); return _modal.getModalTarget(); } private function _configureUI() : Void { _frame = new JFrame( _getModalTarget(), "", false ); _frame.setResizable( false ); _frame.setClosable( false ); _frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE ); _frame.setTitle( Fever.application.getTitle() ); _frame.setCachePreferSizes( false ); _frame.getContentPane().setBorder( new EmptyBorder( null, new Insets( 5, 5, 5, 5 ) ) ); _frame.getContentPane().setLayout( new BorderLayout( 5, 5 ) ); _frame.getContentPane().append( _getControlPanel(), BorderLayout.NORTH ); _frame.getContentPane().append( _getButtonPanel(), BorderLayout.SOUTH ); } private function _getControlPanel() : JPanel { _label = new JLabel( '', JLabel.LEFT ); _containerPanel = new JPanel( new BorderLayout( 5, 5 ) ); _containerPanel.append( _label, BorderLayout.NORTH ); _containerPanel.append( _input.getContainer(), BorderLayout.CENTER ); return _containerPanel; } private function _getButtonPanel() : JPanel { _submitButton = new FvButton( '' ); _submitButton.setPreferredSize( buttonWidth, 22 ); _submitButton.addActionListener( _checkResult, this ); _cancelButton = new FvButton( '' ); _cancelButton.setPreferredSize( buttonWidth, 22 ); _cancelButton.addActionListener( _handleClose, this ); _updateLocalisation(); var p : JPanel = new JPanel( new FlowLayout ( FlowLayout.CENTER, 10, 2 ) ); p.append( _submitButton ); p.append( _cancelButton ); return p; } private function _swapInputComponent( input : FvInputComponent ) : Void { _containerPanel.removeAll(); delete _input; _input = input; _containerPanel.append( _label, BorderLayout.NORTH ); _containerPanel.append( _input.getContainer(), BorderLayout.CENTER ); _containerPanel.revalidate(); } private function _updateLocalisation() : Void { _submitButton.setText( StringUtil.remove( FvAlertResources.getInstance().okLabel, '&' ) ); _cancelButton.setText( StringUtil.remove( FvAlertResources.getInstance().cancelLabel, '&' ) ); } private function _update() : Void { _updateLocalisation(); _frame.revalidateIfNecessary(); _frame.pack(); var location : Point = ASWingUtils.getScreenCenterPosition(); location.x -= _frame.getWidth()/2; location.y -= _frame.getHeight()/2; _frame.setLocation( location ); } private function _checkResult( source : JButton ) : Void { if( _input.isValid() ) _handleResult(); } private function _handleResult( ) : Void { var result = _input.getValue(); var type : String = typeof( _listener ); if ( type == "object" || type == "movieclip") _listener[ String( onInputResultEVENT ) ]( result ); else _listener.apply( this, [ result ] ); _handleClose(); } private function _handleClose( ) : Void { _listener = null; _modal.hide(); _frame.tryToClose(); } private function _getEventProxy( listener, f : Function ) : Function { return Delegate.create.apply( Delegate, [ listener, f ].concat( arguments.splice( 2 ) ) ); } }