CheckboxField checked in Blackberry - blackberry

How will i be able to recognise a check on the Checkboxfield.
Like if a user checks on a check box, a notification is generated.
BR,
Suppi

CheckBoxField class inherited from Field class, which means you should be able to create an action listener.
Use setActionListener method and override the fieldChanged method to do whatever you want.
checkBox.setChangeListener(new FieldChangeListener() {
public void fieldChanged(...params ... ) {
//your code
}
});

Related

VaadinSession attribute and updating session-bound components

I have a Vaadin Navigator with multiple View elements. Each view has a different purpose however some also contain common traits that I have put inside custom components.
One of those custom components is the menu - it is positioned at the top and allows navigation between the different views. I create and add this component inside the constructor of each view (if you are interested in the menu's implementation see the end of this post). Here is a skeleton for each custom view:
class MyViewX implements View {
MenuViewComponent mvc;
public MyViewX() {
mvc = new MenuViewComponent();
addComponent(mvc);
}
#Override
public void enter(ViewChangeEvent event) {
}
}
So far, so good. In order to make things simple I will explain my problem using a simple label and not one of my other custom components but the dependency that I will describe here is the same for those components just like with the label.
Let's say I have a label which sole purpose is to display a greeting with the user's username. In order to do that I use VaadinSession where I store the attribute. This is done by my LoginController, which validates the user by looking into a database and if the user is present, the attribute is set and one of the views is opened automatically. The problem is that VaadinSession.getCurrent().getAttribute("username") returns null when called inside the constructor. This of course makes sense omho because a constructor should not be bound by a session-attribute.
So far I have managed to use the enter() method where there is no problem in retrieving session attributes:
class MyViewX implements View {
MenuViewComponent mvc;
public MyViewX() {
mvc = new MenuViewComponent();
addComponent(mvc);
}
#Override
public void enter(ViewChangeEvent event) {
String username = (String)VaadinSession.getCurrent().getAttribute("username");
Label greeting = new Label("Hello " + username);
addComponent(greeting);
}
}
The issue that comes from this is obvious - whenever I open the view where this label is present, a new label is added so if I re-visit the view 10 times, I will get 10 labels. Even if I move the label to be a class member variable the addComponent(...) is the one that screws things up. Some of my custom components really depend on the username attribute (in order to display user-specific content) hence I also have to place those in the enter(...) method. The addComponent(...) makes a mess out of it. I even tried the dirty way of removing a component and then re-adding it alas! in vain:
class MyViewX implements View {
MenuViewComponent mvc;
Label greeting;
public MyViewX() {
mvc = new MenuViewComponent();
addComponent(mvc);
}
#Override
public void enter(ViewChangeEvent event) {
String username = (String)VaadinSession.getCurrent().getAttribute("username");
greeting = new Label("Hello " + username);
// Remove if present
try { removeComponent(greeting); }
catch(Exception ex) { }
// Add again but with new content
addComponent(greeting);
}
}
but it's still not working. So my question is: what is the simplest way of updating a component that requires session-bound attributes?
The navigation via the menu custom component is omho not the issue here since all components of the menu are loaded in it's constructor. That's why it's also load that component in particular in a view's own constructor. Here is an example of a button in my menu that opens a view:
#SuppressWarnings("serial")
#PreserveOnRefresh
public class MenuViewComponent extends CustomComponent {
public MenuViewComponent(boolean adminMode) {
HorizontalLayout layout = new HorizontalLayout();
Label title = new Label("<h2><b>Vaadin Research Project</b></h2>");
title.setContentMode(ContentMode.HTML);
layout.addComponent(title);
layout.setComponentAlignment(title, Alignment.TOP_LEFT);
Button personalDashboardButton = new Button("Personal dashboard", new Button.ClickListener() {
#Override
public void buttonClick(ClickEvent event) {
getUI().getNavigator().navigateTo(MainController.PERSONALDASHBOARDVIEW);
}
});
personalDashboardButton.setStyleName(BaseTheme.BUTTON_LINK);
layout.addComponent(personalDashboardButton);
layout.setComponentAlignment(personalDashboardButton, Alignment.TOP_CENTER);
// Add other buttons for other views
layout.setSizeUndefined();
layout.setSpacing(true);
setSizeUndefined();
setCompositionRoot(layout);
}
}
PERSONALDASHBOARDVIEW is just one of the many views I have.
It may be worth considering how long should your view instances "live", just as long they're displayed, until the session ends or a mix of the two. With this in mind and depending on what needs to happen when you enter/re-enter a view, you have at least the following 3 options:
1) Recreate the whole view (allowing for early view garbage-collection)
first register a ClassBasedViewProvider (instead of a StaticViewProvider) which does not hold references to the created views:
navigator = new Navigator(this, viewDisplay);
navigator.addProvider(new Navigator.ClassBasedViewProvider(MyView.NAME, MyView.class));
simple view implementation
public class MyView extends VerticalLayout implements View {
public static final String NAME = "myViewName";
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
// initialize tables, charts and all the other cool stuff
addComponent(new SweetComponentWithLotsOfStuff());
}
}
2) Keep some already created components and replace others
public class MyView extends VerticalLayout implements View {
private MySweetComponentWithLotsOfStuff mySweetComponentWithLotsOfStuff;
public MyView() {
// initialize only critical stuff here or things that don't change on enter
addComponent(new MyNavigationBar());
}
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
// oh, so the user does indeed want to see stuff. great, let's do some cleanup first
removeComponent(mySweetComponentWithLotsOfStuff);
// initialize tables, charts and all the other cool stuff
mySweetComponentWithLotsOfStuff = new SweetComponentWithLotsOfStuff();
// show it
addComponent(mySweetComponentWithLotsOfStuff);
}
}
3) Lazy creating and updating (or not) the content when entering
public class MyView extends VerticalLayout implements View {
private boolean isFirstDisplay = true;
private MySweetComponentWithLotsOfStuff mySweetComponentWithLotsOfStuff;
public MyView() {
// initialize only critical stuff here, as the user may not even see this view
}
#Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
// oh, so the user does indeed want to see stuff
if (isFirstDisplay) {
isFirstDisplay = false;
// lazily initialize tables, charts and all the other cool stuff
mySweetComponentWithLotsOfStuff = new SweetComponentWithLotsOfStuff();
addComponent(mySweetComponentWithLotsOfStuff);
} else {
// maybe trigger component updates, or simply don't do anything
mySweetComponentWithLotsOfStuff.updateWhateverIsRequired();
}
}
}
I'm sure (and curious) that there may be other options, but I've mainly used a variation of 1) using spring with prototype views and component tabs.

What is the Meaning of this code??specially View's role in it??it is an android code?

Code goes like this
Button next;
next =(Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
There is a button called "next", it is really a rename of the already existing button R.id.button1 . The method that listens to click events (setOnClickListener) has a method that gets called when the button is actually clicked (onClick), within "onClick(View v)" you want to put all the things that you want the program to do when the button is clicked.

how to disable the button using bitmapbuttonfield in advanced ui examples in blackberry

I am using bitmap button field in advanced ui examples.
By default no method is working for disabling the button in 5.0 jre,
so i have added the below code for disabling and
then disabling functionality of the button is working but setchangelistener is not working
that is my problem
.. here is my code that i added for disabling the button..please check. do i need to change anything in invoke action method?
public boolean isDisable() {
return isDisable;
}
public void setDisable(boolean isDisable) {
this.isDisable = isDisable;
invalidate();
}
public boolean isFocusable() {
return isFocusable && !isDisable;
}
public void setFocusable(boolean isFocusable) {
this.isFocusable = isFocusable;
}
protected boolean invokeAction(int action) {
if (!isDisable){
fieldChangeNotify(0);
}
return true;
}
public boolean setEnabled() {
return false;
}
Here is a discussion on the BlackBerry forums about this.
What I've sometimes done is actually make use of the isEditable() property on Field objects, since editability and being enabled are somewhat similar concepts. If you really want to keep the separate isDisabled() code, that's fine. Just substitute that below where I use isEditable() (remembering to reverse the boolean ... that's one reason to always program in the affirmative ... make your method isEnabled() instead of isDisabled()).
So, instead of any of the code you posted above, I would just add this code to either BitmapButtonField, or BaseButtonField:
public boolean isFocusable() {
return isEditable() && super.isFocusable();
}
and this in BitmapButtonField:
protected void paint( Graphics g ) {
int oldAlpha = g.getGlobalAlpha();
int index = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? FOCUS : NORMAL;
if (!isEditable()) {
g.setGlobalAlpha(100); // alpha is 0 to 255, so this is 100/255
}
g.drawBitmap( 0, 0, _bitmaps[index].getWidth(), _bitmaps[index].getHeight(), _bitmaps[index], 0, 0 );
g.setGlobalAlpha(oldAlpha);
}
And then, I can setup a change listener, or disable the button, like this in my manager class:
BitmapButtonField btn =
new BitmapButtonField(Bitmap.getBitmapResource("button.png"),
Bitmap.getBitmapResource("button-lit.png"));
btn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.alert("Button clicked!");
}
});
btn.setEditable(false); // this disables the button
add(btn);
But, understand, that if you disable a button, that means your change listener won't get called. That's kind of how it's supposed to work. The change listener is only called if the button's enabled and therefore clickable.
Also, note that in order to make the button look different when disabled (not editable), I override paint() to set a different alpha value when the button is disabled. You didn't mention that, so if you don't like it, you can certainly take it out.

How to push new screen from global screen in blackberry?

Here I am display push notification in globalscreen in blackberry, I need to push screen by clicking OK button of the dialog. I want to start app by clicking the ok button.
Please help me.
Thanks in advance!
I'm not 100% sure I understand what you want, but if this doesn't work, just add a comment and I'll try to give you a better answer.
First, read this on pushing global screens
and this on performing actions after receiving global alerts
Your code, if I'm understanding correctly, should be similar to the second link's example.
Then, if you implement the DialogClosedListener, like in the second link, you might have something like this:
called from the background when you get notified:
Dialog myDialog = new Dialog(Dialog.D_OK_CANCEL, "Hello", Dialog.OK, null, 0);
myDialog.setDialogClosedListener(new MyListener());
UiApplication.getUiApplication().pushGlobalScreen(myDialog, 1, true);
implementation of your dialog listener:
private class MyListener implements DialogClosedListener {
public void dialogClosed(Dialog dialog, int choice) {
switch (choice) {
case Dialog.OK:
// ok clicked
UiApplication.getUiApplication().requestForeground();
break;
case Dialog.CANCEL:
// cancel clicked. or escape pressed
break;
default:
break;
}
}
}
And, then in your UiApplication class, you can respond to activation, which will happen if the user selects Ok from the Dialog:
public class MyApp extends UiApplication {
private boolean _nextScreenShowing = false;
public void activate() {
super.activate();
if (!_nextScreenShowing) {
pushScreen(new NextScreen());
_nextScreenShowing = true;
}
}
}
I show the _nextScreenShowing variable, just to make sure you think about whether pushing the next screen is appropriate. It probably won't be every time activate is called. You may need to keep track of that boolean flag by responding to the Application.deactivate() method, or maybe Screen.onExposed() or Screen.onObscured(). All that depends on how your app works.

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