Search in sources :

Example 51 with Form

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);
            }
        }
    }
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle)

Example 52 with Form

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;
}
Also used : BorderLayout(com.codename1.ui.layouts.BorderLayout)

Example 53 with Form

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();
    }
}
Also used : CodenameOneThread(com.codename1.impl.CodenameOneThread)

Example 54 with Form

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);
        }
    }
}
Also used : Transition(com.codename1.ui.animations.Transition) Animation(com.codename1.ui.animations.Animation) Dimension(com.codename1.ui.geom.Dimension)

Example 55 with Form

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);
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Aggregations

Form (com.codename1.ui.Form)90 ActionEvent (com.codename1.ui.events.ActionEvent)41 Component (com.codename1.ui.Component)38 BorderLayout (com.codename1.ui.layouts.BorderLayout)38 Container (com.codename1.ui.Container)26 ActionListener (com.codename1.ui.events.ActionListener)25 Dialog (com.codename1.ui.Dialog)21 Command (com.codename1.ui.Command)19 Hashtable (java.util.Hashtable)17 Label (com.codename1.ui.Label)14 Style (com.codename1.ui.plaf.Style)14 IOException (java.io.IOException)14 TextArea (com.codename1.ui.TextArea)13 Vector (java.util.Vector)12 Button (com.codename1.ui.Button)11 Graphics (com.codename1.ui.Graphics)9 RadioButton (com.codename1.ui.RadioButton)9 Animation (com.codename1.ui.animations.Animation)9 Image (com.codename1.ui.Image)8 UIManager (com.codename1.ui.plaf.UIManager)8