Wednesday, March 27, 2013

Image Button

public class ImageButton extends Field {
    /* Implementation details of ImageButton :   
     */
   
    private Bitmap backgroundImage = null;
   


    private Bitmap pressedImage = null;
   
    private int width = 0;
   
    private boolean focusable = true;
   
    private boolean selected = false;
   
    /**
     * <p>This method can be used for getting the selected.</p>
     * @return The selected.
     */
    public boolean isSelected() {
        return selected; // returning selected.
    }

    /**
     * <p>This method can be used for setting the selected.</p>
     * @param selected The selected to set.
     */
    public void setSelected(boolean selected) {
        this.selected = selected; // Assigning to this.selected.
        invalidate();
    }

    /**
     * <p>This method can be used for setting the focusable.</p>
     * @param focusable The focusable to set.
     */
    public void setFocusable(boolean focusable) {
        this.focusable = focusable; // Assigning to this.focusable.
    }

    /**
     * <p>This method can be used for getting the width.</p>
     * @return The width.
     */
    public int getWidthAssigned() {
        return width; // returning width.
    }

    /**
     * <p>This method can be used for setting the width.</p>
     * @param width The width to set.
     */
    public void setWidthAssigned(int width) {
        this.width = width; // Assigning to this.width.
    }

    /**
     * <p>This method can be used for getting the hieght.</p>
     * @return The hieght.
     */
    public int getHeightAssigned() {
        return height; // returning hieght.
    }

    /**
     * <p>This method can be used for setting the hieght.</p>
     * @param hieght The hieght to set.
     */
    public void setHeightAssigned(int height) {
        this.height = height; // Assigning to this.hieght.
    }

    private int height = 0;
   
   
    /**
     * Constructor for ImageButton.
     */
    public ImageButton(Bitmap backgroundImage, Bitmap pressedImage, int width, int height, long style) {
        super(style);
        this.backgroundImage = backgroundImage;
        this.pressedImage = pressedImage;
        this.width = width;
        this.height = height;
    }

   
    public int getPreferredHeight() {
        if(getHeightAssigned() <= 0)
            return this.backgroundImage.getHeight();
        return getHeightAssigned();
    }

    public int getPreferredWidth() {
        if(getWidthAssigned() <= 0)
            return this.backgroundImage.getWidth();
        return getWidthAssigned();
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#layout(int, int)
     */
    protected void layout(int width, int height) {
        setExtent(Math.min(width, getPreferredWidth()),
                Math.min(height, getPreferredHeight()));
    }

    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#paint(net.rim.device.api.ui.Graphics)
     */
    protected void paint(Graphics graphics) {
        if ((isFocus() || isSelected()) && pressedImage != null) {
            graphics.drawBitmap(0, (getPreferredHeight() - pressedImage.getHeight())/2, getPreferredWidth(), getPreferredHeight(),
                    pressedImage, 0, 0);
        } else if (backgroundImage != null) {
            graphics.drawBitmap(0, (getPreferredHeight() - pressedImage.getHeight())/2, getPreferredWidth(), getPreferredHeight(),
                    backgroundImage, 0, 0);
        } else {
            graphics.setColor(Color.WHITE);
            graphics.fillRoundRect(1, 1, getWidth() - 2, getHeight() - 2, 12,
                    12);
        }
    }
   
    protected void drawFocus(Graphics graphics, boolean on) {
    }
   
    /* (non-Javadoc)
     * @see net.rim.device.api.ui.Field#isFocusable()
     */
    public boolean isFocusable() {
        return focusable;
    }
   
    protected void onFocus(int direction) {
        //  int fieldIndex = getLastFocusedCBIndex();
       //     CustomField field = (CustomField)getField(fieldIndex);
           
        super.onFocus(direction);
        invalidate();
    }

    protected void onUnfocus() {
        super.onUnfocus();
        invalidate();
    }
   
    protected boolean navigationClick(int status, int time) {
        fieldChangeNotify(0);
        return true;
    }
   
    protected boolean keyChar(char character, int status, int time) {
        if (character == Keypad.KEY_ENTER) {
            fieldChangeNotify(0);
            return true;
        }
        return super.keyChar(character, status, time);
    }

    /**
     * <p>This is the method for .</p>
     */
    public void focusLost() {
        onUnfocus();
        invalidate();
    }
}

Coverflow

  public CoverFlowDemoScreen()
    {     
        setTitle("Picture Scroll Field Demo");      
       
        // Initialize an array of Bitmaps
        _bitmaps = new Bitmap[NUM_ENTRIES];
        ScrollEntry[] entries = new ScrollEntry[NUM_ENTRIES];
        for(int i=0;i<30;i++){
             _bitmaps[i] = Bitmap.getBitmapResource(String.valueOf(i+1)+".jpg");
             entries[i] = new ScrollEntry(_bitmaps[i], "BlackBerry", "");
        }
       

        // Initialize the picture scroll field
        _pictureScrollField = new PictureScrollField(107, 156);
        _pictureScrollField.setData(entries, 0);       
        _pictureScrollField.setHighlightStyle(HighlightStyle.ILLUMINATE);
        _pictureScrollField.setHighlightBorderColor(Color.RED);
        _pictureScrollField.setBackground(BackgroundFactory.createSolidBackground(Color.BLACK));
        _pictureScrollField.setLabelsVisible(true);
        _pictureScrollField.setLensShrink(0.4f);
        add(_pictureScrollField);
       
        // Initialize a choice field for highlight style selection
    }   
   

Split String

    public static String[] splitString(String message, String delimiter)
    {
        Vector result = new Vector();
        int pos = 0;
        int len = message.length();
        int lenD = delimiter.length();
        for (int i = 0; i < len; i++)
        {
            if (message.substring(i, i+lenD).equals(delimiter))
            {
                if (i == pos)
                    result.addElement("");
                else if (i>pos)
                    result.addElement(message.substring(pos, i));
                pos = i+lenD;
                i += lenD-1;
            }
        }
        if (pos < len)
            result.addElement(message.substring(pos, len));
       
        String[] reposne = new String[result.size()];
        result.copyInto(reposne);
        return reposne;
    }

Trimmed Text With Respect to Screen Size

public static String getTrimmedTextRespectToScreen(String input, Font font, int widthAvailable){
        if(font.getAdvance(input) > widthAvailable){
                 int length = input.length();
            widthAvailable -= font.getAdvance("...");
            for (int i = 1; i < length; i++) {
                if(font.getAdvance(input, 0, i) >= widthAvailable){
                    return input.substring(0, i-1).trim() + "..";
                }
            }
        }
        return input;
    }

Copy File (SDCard)


    public static void copyFile(String sourceFile, String destinationFile)
    {
        try
        {
            FileConnection sourceFileConnector;
            FileConnection destinationFileConnector;

            sourceFileConnector = (FileConnection) Connector.open(sourceFile,Connector.READ_WRITE);
            destinationFileConnector = (FileConnection) Connector.open(destinationFile,Connector.READ_WRITE);
            if(!destinationFileConnector.exists()) // if file does not exists , create a new one
            {
                destinationFileConnector.create();
            }
            InputStream is = sourceFileConnector.openInputStream();
            OutputStream os =destinationFileConnector.openOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) > 0)
            {
                os.write(buf, 0, len);
            }
            is.close();
            os.close();
        }
        catch(IOException e)
        {

        }
    }