View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id$
29   */
30  
31  package palmed.ui;
32  
33  import javax.microedition.lcdui.Graphics;
34  
35  /***
36   * This class implements a component within a container.
37   *
38   * @author Mathieu Champlon
39   * @version $Revision$ $Date$
40   */
41  public final class Component implements IComponent
42  {
43      /***
44       * The underlying user component.
45       */
46      private final IComponent component_;
47      /***
48       * The component location inside the container.
49       */
50      private int x_, y_;
51  
52      /***
53       * Create a component.
54       *
55       * @param component the underlying user component
56       */
57      public Component( final IComponent component )
58      {
59          if( component == null )
60              throw new IllegalArgumentException( "parameter 'component' is null" );
61          component_ = component;
62      }
63  
64      /***
65       * Move the component to the given position.
66       *
67       * @param x the position abscissa
68       * @param y the position ordinate
69       */
70      public void move( final int x, final int y )
71      {
72          x_ = x;
73          y_ = y;
74      }
75  
76      /***
77       * {@inheritDoc}
78       */
79      public void paint( final Graphics g )
80      {
81          g.translate( x_, y_ );
82          final int clipX = g.getClipX();
83          final int clipY = g.getClipY();
84          final int clipW = g.getClipWidth();
85          final int clipH = g.getClipHeight();
86          g.setClip( 0, 0, component_.getWidth(), component_.getHeight() );
87          component_.paint( g );
88          g.setClip( clipX, clipY, clipW, clipH );
89          g.translate( -x_, -y_ );
90      }
91  
92      /***
93       * {@inheritDoc}
94       */
95      public void resize( final int width, final int height )
96      {
97          component_.resize( width, height );
98      }
99  
100     /***
101      * {@inheritDoc}
102      */
103     public int getLayout()
104     {
105         return component_.getLayout();
106     }
107 
108     /***
109      * {@inheritDoc}
110      */
111     public boolean click( final int x, final int y )
112     {
113         return isInside( x, y ) && component_.click( x - x_, y - y_ );
114     }
115 
116     private boolean isInside( final int x, final int y )
117     {
118         return x >= x_ && x < x_ + getWidth() && y >= y_ && y < y_ + getHeight();
119     }
120 
121     /***
122      * {@inheritDoc}
123      */
124     public void drag( final int x, final int y )
125     {
126         component_.drag( x - x_, y - y_ );
127     }
128 
129     /***
130      * {@inheritDoc}
131      */
132     public void unclick( final int x, final int y )
133     {
134         component_.unclick( x - x_, y - y_ );
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public int getWidth()
141     {
142         return component_.getWidth();
143     }
144 
145     /***
146      * {@inheritDoc}
147      */
148     public int getHeight()
149     {
150         return component_.getHeight();
151     }
152 }