How to create a custom dialog - blackberry

I need to create yes/no confirmation dialog in a foreign language. I think I need to create my own class by extending Dialog ?
Thanks

this should do the trick. My apologies to any Swedish chefs watching.
int answer = Dialog.ask("Gersh gurndy morn-dee burn-dee, burn-dee, flip-flip-flip-flip-flip-flip-flip-flip-flip?", new String[] {"Hokey dokey","Bork bork bork"}, new int[] {Dialog.OK,Dialog.CANCEL}, Dialog.CANCEL);
Edits:
The above explained better:
public final static int NEGATIVE = 0;
public final static int AFIRMATIVE = 1;
public final static int DEFAULT = NEGATIVE;
int answer = Dialog.ask("question?", new String[] {"afirmative button label", "negative button label"}, new int[] {AFIRMATIVE,NEGATIVE}, DEFAULT);
As you can see from the above it is possible to change all the text (language) values on a Dialog just by using this method so you shouldn't need a custom class to create a Dialog in another language.
It's even simpler if you use the standard BB localizing approach the simpler method (Dialog.ask(res.getString(SOMEQUESTION)) will automatically have it's afirmative and negative buttons adjusted for the language set in the phones options. You will only need to add the question as a string resource.
You can find a list of valid methods and constructors here:
http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/ui/component/Dialog.html
More Edits below:
I thought my above answer was what you were after but if you do need to further customize the dialog in a new class you may do it like this:
public class MyDialogScreen extends MainScreen implements LocalResource {
private int exitState;
...
protected void sublayout( int width, int height ) {
setExtent( dialogWidth, dialogHeight );
setPosition( XPOS, YPOS );
layoutDelegate( dialogWidth, dialogHeight );
}
// do some stuff and assign exitState appropriately
// e.g. a button that sets exitState = 1 then pops this screen
// another button that sets exitState = 2 then pops this screen
...
public int getExitState()
{
return this.exitState;
}
In the above I've created a new screen and I have overridden the sublayout method to specify a custom width, height and xy positions in layoutDelegate. When you push this screen you will see it as a dialog like box above the previous screen on the stack at the XY positions you specified.
Make sure to use pushModal. This will allow you to access the getExitState method after the screen has been popped from the display stack.
E.g
MyDialogScreen dialog = new MyDialogScreen();
UiApplication.getUiApplication().pushModalScreen(dialog);
int result = dialog.getExitState();
Cheers
Ray

Related

Place .net control on the sheet

I am using spreadsheetgear, and I want to place the combobox (ComponentOne) into 1 cell. I want that when the user go to this cell, this combobox will activate and show the list to user. After user chose item on the list, it will place this item into the cell value and hide the combobox.
How to do it in Spreadsheetgear.
Thanks,
Doit
I am not familiar with ComponentOne controls, so cannot really speak for that portion of your question. However, regarding a more general approach to embedding custom controls onto a SpreadsheetGear WorkbookView UI control, this is possible by sub-classing the UIManager class, which would allow you to intercept the creation of existing shapes on a worksheet and replace them with your own custom controls.
Below is a simple example that demonstrates this with the Windows Forms WorkbookView control and a sub-class of SpreadsheetGear.Windows.Forms.UIManager. This example just replaces a rectangle AutoShape with a button. You could modify it to show a ComponentOne CheckBox instead.
Note that the UIManager.CreateCustomControl(...) method gets called anytime a shape is scrolled into view / made visible on the WorkbookView. Also note that that your custom control will be disposed of every time it is scrolled out of view or otherwise made invisible. Please see the documentation for more details on this API.
Another important point about shapes and worksheets in general--shapes are not embedded inside a cell. Instead they hover over cells. So there will be no explicit "link" between a given shape and a given cell. The closest you could come to making such an association is with the IShape.TopLeftCell or BottomRightCell properties, which will provide the ranges for which this shape's respective edges reside over. The IShape interface contains a number of other API that you might find useful in your use-case. For instance, you can hide a shape by setting the IShape.Visible property to false.
using System;
using System.Windows.Forms;
using SpreadsheetGear;
using SpreadsheetGear.Shapes;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Create the UIManager replacement.
new MyUIManager(workbookView1.ActiveWorkbookSet);
}
private void buttonRunSample_Click(object sender, EventArgs e)
{
// NOTE: Must acquire a workbook set lock.
workbookView1.GetLock();
try
{
// Get a reference to the active worksheet and window information.
IWorksheetWindowInfo windowInfo = workbookView1.ActiveWorksheetWindowInfo;
IWorksheet worksheet = workbookView1.ActiveWorksheet;
// Get a reference to a cell.
IRange cell = workbookView1.ActiveWorksheet.Cells["B2"];
// Add a placeholder shape to the worksheet's shape collection.
// This shape will be replaced with a custom control.
double left = windowInfo.ColumnToPoints(cell.Column) + 5;
double top = windowInfo.RowToPoints(cell.Row) + 5;
double width = 100;
double height = 30;
IShape shape = worksheet.Shapes.AddShape(AutoShapeType.Rectangle, left, top, width, height);
// Set the name of the shape for identification purposes.
shape.Name = "MyCustomControl";
}
finally
{
// NOTE: Must release the workbook set lock.
workbookView1.ReleaseLock();
}
buttonRunSample.Enabled = false;
}
// UIManager replacement class.
private class MyUIManager : SpreadsheetGear.Windows.Forms.UIManager
{
private Button _customControl;
public MyUIManager(IWorkbookSet workbookSet)
: base(workbookSet)
{
_customControl = null;
}
// Override to substitute a custom control for any existing shape in the worksheet.
// This method is called when a control is first displayed within the WorkbookView.
public override System.Windows.Forms.Control CreateCustomControl(IShape shape)
{
// If the shape name matches...
if (String.Equals(shape.Name, "MyCustomControl"))
{
// Verify that a control does not already exist.
System.Diagnostics.Debug.Assert(_customControl == null);
// Create a custom control and set various properties.
_customControl = new Button();
_customControl.Text = "My Custom Button";
// Add a Click event handler.
_customControl.Click += new EventHandler(CustomControl_Click);
// Add an event handler so that we know when the control
// has been disposed. The control will be disposed when
// it is no longer in the viewable area of the WorkbookView.
_customControl.Disposed += new EventHandler(CustomControl_Disposed);
return _customControl;
}
return base.CreateCustomControl(shape);
}
private void CustomControl_Click(object sender, EventArgs e)
{
MessageBox.Show("Custom Control was Clicked!");
}
private void CustomControl_Disposed(object sender, EventArgs e)
{
// Add any cleanup code here...
// Set the custom control reference to null.
_customControl = null;
}
}
}

Horizontally centering a popup window in Vaadin

I have added a popup window to my main UI as follows:
Window component = new Window();
UI.getCurrent().addWindow(component);
Now, I want my popup to be centered horizontally and e.g. 40 pixels from the top of the screen. As far as I can see Vaadin has 4 methods for positioning my window.
component.center()
component.setPosition(x, y)
component.setPositionX(x)
component.setPositionY(y)
None of these are really what I want. I was hoping at first that setPositionY might help me. This does allow me to get the right distance from the top, but the x-position is now set to 0, where I wanted it to be centered.
The setPosition might have helped if I was able to calculate what the x-position should be, but this would require me to know the width of the component in pixels, but component.getWidth just tells me 100%.
Next I tried to use CSS styling on the component, writing and explicit css rule and adding it to the component with addStyleName. It seems though that Vaadin overrides whatever I wrote in my css with its own defaults...
Any ideas how to get my Window component positioned correctly?
I used the methods getBrowserWindowWidth() and getBrowserWindowHeight() from the com.vaadin.server.Page class for this.
I centered my "log" window horizontally in the lower part of the browser window with
myWindow.setHeight("30%");
myWindow.setWidth("96%");
myWindow.setPosition(
(int) (Page.getCurrent().getBrowserWindowWidth() * 0.02),
(int) (Page.getCurrent().getBrowserWindowHeight() * 0.65)
);
Solution 1: Use SizeReporter
Indeed, setPositionY() will reset the window's centered property to false. As the width of your pop-up and that of your browser window are not know before they appear on the screen, the only way I know to get those values is to use the SizeReporter add-on. Its use is quite straightforward:
public class MyUI extends UI {
private Window popUp;
private SizeReporter popUpSizeReporter;
private SizeReporter windowSizeReporter;
#Override
protected void init(VaadinRequest request) {
Button button = new Button("Content button");
VerticalLayout layout = new VerticalLayout(button);
layout.setMargin(true);
popUp = new Window("Pop-up", layout);
popUp.setPositionY(40);
addWindow(popUp);
popUpSizeReporter = new SizeReporter(popUp);
popUpSizeReporter.addResizeListenerOnce(this::centerPopUp);
windowSizeReporter = new SizeReporter(this);
windowSizeReporter.addResizeListenerOnce(this::centerPopUp);
}
private void centerPopUp(ComponentResizeEvent event) {
int popUpWidth = popUpSizeReporter.getWidth();
int windowWidth = windowSizeReporter.getWidth();
if (popUpWidth == -1 || windowWidth == -1) {
return;
}
popUp.setPositionX((windowWidth - popUpWidth) / 2);
}
}
This piece of code will be okay as long as you don't resize the pop-up. If you do, it will not be automatically recentered. If you replace addResizeListenerOnce() by addResizeListener() then it will automatically recenter the pop-up but you'll get some "UI glitches" as the add-on sends resize events almost continually while you're resizing your pop-up...
You could try to do it using CSS, but I personally avoid CSS as much as I can with Vaadin :).
You'll need to recompile the widgetset after you've added the add-on as a dependency.
Solution 2: Use com.vaadin.ui.JavaScript
I won't vouch for the portability of this solution but I guess it will work on most modern browsers.
public class MyUI extends UI {
private Window popUp;
#Override
protected void init(VaadinRequest request) {
Button button = new Button("Content button");
VerticalLayout layout = new VerticalLayout(button);
layout.setMargin(true);
popUp = new Window("Pop-up", layout);
popUp.setPositionY(40);
popUp.addStyleName("window-center");
addWindow(popUp);
// Add a JS function that can be called from the client.
JavaScript.getCurrent().addFunction("centerWindow", args -> {
popUp.setPositionX((int) ((args.getNumber(1) - args.getNumber(0)) / 2));
});
// Execute the function now. In real code you might want to execute the function just after the window is displayed, probably in your enter() method.
JavaScript.getCurrent().execute("centerWindow(document.getElementsByClassName('window-center')[0].offsetWidth, window.innerWidth)");
}
}

Vaadin focus Grid inside Panel

I have two Grids (both in its own panel), and want to navigate between them using the Tab Key.
To do that I'm trying to focus the Grid inside a Panel (If Tab is pressed, the Grid should gain focus, so I can use the up/Down key to select Items).
Vaadin doesn't provide a .focus() method for Grid. Is there any solution so I can focus the Grid anyway?
Here is small example which shows working scenario with
Tab key pressed
Arrows down/up should points to a row (exactly in Valo this is presented as contour around one cell)
Space makes row selected (if Grid has enabled selection!) - row should be highlighted.
Code example:
#Theme ( ValoTheme.THEME_NAME )
public class MyUI extends UI {
public class A {
String a;
String b;
A(String a, String b) {
this.a = a;
this.b = b;
}
// getters & setters
}
#Override
protected void init ( VaadinRequest vaadinRequest )
{
Grid g = new Grid();
List<A> list = Arrays.asList(new A("a", "b"), new A("aa", "bb"),
new A("aaa", "bbb"));
BeanItemContainer<A> items = new BeanItemContainer<>(A.class, list);
g.setContainerDataSource(items);
Panel p = new Panel(g);
setContent(p);
}
}
Tested: Vaadin 7.5, Java 8, Tomcat 8.
You could try to use:
setFocusedComponent(p);
after setContent(p). This should exactly tells Vaadin to make panel focused. But you still must press tab - once or more (depending on rest of components, which you placed on screen).
But make sure:
Grid is selectable.
Maybe you should press Tab more than once.
Depending on Theme there could be different effects of getting focus (or even select state). It is also possible that you use some predefined project which has blocked grid css to make it lighter. So check if you can highlight one row by click on it.
Without more information I can't help more.
The OP write in an edit:
Solved the problem using Javascript/Jquery. Added this to my Panel that contains the Grid:
public class FileTable extends Panel
{
String id;
public FileTable(String id)
{
this.id=id;
Grid table = new Grid();
initGrid();
fileTable.setId(id);
}
public void focus()
{
JavaScript.getCurrent().execute("$(\"#"+id+" table:first td:first\").click();");
}
}

how to set width of DropDown window in object choice field? customized

i am using an ObjectChoiceField in blackberry now. I have items in String array like "a", "abc", "abcdefg" etc. The application automatically sets the width based on the width of the choice items. I want the dropdown window in ChoiceField having width static which can hold the largest entry in the string array.
My question is, Can i set the dropdown window size? if yes, How could i set the width of the DropDown window (which displays all choice items of your choice field)?
Your question is not clear. In the current implementation, the drop down size is the size of the largest elements.
My guess is you want the size of the "highlighted" element after selection to be of the size of largest element?
And here it is smaller
Clarify what exactly you intend to do?
i had created a custom choice field..
import net.rim.device.api.ui.component.ObjectChoiceField;
/*
* The MyChoiceField is actually an extended class of ObjectChoiceField.
* In this, the design of an ObjectChoiceField is overloaded with providing some additional
* tasks is done. It includes a constructor and a Layout methods.
* In that Layout method the design for the ObjectChoiceField is overrided...
*/
public class MyChoiceField extends ObjectChoiceField
{
public MyChoiceField(String label ,Object[] choices)
{
super(label, choices);
}
protected void layout(int width, int height)
{
setMinimalWidth(width/2-62);
super.layout(width, height);
}
}
Now i got it...
thanks....

Add vertical spacing between fields on VerticalFieldManager

I am adding fields to a VerticalFieldManager. Is there a method of adding vertical spacing between fields?
There's several solutions to this, one being that you can create a custom field to be used as a spacer between your other fields.
private static class SpacerField extends Field
{
private int spacerWidth;
private int spacerHeight;
private SpacerField(int width, int height) {
spacerWidth = width;
spacerHeight = height;
}
protected void layout(int width, int height) {
setExtent(getPreferredWidth(), getPreferredHeight());
}
protected void paint(Graphics g) {
// nothing to paint; this is a blank field
}
public int getPreferredHeight() {
return spacerHeight;
}
public int getPreferredWidth() {
return spacerWidth;
}
}
//...
// Usage
add(new LabelField("Before Spacer"));
add(new SpacerField(0, 100));
add(new LabelField("After Spacer"));
Setting the padding or margins of your contained fields is another solution. It's up to you on what you think is the best way of managing things.
There are more elogant ways of doing this using the setPositionChild() methods but a simple work around is to give your fields padding using the setPadding(int top, int right, int bottom, int left) method.
myField.setPadding(5, 0, 5, 0);
I actually find that most times, what you want to do is call setMargin() on the vertical field manager's child fields, not sePadding(). I certainly think creating a SpacerField is now unnecessarily complex.
myField.setMargin(5, 0, 5, 0);
It depends on which kind of child field we're talking about, and how its size is specified, but in general, setPadding() will actually make the field bigger, which could have a visual impact, depending on how the field's background and border are drawn. setMargin() does not make the field any bigger, which I think is more consistent with what the question is asking.
See Mr Vincenzo's answer here, with pictures!
Note: setMargin() and setPadding() were officially added to the Field API relatively late in the BlackBerry Java evolution, but were available as undocumented methods before that.
Another Note: I have seen strange problems when using setMargin(), like the one described in this question. In that case, I did have to resort to one of the other two solutions here.

Resources