|
| 1 | +/* |
| 2 | +* Copyright (c) 2011 Research In Motion Limited. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package com.samples.toolkit.ui.component; |
| 18 | + |
| 19 | +import net.rim.device.api.system.*; |
| 20 | +import net.rim.device.api.ui.*; |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * Implements all the stuff we don't want to do each time we need a new button |
| 25 | + */ |
| 26 | +public abstract class BaseButtonField extends Field |
| 27 | +{ |
| 28 | + private XYRect _drawFocusTempRect = new XYRect(); |
| 29 | + |
| 30 | + public BaseButtonField() |
| 31 | + { |
| 32 | + this( 0 ); |
| 33 | + } |
| 34 | + |
| 35 | + public BaseButtonField( long style ) |
| 36 | + { |
| 37 | + super( Field.FOCUSABLE | style ); |
| 38 | + } |
| 39 | + |
| 40 | + protected void layout( int width, int height ) |
| 41 | + { |
| 42 | + setExtent( 10, 10 ); |
| 43 | + } |
| 44 | + |
| 45 | + protected void drawFocus( Graphics g, boolean on ) |
| 46 | + { |
| 47 | + getFocusRect( _drawFocusTempRect ); |
| 48 | + |
| 49 | + boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ); |
| 50 | + boolean notEmpty = g.pushContext( _drawFocusTempRect.x, _drawFocusTempRect.y, _drawFocusTempRect.width, _drawFocusTempRect.height, 0, 0 ); |
| 51 | + |
| 52 | + try { |
| 53 | + if( notEmpty ) { |
| 54 | + g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, on ); |
| 55 | + paintBackground( g ); |
| 56 | + paint( g ); |
| 57 | + } |
| 58 | + } finally { |
| 59 | + g.popContext(); |
| 60 | + g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected boolean keyChar( char character, int status, int time ) |
| 65 | + { |
| 66 | + if( character == Characters.ENTER ) { |
| 67 | + clickButton(); |
| 68 | + return true; |
| 69 | + } |
| 70 | + return super.keyChar( character, status, time ); |
| 71 | + } |
| 72 | + |
| 73 | + protected boolean navigationClick( int status, int time ) |
| 74 | + { |
| 75 | + clickButton(); |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + protected boolean trackwheelClick( int status, int time ) |
| 80 | + { |
| 81 | + clickButton(); |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + protected boolean invokeAction( int action ) |
| 86 | + { |
| 87 | + switch( action ) { |
| 88 | + case ACTION_INVOKE: { |
| 89 | + clickButton(); |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + return super.invokeAction( action ); |
| 94 | + } |
| 95 | + |
| 96 | + public void setDirty( boolean dirty ) { |
| 97 | + // We never want to be dirty or muddy |
| 98 | + } |
| 99 | + |
| 100 | + public void setMuddy( boolean muddy ) { |
| 101 | + // We never want to be dirty or muddy |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * A public way to click this button |
| 106 | + */ |
| 107 | + public void clickButton() |
| 108 | + { |
| 109 | + fieldChangeNotify( 0 ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
0 commit comments