How to make a blackberry BitmapField non focusable in runtime - blackberry

How to make a blackberry BitmapField non-focusable in runtime? I want make it dimmed based on a certain event.

Extend the BitmapField to override the isFocusable() method like this:
public class FocusableBitmapField extends BitmapField {
//Default value depending on whether you want it that way.
private boolean focusable = true;
public boolean isFocusable() {
return focusable;
}
public boolean setFocusable(boolean focusable) {
this.focusable = focusable;
}
}

Most of times this works:
field.setEditable(false);
You can also create a non-focusable field by passing the style flag Field.NON_FOCUSABLE or Field.FOCUSABLE to the constructor, but once instantiated you cannot change it's focusable state. Even if you could, then the field won't look "dimmed" or "disabled", but simply the focus will jump to the next focusable field after it. An example of this are non-focusable label fields.
UPDATE: This would work for built in fields like EditFields, Checkbox, RadioButtons, etc. In your case, this does not work since a BitmapField is not "editable", it's a read only field. You can make a trick like #adwiv answer shows, but the "disabled" or gray overlay you'll have to implement it yourself.

Related

Customize SmartGwt ListGrid dynamically for passwords

I have a com.smartgwt.client.widgets.grid.ListGrid for my configurations screen.
I have 3 ListGridFields name, value, isHidden.
I want to use PasswordItem if isHidden is true, and TextItem if isidden is false.
How can I customize the grid?
I tried with setEditorCustomizer, but it only works when I am editing a cell. In view mode I am able to see the text.
I don't think there's a way to do what you want (show the PasswordItem editor when visualizing the ListGrid's fields). As you already found out, setEditorCustomizer works only when in editing mode.
But you can mask the field values. Here is how to do it:
// very important for not having to set all fields all over again
// when the target field is customized
listGrid.setUseAllDataSourceFields(true);
// customize the isHidden field to make it respond to changes and
// hide/show the password field accordingly
ListGridField isHidden = new ListGridField("isHiddenFieldName");
isHidden.addChangedHandler(new ChangedHandler() {
#Override
public void onChanged(ChangedEvent event) {
// the name of this field has to match the name of the field you
// want to hide (as defined in your data source descriptor,
// ListGridField definition, etc).
ListGridField passwordField = new ListGridField("passwordFieldName");
if ((Boolean) event.getValue() == true) {
passwordField.setCellFormatter(new CellFormatter() {
#Override
public String format(Object value, ListGridRecord record, int rowNum, int colNum) {
return ((String) value).replaceAll(".", "*");
}
});
}
// you need to re-add here the isHidden field for the ChangeHandler to
// be present when recreating the ListGrid
listGrid.setFields(isHidden, passwordField);
listGrid.markForRedraw();
}
});
// add the customized field to the listGrid, so that we can have the
// desired ChangeHandler for the isHidden field
listGrid.setFields(isHidden);
Bear in mind that if you hide the value (or use a PassowrdItem), an 'expert' user could see the value, simply because the server is sending the value to the client.
If you actually have a security constraint, you may use DataSourceField.viewRequires, which accepts velocity expressions.

A ButtonField to be displayed on all the screens of blackberry

I am working on a blackberry app where one of the requirements is that a ButtonField has to be displayed on all the screens of the app? How can this be accomplished, since the ButtonField has to be added after 2-3 controls?
Toolbar
Logo
ButtonField (All the screens should have this button)
There are many, many ways to solve this problem. Without seeing a visual description of all your screens, it's a little difficult to know exactly which one will work best for you. But, here's one option:
Create a base class that extends MainScreen, and have that base class add the ButtonField, and make sure that all other fields are added above the buttonfield. You can do this by adding the button field in a footer, that is then aligned with the screen's bottom edge using MainScreen#setStatus(Field).
public class BaseScreen extends MainScreen {
private ButtonField _bottomButton;
/** Can only be called by screen subclasses */
protected BaseScreen() {
// call BaseScreen(long)
this(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
}
protected BaseScreen(long style) {
super(style);
_bottomButton = new ButtonField("Press Me!", ButtonField.CONSUME_CLICK | Field.FIELD_HCENTER);
// TODO: customize your button here ...
Manager footer = new HorizontalFieldManager(Field.USE_ALL_WIDTH);
// just use a vertical field manager to center the bottom button horizontally
Manager spacerVfm = new VerticalFieldManager(Field.USE_ALL_WIDTH | Field.FIELD_HCENTER);
spacerVfm.add(_bottomButton);
footer.add(spacerVfm);
setStatus(footer);
}
/** #return the bottom button, if any subclasses need to access it */
protected ButtonField getBottomButton() {
return _bottomButton;
}
}
Here is then an example of how you'll build all your other screens:
public class BaseTestScreen extends BaseScreen implements FieldChangeListener {
public BaseTestScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
HorizontalFieldManager toolbar = new HorizontalFieldManager();
toolbar.add(new ButtonField("One", ButtonField.CONSUME_CLICK));
toolbar.add(new ButtonField("Two", ButtonField.CONSUME_CLICK));
toolbar.add(new ButtonField("Three", ButtonField.CONSUME_CLICK));
add(toolbar);
BitmapField logo = new BitmapField(Bitmap.getBitmapResource("icon.png"));
add(logo);
// do this if you want each different screen to be able to handle the
// bottom button click event. not necessary ... you can choose to
// handle the click in the BaseScreen class itself.
getBottomButton().setChangeListener(this);
}
public void fieldChanged(Field field, int context) {
if (field == getBottomButton()) {
Dialog.alert("Button Clicked!");
}
}
}
As you can see, this makes it possible to handle the button click (differently) in each screen class you create. Or, you can choose to handle the clicks in the base class (BaseScreen). You'll have to decide which makes sense for you. If the same action is always taken when clicking the button, and the base screen doesn't need additional information to handle the click, then just handle the click in BaseScreen. If not, handle in the subclasses as I show.
P.S. One disadvantage of this approach is that it forces all your screen classes to extend a common base class (BaseScreen). In some situations, this can be limiting. However, on BlackBerry, it's not uncommon to have all your screens extend MainScreen anyway. That said, such an architectural decision is impossible for me to comment on without understanding more about your app.

blackberry jre4.5, highlight URL

I have an object that extends Field, I use the paint method to draw text using graphics.drawText. I want to have url, and when the user scrolls over it, it gets highlighted. and when clicked it invokes the browser to the link.
like the normal behaviour in the blackberry.
Please direct me.
Thanks,
Aly
You can use LabelField as a button like this:
LabelField labelField = new LabelField("label to show",FOCUSABLE){
protected boolean invokeAction(int action) {
if ( action == ACTION_INVOKE){
// browsing...
}
return super.invokeAction( action );
}
};

Vaadin addStyleName problem

I created a TextField with TextChangeListener. When user types in certain values (in this case 'admin') then addStyleName is invoked on that field and font color becomes red. But afterwards, the value is blank and each entered character is being cleared.
Here is the code of the application. Why after adding new style to TextField its value changes?
public class VaadintestApplication extends Application {
#Override
public void init() {
Window mainWindow = new Window("Vaadintest Application");
setTheme("test");
TextField textField = new TextField("username");
textField.setEnabled(true);
textField.setTextChangeEventMode(TextChangeEventMode.EAGER);
textField.addListener(new TextChangeListener() {
public void textChange(TextChangeEvent event) {
if ("admin".equals(event.getText())) {
((TextField) event.getComponent()).addStyleName("text-error");
} else {
((TextField) event.getComponent()).removeStyleName("text-error");
}
}
});
mainWindow.addComponent(textField);
setMainWindow(mainWindow);
}
}
I would guess that the following happens:
The style name change triggers a repaint on the server, causing the TextField component to be serialized again to the client
The client receives the serialization (the whole bloody thing, not just the changed parts, because that's how things work with Vaadin), and hence it changes the contents of the textfield, while ignoring any changes that are pending from the text change listener
Solutions:
Update the value of the TextField at the same time you add/remove the style name: ((TextField) event.getComponent()).setValue(event.getText())
Create a custom client side widget which extends VTextField and add the functionality there

How to add Click Event to BitmapField (fieldchangeListener)?

I'm working on adding a BitmapField to my Blackberry project.
I implemented my class with a FieldChangeListener and added the FieldChangeListener method to my class. I even added a setChangeListener to that particular Bitmap Field, but it's not responding to click events.
How do I fix this?
First, BitmapField is not focusable by default, so you'll need to subclass and override isFocusable to fix that. Then override navigationclick to fire a fieldChanged event. Code snippet for a minimum field:
import net.rim.device.api.ui.component.BitmapField;
public class ClickableBitmapField extends BitmapField {
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
}
In addition to this, you may want to provide some indication of when your field is in focus (unless you only care about touch-screen devices). The default implementation will just draw a highlight on any transparent areas of your bitmap. You can change this by overriding drawFocus, and maybe onFocus and onUnfocus to change the bitmap you display when the focus state changes.

Resources