1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  import org.aswing.*;
     5  import org.aswing.geom.*;
     6  import org.aswing.plaf.*;
     7  import org.aswing.plaf.winxp.*;
     8  import org.aswing.graphices.*;
     9  
    10  class org.aswing.plaf.winxp.WinXpRadioButtonUI extends WinXpToggleButtonUI{
    11  	 // Shared UI object
    12      private static var radioButtonUI:WinXpRadioButtonUI;
    13      
    14      private var defaultIcon:Icon;
    15      private var defaults_radiobutton_initialized:Boolean;
    16      private static var propertyPrefix:String = "RadioButton" + ".";
    17  
    18      // ********************************
    19      //          Create PLAF
    20      // ********************************
    21      public static function createInstance(c:Component):ComponentUI {
    22      	if(radioButtonUI == null){
    23      		radioButtonUI = new WinXpRadioButtonUI();
    24      	}
    25          return radioButtonUI;
    26      }
    27  
    28      private function getPropertyPrefix():String {
    29          return propertyPrefix;
    30      }
    31      
    32      public function WinXpRadioButtonUI() {
    33      	super();
    34      	defaults_radiobutton_initialized = false;
    35      	checkRectsForCountLayout();
    36      }
    37      
    38      // ********************************
    39      //        Install PLAF 
    40      // ********************************
    41      private function installDefaults(b:AbstractButton):Void{
    42          super.installDefaults(b);
    43          if(!defaults_radiobutton_initialized) {
    44              defaultIcon = UIManager.getIcon(getPropertyPrefix() + "icon");
    45              defaults_initialized = true;
    46          }
    47      }
    48  
    49      // ********************************
    50      //        Uninstall PLAF 
    51      // ********************************
    52      private function uninstallDefaults(b:AbstractButton):Void{
    53          super.uninstallDefaults(b);
    54          defaults_radiobutton_initialized = false;
    55      }
    56  
    57      public function getDefaultIcon():Icon {
    58          return defaultIcon;
    59      }    
    60      
    61      /* These rectangles/insets are allocated once for all 
    62       * ButtonUI.paint() calls.  Re-using rectangles rather than 
    63       * allocating them in each paint call substantially reduced the time
    64       * it took paint to run.  Obviously, this method can't be re-entered.
    65       */
    66  	private static var viewRect:Rectangle;
    67      private static var textRect:Rectangle;
    68      private static var iconRect:Rectangle;
    69         
    70      private static function checkRectsForCountLayout():Void{
    71      	if(viewRect == null){
    72  			viewRect = new Rectangle();
    73      		textRect = new Rectangle();
    74      		iconRect = new Rectangle();
    75      	}
    76      }
    77      
    78      public function paint(c:Component, g:Graphics, r:Rectangle):Void{
    79      	var b:AbstractButton = AbstractButton(c);
    80      	var model:ButtonModel = b.getModel();
    81      	paintBackGround(b, g, r);
    82      	
    83      	var insets:Insets = b.getMargin();
    84      	if(insets != null){
    85      		r = insets.getInsideBounds(r);
    86      	}
    87      	viewRect.setRect(r);
    88      	
    89      	textRect.x = textRect.y = textRect.width = textRect.height = 0;
    90          iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
    91  
    92  		var altIcon:Icon = b.getIcon();
    93  
    94          // layout the text and icon
    95          var text:String = ASWingUtils.layoutCompoundLabel(
    96              c.getFont(), b.getText(), altIcon != null ? altIcon : getDefaultIcon(), 
    97              b.getVerticalAlignment(), b.getHorizontalAlignment(),
    98              b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
    99              viewRect, iconRect, textRect, 
   100  	    	b.getText() == null ? 0 : b.getIconTextGap());
   101  		// Paint the radio button
   102  		if (altIcon != null) {
   103  			if (!model.isEnabled()) {
   104  				if (model.isSelected()) {
   105  					altIcon = b.getDisabledSelectedIcon();
   106  				} else {
   107  					altIcon = b.getDisabledIcon();
   108  				}
   109  			} else if (model.isPressed()) {
   110  				altIcon = b.getPressedIcon();
   111  				if (altIcon == null) {
   112  					// Use selected icon
   113  					altIcon = b.getSelectedIcon();
   114  				}
   115  			} else if (model.isSelected()) {
   116  				if (b.isRollOverEnabled() && model.isRollOver()) {
   117  					altIcon = b.getRollOverSelectedIcon();
   118  					if (altIcon == null) {
   119  						altIcon = b.getSelectedIcon();
   120  					}
   121  				} else {
   122  					altIcon = b.getSelectedIcon();
   123  				}
   124  			} else if (b.isRollOverEnabled() && model.isRollOver()) {
   125  				altIcon = b.getRollOverIcon();
   126  			}
   127  
   128  			if (altIcon == null) {
   129  				altIcon = b.getIcon();
   130  			}
   131  		} else {
   132  			altIcon = getDefaultIcon();
   133  		}
   134  		unistallLastPaintIcon(b, altIcon);
   135  		altIcon.paintIcon(b, g, iconRect.x, iconRect.y);
   136      	setJustPaintedIcon(b, altIcon);
   137  	    
   138  	    //paint text
   139          if (text != null && text != ""){
   140  			paintText(b, textRect, text);
   141          }
   142      }
   143      
   144      //just fill rect with background color
   145      private function paintBackGround(c:Component, g:Graphics, b:Rectangle):Void{
   146      	fillRectBackGround(c, g, b);
   147      }
   148      
   149      public function getPreferredSize(c:Component):Dimension{
   150      	var b:AbstractButton = AbstractButton(c);
   151      	var icon:Icon = b.getIcon();
   152      	if(icon == null) icon = getDefaultIcon();
   153      	var text:String = b.getText();
   154      	return getButtonPreferredSize(b, icon, text);
   155      }
   156  
   157      public function getMinimumSize(c:Component):Dimension{
   158      	var b:AbstractButton = AbstractButton(c);
   159      	var icon:Icon = b.getIcon();
   160      	if(icon == null) icon = getDefaultIcon();
   161      	var text:String = b.getText();
   162      	return getButtonMinimumSize(b, icon, text);
   163      }  
   164  }
   165