/*----------------------------------------------------------------------------- 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.geom.Point; import org.aswing.Insets; import org.aswing.JComboBox; import org.aswing.JFrame; import org.aswing.JMenu; import org.aswing.JMenuBar; import org.aswing.JMenuItem; import com.bourre.commands.Delegate; import com.bourre.core.IAccessor; import fever.app.accelerator.ShortcutLocator; import fever.app.accelerator.ShortcutMap; import fever.app.local.Localisation; import fever.app.local.LocalisationEvent; import fever.app.local.LocalisationListener; import fvaswing.components.calculator.FvCalculatorResources; import fvaswing.components.calculator.FvCalculatorUI; import fvaswing.FvAsWing; /** * A simple calculator component ( JFrame ). * * @see fvaswing.components.calculator.FvCalculatorUI * * @author Romain Ecarnot */ class fvaswing.components.FvCalculator implements LocalisationListener { //------------------------------------------------------------------------- // Private properties //------------------------------------------------------------------------- private static var _instance : FvCalculator; private static var _smid : String = 'FvCalculatorSM'; private var _frame : JFrame; private var _bar : JMenuBar; private var _editMenu : JMenu; private var _copyItem : JMenuItem; private var _toggleItem : JMenuItem; private var _ui : FvCalculatorUI; private var _accessor : IAccessor; //------------------------------------------------------------------------- // Public API //------------------------------------------------------------------------- /** Read only. Shortcut map ID for calculator */ public static function get SHORTCUT_MAP() : String { return _smid; } /** * Returns {@link FvCalculator} instance. */ public static function getInstance() : FvCalculator { if( !_instance ) _instance = new FvCalculator(); return _instance; } /** * Opens dialog. */ public function open() : Void { _frame.show(); _frame.setActive( true ); } /** * Connects Pixlib Accessor to calculator result. * *
When calculator is closed, result is automatically sent to accessor instance. * *
Example * {@code * var input : JTextField = new JTextField( "", 15 ); * calculator.connect( AccessorFactory.getInstance( input, input.setText, input.getText ) ); * calculator.open(); * } * * @param accessor IAccessor instance ( take a look at Pixlib bourre.core package ) */ public function connect( accessor : IAccessor ) : Void { _accessor = accessor; } /** * Releases accessor connection. */ public function disconnect() : Void { _accessor = null; } /** * Triggered when Localisation language change. */ public function onLocalisationUpdate( event : LocalisationEvent ) : Void { _frame.setTitle( FvCalculatorResources.getInstance().title ); _editMenu.setText( FvCalculatorResources.getInstance().menu ); _copyItem.setText( FvCalculatorResources.getInstance().copy ); _toggleItemLabel(); } //------------------------------------------------------------------------- // Private implementation //------------------------------------------------------------------------- /** * Constructor. */ private function FvCalculator() { _configureUI(); Localisation.addLocalisationListener( this ); } /** * Prepares dialog. */ private function _configureUI() : Void { _configureBasedFrame(); _ui = new FvCalculatorUI(); _ui.addEventListener( JComboBox.ON_CREATED, _frame.pack, _frame ); _frame.getContentPane().setBorder( new EmptyBorder( null, new Insets( 5,5,5,5 ) ) ); _frame.getContentPane().append( _createMenubar(), BorderLayout.NORTH ); _frame.getContentPane().append ( _ui, BorderLayout.CENTER ); _frame.addEventListener( JFrame.ON_WINDOW_ACTIVATED, Delegate.create( this, _toggleShortcutMap, true ) ); _frame.addEventListener( JFrame.ON_WINDOW_DEACTIVATED, Delegate.create( this, _toggleShortcutMap, false ) ); _frame.pack(); var location : Point = ASWingUtils.getScreenCenterPosition(); location.x -= _frame.getWidth()/2; location.y -= _frame.getHeight()/2; _frame.setLocation( location ); } private function _toggleShortcutMap( source : JFrame, enabled : Boolean ) : Void { var m : ShortcutMap = ShortcutLocator.getMap( SHORTCUT_MAP ); if( enabled ) m.load( ); else m.unload( ); } /** * Configures the based {@code JFrame} container. */ private function _configureBasedFrame() : Void { _frame = new JFrame( FvAsWing.getInstance().getRootMovieClip(), "", false ); _frame.setResizable( false ); _frame.setClosable( true ); _frame.setDefaultCloseOperation( JFrame.HIDE_ON_CLOSE ); _frame.addEventListener( JFrame.ON_WINDOW_CLOSING, _sendResult, this ); } private function _createMenubar() : JMenuBar { _editMenu = new JMenu( FvCalculatorResources.getInstance().menu ); _copyItem = _editMenu.addMenuItem( FvCalculatorResources.getInstance().copy ); _copyItem.addActionListener( _handleCopyResult, this ); _toggleItem = _editMenu.addMenuItem( FvCalculatorResources.getInstance().classic); _toggleItem.addActionListener( _handleToggleType, this ); _bar = new JMenuBar(); _bar.append( _editMenu ); return _bar; } private function _handleCopyResult() : Void { System.setClipboard( _ui.getResult().toString() ); } private function _handleToggleType() : Void { _ui.setCalculatorType( Number( !Boolean( _ui.getCalculatorType() ) ) ); _toggleItemLabel(); _frame.pack(); } private function _toggleItemLabel() : Void { if( _ui.getCalculatorType() ) _toggleItem.setText( FvCalculatorResources.getInstance().classic ); else _toggleItem.setText( FvCalculatorResources.getInstance().scientific ); } private function _sendResult() : Void { if( _accessor ) _accessor.setValue( _ui.getResult() ); } }