BlackBerry adding fields to MainScreen through another class - blackberry

I want to make a tab bar that will display at the bottom of a couple of MainScreens, and I thought creating a single class would be the right way to go.
The problem, not surprisingly since I'm new, is that when I try to add a field in my class how does it place it in the MainScreen? Do I have to send some sort of reference to the screen I want to work with?
package mypackage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.container.HorizontalFieldManager;
public class CustomTabBar {
private HorizontalFieldManager bar;
public CustomTabBar(){
}
public void buildBar(){
bar = new HorizontalFieldManager();
add(bar); //Where am I adding this?!
Bitmap eventIcon = Bitmap.getBitmapResource("eventsicon.png");
Bitmap eventIcon_hover = Bitmap.getBitmapResource("eventsicon-hover.png");
CustomMenuField eventIconField = new CustomMenuField(eventIcon_hover,eventIcon, "TheatreScreen");
}
}

If you want to add child objects directly to a MainScreen, then yes, you need to pass a reference to the MainScreen around, eg:
public class CustomTabBar {
private HorizontalFieldManager bar;
public void buildBar(MainScreen main) {
bar = new HorizontalFieldManager();
Bitmap eventIcon = Bitmap.getBitmapResource("eventsicon.png");
Bitmap eventIcon_hover = Bitmap.getBitmapResource("eventsicon-hover.png");
CustomMenuField eventIconField = new CustomMenuField(eventIcon_hover,eventIcon, "TheatreScreen");
bar.add(eventIconField);
main.add(bar);
}
}
.
public class MyMainScreen extends MainScreen {
private CustomTabBar tab;
public MyMainScreen() {
...
tab = new CustomTabBar();
tab.buildBar(this);
...
}
}
I think a better solution would be to change your class to extend from HorizontalFieldManager instead of encapsulating it, eg:
public class CustomTabBar extends HorizontalFieldManager {
public CustomTabBar(long style) {
super(style);
Bitmap eventIcon = Bitmap.getBitmapResource("eventsicon.png");
Bitmap eventIcon_hover = Bitmap.getBitmapResource("eventsicon-hover.png");
CustomMenuField eventIconField = new CustomMenuField(eventIcon_hover,eventIcon, "TheatreScreen");
add(eventIconField);
}
}
.
public class MyMainScreen extends MainScreen {
private CustomTabBar tab;
public MyMainScreen() {
...
tab = new CustomTabBar(...);
add(tab);
...
}
}

You need the MainScreen object, and you want to use setStatus(this) to put your tab bar at the bottom of the screen. The status section of the MainScreen stays visible, and is always at the bottom of the screen. The Banner is always at the top, and the Title is just below the banner. Everything else sits in a scrollable area between the title/banner and the status sections of the screen.

Related

Expanding a component in a PopupView - Vaadin

I am using Vaadin 8.5.1 with the Vaading Desiin combination with Spring Boot 2.0.4.
Currently I am trying to add a PopupView at the bottom of the page which opens on button click. In the Popup there is a vertical layout including two components: a HorizontalSplitPanel and a Button. The PopupView should have the width of the current BrowserWindow and one third of the height.
The HorizontalSplitPanel should use all the space in the popup, which is not needed for the button.
What I did:
#SpringComponent
#UIScope
public class View extends VerticalLayout implements Observer {
private final PopupContentView popupContentView;
private PopupView popup;
#Autowired
public View(PopupContentView popupContentView) {
this.popupContentView = popupContentView;
}
#PostConstruct
void init() {
button.addClickListener(clickEvent -> openPopup());
}
private void openPopup() {
if (popup == null) {
setSizeOfPopUp();
// popup will adjust automatically to size of content
popup = new PopupView(null, popupContentView);
popup.addPopupVisibilityListener(event -> {
if (event.isPopupVisible()) {
popupContentView.build(this::submitted);
}
});
popup.setHideOnMouseOut(false);
this.addComponent(popup);
}
popup.setPopupVisible(true);
}
private void setSizeOfPopUp() {
if (popupContentView != null) {
popupContentView.setWidth(Page.getCurrent().getBrowserWindowWidth(), Unit.PIXELS);
popupContentView.setHeight(((float) Page.getCurrent().getBrowserWindowHeight()) / 3, Unit.PIXELS);
}
}
private void submitted() {
// do some stuff
}
#Override
public void update(Observable observable, Object o) {
if (observable instanceof BrowserWindowResizeListenerObservable) {
setSizeOfPopUp();
}
}
}
#Service
public class BrowserWindowResizeListenerObservable extends Observable implements Page.BrowserWindowResizeListener {
#Override
public void browserWindowResized(Page.BrowserWindowResizeEvent browserWindowResizeEvent) {
this.setChanged();
this.notifyObservers();
}
}
#SpringComponent
#UIScope
public class PopupContentView extends VerticalLayout {
private SubmitCallback submitCallback;
private Button submitBtn;
#PostConstruct
void init() {
super.init();
}
void build(#NotNull SubmitCallback) {
removeAllComponents();
this.addComponent(horizontalSplitPanel);
this.addComponent(submitBtn);
this.setExpandRatio(horizontalSplitPanel, 1.0f);
this.submitCallback = callback;
}
private void submit() {
submitCallback.submit(someContent);
}
#FunctionalInterface
public interface SubmitCallback {
void submit(SomeContent someContent);
}
}
As you can see, I have a main view, a view for the content and a listener class.
What I want to happen is that the popup is visible on button click and contains the content view with the panel and the submit button. The panel takes the rest of the space, which is not needed for the button. and the popup is fully filled with content.
What actually happens is that the panel takes the full space of the popup and the button will be shown below the popup.
However, when I resize the window and the resizing event gets fired, everything is fine and the button is no longer below the popup.
It seems to be that the padding and the margin (which are the HTML implementation of the expand ratio in Vaadin) are calculated at an earlier stage and get triggered again when resizing the window. However, I have no clue when and what I need to do, to trigger it.
Does anyone have an idea, how can fix this?
EDIT:
When I have a Tree component or a DateField component in the PopupView and then expand a tree element or change the value of the DateField by selecting a value from the Date popup, the resizing is done correctly and everything is fine.
I think in your case the method of checking Browser window size and calculating target pixel size is too complex for your case. I would recommend just to set the width of the popup to be 100% and height to be 33%, like component.setHeight("33%"), yes you can use percentages for width and height instead of pixels. Then sizing is done by CSS, and it will react faster to browser window sizing without server round trip.

Xamarin.iOS: Create a custom button with an additional object

In my Xamarin.iOS application there are many buttons with the same scheme but different from to standart UIButton. I created one class for the buttons because most of the features are same but for example the textcolor or the backgroundcolor are different.
So how can I put an extra information about any button in the storyboard?
And how can I react on it in code?
You can make your custom element visible in for the designer with the DesignTimeVisible and the Register Attribute like
[Register ("CustomButton"), DesignTimeVisible (true)]
public class CustomButton: UIButton {
[Export ("CustomProperty "), Browsable (true)]
public int CustomProperty {get; set;}
public CustomButton(IntPtr handle) : base (handle) { }
public CustomButton()
{
// Called when created from code.
Initialize ();
}
public override void AwakeFromNib ()
{
// Called when loaded from xib or storyboard.
Initialize ();
}
void Initialize ()
{
// Common initialization code here.
CustomProperty = 0xB00B5;
}
}
For all properties that you want to set in the Designer you just hae to add Export and Browsable (true). In the Initializeyou can set all the vlaues of the common properties.
It will appear in the Toolbox under Custom Components. You might have to rebuild.
And the Custom Property can be modified in the Properties pane
More info: https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_overview/

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.

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.

How to invoke paint method in blackberry

How can i invoke paint multiple time in my app i tried invalidate but but it is not calling paint i think can anybody provide sample code for invoking paint multiple time.
package mypackage;
import com.rss.logger.Log;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
/**
* A class extending the MainScreen class, which provides default standard
* behavior for BlackBerry GUI applications.
*/
public final class MyScreen extends MainScreen
{
BitmapField objBitmapField;
boolean objBoolean;
int postion=0;
public MyScreen()
{
objBitmapField=new BitmapField(Bitmap.getBitmapResource("bb.png"));
// Set the displayed title of the screen
setTitle("MyTitle");
objBoolean=false;
new AnimationThread().start();
}
private class AnimationThread extends Thread{
public void run() {
super.run();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run()
{
objBoolean=true;
//Add a new LabelField to the screen.
//theScreen.add(new LabelField("Hello there.");
//Call the screen’s invalidate method to
//force the screen to redraw itself.
//Note that invalidate can be called
//at the screen, manager or field level,
//which means you can inform the
//BlackBerry to only redraw the area that
//has changed.
for (int i = 0; i < 20; i++) {
try {
sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.info("in run");
postion=postion+5;
MyScreen.this.invalidate();
}
}
});
}
}
protected void paint(Graphics graphics) {
super.paint(graphics);
Log.info("in paint");
if(objBoolean)
graphics.drawBitmap(20, postion, 50, 50, Bitmap.getBitmapResource("bb.png"), 30, 40);
}
}
Hi piyus find the working code. invalidate() need to call in the paint() method. And one thing is also important that should not create object in paint method.
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.container.MainScreen;
public final class MyScreen extends MainScreen
{
private boolean objBoolean=false;
private int postion=0;
private Bitmap bb=Bitmap.getBitmapResource("bb.png");
public MyScreen()
{
setTitle("MyTitle");
new AnimationThread().start();
}
private class AnimationThread extends Thread
{
public void run()
{
objBoolean=true;
for (int i = 0; i < 20; i++)
{
try
{
sleep(200);
}
catch (InterruptedException e)
{
}
postion=postion+5;
}
}
}
protected void paint(Graphics graphics)
{
super.paint(graphics);
if(objBoolean)
graphics.drawBitmap(20, postion, 50, 50, bb, 30, 40);
invalidate();
}
}
paint() method can be invoked either by using invalidate method or by creating an object of the class that you are using now.
I think there is Typo Error in the sample code you have mentioned here. One cannot have a class inside a class just by defining it. Coming to the issue you are facing
While, calling the invalidate method, as you are using it as MyScreen.this.invalidate which is not the right way to call it, one cannot call the paint method from another class, as in your case it is AnimationThread is not possible. The propreitary feature of Screen class and the paint method is, it gets called once the Object of the screen is created.
And as I understand one can call the invalidate method for a manager or a screen, inside the same class but not by creating an object of the screen in another class and by calling it.
It should be called simply without using any objects simply, invalidate(); , gets the screen repainted or invalidated.
One can also achieve it by creating an object of the same screen, may be if one wanted to display the screen in a different way, they can create an object by using different arguments, and handle it accordingly in the paint method.
Here, whenever the thread is run, you can create an object and handle it according to your choice.
As a sample :
public class theScreen extends MainScreen{
int dummy;
public theScreen(int firstArg){
// Handle the firstArgument here
dummy=0;
}
public theScreen(int secondArg,int dummy){
// Handle the second Argument here
this.dummy = dummy;
}
public void paint(Graphics graphcis)
{
if(dummy == 0)
{
//Handle what you would like to paint here
}
else{
//Handle with a different paint here
}
}}
Hope this resolves your problem
Happy Coding.
Cheers
You may try using Screen.doPaint().
Is your Bitmap big enough to be completely drawn starting from top/left (30, 40)? It may just be that it doesn't look like it's painting because you're outside of the bounds that make sense for it to be drawn.

Resources