1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4  import org.aswing.ASTextExtent;
     5  import org.aswing.ASWingUtils;
     6  import org.aswing.Component;
     7  import org.aswing.Event;
     8  import org.aswing.geom.Dimension;
     9  import org.aswing.geom.Rectangle;
    10  import org.aswing.graphices.Graphics;
    11  import org.aswing.JProgressBar;
    12  import org.aswing.LookAndFeel;
    13  import org.aswing.plaf.ProgressBarUI;
    14  import org.aswing.plaf.UIResource;
    15  import org.aswing.UIManager;
    16  
    17  /**
    18   * @author iiley
    19   */
    20  class org.aswing.plaf.basic.BasicProgressBarUI extends ProgressBarUI {
    21  	
    22  	private var stringText:TextField;
    23  	private var stateListener:Object;
    24  	
    25  	public function BasicProgressBarUI() {
    26  		super();
    27  	}
    28  
    29  	public function installUI(c:Component):Void{
    30  		installDefaults(JProgressBar(c));
    31  		installListeners(JProgressBar(c));
    32  	}
    33  	
    34  	public function uninstallUI(c:Component):Void{
    35  		uninstallDefaults(JProgressBar(c));
    36  		uninstallListeners(JProgressBar(c));
    37  		stringText.removeTextField();
    38  	}
    39  	
    40  	public function installDefaults(s:JProgressBar):Void{
    41  		LookAndFeel.installColorsAndFont(s, "ProgressBar.background", "ProgressBar.foreground", "ProgressBar.font");
    42  		LookAndFeel.installBasicProperties(s, "ProgressBar.");
    43  		LookAndFeel.installBorder(s, "ProgressBar.border");
    44  		
    45  		if(s.getIcon() === undefined || s.getIcon() instanceof UIResource){
    46  			s.setIcon(UIManager.getIcon("ProgressBar.icon"));
    47  		}
    48  	}
    49  	
    50  	public function uninstallDefaults(s:JProgressBar):Void{
    51  		LookAndFeel.uninstallBorder(s);
    52  		if(s.getIcon() instanceof UIResource){
    53  			s.setIcon(undefined);
    54  		}
    55  	}
    56  	public function installListeners(s:JProgressBar):Void{
    57  		stateListener = s.addChangeListener(__stateChanged, this);
    58  	}
    59  	public function uninstallListeners(s:JProgressBar):Void{
    60  		s.removeEventListener(stateListener);
    61  	}
    62  	
    63  	public function create(c:Component):Void{
    64  		stringText = c.createTextField("p_text");
    65  	}
    66  	
    67  	private function __stateChanged(event:Event):Void{
    68  		var s:JProgressBar = JProgressBar(event.getSource());
    69  		s.repaint();
    70  	}
    71  	
    72  	public function paint(c:Component, g:Graphics, b:Rectangle):Void{
    73  		var sp:JProgressBar = JProgressBar(c);
    74  		
    75  		sp.getIcon().paintIcon(c, g, b.x, b.y);
    76  		
    77  		if(sp.getString() != null && sp.getString().length>0){
    78  			stringText.text = sp.getString();
    79  	    	ASWingUtils.applyTextFontAndColor(stringText, sp.getFont(), sp.getForeground());
    80  			
    81  			if (sp.getOrientation() == JProgressBar.VERTICAL){
    82  				stringText._rotation = -90;
    83  				stringText._x = Math.round(b.x + (b.width - stringText._width)/2);
    84  				stringText._y = Math.round(b.y + (b.height - stringText._height)/2 + stringText._height);
    85  			}else{
    86  				stringText._rotation = 0;
    87  				stringText._x = Math.round(b.x + (b.width - stringText._width)/2);
    88  				stringText._y = Math.round(b.y + (b.height - stringText._height)/2);
    89  			}
    90  		}else{
    91  			stringText.text = "";
    92  		}
    93  	}
    94  		
    95  	public function getPreferredSize(c:Component):Dimension{
    96  		var sp:JProgressBar = JProgressBar(c);
    97  		var size:Dimension;
    98  		if (sp.getOrientation() == JProgressBar.VERTICAL){
    99  			size = getPreferredInnerVertical();
   100  		}else{
   101  			size = getPreferredInnerHorizontal();
   102  		}
   103  		
   104  		if(sp.getString() != null){
   105  			var extent:ASTextExtent = c.getFont().getASTextFormat().getTextExtent(sp.getString());
   106  			if (sp.getOrientation() == JProgressBar.VERTICAL){
   107  				size.width = Math.max(size.width, extent.getTextFieldHeight());
   108  				size.height = Math.max(size.height, extent.getTextFieldWidth());
   109  			}else{
   110  				size.width = Math.max(size.width, extent.getTextFieldWidth());
   111  				size.height = Math.max(size.height, extent.getTextFieldHeight());
   112  			}
   113  		}
   114  		return sp.getInsets().roundsSize(size);
   115  	}
   116      public function getMaximumSize(c:Component):Dimension{
   117  		return new Dimension(Number.MAX_VALUE, Number.MAX_VALUE);
   118      }
   119      public function getMinimumSize(c:Component):Dimension{
   120  		return c.getInsets().roundsSize(new Dimension(1, 1));
   121      }
   122      
   123      private function getPreferredInnerHorizontal():Dimension{
   124      	return new Dimension(80, 12);
   125      }
   126      private function getPreferredInnerVertical():Dimension{
   127      	return new Dimension(12, 80);
   128      }
   129  }
   130