Search in sources :

Example 81 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class SideMenuBar method createMenu.

private Form createMenu(final String placement) {
    final Form m = new Form() {

        private boolean pressedInRightPanel;

        private boolean manualMotionLock;

        protected boolean shouldSendPointerReleaseToOtherForm() {
            return true;
        }

        void actionCommandImpl(Command cmd, ActionEvent ev) {
            if (cmd instanceof SideMenuBar.CommandWrapper) {
                cmd = ((SideMenuBar.CommandWrapper) cmd).cmd;
                ev = new ActionEvent(cmd, ActionEvent.Type.Command);
            }
            final Command c = cmd;
            final ActionEvent e = ev;
            Display.getInstance().scheduleBackgroundTask(new Runnable() {

                public void run() {
                    Display.getInstance().invokeAndBlock(new Runnable() {

                        public void run() {
                            while (Display.getInstance().getCurrent() != parent) {
                                try {
                                    Thread.sleep(40);
                                } catch (Exception ex) {
                                }
                            }
                        }
                    });
                    Display.getInstance().callSerially(new Runnable() {

                        public void run() {
                            parent.actionCommandImpl(c, e);
                        }
                    });
                }
            });
        }

        protected void sizeChanged(int w, int h) {
            Style formStyle = getStyle();
            int width = w - (formStyle.getHorizontalMargins());
            parent.sizeChangedInternal(w, h);
            // close the menu
            if (getWidth() != width) {
                closeMenu();
            }
            super.sizeChanged(w, h);
        }

        public void pointerPressed(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerPressed(x, y);
            if (rightPanel.contains(x, y)) {
                pressedInRightPanel = true;
            }
        }

        public void pointerDragged(int[] x, int[] y) {
            if (manualMotionLock) {
                return;
            }
            if (!transitionRunning && pressedInRightPanel) {
                dragActivated = true;
                pressedInRightPanel = false;
            }
            if (dragActivated) {
                setMenuGlassPane(menu, placement);
                draggedX = x[0];
                repaint();
                return;
            }
            super.pointerDragged(x, y);
        }

        public void pointerReleased(int x, int y) {
            if (manualMotionLock) {
                return;
            }
            super.pointerReleased(x, y);
            boolean isRTLValue = isRTL();
            if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                isRTLValue = !isRTLValue;
            }
            int displayWidth = Display.getInstance().getDisplayWidth();
            if (isRTLValue) {
                if (!transitionRunning && dragActivated && x < (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            } else {
                if (!transitionRunning && dragActivated && x > (displayWidth - rightPanel.getWidth()) / 2) {
                    final Motion motion = Motion.createEaseInOutMotion(draggedX, Display.getInstance().getDisplayWidth() - rightPanel.getWidth(), 200);
                    motion.start();
                    registerAnimated(new Animation() {

                        public boolean animate() {
                            draggedX = motion.getValue();
                            if (motion.isFinished()) {
                                dragActivated = false;
                                Display.getInstance().getCurrent().setGlassPane(null);
                                deregisterAnimated(this);
                            }
                            return true;
                        }

                        public void paint(Graphics g) {
                            repaint();
                        }
                    });
                    return;
                }
            }
            if (dragActivated || rightPanel.contains(x, y)) {
                setMenuGlassPane(menu, placement);
                draggedX = x;
                int start = x;
                int end = 0;
                if (isRTLValue) {
                    end = getWidth();
                }
                final Motion motion = Motion.createEaseInOutMotion(start, end, getUIManager().getThemeConstant("sideMenuAnimSpeedInt", 300));
                motion.start();
                manualMotionLock = true;
                sideSwipePotential = false;
                rightSideSwipePotential = false;
                topSwipePotential = false;
                registerAnimated(new Animation() {

                    public boolean animate() {
                        draggedX = motion.getValue();
                        if (motion.isFinished()) {
                            dragActivated = false;
                        }
                        return true;
                    }

                    public void paint(Graphics g) {
                        repaint();
                        if (draggedX == motion.getDestinationValue() && motion.isFinished()) {
                            parent.setTransitionInAnimator(CommonTransitions.createEmpty());
                            parent.show();
                            deregisterAnimated(this);
                            Display.getInstance().callSerially(new Runnable() {

                                public void run() {
                                    clean();
                                }
                            });
                        }
                    }
                });
            }
        }

        public void keyReleased(int keyCode) {
            if (keyCode == leftSK) {
                if (transitionRunning) {
                    return;
                }
                closeMenu();
                return;
            }
            super.keyReleased(keyCode);
        }
    };
    m.setScrollable(false);
    m.removeComponentFromForm(m.getTitleArea());
    m.putClientProperty("Menu", "true");
    m.setTransitionInAnimator(CommonTransitions.createEmpty());
    m.setTransitionOutAnimator(CommonTransitions.createEmpty());
    m.setBackCommand(new Command("") {

        public void actionPerformed(ActionEvent evt) {
            if (transitionRunning) {
                return;
            }
            closeMenu();
        }
    });
    m.setLayout(new BorderLayout());
    if (Display.getInstance().areMutableImagesFast()) {
        rightPanel = new Container(new BorderLayout());
    } else {
        rightPanel = new Container(new BorderLayout()) {

            public void paintBackground(Graphics g) {
            }

            public void paintBackgrounds(Graphics g) {
            }

            public void paint(Graphics g) {
                Component c = (Component) rightPanel.getClientProperty("$parent");
                // not sure why its happening: https://code.google.com/p/codenameone/issues/detail?id=1072
                if (c != null) {
                    boolean b = c.isVisible();
                    c.setVisible(true);
                    int x = getAbsoluteX();
                    g.translate(x, 0);
                    Container.sidemenuBarTranslation = x;
                    c.paintComponent(g, true);
                    Container.sidemenuBarTranslation = 0;
                    g.translate(-x, 0);
                    c.setVisible(b);
                }
            }
        };
    }
    if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
        int v = 0;
        if (Display.getInstance().isPortrait()) {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 2 / 3;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizePortraitInt", -1);
                if (v < 0) {
                    v = openButton.getHeight();
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        } else {
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("topMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("topMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getHeight() * 4 / 10;
                } else {
                    v = m.getHeight() / 100 * v;
                }
            }
        }
        rightPanel.setPreferredH(v);
    } else {
        if (Display.getInstance().isPortrait()) {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabPortraitInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 2 / 3;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizePortraitInt", -1);
                if (v < 0) {
                    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
                        if (isRTL()) {
                            v = openButton.getWidth();
                        } else {
                            v = rightSideButton.getWidth();
                        }
                    } else {
                        v = openButton.getWidth();
                    }
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        } else {
            int v = 0;
            if (Display.getInstance().isTablet()) {
                v = getUIManager().getThemeConstant("sideMenuSizeTabLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 3 / 4;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            } else {
                v = getUIManager().getThemeConstant("sideMenuSizeLandscapeInt", -1);
                if (v < 0) {
                    v = m.getWidth() * 4 / 10;
                } else {
                    v = m.getWidth() / 100 * v;
                }
            }
            rightPanel.setPreferredW(v);
        }
    }
    if (sidePanel != null) {
        sidePanel.removeAll();
        sidePanel = null;
    }
    sidePanel = createSideNavigationComponent(getCommands(), placement);
    if (placement == COMMAND_PLACEMENT_VALUE_RIGHT) {
        m.addComponent(BorderLayout.WEST, rightPanel);
        m.addComponent(BorderLayout.CENTER, sidePanel);
    } else {
        if (placement == COMMAND_PLACEMENT_VALUE_TOP) {
            m.addComponent(BorderLayout.NORTH, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
            Button button = new Button(" ");
            button.setUIID("Container");
            button.setPreferredH(Display.getInstance().getDisplayHeight() / 10);
            m.addComponent(BorderLayout.SOUTH, button);
            button.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    closeMenu();
                }
            });
        } else {
            m.addComponent(BorderLayout.EAST, rightPanel);
            m.addComponent(BorderLayout.CENTER, sidePanel);
        }
    }
    m.putClientProperty("cn1$sideMenuParent", this);
    return m;
}
Also used : Motion(com.codename1.ui.animations.Motion) ActionEvent(com.codename1.ui.events.ActionEvent) BorderLayout(com.codename1.ui.layouts.BorderLayout) ActionListener(com.codename1.ui.events.ActionListener) Style(com.codename1.ui.plaf.Style) Animation(com.codename1.ui.animations.Animation)

Example 82 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class HTMLForm method submit.

/**
 * Called when the a form submit is needed.
 * This querys all form fields, creates a URL accordingly and sets it to the HTMLComponent
 */
void submit(String submitKey, String submitVal) {
    if (action == null) {
        return;
    }
    // If this is turned to true anywhere, the form will not be submitted
    boolean error = false;
    String url = action;
    String params = null;
    if (comps.size() > 0) {
        params = "";
        for (Enumeration e = comps.keys(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            Object input = comps.get(key);
            key = HTMLUtils.encodeString(key);
            String value = "";
            if (input instanceof String) {
                // hidden
                value = HTMLUtils.encodeString((String) input);
                params += key + "=" + value + "&";
            } else if (input instanceof Hashtable) {
                // checkbox / radiobutton
                Hashtable options = (Hashtable) input;
                for (Enumeration e2 = options.keys(); e2.hasMoreElements(); ) {
                    Button b = (Button) e2.nextElement();
                    if (b.isSelected()) {
                        params += key + "=" + HTMLUtils.encodeString((String) options.get(b)) + "&";
                    }
                }
            } else if (input instanceof TextArea) {
                // catches both textareas and text input fields
                TextArea tf = ((TextArea) input);
                String text = tf.getText();
                String errorMsg = null;
                if (HTMLComponent.SUPPORT_INPUT_FORMAT) {
                    boolean ok = false;
                    if (text.equals("")) {
                        // check empty - Note that emptyok/-wap-input-required overrides input format
                        if (emptyNotOk.contains(tf)) {
                            errorMsg = htmlC.getUIManager().localize("html.format.emptynotok", "Field can't be empty");
                            error = true;
                        } else if (emptyOk.contains(tf)) {
                            ok = true;
                        }
                    }
                    if ((!error) && (!ok)) {
                        // If there's already an error or it has been cleared by the emptyOK field, no need to check
                        HTMLInputFormat inputFormat = (HTMLInputFormat) inputFormats.get(tf);
                        if ((inputFormat != null) && (!inputFormat.verifyString(text))) {
                            String emptyStr = "";
                            if (emptyOk.contains(tf)) {
                                emptyStr = htmlC.getUIManager().localize("html.format.oremptyok", " or an empty string");
                            } else if (emptyNotOk.contains(tf)) {
                                emptyStr = htmlC.getUIManager().localize("html.format.andemptynotok", " and cannot be an empty string");
                            }
                            errorMsg = htmlC.getUIManager().localize("html.format.errordesc", "Malformed text. Correct value: ") + inputFormat.toString() + emptyStr;
                            error = true;
                        }
                    }
                }
                if (htmlC.getHTMLCallback() != null) {
                    int type = HTMLCallback.FIELD_TEXT;
                    if ((tf.getConstraint() & TextArea.PASSWORD) != 0) {
                        type = HTMLCallback.FIELD_PASSWORD;
                    }
                    text = htmlC.getHTMLCallback().fieldSubmitted(htmlC, tf, url, key, text, type, errorMsg);
                }
                if (errorMsg == null) {
                    params += key + "=" + HTMLUtils.encodeString(text) + "&";
                }
            } else if (input instanceof ComboBox) {
                // drop down lists (single selection)
                Object item = ((ComboBox) input).getSelectedItem();
                if (item instanceof OptionItem) {
                    value = ((OptionItem) item).getValue();
                    params += key + "=" + HTMLUtils.encodeString(value) + "&";
                }
            // if not - value may be an OPTGROUP label in an only optgroup combobox
            } else if (input instanceof MultiComboBox) {
                // drop down lists (multiple selection)
                Vector selected = ((MultiComboBox) input).getSelected();
                for (int i = 0; i < selected.size(); i++) {
                    Object item = selected.elementAt(i);
                    if (item instanceof OptionItem) {
                        value = ((OptionItem) item).getValue();
                        params += key + "=" + HTMLUtils.encodeString(value) + "&";
                    }
                // if not - value may be an OPTGROUP label in an only optgroup combobox
                }
            }
        }
        if (params.endsWith("&")) {
            // trim the extra &
            params = params.substring(0, params.length() - 1);
        }
    }
    // Add the submit button param, only if the key is non-null (unnamed submit buttons are not passed as parameters)
    if (submitKey != null) {
        if (params == null) {
            params = "";
        }
        if (!params.equals("")) {
            params = params + "&";
        }
        params = params + HTMLUtils.encodeString(submitKey) + "=" + HTMLUtils.encodeString(submitVal);
    }
    if (!error) {
        DocumentInfo docInfo = new DocumentInfo(url, params, isPostMethod);
        if ((encType != null) && (!encType.equals(""))) {
            docInfo.setEncoding(encType);
        }
        htmlC.setPage(docInfo);
    }
}
Also used : Enumeration(java.util.Enumeration) TextArea(com.codename1.ui.TextArea) Hashtable(java.util.Hashtable) ComboBox(com.codename1.ui.ComboBox) RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button) Vector(java.util.Vector)

Example 83 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class MorphTransition method initTransition.

/**
 * {@inheritDoc}
 */
public final void initTransition() {
    animationMotion = Motion.createEaseInOutMotion(0, 255, duration);
    animationMotion.start();
    Container s = (Container) getSource();
    Container d = (Container) getDestination();
    Iterator<String> keyIterator = fromTo.keySet().iterator();
    int size = fromTo.size();
    fromToComponents = new CC[size];
    Form destForm = d.getComponentForm();
    Form sourceForm = s.getComponentForm();
    for (int iter = 0; iter < size; iter++) {
        String k = keyIterator.next();
        String v = fromTo.get(k);
        Component sourceCmp = findByName(s, k);
        Component destCmp = findByName(d, v);
        if (sourceCmp == null || destCmp == null) {
            continue;
        }
        CC cc = new CC(sourceCmp, destCmp, sourceForm, destForm);
        fromToComponents[iter] = cc;
        cc.placeholderDest = new Label();
        cc.placeholderDest.setVisible(false);
        Container destParent = cc.dest.getParent();
        cc.placeholderDest.setX(cc.dest.getX());
        cc.placeholderDest.setY(cc.dest.getY() - destForm.getContentPane().getY());
        cc.placeholderDest.setWidth(cc.dest.getWidth());
        cc.placeholderDest.setHeight(cc.dest.getHeight());
        cc.placeholderDest.setPreferredSize(new Dimension(cc.dest.getWidth(), cc.dest.getHeight()));
        destParent.replace(cc.dest, cc.placeholderDest, null);
        destForm.getLayeredPane().addComponent(cc.dest);
        cc.placeholderSrc = new Label();
        cc.placeholderSrc.setVisible(false);
        cc.placeholderSrc.setX(cc.source.getX());
        cc.placeholderSrc.setY(cc.source.getY() - sourceForm.getContentPane().getY());
        cc.placeholderSrc.setWidth(cc.source.getWidth());
        cc.placeholderSrc.setHeight(cc.source.getHeight());
        cc.placeholderSrc.setPreferredSize(new Dimension(cc.source.getWidth(), cc.source.getHeight()));
        cc.originalContainer = cc.source.getParent();
        cc.originalConstraint = cc.originalContainer.getLayout().getComponentConstraint(cc.source);
        cc.originalOffset = cc.originalContainer.getComponentIndex(cc.source);
        cc.originalContainer.replace(cc.source, cc.placeholderSrc, null);
        cc.originalContainer.getComponentForm().getLayeredPane().addComponent(cc.source);
    }
}
Also used : Container(com.codename1.ui.Container) Form(com.codename1.ui.Form) Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

Example 84 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class FullScreenAdService method bindTransitionAd.

/**
 * Binds an ad to appear periodically after a given timeout
 * @param timeForNext the timeout in which an ad should be shown in milliseconds
 */
public void bindTransitionAd(final int timeForNext) {
    Runnable onTransitionAndExit = new Runnable() {

        private long lastTime = System.currentTimeMillis();

        public void run() {
            long t = System.currentTimeMillis();
            if (t - lastTime > timeForNext) {
                lastTime = t;
                Component c = getPendingAd();
                if (c != null) {
                    Form adForm = new AdForm(c);
                    adForm.show();
                }
            }
        }
    };
    CodenameOneImplementation.setOnCurrentFormChange(onTransitionAndExit);
    CodenameOneImplementation.setOnExit(onTransitionAndExit);
    Timer t = new Timer();
    int tm = Math.max(5000, timeForNext - 600);
    t.schedule(new TimerTask() {

        public void run() {
            if (!hasPendingAd()) {
                ConnectionRequest r = createAdRequest();
                r.setPriority(ConnectionRequest.PRIORITY_LOW);
                r.setTimeout(timeout);
                NetworkManager.getInstance().addToQueue(r);
            }
        }
    }, tm, tm);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) Timer(java.util.Timer) TimerTask(java.util.TimerTask) Form(com.codename1.ui.Form) Component(com.codename1.ui.Component)

Example 85 with Form

use of com.codename1.ui.Form in project CodenameOne by codenameone.

the class FullScreenAdService method showWelcomeAd.

/**
 * Invoked on application startup, this code will download an ad or timeout
 */
public void showWelcomeAd() {
    if (!UIManager.getInstance().wasThemeInstalled()) {
        if (Display.getInstance().hasNativeTheme()) {
            Display.getInstance().installNativeTheme();
        }
    }
    ConnectionRequest r = createAdRequest();
    r.setPriority(ConnectionRequest.PRIORITY_HIGH);
    r.setTimeout(timeout);
    InfiniteProgress ip = new InfiniteProgress();
    Dialog ipDialog = ip.showInifiniteBlocking();
    NetworkManager.getInstance().addToQueueAndWait(r);
    if (failed()) {
        ipDialog.dispose();
        if (!allowWithoutNetwork) {
            ipDialog.dispose();
            Dialog.show("Network Error", "Please try again later", "Exit", null);
            Display.getInstance().exitApplication();
        } else {
            return;
        }
    }
    Component c = getPendingAd();
    if (c != null) {
        Form adForm = new AdForm(c);
        adForm.setTransitionInAnimator(CommonTransitions.createEmpty());
        adForm.setTransitionOutAnimator(CommonTransitions.createEmpty());
        adForm.show();
    }
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) InfiniteProgress(com.codename1.components.InfiniteProgress) Form(com.codename1.ui.Form) Dialog(com.codename1.ui.Dialog) Component(com.codename1.ui.Component)

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