use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Container method scrollComponentToVisible.
/**
* Makes sure the component is visible in the scroll if this container is
* scrollable
*
* @param c the component that will be scrolling for visibility
*/
public void scrollComponentToVisible(Component c) {
if (isScrollable()) {
if (c != null) {
Rectangle r = c.getVisibleBounds();
if (c.getParent() != null) {
// special case for the first component to allow the user to scroll all the
// way to the top
Form f = getComponentForm();
if (f != null && f.getInvisibleAreaUnderVKB() == 0 && f.findFirstFocusable() == c) {
// support this use case only if the component doesn't explicitly declare visible bounds
if (r == c.getBounds() && !Display.getInstance().isTouchScreenDevice()) {
scrollRectToVisible(new Rectangle(0, 0, c.getX() + Math.min(c.getWidth(), getWidth()), c.getY() + Math.min(c.getHeight(), getHeight())), this);
return;
}
}
}
boolean moveToVisible = true;
boolean large = c.getVisibleBounds().getSize().getHeight() > getHeight() || c.getVisibleBounds().getSize().getWidth() > getWidth();
if (large) {
int x = getScrollX();
int y = getScrollY();
int w = getWidth();
int h = getHeight();
boolean visible = contains(c) && Rectangle.intersects(c.getAbsoluteX(), c.getAbsoluteY(), c.getWidth(), c.getHeight(), getAbsoluteX() + x, getAbsoluteY() + y, w, h);
// if this is a big component no need to scroll to the begining if it's
// partially visible
moveToVisible = !visible;
}
if (moveToVisible) {
scrollRectToVisible(r.getX(), r.getY(), Math.min(r.getSize().getWidth(), getWidth()), Math.min(r.getSize().getHeight(), getHeight()), c);
}
}
}
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Dialog method show.
/**
* Shows a modal dialog with the given component as its "body" placed in the
* center.
*
* @param title title for the dialog
* @param body component placed in the center of the dialog
* @param defaultCommand command to be assigned as the default command or null
* @param cmds commands that are added to the form any click on any command
* will dispose the form
* @param type the type of the alert one of TYPE_WARNING, TYPE_INFO,
* TYPE_ERROR, TYPE_CONFIRMATION or TYPE_ALARM
* @param icon the icon for the dialog, can be null
* @param timeout a timeout after which null would be returned if timeout is 0 inifinite time is used
* @param transition the transition installed when the dialog enters/leaves
* @return the command pressed by the user
*/
public static Command show(String title, Component body, Command defaultCommand, Command[] cmds, int type, Image icon, long timeout, Transition transition) {
Dialog dialog = new Dialog(title);
dialog.dialogType = type;
dialog.setTransitionInAnimator(transition);
dialog.setTransitionOutAnimator(transition);
dialog.lastCommandPressed = null;
dialog.setLayout(new BorderLayout());
if (cmds != null) {
if (commandsAsButtons) {
dialog.placeButtonCommands(cmds);
} else {
for (int iter = 0; iter < cmds.length; iter++) {
dialog.addCommand(cmds[iter]);
}
}
// maps the first command to back
if (cmds.length == 1 || cmds.length == 2) {
dialog.setBackCommand(cmds[0]);
}
}
if (defaultCommand != null) {
dialog.setDefaultCommand(defaultCommand);
}
dialog.addComponent(BorderLayout.CENTER, body);
if (icon != null) {
dialog.addComponent(BorderLayout.EAST, new Label(icon));
}
if (timeout != 0) {
dialog.setTimeout(timeout);
}
if (body.isScrollable() || disableStaticDialogScrolling) {
dialog.setScrollable(false);
}
dialog.show();
return dialog.lastCommandPressed;
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Display method init.
/**
* This is the INTERNAL Display initialization method, it will be removed in future versions of the API.
* This method must be called before any Form is shown
*
* @param m platform specific object used by the implementation
* @deprecated this method is invoked internally do not invoke it!
*/
public static void init(Object m) {
if (!INSTANCE.codenameOneRunning) {
INSTANCE.codenameOneRunning = true;
INSTANCE.displayInitTime = System.currentTimeMillis();
// restore menu state from previous run if exists
int commandBehaviour = COMMAND_BEHAVIOR_DEFAULT;
if (INSTANCE.impl != null) {
commandBehaviour = INSTANCE.impl.getCommandBehavior();
}
INSTANCE.impl = (CodenameOneImplementation) ImplementationFactory.getInstance().createImplementation();
INSTANCE.impl.setDisplayLock(lock);
INSTANCE.impl.initImpl(m);
INSTANCE.codenameOneGraphics = new Graphics(INSTANCE.impl.getNativeGraphics());
INSTANCE.codenameOneGraphics.paintPeersBehind = INSTANCE.impl.paintNativePeersBehind();
INSTANCE.impl.setCodenameOneGraphics(INSTANCE.codenameOneGraphics);
// only enable but never disable the third softbutton
if (INSTANCE.impl.isThirdSoftButton()) {
INSTANCE.thirdSoftButton = true;
}
if (INSTANCE.impl.getSoftkeyCount() > 0) {
MenuBar.leftSK = INSTANCE.impl.getSoftkeyCode(0)[0];
if (INSTANCE.impl.getSoftkeyCount() > 1) {
MenuBar.rightSK = INSTANCE.impl.getSoftkeyCode(1)[0];
if (INSTANCE.impl.getSoftkeyCode(1).length > 1) {
MenuBar.rightSK2 = INSTANCE.impl.getSoftkeyCode(1)[1];
}
}
}
MenuBar.backSK = INSTANCE.impl.getBackKeyCode();
MenuBar.backspaceSK = INSTANCE.impl.getBackspaceKeyCode();
MenuBar.clearSK = INSTANCE.impl.getClearKeyCode();
INSTANCE.PATHLENGTH = INSTANCE.impl.getDragPathLength();
INSTANCE.dragPathX = new float[INSTANCE.PATHLENGTH];
INSTANCE.dragPathY = new float[INSTANCE.PATHLENGTH];
INSTANCE.dragPathTime = new long[INSTANCE.PATHLENGTH];
com.codename1.util.StringUtil.setImplementation(INSTANCE.impl);
com.codename1.io.Util.setImplementation(INSTANCE.impl);
// generally its probably a bug but we can let it slide...
if (INSTANCE.edt == null) {
INSTANCE.touchScreen = INSTANCE.impl.isTouchDevice();
// initialize the Codename One EDT which from now on will take all responsibility
// for the event delivery.
INSTANCE.edt = new CodenameOneThread(new RunnableWrapper(null, 3), "EDT");
INSTANCE.impl.setThreadPriority(INSTANCE.edt, INSTANCE.impl.getEDTThreadPriority());
INSTANCE.edt.start();
}
INSTANCE.impl.postInit();
INSTANCE.setCommandBehavior(commandBehaviour);
} else {
INSTANCE.impl.confirmControlView();
}
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Display method setCurrent.
/**
* Displays the given Form on the screen.
*
* @param newForm the Form to Display
*/
void setCurrent(final Form newForm, boolean reverse) {
if (edt == null) {
throw new IllegalStateException("Initialize must be invoked before setCurrent!");
}
Form current = impl.getCurrentForm();
if (autoFoldVKBOnFormSwitch && !(newForm instanceof Dialog)) {
setShowVirtualKeyboard(false);
}
if (current == newForm) {
current.revalidate();
current.repaint();
current.onShowCompletedImpl();
return;
}
if (impl.isEditingText()) {
switch(showDuringEdit) {
case SHOW_DURING_EDIT_ALLOW_DISCARD:
break;
case SHOW_DURING_EDIT_ALLOW_SAVE:
impl.saveTextEditingState();
break;
case SHOW_DURING_EDIT_EXCEPTION:
throw new IllegalStateException("Show during edit");
case SHOW_DURING_EDIT_IGNORE:
return;
case SHOW_DURING_EDIT_SET_AS_NEXT:
impl.setCurrentForm(newForm);
return;
}
}
if (!isEdt()) {
callSerially(new RunnableWrapper(newForm, null, reverse));
return;
}
if (current != null) {
if (current.isInitialized()) {
current.deinitializeImpl();
} else {
Form fg = getCurrentUpcoming();
if (fg != current) {
if (fg.isInitialized()) {
fg.deinitializeImpl();
}
}
}
}
if (!newForm.isInitialized()) {
newForm.initComponentImpl();
}
if (newForm.getWidth() != getDisplayWidth() || newForm.getHeight() != getDisplayHeight()) {
newForm.setSize(new Dimension(getDisplayWidth(), getDisplayHeight()));
newForm.setShouldCalcPreferredSize(true);
newForm.layoutContainer();
} else {
// if shouldLayout is true
newForm.layoutContainer();
}
boolean transitionExists = false;
if (animationQueue != null && animationQueue.size() > 0) {
Object o = animationQueue.get(animationQueue.size() - 1);
if (o instanceof Transition) {
current = (Form) ((Transition) o).getDestination();
impl.setCurrentForm(current);
}
}
if (current != null) {
// to the correct parent!
if (current instanceof Dialog && ((Dialog) current).isMenu()) {
Transition t = current.getTransitionOutAnimator();
if (t != null) {
// go back to the parent form first
if (((Dialog) current).getPreviousForm() != null) {
initTransition(t.copy(false), current, ((Dialog) current).getPreviousForm());
}
}
current = ((Dialog) current).getPreviousForm();
impl.setCurrentForm(current);
}
// prevent the transition from occurring from a form into itself
if (newForm != current) {
if ((current != null && current.getTransitionOutAnimator() != null) || newForm.getTransitionInAnimator() != null) {
if (animationQueue == null) {
animationQueue = new ArrayList<Animation>();
}
// transitions which are a bit sensitive
if (current != null && (!(newForm instanceof Dialog))) {
Transition t = current.getTransitionOutAnimator();
if (current != null && t != null) {
transitionExists = initTransition(t.copy(reverse), current, newForm);
}
}
if (current != null && !(current instanceof Dialog)) {
Transition t = newForm.getTransitionInAnimator();
if (t != null) {
transitionExists = initTransition(t.copy(reverse), current, newForm);
}
}
}
}
}
synchronized (lock) {
lock.notify();
}
if (!transitionExists) {
if (animationQueue == null || animationQueue.size() == 0) {
setCurrentForm(newForm);
} else {
// we need to add an empty transition to "serialize" this
// screen change...
Transition t = CommonTransitions.createEmpty();
initTransition(t, current, newForm);
}
}
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Form method showModal.
/**
* This method shows the form as a modal alert allowing us to produce a behavior
* of an alert/dialog box. This method will block the calling thread even if the
* calling thread is the EDT. Notice that this method will not release the block
* until dispose is called even if show() from another form is called!
* <p>Modal dialogs Allow the forms "content" to "hang in mid air" this is especially useful for
* dialogs where you would want the underlying form to "peek" from behind the
* form.
*
* @param top space in pixels between the top of the screen and the form
* @param bottom space in pixels between the bottom of the screen and the form
* @param left space in pixels between the left of the screen and the form
* @param right space in pixels between the right of the screen and the form
* @param includeTitle whether the title should hang in the top of the screen or
* be glued onto the content pane
* @param modal indictes if this is a modal or modeless dialog true for modal dialogs
*/
void showModal(int top, int bottom, int left, int right, boolean includeTitle, boolean modal, boolean reverse) {
Display.getInstance().flushEdt();
if (previousForm == null) {
previousForm = Display.getInstance().getCurrent();
// special case for application opening with a dialog before any form is shown
if (previousForm == null) {
previousForm = new Form();
previousForm.show();
} else {
if (previousForm instanceof Dialog) {
Dialog previousDialog = (Dialog) previousForm;
if (previousDialog.isDisposed()) {
previousForm = Display.getInstance().getCurrentUpcoming();
}
}
}
}
previousForm.tint = true;
Painter p = getStyle().getBgPainter();
if (top > 0 || bottom > 0 || left > 0 || right > 0) {
if (!title.isVisible()) {
includeTitle = false;
}
Style titleStyle = title.getStyle();
titleStyle.removeListeners();
Style contentStyle = contentPane.getUnselectedStyle();
contentStyle.removeListeners();
if (includeTitle) {
titleStyle.setMargin(Component.TOP, top, false);
titleStyle.setMargin(Component.BOTTOM, 0, false);
titleStyle.setMargin(Component.LEFT, left, false);
titleStyle.setMargin(Component.RIGHT, right, false);
contentStyle.setMargin(Component.TOP, 0, false);
contentStyle.setMargin(Component.BOTTOM, bottom, false);
contentStyle.setMargin(Component.LEFT, left, false);
contentStyle.setMargin(Component.RIGHT, right, false);
} else {
titleStyle.setMargin(Component.TOP, 0, false);
titleStyle.setMargin(Component.BOTTOM, 0, false);
titleStyle.setMargin(Component.LEFT, 0, false);
titleStyle.setMargin(Component.RIGHT, 0, false);
contentStyle.setMargin(Component.TOP, top, false);
contentStyle.setMargin(Component.BOTTOM, bottom, false);
contentStyle.setMargin(Component.LEFT, left, false);
contentStyle.setMargin(Component.RIGHT, right, false);
}
titleStyle.setMarginUnit(null);
contentStyle.setMarginUnit(null);
initDialogBgPainter(p, previousForm);
revalidate();
}
initFocused();
if (getTransitionOutAnimator() == null && getTransitionInAnimator() == null) {
initLaf(getUIManager());
}
initComponentImpl();
Display.getInstance().setCurrent(this, reverse);
onShow();
if (modal) {
// called to display a dialog and wait for modality
Display.getInstance().invokeAndBlock(new RunnableWrapper(this, p, reverse));
// if the virtual keyboard was opend by the dialog close it
Display.getInstance().setShowVirtualKeyboard(false);
}
}
Aggregations