/*----------------------------------------------------------------------------- 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.ASColor; import org.aswing.ASFont; import org.aswing.Icon; import org.aswing.JLabel; import org.aswing.UIManager; import org.aswing.util.Delegate; import fever.app.accelerator.Keyboard; import fever.app.accelerator.KeyDefinition; import fever.app.accelerator.ShortcutLocator; import fever.app.accelerator.ShortcutMap; import fever.app.local.Localisation; import fever.core.Resources; import fever.medias.sound.AppSoundFactory; import fever.structures.ShortcutString; import fvaswing.FvAsWing; import fvaswing.structure.FvFont; /** * Label act as a button. * *

Thanks to iiley for this class, you can retreive it in Aswing extension page * named XLabelButton :
* http://www.aswing.org/display/AsWing/XLabelButton * *

{@link FvLabelButton} class is defined here to avoid mulitple different classes * location. * *

Fever feature like : *

* * {@code * var button : FvLabelButton = new FvLabelButton( 'click me please' ); * button.setSoundID( 'click' ); * button.setShortcut( new KeyCombo( Keyboard.onKeyCONTROL, Keyboard.onKeyO ) ); * button.setToolTipText( 'open file' ); * button.addActionListener( _onClick, this ); * } * *

You can use auto label shortcut definition too : ( passing {@code null} as * {@code key} argument : * {@code * var button : FvLabelButton = new FvLabelButton( 'click &me please' ); * button.setShortcut( null, 'myKeyMap' ); * button.setToolTipText( 'open file' ); * button.addActionListener( _onClick, this ); * } * * @author Romain Ecarnot */ class fvaswing.components.FvLabelButton extends JLabel { //------------------------------------------------------------------------- // Private properties //------------------------------------------------------------------------- private var _normalColor : ASColor; private var _hightlightColor : ASColor; private var _disabledColor : ASColor; private var _soundID : String; private var _keymapName : String; private var _keyDef : KeyDefinition; private var _sString : ShortcutString; private var _storeTip : String; private var font : FvFont; //override super class type private var _localNodeID : String; //------------------------------------------------------------------------- // Public API //------------------------------------------------------------------------- /** * FvLabelButton( text : String, icon : Icon, horizontalAlignment : Number )
* FvLabelButton( text : String, icon : Icon )
* FvLabelButton( text : String, horizontalAlignment : Number)
* FvLabelButton( text : String )
* FvLabelButton( icon : Icon, horizontalAlignment : Number)
* FvLabelButton( icon : Icon )
* FvLabelButton()
*/ public function FvLabelButton( caption, icon, horizontalAlignment : Number ) { super( caption, icon, horizontalAlignment ); _updateTextContent( text ); _initColorsWithConstants(); setUseHandCursor( true ); var lis:Object = new Object(); lis[ JLabel.ON_ROLLOVER ] = Delegate.create( this, _highlight ); lis[ JLabel.ON_ROLLOUT ] = Delegate.create( this, _normal ); lis[ JLabel.ON_PRESS ] = lis[ JLabel.ON_ROLLOUT ]; addEventListener(lis); _normal(); setFocusable( true ); addEventListener( JLabel.ON_KEY_UP, _handleKeyUp, this ); } /** * Sets text and icon at one method here. * * @param text the text for the label * @param icon the default icon for the label */ public function setContent( caption : String, icon : Icon ) : Void { setText( caption ); if( this.icon != icon ) { uninstallIconWhenNextPaint(this.icon); this.icon = icon; repaint(); invalidate(); } } /** * Sets button label. * *

Checks if passed-in {@code caption} contains {@link #SHORTCUT_CHAR}.
* If {@code true} automatically build a new shortcut for button. * * @param caption Button label */ public function setText( caption : String ) : Void { if( caption != text ) { _updateTextContent( caption ); } } /** * Returns button label.
* Without any shortcut marker ( if present ) */ public function getText() : String { return _sString.text; } /** * Sets sound id to play when user click on button. * *

Sounds id is searched in current * {@link fever.medias.sound.AppSoundFactory} factory. */ public function setSoundID( soundID : String ) : Void { if( soundID ) _soundID = soundID; } /** * Returns sound id associated to button click. */ public function getSoundID() : String { return _soundID; } /** * addActionListener(fuc:Function, obj:Object)
* addActionListener(fuc:Function) *

* Adds a action listener to this button. Buttons fire a action event when * user released on it. * @param fuc the listener function. * @param Context in which to run the function of param func. * @return the listener just added. * @see EventDispatcher#ON_ACT * @see Component#ON_RELEASE */ public function addActionListener( fuc : Function, obj : Object ) : Object { return addEventListener(ON_ACT, fuc, obj); } /** * Enable or disable the component. */ public function setEnabled( b : Boolean ) : Void { if( b != isEnabled() ) { setForeground( b ? _normalColor : _disabledColor ); repaint(); } super.setEnabled(b); } /** * Sets foreground color for "normal" button state. */ public function setNormalColor( color : ASColor ) : Void { if( color != undefined ) { _normalColor = color; _normal(); } } /** * Returns foreground color for "normal" button state. */ public function getNormalColor() : ASColor { return _normalColor; } /** * Sets foreground color for "hightlight" button state. */ public function setHightlightColor( color : ASColor ) : Void { if( color != undefined ) _hightlightColor = color; } /** * Returns foreground color for "hightlight" button state. */ public function getHightlightColor() : ASColor { return _hightlightColor; } /** * Sets foreground color for "disabled" button state. */ public function setDisabledColor( color : ASColor ) : Void { if( color != undefined ) _disabledColor = color; } /** * Returns foreground color for "disabled" button state. */ public function getDisabledColor() : ASColor { return _disabledColor; } /** * Sets new key shortcut to button. * *

Pass {@code key == null} to enable built-in label shortcut * definition. */ public function setShortcut( key : KeyDefinition, keymapID : String ) : Void { var keyMap : ShortcutMap; if( ShortcutLocator.isRegistered( keymapID ) ) { keyMap = ShortcutLocator.getMap( keymapID ); _keymapName = keyMap.getName(); } else { keyMap = ShortcutMap.getApplicationMap(); _keymapName = null; } if( key ) { _applyShortcut( keyMap, key ); if( _sString.enabled ) font.removeShortcutIndex(); } else if( _sString.enabled ) { _applyShortcut( keyMap, Keyboard.getCharStruct( _sString.getMarker() ) ); } else { if( _keyDef ) releaseShortcut(); } } /** * Releases key shortcut associated to button. */ public function releaseShortcut() : Void { var keymap : ShortcutMap = ( _keymapName ) ? ShortcutLocator.getMap( _keymapName ) : ShortcutMap.getApplicationMap(); keymap.unregister( _keyDef ); _keyDef = null; if( _storeTip ) setToolTipText(); } /** * Connects button to localisation API. * * @param nodeID Full qualified node path to translation node in local file * @param resource Custom resource where to find {@code nodeID} node */ public function setLocalisationID( nodeID : String, resource : Resources ) : Void { if( nodeID != _localNodeID ) { if( resource || Localisation.hasResource( nodeID ) ) { _localNodeID = nodeID; Localisation.connectInstance( this, setText, nodeID, resource ); } else Localisation.disconnectInstance( this ); } } /** * Returns translation node path. */ public function getLocalisationID() : String { return _localNodeID; } /** * Registers the text to display in a tool tip. * *

Take care if a shortcut is defined.
* If {@code true} add '( shortcut name )' at the end of tooltip. */ public function setToolTipText( caption : String ) : Void { _storeTip = caption; super.setToolTipText( _buildToolTipText() ); } /** * Returns tooltips without shortcut caption. */ public function getTooltipTextWithoutShortcut() : String { return _storeTip; } /** * Sets the text font for this component.
* this method will cause a repaint and revalidate method call.
* * @param newFont the font to set for this component. */ public function setFont( newFont : ASFont ) : Void { if( font != newFont ) { font = FvFont.getFromAsFont( newFont ); _applyMarkerOnFont(); } } //------------------------------------------------------------------------- // Private implementation //------------------------------------------------------------------------- private function _updateTextContent( caption : String ) : Void { _sString = new ShortcutString( caption ); _applyMarkerOnFont(); if( _keyDef || _keymapName ) setShortcut( null, _keymapName ); repaint(); invalidate(); } private function _initColorsWithConstants() : Void { _normalColor = ASColor.BLACK; _hightlightColor = new ASColor(0x669900); _disabledColor = UIManager.getColor( 'Button.foreground' ); } private function _handleKeyUp() : Void { if( Key.getCode() == Key.SPACE ) fireActionEvent(); } private function _highlight() : Void { setForeground( _hightlightColor ); repaint(); } private function _normal() : Void { setForeground( _normalColor ); repaint(); } private function __onRelease() : Void { super.__onRelease(); _checkSound(); dispatchEvent( ON_ACT, createEventObj( ON_ACT ) ); } private function _checkSound( ) : Void { if( _soundID ) AppSoundFactory.getSoundFactory().getSound( _soundID ).start(); } private function _buildToolTipText() : String { if( _keyDef && !_sString.enabled ) { return ( _storeTip + ' ( ' + _keyDef.getName() + ' )' ); } return _storeTip; } private function _applyShortcut( keyMap : ShortcutMap, key : KeyDefinition ) : Void { if( _keyDef ) releaseShortcut(); keyMap.registerCustomType( FvAsWing.AWSHORTCUT, key, this ); _keyDef = key; if( _storeTip ) setToolTipText(); } private function _applyMarkerOnFont() : Void { if( _sString.enabled ) font.setShortcutIndex( _sString.index ); else font.removeShortcutIndex(); setFontValidated( false ); repaint(); revalidate(); } }