Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
geeneeus
Posts: 80
Registered: ‎09-12-2009
My Device: Bold 9700
My Carrier: Vodafone UK

Code to Share

[ Edited ]

Hi folks, I've been developing for the BlackBerry for the past year and think its an awesome market and enjoy contributing to the forums and helping folk out.

 

One thing that I recently made has been useful for me which I hope will be useful for everyone and would like to share is an InlineEditField which as the name suggests will allow someone to edit the field as if you would an excel cell where by clicking will open edit mode and clicking again will close edit mode.

 

Do with it what you will but I hope it helps and guides others. So extend, fix, update and share as you wish on this thread.

 

If you find anything wrong with the code let me know, since this is a stripped version of the customizations I previously had, it may have some issues.

 

 

import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.XYRect;
import net.rim.device.api.ui.component.AutoTextEditField;

public class InlineEditField extends AutoTextEditField {
private int FOCUS_COLOR = Color.WHITE; // Colour of text on focus;
private int UNFOCUS_COLOR = Color.BLACK; // Colour of text on unfocus;

public InlineEditField(String initialValue) {
super("", initialValue);
editOff();
setPadding(new XYEdges(0, 0, 0, 3)); // Make some space; (not required)
}

public int getPreferredWidth() {
return (Display.getWidth()/3);
}

public int getPreferredHeight() {
return Font.getDefault().getHeight(); // This will limit the field to only one line. This can be changed!
}

protected void layout(int width, int height) {
super.layout(getPreferredWidth(), getPreferredHeight());
super.setExtent(getPreferredWidth(), getPreferredHeight());
}

public boolean isFocusable() {
return true;
}

protected void drawFocus(Graphics graphics, boolean on) {
if(!isEditable()) {
/** Draw custom focus box; If you are unsure about how to do this then just ask and I will share the code. */
drawHighlightRegion(graphics, HIGHLIGHT_SELECT, false, 0, 0, getPreferredWidth(), getPreferredHeight());
} // Draws focus,
super.drawFocus(graphics, false);
if(isEditable()) {
/**
* You do not need to understand this code but this was required to
* draw a tidy focus area, you can edit this if you understand it.
*/
XYRect xy = new XYRect();
getFocusRect(xy);
graphics.copyArea(xy, 0, 0);
graphics.setBackgroundColor(Color.ORANGE);
graphics.clear(xy);
paint(graphics);
graphics.invert(xy);
}
}

protected boolean keyChar(char key, int status, int time) {
if(key == Keypad.KEY_ENTER) { // Turn off edit mode when the enter key is pressed; This can be changed to any key you desire.
editOff();
return true;
}
return super.keyChar(key, status, time);
}

protected void onFocus(int direction) {
invalidate();
super.onFocus(direction);
}

protected void onUnfocus() {
editOff(); // If focus is moved from this field then turn off edit mode;
invalidate();
super.onUnfocus();
}

/**
* Clicking on this field will turn on/off edit mode and update the UI as appropriate.
*/
protected boolean navigationClick(int status, int time) {
if(!isEditable()) {
editOn();
setCursorPosition(getTextLength());
} else {
editOff();
}
layout(getPreferredWidth(), getPreferredHeight());
updateLayout();
invalidate();
return true;
}

public void editOff() {
setEditable(false);
FieldChangeListener field = getChangeListener();
if(field != null) { // Reports to any change listener that the edit mode has changed. This can be useful but can be removed.
field.fieldChanged(this, EDIT_MODE_CHANGED);
}
}

public void editOn() {
setEditable(true);
FieldChangeListener field = getChangeListener();
if(field != null) { // Reports to any change listener that the edit mode has changed. This can be useful but can be removed.
field.fieldChanged(this, EDIT_MODE_CHANGED);
}
}

protected void paint(Graphics graphics) {
if(isFocus() && !isEditable())
graphics.setColor(FOCUS_COLOR); // Draw focus colour if focused and not in edit mode, can be adjusted as required;
else
graphics.setColor(UNFOCUS_COLOR);
super.paint(graphics);
}

public static final int EDIT_MODE_CHANGED = 21958731;
/** Randomly generated int value to indicate a change in EDIT MODE;
* Again you don't need to know the exact value but just use
* InlineEditField.EDIT_MODE_CHANGED to check to see if the edit mode
* was changed in the change listener that is set on this field.
*/
}

 

If you need any more explanations of this code then just ask.

 

 

Please share anything you have created that you think will help others (aslong as you are happy to share it) and I'll be sure to come back and share anything else that I have made that has been rather useful.

 

(Note: this is not limited to UI, you can share backend code that you find will be useful to others.)

Genius Development Scotland
Website: http://www.genius-dev.co.uk
Please use plain text.
New Developer
longmilk
Posts: 126
Registered: ‎06-09-2010
My Device: 9000
My Carrier: T Mobile

Re: Code to Share

[ Edited ]

Problem is:

 

cannot find symbol
symbol  : variable Util
location: class InlineEditField
                    Util.rScroll(this, dx, dy); // Implements a restricted scroll. This code will remain hidden but will be shared if requested.

Please use plain text.
Developer
geeneeus
Posts: 80
Registered: ‎09-12-2009
My Device: Bold 9700
My Carrier: Vodafone UK

Re: Code to Share

I've edited the code to fix that issue. You can remove that line as it is not necessary. Thanks for pointing that out! :smileyhappy:

Genius Development Scotland
Website: http://www.genius-dev.co.uk
Please use plain text.