| |
"A UI Framework Fot The MIDP API"
Vol. 6, Issue 6, p. 160
Listing 1: MContainer provides contextual management of MComponents
import java.util.*;
import javax.microedition.lcdui.*;
abstract public class MContainer extends Canvas
{
private Vector components = new Vector();
private MFocusManager focusManager;
protected MContainer ()
{
focusManager = new MFocusManager( this );
} // constructor
protected void add ( MComponent component,
int x, // screen
int y ) // coordinates
{
component.setX( x );
component.setY( y );
component.setContainer( this );
components.addElement( component );
if ( components.size() == 1 )
{ // first component on the screen
component.setFocus( true );
}
} // add
// gets component at this rank, where rank is
// the order in which the component
// was added to the container
MComponent getComponent ( int rank )
{
Object obj = components.elementAt( rank );
return ( obj == null ? null :
(MComponent ) obj );
} // getComponent
// gets the number of components in the container
protected int getComponentCount ()
{ return components.size(); }
protected void keyPressed( int keyCode )
{ focusManager.keyPressed( keyCode ); }
protected void paint( Graphics g )
{ // paint each component
for ( Enumeration enum = components.elements();
enum.hasMoreElements(); )
{
MComponent nextComponent =
( MComponent ) enum.nextElement();
nextComponent.paint( g );
}
} // paint
}// MContainer
Listing 2: MComponent, the base class for all custom visual components
import java.util.*;
import javax.microedition.lcdui.*;
abstract public class MComponent
{
final MContainer getContainer()
{ return this.container; }
abstract public int getHeight();
abstract public int getWidth();
final public int getX()
{return this.x;}
final public int getY()
{return this.y;}
public boolean hasFocus( )
{ return this.hasFocus; }
abstract protected void keyPressed(int keyCode);
abstract protected void paint( Graphics g );
// container in which this component is placed
private MContainer container;
private boolean hasFocus = false;
private int x, y; // screen coordinates
protected void
processFocusEvent( MFocusEvent event )
{
hasFocus =
( event.getEventId() ==
MFocusEvent.FOCUS_GAINED );
} // processFocusEvent
protected void repaint ()
{
if ( container != null )
container.repaint();
} // repaint
final void setContainer( MContainer container )
{ this.container = container; }
void setFocus ( boolean state )
{ hasFocus = state; }
final void setX(int value)
{this.x = value;}
final void setY(int value)
{this.y = value;}
}// MComponent
Listing 3: MFocusManager provides navigation between MComponents
import javax.microedition.lcdui.*;
public class MFocusManager
{
// offsets to next and previous component
private final static int NEXT = 1;
private final static int PREVIOUS = -1;
// Values of the keys to move right and left
private int rightKey, leftKey;
// Container of components to manage focus for
private MContainer container;
// The rank of the component with focus
private int focusedComponentRank = 0;
public MFocusManager ( MContainer container )
{
this.container = container;
leftKey = container.getKeyCode(Canvas.LEFT);
rightKey = container.getKeyCode(Canvas.RIGHT);
} // constructor
protected void
changeFocus( int offset ) // NEXT or PREVIOUS
{
// notify currently focused component of change
MFocusEvent event =
createFocusEvent( focusedComponentRank,
MFocusEvent.FOCUS_LOST );
container.
getComponent( focusedComponentRank ).
processFocusEvent( event );
focusedComponentRank += offset;
// notify newly focused component of change
event =
createFocusEvent( focusedComponentRank,
MFocusEvent.FOCUS_GAINED );
container.
getComponent( focusedComponentRank ).
processFocusEvent( event );
container.repaint(); // update display
} // changeFocus
protected MFocusEvent
createFocusEvent( int componentRank,
int eventId )
{
return
new MFocusEvent( container.
getComponent(componentRank),
eventId );
} // createFocusEvent
protected void keyPressed( int keyCode )
{
if ( keyCode == rightKey &&
focusedComponentRank <
container.getComponentCount() - 1 )
{ // not the last component
changeFocus( NEXT );
}
else if ( keyCode == leftKey &&
focusedComponentRank != 0 )
{ // not the first component
changeFocus( PREVIOUS );
}
else // pass the key stroke to the component
{ // with focus
container.
getComponent( focusedComponentRank ).
keyPressed( keyCode );
}
} // keyPressed
}// MFocusManager
Listing 4: MFocusEvents are delivered to MComponents to notify them when they gain and lose focus.
public class MFocusEvent
{
public final static int FOCUS_GAINED = 1;
public final static int
FOCUS_LOST = -FOCUS_GAINED;
private MComponent source;
private int eventId; //FOCUS_GAINED or FOCUS_LOST
MFocusEvent ( MComponent source, int eventId )
{
this.source = source;
this.eventId = eventId;
} // constructor
public int getEventId ()
{ return this.eventId; }
public MComponent getSource ()
{ return this.source; }
}// MFocusEvent
|
|