1  /*
     2   Copyright aswing.org, see the LICENCE.txt.
     3  */
     4   
     5  import org.aswing.ASColor;
     6  import org.aswing.Component;
     7  import org.aswing.geom.Dimension;
     8  import org.aswing.geom.Rectangle;
     9  import org.aswing.graphices.Graphics;
    10  import org.aswing.graphices.SolidBrush;
    11   
    12  /**
    13   * The base class for all UI delegate objects in the Swing pluggable look and feel architecture. 
    14   * The UI delegate object for a Swing component is responsible for implementing 
    15   * the aspects of the component that depend on the look and feel. The JComponent 
    16   * class invokes methods from this class in order to delegate operations (painting, 
    17   * layout calculations, etc.) that may vary depending on the look and feel installed. 
    18   * <b>Client programs should not invoke methods on this class directly.</b>
    19   * 
    20   * @author iiley
    21   */
    22  class org.aswing.plaf.ComponentUI{
    23      /**
    24       * Sole constructor. (For invocation by subclass constructors,
    25       * typically implicit.)
    26       */
    27      public function ComponentUI() {
    28      }
    29  
    30      /**
    31       * Configures the specified component appropriate for the look and feel.
    32       * This method is invoked when the <code>ComponentUI</code> instance is being installed
    33       * as the UI delegate on the specified component.  This method should
    34       * completely configure the component for the look and feel,
    35       * including the following:
    36       * <ol>
    37       * <li>Install any default property values for color, fonts, borders,
    38       *     icons, opacity, etc. on the component.  Whenever possible, 
    39       *     property values initialized by the client program should <i>not</i> 
    40       *     be overridden.
    41       * <li>Install a <code>LayoutManager</code> on the component if necessary.
    42       * <li>Create/add any required sub-components to the component.
    43       * <li>Create/install event listeners on the component.
    44       * <li>Install keyboard UI (mnemonics, traversal, etc.) on the component.
    45       * <li>Initialize any appropriate instance data.
    46       * </ol>
    47       * @param c the component where this UI delegate is being installed
    48       *
    49       * @see #uninstallUI
    50       * @see org.aswing.Component#setUI
    51       * @see org.aswing.Component#updateUI
    52       */
    53      public function installUI(c:Component):Void{
    54      }
    55  
    56      /**
    57       * Reverses configuration which was done on the specified component during
    58       * <code>installUI</code>.  This method is invoked when this 
    59       * <code>ComponentUI</code> instance is being removed as the UI delegate 
    60       * for the specified component.  This method should undo the
    61       * configuration performed in <code>installUI</code>, being careful to 
    62       * leave the <code>Component</code> instance in a clean state (no 
    63       * extraneous listeners, look-and-feel-specific property objects, etc.).
    64       * This should include the following:
    65       * <ol>
    66       * <li>Remove any UI-set borders from the component.
    67       * <li>Remove any UI-set layout managers on the component.
    68       * <li>Remove any UI-added sub-components from the component.
    69       * <li>Remove any UI-added event listeners from the component.
    70       * <li>Remove any UI-installed keyboard UI from the component.
    71       * <li>Remove any UI-added MCs from this component.
    72       * <li>Nullify any allocated instance data objects to allow for GC.
    73       * </ol>
    74       * @param c the component from which this UI delegate is being removed;
    75       *          this argument is often ignored,
    76       *          but might be used if the UI object is stateless
    77       *          and shared by multiple components
    78       *
    79       * @see #installUI
    80       * @see org.aswing.Component#updateUI
    81       */
    82      public function uninstallUI(c:Component):Void{
    83      }
    84      
    85      /**
    86       * Notifies this UI delegate that it's time to create the specified
    87       * component's ui MCs.  This method is invoked by <code>Component</code> 
    88       * when the specified component is being created.
    89       *
    90       * <p>In general this method need not be overridden by subclasses;
    91       * all look-and-feel ui creating code should reside in this method.
    92       * 
    93       * @param c the component being painted;
    94       *          this argument is often ignored,
    95       *          but might be used if the UI object is stateless
    96       *          and shared by multiple components
    97       * 
    98       * @see org.aswing.Component#create
    99       */    
   100      public function create(c:Component):Void{
   101      }
   102      
   103      /**
   104       * Notifies this UI delegate that it's time to paint the specified
   105       * component.  This method is invoked by <code>Component</code> 
   106       * when the specified component is being painted.
   107       *
   108       * <p>In general this method need be overridden by subclasses;
   109       * all look-and-feel rendering code should reside in this method.
   110       * And there is a default background paint method, you should call
   111       * it in your overridden paint method.
   112       * 
   113       * @param c the component being painted;
   114       *          this argument is often ignored,
   115       *          but might be used if the UI object is stateless
   116       *          and shared by multiple components.
   117       * @param g the Graphics context in which to paint.
   118       * @param b the bounds to paint UI in.
   119       * 
   120       * @see org.aswing.Component#paint
   121       * @see #paintBackGround
   122       */
   123      public function paint(c:Component, g:Graphics, b:Rectangle):Void{
   124      	paintBackGround(c, g, b);
   125      }
   126      
   127      /**
   128       * The default background paint function. 
   129       * paint the background with the background color in the component rect.
   130       * If your UI want to paint a different background for your component, 
   131       * override this method.
   132       * <p>
   133       * Generally you should call this method in the <code>paint</code> method.
   134       * override this method to paint different background.
   135       * @param c the component whose background being painted.
   136       * @param g the Graphics context in which to paint.
   137       * @param b the bounds to paint background in.
   138       * 
   139       * @see #paint
   140       */
   141      private function paintBackGround(c:Component, g:Graphics, b:Rectangle):Void{
   142      	if(c.isOpaque())
   143      		fillRectBackGround(c, g, b);
   144      }
   145      
   146      /**
   147       * Utils method to paint the background of the component with the background 
   148       * color in the component rect.
   149       * 
   150       * @param c the component whose background being painted.
   151       * @param g the Graphics context in which to paint.
   152       * @param b the bounds to paint background in.
   153       */
   154      public static function fillRectBackGround(c:Component, g:Graphics, b:Rectangle):Void{
   155   		var bgColor:ASColor = (c.getBackground() == null ? ASColor.WHITE : c.getBackground());
   156  		g.fillRectangle(new SolidBrush(bgColor), b.x, b.y, b.width, b.height);
   157      }
   158      
   159      /**
   160       * Returns the specified component's preferred size appropriate for
   161       * the look and feel.  If <code>null</code> is returned, the preferred
   162       * size will be calculated by the component's layout manager instead 
   163       * (this is the preferred approach for any component with a specific
   164       * layout manager installed).  The default implementation of this 
   165       * method returns <code>null</code>.
   166       *
   167       * @param c the component whose preferred size is being queried;
   168       *          this argument is often ignored,
   169       *          but might be used if the UI object is stateless
   170       *          and shared by multiple components
   171       *
   172       * @see org.aswing.Component#getPreferredSize
   173       * @see org.aswing.LayoutManager#preferredLayoutSize
   174       */
   175      public function getPreferredSize(c:Component):Dimension{
   176  		return null;
   177      }
   178  
   179      /**
   180       * Returns the specified component's minimum size appropriate for
   181       * the look and feel.  If <code>null</code> is returned, the minimum
   182       * size will be calculated by the component's layout manager instead 
   183       * (this is the preferred approach for any component with a specific
   184       * layout manager installed).  The default implementation of this 
   185       * method invokes <code>getPreferredSize</code> and returns that value.
   186       *
   187       * @param c the component whose minimum size is being queried;
   188       *          this argument is often ignored,
   189       *          but might be used if the UI object is stateless
   190       *          and shared by multiple components
   191       *
   192       * @return a <code>Dimension</code> object or <code>null</code>
   193       *
   194       * @see org.aswing.Component#getMinimumSize
   195       * @see org.aswing.LayoutManager#minimumLayoutSize
   196       * @see #getPreferredSize
   197       */
   198      public function getMinimumSize(c:Component):Dimension{
   199  		return getPreferredSize(c);
   200      }
   201  
   202      /**
   203       * Returns the specified component's maximum size appropriate for
   204       * the look and feel.  If <code>null</code> is returned, the maximum
   205       * size will be calculated by the component's layout manager instead 
   206       * (this is the preferred approach for any component with a specific
   207       * layout manager installed).  The default implementation of this 
   208       * method invokes <code>getPreferredSize</code> and returns that value.
   209       *
   210       * @param c the component whose maximum size is being queried;
   211       *          this argument is often ignored,
   212       *          but might be used if the UI object is stateless
   213       *          and shared by multiple components
   214       * @return a <code>Dimension</code> object or <code>null</code>
   215       *
   216       * @see org.aswing.Component#getMaximumSize
   217       * @see org.aswing.LayoutManager#maximumLayoutSize
   218       * @see #getPreferredSize
   219       */
   220      public function getMaximumSize(c:Component):Dimension{
   221  		return getPreferredSize(c);
   222      }    
   223  }
   224